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

Contents of /eclass/alternatives.eclass

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.17 - (hide annotations) (download)
Mon Aug 22 04:46:31 2011 UTC (2 years, 10 months ago) by vapier
Branch: MAIN
Changes since 1.16: +5 -6 lines
fix random bugs in eclass documentation, and convert to new @AUTHOR tag

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

  ViewVC Help
Powered by ViewVC 1.1.20