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

Diff of /eclass/alternatives.eclass

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

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

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

  ViewVC Help
Powered by ViewVC 1.1.20