Gentoo Archives: gentoo-commits

From: Sebastien Fabbro <bicatali@g.o>
To: gentoo-commits@l.g.o
Subject: [gentoo-commits] dev/bicatali:master commit in: eclass/
Date: Wed, 23 Feb 2011 05:40:23
Message-Id: ef35bfeb161a9231505189a2cc5ad1bc27b2791d.bicatali@gentoo
1 commit: ef35bfeb161a9231505189a2cc5ad1bc27b2791d
2 Author: Sébastien Fabbro <bicatali <AT> gentoo <DOT> org>
3 AuthorDate: Wed Feb 23 05:20:34 2011 +0000
4 Commit: Sebastien Fabbro <bicatali <AT> gentoo <DOT> org>
5 CommitDate: Wed Feb 23 05:20:34 2011 +0000
6 URL: http://git.overlays.gentoo.org/gitweb/?p=dev/bicatali.git;a=commit;h=ef35bfeb
7
8 added alternatives-2 eclass
9
10 ---
11 eclass/alternatives-2.eclass | 155 ++++++++++++++++++++++++++++++++++++++++++
12 1 files changed, 155 insertions(+), 0 deletions(-)
13
14 diff --git a/eclass/alternatives-2.eclass b/eclass/alternatives-2.eclass
15 new file mode 100644
16 index 0000000..ee6a549
17 --- /dev/null
18 +++ b/eclass/alternatives-2.eclass
19 @@ -0,0 +1,155 @@
20 +# Copyright 2008, 2009 Bo Ørsted Andresen
21 +# Copyright 2008, 2009 Mike Kelly
22 +# Copyright 2009 David Leverton
23 +# 2010: Adapted for Gentoo by Sebastien Fabbro, who does not like copyrights
24 +# Distributed under the terms of the GNU General Public License v2
25 +
26 +# If your package provides pkg_postinst or pkg_prerm phases, you need to be
27 +# sure you explicitly run alternatives_pkg_{postinst,prerm} where appropriate.
28 +
29 +EAPI=3
30 +ALTERNATIVES_DIR="/etc/env.d/alternatives"
31 +
32 +DEPENDS=">=app-admin/eselect-1.2.90"
33 +RDEPENDS="${DEPENDS}"
34 +
35 +# alternatives_for alternative provider importance source target [ source target [...]]
36 +alternatives_for() {
37 + #echo alternatives_for "${@}"
38 +
39 + (( $# >= 5 )) && (( ($#-3)%2 == 0)) || die "${FUNCNAME} requires exactly 3+N*2 arguments where N>=1"
40 + local x dupl alternative=${1} provider=${2} importance=${3} index unique src target ret=0
41 + shift 3
42 +
43 + # make sure importance is a signed integer
44 + if [[ -n ${importance} ]] && ! [[ ${importance} =~ ^[0-9]+(\.[0-9]+)*$ ]]; then
45 + eerror "Invalid importance (${importance}) detected"
46 + ((ret++))
47 + fi
48 +
49 + [[ -d "${ED}${ALTERNATIVES_DIR}/${alternative}/${provider}" ]] || dodir "${ALTERNATIVES_DIR}/${alternative}/${provider}"
50 +
51 + # keep track of provided alternatives for use in pkg_{postinst,prerm}. keep a mapping between importance and
52 + # provided alternatives and make sure the former is set to only one value
53 + if ! has "${alternative}:${provider}" "${ALTERNATIVES_PROVIDED[@]}"; then
54 + index=${#ALTERNATIVES_PROVIDED[@]}
55 + ALTERNATIVES_PROVIDED+=( "${alternative}:${provider}" )
56 + ALTERNATIVES_IMPORTANCE[index]=${importance}
57 + [[ -n ${importance} ]] && echo "${importance}" > "${ED}${ALTERNATIVES_DIR}/${alternative}/${provider}/_importance"
58 + else
59 + for((index=0;index<${#ALTERNATIVES_PROVIDED[@]};index++)); do
60 + if [[ ${alternative}:${provider} == ${ALTERNATIVES_PROVIDED[index]} ]]; then
61 + if [[ -n ${ALTERNATIVES_IMPORTANCE[index]} ]]; then
62 + if [[ -n ${importance} && ${ALTERNATIVES_IMPORTANCE[index]} != ${importance} ]]; then
63 + eerror "Differing importance (${ALTERNATIVES_IMPORTANCE[index]} != ${importance}) detected"
64 + ((ret++))
65 + fi
66 + else
67 + ALTERNATIVES_IMPORTANCE[index]=${importance}
68 + [[ -n ${importance} ]] && echo "${importance}" > "${ED}${ALTERNATIVES_DIR}/${alternative}/${provider}/_importance"
69 + fi
70 + fi
71 + done
72 + fi
73 +
74 + while (( $# >= 2 )); do
75 + src=${1//+(\/)/\/}; target=${2//+(\/)/\/}
76 + if [[ ${src} != /* ]]; then
77 + eerror "Source path must be absolute, but got ${src}"
78 + ((ret++))
79 +
80 + else
81 + local reltarget= dir=${ALTERNATIVES_DIR}/${alternative}/${provider}${src%/*}
82 + while [[ -n ${dir} ]]; do
83 + reltarget+=../
84 + dir=${dir%/*}
85 + done
86 +
87 + reltarget=${reltarget%/}
88 + [[ ${target} == /* ]] || reltarget+=${src%/*}/
89 + reltarget+=${target}
90 + dodir "${ALTERNATIVES_DIR}/${alternative}/${provider}${src%/*}"
91 + dosym "${reltarget}" "${ALTERNATIVES_DIR}/${alternative}/${provider}${src}"
92 +
93 + # say ${ED}/sbin/init exists and links to /bin/systemd (which doesn't exist yet)
94 + # the -e test will fail, so check for -L also
95 + if [[ -e ${ED}${src} || -L ${ED}${src} ]]; then
96 + local fulltarget=${target}
97 + [[ ${fulltarget} != /* ]] && fulltarget=${src%/*}/${fulltarget}
98 + if [[ -e ${ED}${fulltarget} || -L ${ED}${fulltarget} ]]; then
99 + die "${src} defined as provider for ${fulltarget}, but both already exist in \${ED}"
100 + else
101 + mv "${ED}${src}" "${ED}${fulltarget}" || die
102 + fi
103 + fi
104 + fi
105 + shift 2
106 + done
107 +
108 + [[ ${ret} -eq 0 ]] || die "Errors detected for ${provider}, provided for ${alternative}"
109 +}
110 +
111 +cleanup_old_alternatives_module() {
112 + local alt=${1} old_module="${EROOT}/usr/share/eselect/modules/${alt}.eselect"
113 + if [[ -f "${old_module}" && "$(source "${old_module}" &>/dev/null; echo "${ALTERNATIVE}")" == "${alt}" ]]; then
114 + local version="$(source "${old_module}" &>/dev/null; echo "${VERSION}")"
115 + if [[ "${version}" == "0.1" || "${version}" == "20080924" ]]; then
116 + echo rm "${old_module}"
117 + rm "${old_module}" || eerror "rm ${old_module} failed"
118 + fi
119 + fi
120 +}
121 +
122 +alternatives-2_pkg_postinst() {
123 + local a alt provider module_version="20090908"
124 + for a in "${ALTERNATIVES_PROVIDED[@]}"; do
125 + alt="${a%:*}"
126 + provider="${a#*:}"
127 + if [[ ! -f "${EROOT}/usr/share/eselect/modules/auto/${alt}.eselect" \
128 + || "$(source "${EROOT}/usr/share/eselect/modules/auto/${alt}.eselect" &>/dev/null; echo "${VERSION}")" \
129 + -ne "${module_version}" ]]; then
130 + #einfo "Creating alternatives module for ${alt}"
131 + if [[ ! -d ${EROOT}/usr/share/eselect/modules/auto ]]; then
132 + install -d "${EROOT}"/usr/share/eselect/modules/auto || eerror "Could not create eselect modules dir"
133 + fi
134 + cat > "${EROOT}/usr/share/eselect/modules/auto/${alt}.eselect" <<-EOF
135 + # This module was automatically generated by alternatives.eclass
136 + DESCRIPTION="Alternatives for ${alt}"
137 + VERSION="${module_version}"
138 + MAINTAINER="eselect@g.o"
139 + ESELECT_MODULE_GROUP="Alternatives"
140 +
141 + ALTERNATIVE="${alt}"
142 +
143 + inherit alternatives
144 + EOF
145 + fi
146 +
147 + #echo eselect "${alt}" update "${provider}"
148 + einfo "Creating ${provider} alternative module for ${alt}"
149 + eselect "${alt}" update "${provider}"
150 +
151 + cleanup_old_alternatives_module ${alt}
152 + done
153 +}
154 +
155 +alternatives-2_pkg_prerm() {
156 + local a alt provider p ignore
157 + [[ -n ${REPLACED_BY_ID} ]] || ignore=" --ignore"
158 + for a in "${ALTERNATIVES_PROVIDED[@]}"; do
159 + alt="${a%:*}"
160 + provider="${a#*:}"
161 + #echo "Making sure ${alt} has a valid provider"
162 + #echo eselect "${alt}" update${ignore} "${provider}"
163 + eselect "${alt}" update${ignore} "${provider}" && continue
164 + einfo "Removed ${provider} alternative module for ${alt}, current is $(eselect ${alt} show)"
165 + if [[ $? -eq 2 ]]; then
166 + einfo "Cleaning up unused alternatives module for ${alt}"
167 + echo rm "${EROOT}/usr/share/eselect/modules/auto/${alt}.eselect"
168 + rm "${EROOT}/usr/share/eselect/modules/auto/${alt}.eselect" ||
169 + eerror rm "${EROOT}/usr/share/eselect/modules/auto/${alt}.eselect" failed
170 + fi
171 + done
172 +}
173 +
174 +EXPORT_FUNCTIONS pkg_postinst pkg_prerm