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

Contents of /eclass/versionator.eclass

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.2 - (show annotations) (download)
Fri Sep 10 21:42:21 2004 UTC (9 years, 11 months ago) by ciaranm
Branch: MAIN
Changes since 1.1: +100 -1 lines
add experimental version_is_at_least

1 # Copyright 1999-2004 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3 # $Header: /var/cvsroot/gentoo-x86/eclass/versionator.eclass,v 1.1 2004/09/10 18:45:01 ciaranm Exp $
4 #
5 # Original Author: Ciaran McCreesh <ciaranm@gentoo.org>
6 #
7 # This eclass provides functions which simplify manipulating $PV and similar
8 # variables. Most functions default to working with $PV, although other
9 # values can be used.
10 #
11 # Simple Example 1: $PV is 1.2.3b, we want 1_2.3b:
12 # MY_PV=$(replace_version_separator 1 '_' )
13 #
14 # Simple Example 2: $PV is 1.4.5, we want 1:
15 # MY_MAJORV=$(get_major_version )
16 #
17 # Full list of user usable functions provided by this eclass (see the functions
18 # themselves for documentation):
19 # get_all_version_components ver_str
20 # get_version_components ver_str
21 # get_major_version ver_str
22 # get_version_component_range range ver_str
23 # get_after_major_version ver_str
24 # replace_version_separator index newvalue ver_str
25 # replace_all_version_separators newvalue ver_str
26 #
27 # There's also:
28 # version_is_at_least want have
29 # but it's *really* experimental...
30
31 ECLASS=versionator
32 INHERITED="$INHERITED $ECLASS"
33
34 shopt -s extglob
35
36 # Split up a version string into its component parts. If no parameter is
37 # supplied, defaults to $PV.
38 # 0.8.3 -> 0 . 8 . 3
39 # 7c -> 7 c
40 # 3.0_p2 -> 3 . 0 _ p2
41 # 20040905 -> 20040905
42 # 3.0c-r1 -> 3 . 0 c - r1
43 get_all_version_components() {
44 local ver_str=${1:-${PV}} result result_idx=0
45 result=( )
46
47 # sneaky cache trick cache to avoid having to parse the same thing several
48 # times.
49 if [[ "${VERSIONATOR_CACHE_VER_STR}" == "${ver_str}" ]] ; then
50 echo ${VERSIONATOR_CACHE_RESULT}
51 return
52 fi
53 export VERSIONATOR_CACHE_VER_STR="${ver_str}"
54
55 while [[ -n "$ver_str" ]] ; do
56 case "${ver_str:0:1}" in
57 # number: parse whilst we have a number
58 [[:digit:]])
59 result[$result_idx]="${ver_str%%[^[:digit:]]*}"
60 ver_str="${ver_str##+([[:digit:]])}"
61 result_idx=$(($result_idx + 1))
62 ;;
63
64 # separator: single character
65 [-_.])
66 result[$result_idx]="${ver_str:0:1}"
67 ver_str="${ver_str:1}"
68 result_idx=$(($result_idx + 1))
69 ;;
70
71 # letter: grab the letters plus any following numbers
72 [[:alpha:]])
73 local not_match="${ver_str##+([[:alpha:]])*([[:digit:]])}"
74 result[$result_idx]=${ver_str:0:$((${#ver_str} - ${#not_match}))}
75 ver_str="${not_match}"
76 result_idx=$(($result_idx + 1))
77 ;;
78
79 # huh?
80 *)
81 result[$result_idx]="${ver_str:0:1}"
82 ver_str="${ver_str:1}"
83 result_idx=$(($result_idx + 1))
84 ;;
85 esac
86 done
87
88 export VERSIONATOR_CACHE_RESULT="${result[@]}"
89 echo ${result[@]}
90 }
91
92 # Get the important version components, excluding '.', '-' and '_'. Defaults to
93 # $PV if no parameter is supplied.
94 # 0.8.3 -> 0 8 3
95 # 7c -> 7 c
96 # 3.0_p2 -> 3 0 p2
97 # 20040905 -> 20040905
98 # 3.0c-r1 -> 3 0 c r1
99 get_version_components() {
100 local c="$(get_all_version_components "${1:-${PV}}")"
101 c=( ${c[@]//[-._]/ } )
102 echo ${c[@]}
103 }
104
105 # Get the major version of a value. Defaults to $PV if no parameter is supplied.
106 # 0.8.3 -> 0
107 # 7c -> 7
108 # 3.0_p2 -> 3
109 # 20040905 -> 20040905
110 # 3.0c-r1 -> 3
111 get_major_version() {
112 local c
113 c=( $(get_all_version_components "${1:-${PV}}" ) )
114 echo ${c[0]}
115 }
116
117 # Get a particular component or range of components from the version. If no
118 # version parameter is supplied, defaults to $PV.
119 # 1 1.2.3 -> 1
120 # 1-2 1.2.3 -> 1.2
121 # 2- 1.2.3 -> 2.3
122 get_version_component_range() {
123 local c v="${2:-${PV}}" range="${1}" range_start range_end i=-1 j=0
124 c=( $(get_all_version_components ${v} ) )
125 range_start="${range%-*}" ; range_start="${range_start:-1}"
126 range_end="${range#*-}" ; range_end="${range_end:-${#c[@]}}"
127
128 while (( j < ${range_start} )) ; do
129 i=$(($i + 1))
130 [[ $i -gt ${#c[@]} ]] && return
131 [[ -n "${c[${i}]//[-._]}" ]] && j=$(($j + 1))
132 done
133
134 while (( j <= ${range_end} )) ; do
135 echo -n ${c[$i]}
136 [[ $i -gt ${#c[@]} ]] && return
137 [[ -n "${c[${i}]//[-._]}" ]] && j=$(($j + 1))
138 i=$(($i + 1))
139 done
140 }
141
142 # Get everything after the major version and its separator (if present) of a
143 # value. Defaults to $PV if no parameter is supplied.
144 # 0.8.3 -> 8.3
145 # 7c -> c
146 # 3.0_p2 -> 0_p2
147 # 20040905 -> (empty string)
148 # 3.0c-r1 -> 0c-r1
149 get_after_major_version() {
150 echo $(get_version_component_range 2- "${1:-PV}" )
151 }
152
153 # Replace the $1th separator with $2 in $3 (defaults to $PV if $3 is not
154 # supplied). If there are fewer than $1 separators, don't change anything.
155 # 1 '_' 1.2.3 -> 1_2.3
156 # 2 '_' 1.2.3 -> 1.2_3
157 # 1 '_' 1b-2.3 -> 1b_2.3
158 replace_version_separator() {
159 local i c found=0 v="${3:-${PV}}"
160 c=( $(get_all_version_components ${v} ) )
161 for (( i = 0 ; i < ${#c[@]} ; i = $i + 1 )) ; do
162 if [[ -n "${c[${i}]//[^-._]}" ]] ; then
163 found=$(($found + 1))
164 if [[ "$found" == "${1:-1}" ]] ; then
165 c[${i}]="${2}"
166 break
167 fi
168 fi
169 done
170 c=${c[@]}
171 echo ${c// }
172 }
173
174 # Replace all version separators in $2 (defaults to $PV) with $1.
175 # '_' 1b.2.3 -> 1b_2_3
176 replace_all_version_separators() {
177 local c
178 c=( $(get_all_version_components "${2:-${PV}}" ) )
179 c="${c[@]//[-._]/$1}"
180 echo ${c// }
181 }
182
183 # Is $2 (defaults to $PVR) at least version $1? Intended for use in eclasses
184 # only. Not very reliable, doesn't understand most things, make sure you test
185 # reaaaallly well before using this. Prod ciaranm if you need it to support more
186 # things... WARNING: DOES NOT HANDLE 1.2b style versions. WARNING: not very well
187 # tested, needs lots of work before it's totally reliable. Use with extreme
188 # caution.
189 version_is_at_least() {
190 local want_s="$1" have_s="${2:-${PVR}}" want_c have_c
191 want_c=( $(get_version_components "$want_s" ) )
192 have_c=( $(get_version_components "$have_s" ) )
193
194 # Stage 1: compare the version numbers part.
195 local done_w="" done_h="" i=0
196 while [[ -z "${done_w}" ]] || [[ -z "${done_h}" ]] ; do
197 local cur_w="${want_c[$i]}" cur_h="${have_c[$i]}"
198 [[ -z "${cur_w##[^[:digit:]]*}" ]] && done_w="yes"
199 [[ -z "${cur_h##[^[:digit:]]*}" ]] && done_h="yes"
200 [[ -z "${done_w}" ]] || cur_w=0
201 [[ -z "${done_h}" ]] || cur_h=0
202 if [[ ${cur_w} -lt ${cur_h} ]] ; then return 0 ; fi
203 if [[ ${cur_w} -gt ${cur_h} ]] ; then return 1 ; fi
204 i=$(($i + 1))
205 done
206
207 local part
208 for part in "_alpha" "_beta" "_pre" "_rc" "_p" "-r" ; do
209 local part_w= part_h=
210
211 for (( i = 0 ; i < ${#want_c[@]} ; i = $i + 1 )) ; do
212 if [[ -z "${want_c[$i]##${part#[-._]}*}" ]] ; then
213 part_w="${want_c[$i]##${part#[-._]}}"
214 break
215 fi
216 done
217 for (( i = 0 ; i < ${#have_c[@]} ; i = $i + 1 )) ; do
218 if [[ -z "${have_c[$i]##${part#[-._]}*}" ]] ; then
219 part_h="${have_c[$i]##${part#[-._]}}"
220 break
221 fi
222 done
223
224 if [[ "${part}" == "_p" ]] || [[ "${part}" == "-r" ]] ; then
225 # if present in neither want nor have, go to the next item
226 [[ -z "${part_w}" ]] && [[ -z "${part_h}" ]] && continue
227
228 [[ -z "${part_w}" ]] && [[ -n "${part_h}" ]] && return 0
229 [[ -n "${part_w}" ]] && [[ -z "${part_h}" ]] && return 1
230
231 if [[ ${part_w} -lt ${part_h} ]] ; then return 0 ; fi
232 if [[ ${part_w} -gt ${part_h} ]] ; then return 1 ; fi
233
234 else
235 # if present in neither want nor have, go to the next item
236 [[ -z "${part_w}" ]] && [[ -z "${part_h}" ]] && continue
237
238 [[ -z "${part_w}" ]] && [[ -n "${part_h}" ]] && return 1
239 [[ -n "${part_w}" ]] && [[ -z "${part_h}" ]] && return 0
240
241 if [[ ${part_w} -lt ${part_h} ]] ; then return 0 ; fi
242 if [[ ${part_w} -gt ${part_h} ]] ; then return 1 ; fi
243 fi
244 done
245
246 return 0
247 }
248
249 # Test function thing. To use, source versionator.eclass and then run it.
250 __versionator__test_version_is_at_least() {
251 version_is_at_least "1.2" "1.2" || echo "test 1 failed"
252 version_is_at_least "1.2" "1.2.3" || echo "test 2 failed"
253 version_is_at_least "1.2.3" "1.2" && echo "test 3 failed"
254
255 version_is_at_least "1.2_beta1" "1.2" || echo "test 4 failed"
256 version_is_at_least "1.2_alpha1" "1.2" || echo "test 5 failed"
257 version_is_at_least "1.2_alpha1" "1.2_beta1" || echo "test 6 failed"
258
259 version_is_at_least "1.2" "1.2_beta1" && echo "test 7 failed"
260 version_is_at_least "1.2" "1.2_alpha1" && echo "test 8 failed"
261 version_is_at_least "1.2_beta1" "1.2_alpha1" && echo "test 9 failed"
262
263 version_is_at_least "1.2_beta1" "1.2_beta1" || echo "test 10 failed"
264 version_is_at_least "1.2_beta2" "1.2_beta1" && echo "test 11 failed"
265 version_is_at_least "1.2_beta2" "1.2_beta3" || echo "test 12 failed"
266
267 version_is_at_least "1.2-r1" "1.2" && echo "test 13 failed"
268 version_is_at_least "1.2" "1.2-r1" || echo "test 14 failed"
269 version_is_at_least "1.2-r1" "1.3" || echo "test 15 failed"
270 version_is_at_least "1.2-r1" "1.2-r2" || echo "test 16 failed"
271 version_is_at_least "1.2-r3" "1.2-r2" && echo "test 17 failed"
272
273 version_is_at_least "1.2-r1" "1.2_beta2-r3" && echo "test 18 failed"
274 version_is_at_least "1.2-r1" "1.3_beta2-r3" || echo "test 19 failed"
275 return 0
276 }

  ViewVC Help
Powered by ViewVC 1.1.20