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

Diff of /eclass/alternatives.eclass

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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

Legend:
Removed from v.1.1.1.1  
changed lines
  Added in v.1.18

  ViewVC Help
Powered by ViewVC 1.1.20