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

Contents of /eclass/alternatives.eclass

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.15 - (hide annotations) (download)
Wed Sep 10 08:10:31 2008 UTC (6 years, 3 months ago) by pva
Branch: MAIN
Changes since 1.14: +54 -41 lines
Make eclass-manpages ready, bug #210058, thank mren for report.

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

  ViewVC Help
Powered by ViewVC 1.1.20