/[gentoo-x86]/eclass/alternatives.eclass
Gentoo

Diff of /eclass/alternatives.eclass

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.4 Revision 1.16
1# Copyright 1999-2003 Gentoo Technologies, Inc. 1# Copyright 1999-2008 Gentoo Foundation
2# Distributed under the terms of the GNU General Public License v2 2# Distributed under the terms of the GNU General Public License v2
3# $Header: /var/cvsroot/gentoo-x86/eclass/alternatives.eclass,v 1.4 2003/11/01 17:35:59 liquidx Exp $ 3# $Header: /var/cvsroot/gentoo-x86/eclass/alternatives.eclass,v 1.16 2010/11/24 06:49:41 ulm Exp $
4 4
5# Author : Alastair Tse <liquidx@gentoo.org> (03 Oct 2003) 5# @ECLASS: alternatives.eclass
6# Short Desc: Creates symlink to the latest version of multiple slotted 6# @MAINTAINER:
7# packages.
8# 7#
9# Long Desc: 8# Original author : Alastair Tse <liquidx@gentoo.org> (03 Oct 2003)
9# @BLURB: Creates symlink to the latest version of multiple slotted packages.
10# @DESCRIPTION:
11# When a package is SLOT'ed, very often we need to have a symlink to the
12# latest version. However, depending on the order the user has merged them,
13# more often than not, the symlink maybe clobbered by the older versions.
10# 14#
11# When a package is SLOT'ed, very often we need to have a symlink to the 15# This eclass provides a convenience function that needs to be given a
12# latest version. However, depending on the order the user has merged them, 16# list of alternatives (descending order of recent-ness) and the symlink.
13# more often than not, the symlink maybe clobbered by the older versions. 17# It will choose the latest version it can find installed and create
18# the desired symlink.
14# 19#
15# This eclass provides a convenience function that needs to be given a 20# There are two ways to use this eclass. First is by declaring two variables
16# list of alternatives (descending order of recent-ness) and the symlink. 21# $SOURCE and $ALTERNATIVES where $SOURCE is the symlink to be created and
17# It will choose the latest version if can find installed and create 22# $ALTERNATIVES is a list of alternatives. Second way is the use the function
18# the desired symlink. 23# alternatives_makesym() like the example below.
24# @EXAMPLE:
25# pkg_postinst() {
26# alternatives_makesym "/usr/bin/python" "/usr/bin/python2.3" "/usr/bin/python2.2"
27# }
19# 28#
20# There are two ways to use this eclass. First is by declaring two variables 29# The above example will create a symlink at /usr/bin/python to either
21# $SOURCE and $ALTERNATIVES where $SOURCE is the symlink to be created and 30# /usr/bin/python2.3 or /usr/bin/python2.2. It will choose python2.3 over
22# $ALTERNATIVES is a list of alternatives. Second way is the use the function 31# python2.2 if both exist.
23# alternatives_makesym() like the example below.
24# 32#
25# Example: 33# Alternatively, you can use this function:
26#
27# pkg_postinst() {
28# alternatives_makesym "/usr/bin/python" "/usr/bin/python2.3" "/usr/bin/python2.2"
29# }
30# 34#
31# The above example will create a symlink at /usr/bin/python to either 35# pkg_postinst() {
32# /usr/bin/python2.3 or /usr/bin/python2.2. It will choose python2.3 over 36# alternatives_auto_makesym "/usr/bin/python" "/usr/bin/python[0-9].[0-9]"
33# python2.2 if both exist. 37# }
34# 38#
35# Alternatively, you can use this function:
36#
37# pkg_postinst() {
38# alternatives_auto_makesym "/usr/bin/python" "/usr/bin/python[0-9].[0-9]"
39# }
40#
41# This will use bash pathname expansion to fill a list of alternatives it can 39# This will use bash pathname expansion to fill a list of alternatives it can
42# link to. It is probably more robust against version upgrades. You should 40# link to. It is probably more robust against version upgrades. You should
43# consider using this unless you are want to do something special. 41# consider using this unless you are want to do something special.
44#
45ECLASS="alternatives"
46INHERITED="$INHERITED $ECLASS"
47 42
43# @ECLASS-VARIABLE: SOURCE
44# @DEFAULT_UNSET
45# @DESCRIPTION:
46# The symlink to be created
47
48# @ECLASS-VARIABLE: ALTERNATIVES
49# @DEFAULT_UNSET
50# @DESCRIPTION:
51# The list of alternatives
52
53# @FUNCTION: alternatives_auto_makesym
54# @DESCRIPTION:
48# automatic deduction based on a symlink and a regex mask 55# automatic deduction based on a symlink and a regex mask
49alternatives_auto_makesym() { 56alternatives_auto_makesym() {
50 local SYMLINK REGEX ALT myregex 57 local SYMLINK REGEX ALT myregex
51 SYMLINK=$1 58 SYMLINK=$1
52 REGEX=$2 59 REGEX=$2
53 if [ "${REGEX:0:1}" != "/" ] 60 if [ "${REGEX:0:1}" != "/" ]
54 then 61 then
55 #not an absolute path: 62 #not an absolute path:
56 #inherit the root directory of our main link path for our regex search 63 #inherit the root directory of our main link path for our regex search
57 myregex="${SYMLINK%/*}/${REGEX}" 64 myregex="${SYMLINK%/*}/${REGEX}"
58 else 65 else
59 myregex=${REGEX} 66 myregex=${REGEX}
60 fi 67 fi
61 ALT="`echo ${myregex} | sort -r`" 68
69 # sort a space delimited string by converting it to a multiline list
70 # and then run sort -r over it.
71 # make sure we use ${ROOT} because otherwise stage-building will break
72 ALT="$(for i in $(echo ${ROOT}${myregex}); do echo ${i#${ROOT}}; done | sort -r)"
62 alternatives_makesym ${SYMLINK} ${ALT} 73 alternatives_makesym ${SYMLINK} ${ALT}
63} 74}
64 75
65alternatives_makesym() { 76alternatives_makesym() {
66 local ALTERNATIVES="" 77 local ALTERNATIVES=""
67 local SYMLINK="" 78 local SYMLINK=""
68 local alt pref 79 local alt pref
80
69 # usage: alternatives_makesym <resulting symlink> [alternative targets..] 81 # usage: alternatives_makesym <resulting symlink> [alternative targets..]
70 SYMLINK=$1 82 SYMLINK=$1
71 pref=${ROOT:0:${#ROOT}-1} 83 # this trick removes the trailing / from ${ROOT}
84 pref=$(echo ${ROOT} | sed 's:/$::')
72 shift 85 shift
73 ALTERNATIVES=$@ 86 ALTERNATIVES=$@
74 87
75 # step through given alternatives from first to last 88 # step through given alternatives from first to last
76 # and if one exists, link it and finish. 89 # and if one exists, link it and finish.
77 90
78 for alt in ${ALTERNATIVES}; do 91 for alt in ${ALTERNATIVES}; do
79 if [ -f "${pref}${alt}" ]; then 92 if [ -f "${pref}${alt}" ]; then
80 einfo "Linking ${alt} to ${pref}${SYMLINK}"
81 [ -L "${pref}${SYMLINK}" ] && rm -f ${pref}${SYMLINK}
82 #are files in same directory? 93 #are files in same directory?
83 if [ "${alt%/*}" = "${SYMLINK%/*}" ] 94 if [ "${alt%/*}" = "${SYMLINK%/*}" ]
84 then 95 then
85 #yes; strip leading dirname from alt to create relative symlink 96 #yes; strip leading dirname from alt to create relative symlink
97 einfo "Linking ${alt} to ${pref}${SYMLINK} (relative)"
86 ln -s ${alt##*/} ${pref}${SYMLINK} 98 ln -sf ${alt##*/} ${pref}${SYMLINK}
87 else 99 else
88 #no; keep absolute path 100 #no; keep absolute path
101 einfo "Linking ${alt} to ${pref}${SYMLINK} (absolute)"
89 ln -s ${pref}${alt} ${pref}${SYMLINK} 102 ln -sf ${pref}${alt} ${pref}${SYMLINK}
90 fi 103 fi
91 break 104 break
92 fi 105 fi
93 done 106 done
94 107
95 # report any errors 108 # report any errors
96 if [ ! -L ${pref}${SYMLINK} ]; then 109 if [ ! -L ${pref}${SYMLINK} ]; then
97 ewarn "Unable to establish ${pref}${SYMLINK} symlink" 110 ewarn "Unable to establish ${pref}${SYMLINK} symlink"
98 else 111 else
99 # we need to check for either the target being in relative path form 112 # we need to check for either the target being in relative path form
100 # or absolute path form 113 # or absolute path form
101 if [ ! -f "`dirname ${pref}${SYMLINK}`/`readlink ${pref}${SYMLINK}`" -a \ 114 if [ ! -f "`dirname ${pref}${SYMLINK}`/`readlink ${pref}${SYMLINK}`" -a \
102 ! -f "`readlink ${pref}${SYMLINK}`" ]; then 115 ! -f "`readlink ${pref}${SYMLINK}`" ]; then
103 ewarn "${pref}${SYMLINK} is a dead symlink." 116 ewarn "Removing dead symlink ${pref}${SYMLINK}"
117 rm -f ${pref}${SYMLINK}
104 fi 118 fi
105 fi 119 fi
106} 120}
107 121
122# @FUNCTION: alernatives-pkg_postinst
123# @DESCRIPTION:
124# The alternatives pkg_postinst, this function will be exported
108alternatives_pkg_postinst() { 125alternatives_pkg_postinst() {
109 if [ -n "${ALTERNATIVES}" -a -n "${SOURCE}" ]; then 126 if [ -n "${ALTERNATIVES}" -a -n "${SOURCE}" ]; then
110 alternatives_makesym ${SOURCE} ${ALTERNATIVES} 127 alternatives_makesym ${SOURCE} ${ALTERNATIVES}
111 fi 128 fi
112} 129}
113 130
131# @FUNCTION: alternatives_pkg_postrm
132# @DESCRIPTION:
133# The alternatives pkg_postrm, this function will be exported
114alternatives_pkg_postrm() { 134alternatives_pkg_postrm() {
115 if [ -n "${ALTERNATIVES}" -a -n "${SOURCE}" ]; then 135 if [ -n "${ALTERNATIVES}" -a -n "${SOURCE}" ]; then
116 alternatives_makesym ${SOURCE} ${ALTERNATIVES} 136 alternatives_makesym ${SOURCE} ${ALTERNATIVES}
117 fi 137 fi
118} 138}
119 139
120EXPORT_FUNCTIONS pkg_postinst pkg_postrm 140EXPORT_FUNCTIONS pkg_postinst pkg_postrm
121

Legend:
Removed from v.1.4  
changed lines
  Added in v.1.16

  ViewVC Help
Powered by ViewVC 1.1.20