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

Contents of /eclass/alternatives.eclass

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.14 - (hide annotations) (download)
Tue May 15 15:20:59 2007 UTC (7 years, 5 months ago) by ulm
Branch: MAIN
Changes since 1.13: +3 -2 lines
Fix alternatives.eclass to remove dead symlinks.

1 vapier 1.9 # Copyright 1999-2004 Gentoo Foundation
2 liquidx 1.1 # Distributed under the terms of the GNU General Public License v2
3 ulm 1.14 # $Header: /var/cvsroot/gentoo-x86/eclass/alternatives.eclass,v 1.13 2006/03/25 02:37:04 agriffis Exp $
4 liquidx 1.1
5     # Author : Alastair Tse <liquidx@gentoo.org> (03 Oct 2003)
6     # Short Desc: Creates symlink to the latest version of multiple slotted
7     # packages.
8     #
9     # Long Desc:
10     #
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.
14     #
15 swegener 1.12 # This eclass provides a convenience function that needs to be given a
16 liquidx 1.1 # list of alternatives (descending order of recent-ness) and the symlink.
17 agriffis 1.13 # It will choose the latest version it can find installed and create
18 swegener 1.12 # the desired symlink.
19 liquidx 1.1 #
20 swegener 1.12 # 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
22 liquidx 1.1 # $ALTERNATIVES is a list of alternatives. Second way is the use the function
23     # alternatives_makesym() like the example below.
24     #
25     # Example:
26 swegener 1.12 #
27 liquidx 1.1 # pkg_postinst() {
28     # alternatives_makesym "/usr/bin/python" "/usr/bin/python2.3" "/usr/bin/python2.2"
29     # }
30     #
31     # The above example will create a symlink at /usr/bin/python to either
32     # /usr/bin/python2.3 or /usr/bin/python2.2. It will choose python2.3 over
33     # python2.2 if both exist.
34     #
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 swegener 1.12 #
41 liquidx 1.1 # 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
43     # consider using this unless you are want to do something special.
44 swegener 1.12 #
45 liquidx 1.1
46     # automatic deduction based on a symlink and a regex mask
47     alternatives_auto_makesym() {
48 liquidx 1.6 local SYMLINK REGEX ALT myregex
49     SYMLINK=$1
50 liquidx 1.1 REGEX=$2
51 swegener 1.12 if [ "${REGEX:0:1}" != "/" ]
52 liquidx 1.6 then
53     #not an absolute path:
54     #inherit the root directory of our main link path for our regex search
55     myregex="${SYMLINK%/*}/${REGEX}"
56 liquidx 1.1 else
57 liquidx 1.6 myregex=${REGEX}
58     fi
59 liquidx 1.8
60 liquidx 1.6 # sort a space delimited string by converting it to a multiline list
61     # and then run sort -r over it.
62 liquidx 1.8 # 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 liquidx 1.6 alternatives_makesym ${SYMLINK} ${ALT}
65 liquidx 1.1 }
66    
67     alternatives_makesym() {
68     local ALTERNATIVES=""
69 liquidx 1.6 local SYMLINK=""
70     local alt pref
71 swegener 1.12
72 liquidx 1.1 # usage: alternatives_makesym <resulting symlink> [alternative targets..]
73 liquidx 1.6 SYMLINK=$1
74 liquidx 1.8 # this trick removes the trailing / from ${ROOT}
75     pref=$(echo ${ROOT} | sed 's:/$::')
76 liquidx 1.1 shift
77     ALTERNATIVES=$@
78    
79     # step through given alternatives from first to last
80     # and if one exists, link it and finish.
81 swegener 1.12
82 liquidx 1.1 for alt in ${ALTERNATIVES}; do
83 liquidx 1.6 if [ -f "${pref}${alt}" ]; then
84     #are files in same directory?
85     if [ "${alt%/*}" = "${SYMLINK%/*}" ]
86     then
87     #yes; strip leading dirname from alt to create relative symlink
88 liquidx 1.8 einfo "Linking ${alt} to ${pref}${SYMLINK} (relative)"
89 liquidx 1.7 ln -sf ${alt##*/} ${pref}${SYMLINK}
90 liquidx 1.6 else
91     #no; keep absolute path
92 liquidx 1.8 einfo "Linking ${alt} to ${pref}${SYMLINK} (absolute)"
93 liquidx 1.7 ln -sf ${pref}${alt} ${pref}${SYMLINK}
94 liquidx 1.1 fi
95     break
96     fi
97     done
98 swegener 1.12
99 liquidx 1.1 # report any errors
100 liquidx 1.6 if [ ! -L ${pref}${SYMLINK} ]; then
101     ewarn "Unable to establish ${pref}${SYMLINK} symlink"
102     else
103     # we need to check for either the target being in relative path form
104     # or absolute path form
105     if [ ! -f "`dirname ${pref}${SYMLINK}`/`readlink ${pref}${SYMLINK}`" -a \
106     ! -f "`readlink ${pref}${SYMLINK}`" ]; then
107 ulm 1.14 ewarn "Removing dead symlink ${pref}${SYMLINK}"
108     rm -f ${pref}${SYMLINK}
109 liquidx 1.6 fi
110 swegener 1.12 fi
111     }
112 liquidx 1.1
113     alternatives_pkg_postinst() {
114     if [ -n "${ALTERNATIVES}" -a -n "${SOURCE}" ]; then
115     alternatives_makesym ${SOURCE} ${ALTERNATIVES}
116 swegener 1.12 fi
117 liquidx 1.1 }
118    
119     alternatives_pkg_postrm() {
120     if [ -n "${ALTERNATIVES}" -a -n "${SOURCE}" ]; then
121     alternatives_makesym ${SOURCE} ${ALTERNATIVES}
122     fi
123     }
124    
125     EXPORT_FUNCTIONS pkg_postinst pkg_postrm

  ViewVC Help
Powered by ViewVC 1.1.20