| 1 |
liquidx |
1.1 |
# Copyright 1999-2003 Gentoo Technologies, Inc.
|
| 2 |
|
|
# Distributed under the terms of the GNU General Public License v2
|
| 3 |
liquidx |
1.8 |
# $Header: /home/cvsroot/gentoo-x86/eclass/alternatives.eclass,v 1.7 2003/11/24 10:57:06 liquidx 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 |
|
|
# 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 |
liquidx |
1.6 |
local SYMLINK REGEX ALT myregex
|
| 51 |
|
|
SYMLINK=$1
|
| 52 |
liquidx |
1.1 |
REGEX=$2
|
| 53 |
liquidx |
1.6 |
if [ "${REGEX:0:1}" != "/" ]
|
| 54 |
|
|
then
|
| 55 |
|
|
#not an absolute path:
|
| 56 |
|
|
#inherit the root directory of our main link path for our regex search
|
| 57 |
|
|
myregex="${SYMLINK%/*}/${REGEX}"
|
| 58 |
liquidx |
1.1 |
else
|
| 59 |
liquidx |
1.6 |
myregex=${REGEX}
|
| 60 |
|
|
fi
|
| 61 |
liquidx |
1.8 |
|
| 62 |
liquidx |
1.6 |
# sort a space delimited string by converting it to a multiline list
|
| 63 |
|
|
# and then run sort -r over it.
|
| 64 |
liquidx |
1.8 |
# make sure we use ${ROOT} because otherwise stage-building will break
|
| 65 |
|
|
ALT="$(for i in $(echo ${ROOT}${myregex}); do echo ${i#${ROOT}}; done | sort -r)"
|
| 66 |
liquidx |
1.6 |
alternatives_makesym ${SYMLINK} ${ALT}
|
| 67 |
liquidx |
1.1 |
}
|
| 68 |
|
|
|
| 69 |
|
|
alternatives_makesym() {
|
| 70 |
|
|
local ALTERNATIVES=""
|
| 71 |
liquidx |
1.6 |
local SYMLINK=""
|
| 72 |
|
|
local alt pref
|
| 73 |
liquidx |
1.8 |
|
| 74 |
liquidx |
1.1 |
# usage: alternatives_makesym <resulting symlink> [alternative targets..]
|
| 75 |
liquidx |
1.6 |
SYMLINK=$1
|
| 76 |
liquidx |
1.8 |
# this trick removes the trailing / from ${ROOT}
|
| 77 |
|
|
pref=$(echo ${ROOT} | sed 's:/$::')
|
| 78 |
liquidx |
1.1 |
shift
|
| 79 |
|
|
ALTERNATIVES=$@
|
| 80 |
|
|
|
| 81 |
|
|
# step through given alternatives from first to last
|
| 82 |
|
|
# and if one exists, link it and finish.
|
| 83 |
|
|
|
| 84 |
|
|
for alt in ${ALTERNATIVES}; do
|
| 85 |
liquidx |
1.6 |
if [ -f "${pref}${alt}" ]; then
|
| 86 |
|
|
#are files in same directory?
|
| 87 |
|
|
if [ "${alt%/*}" = "${SYMLINK%/*}" ]
|
| 88 |
|
|
then
|
| 89 |
|
|
#yes; strip leading dirname from alt to create relative symlink
|
| 90 |
liquidx |
1.8 |
einfo "Linking ${alt} to ${pref}${SYMLINK} (relative)"
|
| 91 |
liquidx |
1.7 |
ln -sf ${alt##*/} ${pref}${SYMLINK}
|
| 92 |
liquidx |
1.6 |
else
|
| 93 |
|
|
#no; keep absolute path
|
| 94 |
liquidx |
1.8 |
einfo "Linking ${alt} to ${pref}${SYMLINK} (absolute)"
|
| 95 |
liquidx |
1.7 |
ln -sf ${pref}${alt} ${pref}${SYMLINK}
|
| 96 |
liquidx |
1.1 |
fi
|
| 97 |
|
|
break
|
| 98 |
|
|
fi
|
| 99 |
|
|
done
|
| 100 |
|
|
|
| 101 |
|
|
# report any errors
|
| 102 |
liquidx |
1.6 |
if [ ! -L ${pref}${SYMLINK} ]; then
|
| 103 |
|
|
ewarn "Unable to establish ${pref}${SYMLINK} symlink"
|
| 104 |
|
|
else
|
| 105 |
|
|
# we need to check for either the target being in relative path form
|
| 106 |
|
|
# or absolute path form
|
| 107 |
|
|
if [ ! -f "`dirname ${pref}${SYMLINK}`/`readlink ${pref}${SYMLINK}`" -a \
|
| 108 |
|
|
! -f "`readlink ${pref}${SYMLINK}`" ]; then
|
| 109 |
|
|
ewarn "${pref}${SYMLINK} is a dead symlink."
|
| 110 |
|
|
fi
|
| 111 |
liquidx |
1.1 |
fi
|
| 112 |
|
|
}
|
| 113 |
|
|
|
| 114 |
|
|
alternatives_pkg_postinst() {
|
| 115 |
|
|
if [ -n "${ALTERNATIVES}" -a -n "${SOURCE}" ]; then
|
| 116 |
|
|
alternatives_makesym ${SOURCE} ${ALTERNATIVES}
|
| 117 |
|
|
fi
|
| 118 |
|
|
}
|
| 119 |
|
|
|
| 120 |
|
|
alternatives_pkg_postrm() {
|
| 121 |
|
|
if [ -n "${ALTERNATIVES}" -a -n "${SOURCE}" ]; then
|
| 122 |
|
|
alternatives_makesym ${SOURCE} ${ALTERNATIVES}
|
| 123 |
|
|
fi
|
| 124 |
|
|
}
|
| 125 |
|
|
|
| 126 |
|
|
EXPORT_FUNCTIONS pkg_postinst pkg_postrm
|
| 127 |
|
|
|