1 |
# Copyright 1999-2003 Gentoo Technologies, Inc. |
2 |
# Distributed under the terms of the GNU General Public License v2 |
3 |
# $Header: /home/cvsroot/gentoo-x86/eclass/alternatives.eclass,v 1.2 2003/10/07 20:26:50 liquidx Exp $ |
4 |
|
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 |
# 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 if 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 |
# |
25 |
# Example: |
26 |
# |
27 |
# 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 |
# |
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 |
43 |
# consider using this unless you are want to do something special. |
44 |
# |
45 |
ECLASS="alternatives" |
46 |
INHERITED="$INHERITED $ECLASS" |
47 |
|
48 |
# automatic deduction based on a symlink and a regex mask |
49 |
alternatives_auto_makesym() { |
50 |
local SOURCE REGEX ALT |
51 |
local unsorted |
52 |
SOURCE=$1 |
53 |
REGEX=$2 |
54 |
|
55 |
ALT="`ls -1 --color=never ${ROOT}${REGEX} | sed -e "s:^${ROOT}::" | sort -r | xargs`" |
56 |
if [ -n "${ALT}" ]; then |
57 |
alternatives_makesym ${SOURCE} ${ALT} |
58 |
else |
59 |
eerror "regex ${REGEX} doesn't match any files." |
60 |
fi |
61 |
} |
62 |
|
63 |
alternatives_makesym() { |
64 |
local ALTERNATIVES="" |
65 |
local SOURCE="" |
66 |
|
67 |
# usage: alternatives_makesym <resulting symlink> [alternative targets..] |
68 |
SOURCE=$1 |
69 |
shift |
70 |
ALTERNATIVES=$@ |
71 |
|
72 |
# step through given alternatives from first to last |
73 |
# and if one exists, link it and finish. |
74 |
|
75 |
for alt in ${ALTERNATIVES}; do |
76 |
if [ -f "${ROOT}${alt}" ]; then |
77 |
if [ -L "${ROOT}${SOURCE}" ]; then |
78 |
rm -f ${ROOT}${SOURCE} |
79 |
fi |
80 |
einfo "Linking ${alt} to ${SOURCE}" |
81 |
# we do this instead of ${ROOT}${SOURCE} because if |
82 |
# ROOT=/, then a symlink to //usr/bin/python confuses distutils |
83 |
cd ${ROOT}; ln -s ${alt} ${SOURCE} |
84 |
break |
85 |
fi |
86 |
done |
87 |
|
88 |
# report any errors |
89 |
if [ ! -L ${ROOT}${SOURCE} ]; then |
90 |
ewarn "Unable to establish ${SOURCE} symlink" |
91 |
elif [ ! -f "`readlink ${ROOT}${SOURCE}`" -a ! -f "${ROOT}`readlink ${ROOT}${SOURE}`" ]; then |
92 |
ewarn "${SOURCE} is a dead symlink." |
93 |
fi |
94 |
} |
95 |
|
96 |
alternatives_pkg_postinst() { |
97 |
if [ -n "${ALTERNATIVES}" -a -n "${SOURCE}" ]; then |
98 |
alternatives_makesym ${SOURCE} ${ALTERNATIVES} |
99 |
fi |
100 |
} |
101 |
|
102 |
alternatives_pkg_postrm() { |
103 |
if [ -n "${ALTERNATIVES}" -a -n "${SOURCE}" ]; then |
104 |
alternatives_makesym ${SOURCE} ${ALTERNATIVES} |
105 |
fi |
106 |
} |
107 |
|
108 |
EXPORT_FUNCTIONS pkg_postinst pkg_postrm |
109 |
|