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

Contents of /eclass/versionator.eclass

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.12 - (show annotations) (download)
Wed Jan 10 05:49:10 2007 UTC (7 years, 11 months ago) by antarus
Branch: MAIN
Changes since 1.11: +66 -13 lines
Fix to versionator to enable extglobbing.

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.11 2006/08/30 16:14:13 spb 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 # delete_version_separator index ver_str
27 # delete_all_version_separators ver_str
28 # get_version_component_count ver_str
29 # get_last_version_component_index ver_str
30 #
31 # Rather than being a number, the index parameter can be a separator character
32 # such as '-', '.' or '_'. In this case, the first separator of this kind is
33 # selected.
34 #
35 # There's also:
36 # version_is_at_least want have
37 # which may be buggy, so use with caution.
38
39 # Quick function to toggle the shopts required for some functions on and off
40 # Used because we can't set extglob in global scope anymore (QA Violation)
41 __versionator_shopt_toggle() {
42 VERSIONATOR_RECURSION=${VERSIONATOR_RECURSION:-0}
43 case "$1" in
44 "on")
45 if [[ $VERSIONATOR_RECURSION -lt 1 ]] ; then
46 VERSIONATOR_OLD_EXTGLOB=$(shopt -p extglob)
47 shopt -s extglob
48 fi
49 VERSIONATOR_RECURSION=$(( $VERSIONATOR_RECURSION + 1 ))
50 ;;
51 "off")
52 VERSIONATOR_RECURSION=$(( $VERSIONATOR_RECURSION - 1 ))
53 if [[ $VERSIONATOR_RECURSION -lt 1 ]] ; then
54 eval $VERSIONATOR_OLD_EXTGLOB
55 fi
56 ;;
57 esac
58 return 0
59 }
60
61 # Split up a version string into its component parts. If no parameter is
62 # supplied, defaults to $PV.
63 # 0.8.3 -> 0 . 8 . 3
64 # 7c -> 7 c
65 # 3.0_p2 -> 3 . 0 _ p2
66 # 20040905 -> 20040905
67 # 3.0c-r1 -> 3 . 0 c - r1
68 get_all_version_components() {
69 __versionator_shopt_toggle on
70 local ver_str=${1:-${PV}} result result_idx=0
71 result=( )
72
73 # sneaky cache trick cache to avoid having to parse the same thing several
74 # times.
75 if [[ "${VERSIONATOR_CACHE_VER_STR}" == "${ver_str}" ]] ; then
76 echo ${VERSIONATOR_CACHE_RESULT}
77 __versionator_shopt_toggle off
78 return
79 fi
80 export VERSIONATOR_CACHE_VER_STR="${ver_str}"
81
82 while [[ -n "$ver_str" ]] ; do
83 case "${ver_str:0:1}" in
84 # number: parse whilst we have a number
85 [[:digit:]])
86 result[$result_idx]="${ver_str%%[^[:digit:]]*}"
87 ver_str="${ver_str##+([[:digit:]])}"
88 result_idx=$(($result_idx + 1))
89 ;;
90
91 # separator: single character
92 [-_.])
93 result[$result_idx]="${ver_str:0:1}"
94 ver_str="${ver_str:1}"
95 result_idx=$(($result_idx + 1))
96 ;;
97
98 # letter: grab the letters plus any following numbers
99 [[:alpha:]])
100 local not_match="${ver_str##+([[:alpha:]])*([[:digit:]])}"
101 result[$result_idx]=${ver_str:0:$((${#ver_str} - ${#not_match}))}
102 ver_str="${not_match}"
103 result_idx=$(($result_idx + 1))
104 ;;
105
106 # huh?
107 *)
108 result[$result_idx]="${ver_str:0:1}"
109 ver_str="${ver_str:1}"
110 result_idx=$(($result_idx + 1))
111 ;;
112 esac
113 done
114
115 export VERSIONATOR_CACHE_RESULT="${result[@]}"
116 echo ${result[@]}
117 __versionator_shopt_toggle off
118 }
119
120 # Get the important version components, excluding '.', '-' and '_'. Defaults to
121 # $PV if no parameter is supplied.
122 # 0.8.3 -> 0 8 3
123 # 7c -> 7 c
124 # 3.0_p2 -> 3 0 p2
125 # 20040905 -> 20040905
126 # 3.0c-r1 -> 3 0 c r1
127 get_version_components() {
128 __versionator_shopt_toggle on
129 local c="$(get_all_version_components "${1:-${PV}}")"
130 c=( ${c[@]//[-._]/ } )
131 echo ${c[@]}
132 __versionator_shopt_toggle off
133 }
134
135 # Get the major version of a value. Defaults to $PV if no parameter is supplied.
136 # 0.8.3 -> 0
137 # 7c -> 7
138 # 3.0_p2 -> 3
139 # 20040905 -> 20040905
140 # 3.0c-r1 -> 3
141 get_major_version() {
142 __versionator_shopt_toggle on
143 local c
144 c=( $(get_all_version_components "${1:-${PV}}" ) )
145 echo ${c[0]}
146 __versionator_shopt_toggle off
147 }
148
149 # Get a particular component or range of components from the version. If no
150 # version parameter is supplied, defaults to $PV.
151 # 1 1.2.3 -> 1
152 # 1-2 1.2.3 -> 1.2
153 # 2- 1.2.3 -> 2.3
154 get_version_component_range() {
155 __versionator_shopt_toggle on
156 local c v="${2:-${PV}}" range="${1}" range_start range_end i=-1 j=0
157 c=( $(get_all_version_components ${v} ) )
158 range_start="${range%-*}" ; range_start="${range_start:-1}"
159 range_end="${range#*-}" ; range_end="${range_end:-${#c[@]}}"
160
161 while (( j < ${range_start} )) ; do
162 i=$(($i + 1))
163 [[ $i -gt ${#c[@]} ]] && __versionator_shopt_toggle off && return
164 [[ -n "${c[${i}]//[-._]}" ]] && j=$(($j + 1))
165 done
166
167 while (( j <= ${range_end} )) ; do
168 echo -n ${c[$i]}
169 [[ $i -gt ${#c[@]} ]] && __versionator_shopt_toggle off && return
170 [[ -n "${c[${i}]//[-._]}" ]] && j=$(($j + 1))
171 i=$(($i + 1))
172 done
173 __versionator_shopt_toggle off
174 }
175
176 # Get everything after the major version and its separator (if present) of a
177 # value. Defaults to $PV if no parameter is supplied.
178 # 0.8.3 -> 8.3
179 # 7c -> c
180 # 3.0_p2 -> 0_p2
181 # 20040905 -> (empty string)
182 # 3.0c-r1 -> 0c-r1
183 get_after_major_version() {
184 __versionator_shopt_toggle on
185 echo $(get_version_component_range 2- "${1:-${PV}}" )
186 __versionator_shopt_toggle off
187 }
188
189 # Replace the $1th separator with $2 in $3 (defaults to $PV if $3 is not
190 # supplied). If there are fewer than $1 separators, don't change anything.
191 # 1 '_' 1.2.3 -> 1_2.3
192 # 2 '_' 1.2.3 -> 1.2_3
193 # 1 '_' 1b-2.3 -> 1b_2.3
194 # Rather than being a number, $1 can be a separator character such as '-', '.'
195 # or '_'. In this case, the first separator of this kind is selected.
196 replace_version_separator() {
197 __versionator_shopt_toggle on
198 local w i c found=0 v="${3:-${PV}}"
199 w=${1:-1}
200 c=( $(get_all_version_components ${v} ) )
201 if [[ "${w//[[:digit:]]/}" == "${w}" ]] ; then
202 # it's a character, not an index
203 for (( i = 0 ; i < ${#c[@]} ; i = $i + 1 )) ; do
204 if [[ "${c[${i}]}" == "${w}" ]] ; then
205 c[${i}]="${2}"
206 break
207 fi
208 done
209 else
210 for (( i = 0 ; i < ${#c[@]} ; i = $i + 1 )) ; do
211 if [[ -n "${c[${i}]//[^-._]}" ]] ; then
212 found=$(($found + 1))
213 if [[ "$found" == "${w}" ]] ; then
214 c[${i}]="${2}"
215 break
216 fi
217 fi
218 done
219 fi
220 c=${c[@]}
221 echo ${c// }
222 __versionator_shopt_toggle off
223 }
224
225 # Replace all version separators in $2 (defaults to $PV) with $1.
226 # '_' 1b.2.3 -> 1b_2_3
227 replace_all_version_separators() {
228 __versionator_shopt_toggle on
229 local c
230 c=( $(get_all_version_components "${2:-${PV}}" ) )
231 c="${c[@]//[-._]/$1}"
232 echo ${c// }
233 __versionator_shopt_toggle off
234 }
235
236 # Delete the $1th separator in $2 (defaults to $PV if $2 is not supplied). If
237 # there are fewer than $1 separators, don't change anything.
238 # 1 1.2.3 -> 12.3
239 # 2 1.2.3 -> 1.23
240 # 1 1b-2.3 -> 1b2.3
241 # Rather than being a number, $1 can be a separator character such as '-', '.'
242 # or '_'. In this case, the first separator of this kind is deleted.
243 delete_version_separator() {
244 __versionator_shopt_toggle on
245 replace_version_separator "${1}" "" "${2}"
246 __versionator_shopt_toggle off
247 }
248
249 # Delete all version separators in $1 (defaults to $PV).
250 # 1b.2.3 -> 1b23
251 delete_all_version_separators() {
252 __versionator_shopt_toggle on
253 replace_all_version_separators "" "${1}"
254 __versionator_shopt_toggle off
255 }
256
257 # How many version components are there in $1 (defaults to $PV)?
258 # 1.0.1 -> 3
259 # 3.0c-r1 -> 4
260 #
261 get_version_component_count() {
262 __versionator_shopt_toggle on
263 local a
264 a=( $(get_version_components "${1:-${PV}}" ) )
265 echo ${#a[@]}
266 __versionator_shopt_toggle off
267 }
268
269 # What is the index of the last version component in $1 (defaults to $PV)?
270 # Equivalent to get_version_component_count - 1.
271 # 1.0.1 -> 3
272 # 3.0c-r1 -> 4
273 #
274 get_last_version_component_index() {
275 __versionator_shopt_toggle on
276 echo $(( $(get_version_component_count "${1:-${PV}}" ) - 1 ))
277 __versionator_shopt_toggle off
278 }
279
280 # Is $2 (defaults to $PVR) at least version $1? Intended for use in eclasses
281 # only. May not be reliable, be sure to do very careful testing before actually
282 # using this. Prod ciaranm if you find something it can't handle.
283 version_is_at_least() {
284 __versionator_shopt_toggle on
285 local want_s="$1" have_s="${2:-${PVR}}" r
286 version_compare "${want_s}" "${have_s}"
287 r=$?
288 case $r in
289 1|2)
290 __versionator_shopt_toggle off
291 return 0
292 ;;
293 3)
294 __versionator_shopt_toggle off
295 return 1
296 ;;
297 *)
298 __versionator_shopt_toggle off
299 die "versionator compare bug [atleast, ${want_s}, ${have_s}, ${r}]"
300 ;;
301 esac
302 __versionator_shopt_toggle off
303 }
304
305 # Takes two parameters (a, b) which are versions. If a is an earlier version
306 # than b, returns 1. If a is identical to b, return 2. If b is later than a,
307 # return 3. You probably want version_is_at_least rather than this function.
308 # May not be very reliable. Test carefully before using this.
309 version_compare() {
310 __versionator_shopt_toggle on
311 local ver_a=${1} ver_b=${2} parts_a parts_b cur_idx_a=0 cur_idx_b=0
312 parts_a=( $(get_all_version_components "${ver_a}" ) )
313 parts_b=( $(get_all_version_components "${ver_b}" ) )
314
315 ### compare number parts.
316 local inf_loop=0
317 while true ; do
318 inf_loop=$(( ${inf_loop} + 1 ))
319 [[ ${inf_loop} -gt 20 ]] && \
320 die "versionator compare bug [numbers, ${ver_a}, ${ver_b}]"
321
322 # grab the current number components
323 local cur_tok_a=${parts_a[${cur_idx_a}]}
324 local cur_tok_b=${parts_b[${cur_idx_b}]}
325
326 # number?
327 if [[ -n ${cur_tok_a} ]] && [[ -z ${cur_tok_a//[[:digit:]]} ]] ; then
328 cur_idx_a=$(( ${cur_idx_a} + 1 ))
329 [[ ${parts_a[${cur_idx_a}]} == "." ]] \
330 && cur_idx_a=$(( ${cur_idx_a} + 1 ))
331 else
332 cur_tok_a=""
333 fi
334
335 if [[ -n ${cur_tok_b} ]] && [[ -z ${cur_tok_b//[[:digit:]]} ]] ; then
336 cur_idx_b=$(( ${cur_idx_b} + 1 ))
337 [[ ${parts_b[${cur_idx_b}]} == "." ]] \
338 && cur_idx_b=$(( ${cur_idx_b} + 1 ))
339 else
340 cur_tok_b=""
341 fi
342
343 # done with number components?
344 [[ -z ${cur_tok_a} ]] && [[ -z ${cur_tok_b} ]] && break
345
346 # to avoid going into octal mode, strip any leading zeros. otherwise
347 # bash will throw a hissy fit on versions like 6.3.068.
348 cur_tok_a=${cur_tok_a##+(0)}
349 cur_tok_b=${cur_tok_b##+(0)}
350
351 # if a component is blank, make it zero.
352 [[ -z ${cur_tok_a} ]] && cur_tok_a=0
353 [[ -z ${cur_tok_b} ]] && cur_tok_b=0
354
355 # compare
356 [[ ${cur_tok_a} -lt ${cur_tok_b} ]] && __versionator_shopt_toggle off && return 1
357 [[ ${cur_tok_a} -gt ${cur_tok_b} ]] && __versionator_shopt_toggle off && return 3
358 done
359
360 ### number parts equal. compare letter parts.
361 local letter_a=
362 letter_a=${parts_a[${cur_idx_a}]}
363 if [[ ${#letter_a} -eq 1 ]] && [[ -z ${letter_a/[a-z]} ]] ; then
364 cur_idx_a=$(( ${cur_idx_a} + 1 ))
365 else
366 letter_a="@"
367 fi
368
369 local letter_b=
370 letter_b=${parts_b[${cur_idx_b}]}
371 if [[ ${#letter_b} -eq 1 ]] && [[ -z ${letter_b/[a-z]} ]] ; then
372 cur_idx_b=$(( ${cur_idx_b} + 1 ))
373 else
374 letter_b="@"
375 fi
376
377 # compare
378 [[ ${letter_a} < ${letter_b} ]] && __versionator_shopt_toggle off && return 1
379 [[ ${letter_a} > ${letter_b} ]] && __versionator_shopt_toggle off && return 3
380
381 ### letter parts equal. compare suffixes in order.
382 local suffix rule part r_lt r_gt
383 for rule in "alpha=1" "beta=1" "pre=1" "rc=1" "p=3" "r=3" ; do
384 suffix=${rule%%=*}
385 r_lt=${rule##*=}
386 [[ ${r_lt} -eq 1 ]] && r_gt=3 || r_gt=1
387
388 local suffix_a=
389 for part in ${parts_a[@]} ; do
390 [[ ${part#${suffix}} != ${part} ]] && \
391 [[ -z ${part##${suffix}*([[:digit:]])} ]] && \
392 suffix_a=${part#${suffix}}0
393 done
394
395 local suffix_b=
396 for part in ${parts_b[@]} ; do
397 [[ ${part#${suffix}} != ${part} ]] && \
398 [[ -z ${part##${suffix}*([[:digit:]])} ]] && \
399 suffix_b=${part#${suffix}}0
400 done
401
402 [[ -z ${suffix_a} ]] && [[ -z ${suffix_b} ]] && continue
403
404 [[ -z ${suffix_a} ]] && __versionator_shopt_toggle off && return ${r_gt}
405 [[ -z ${suffix_b} ]] && __versionator_shopt_toggle off && return ${r_lt}
406
407 # avoid octal problems
408 suffix_a=${suffix_a##+(0)} ; suffix_a=${suffix_a:-0}
409 suffix_b=${suffix_b##+(0)} ; suffix_b=${suffix_b:-0}
410
411 [[ ${suffix_a} -lt ${suffix_b} ]] && __versionator_shopt_toggle off && return 1
412 [[ ${suffix_a} -gt ${suffix_b} ]] && __versionator_shopt_toggle off && return 3
413 done
414
415 ### no differences.
416 __versionator_shopt_toggle off
417 return 2
418 }
419
420 # Returns its parameters sorted, highest version last. We're using a quadratic
421 # algorithm for simplicity, so don't call it with more than a few dozen items.
422 # Uses version_compare, so be careful.
423 version_sort() {
424 __versionator_shopt_toggle on
425 local items= left=0
426 items=( $@ )
427 while [[ ${left} -lt ${#items[@]} ]] ; do
428 local lowest_idx=${left}
429 local idx=$(( ${lowest_idx} + 1 ))
430 while [[ ${idx} -lt ${#items[@]} ]] ; do
431 version_compare "${items[${lowest_idx}]}" "${items[${idx}]}"
432 [[ $? -eq 3 ]] && lowest_idx=${idx}
433 idx=$(( ${idx} + 1 ))
434 done
435 local tmp=${items[${lowest_idx}]}
436 items[${lowest_idx}]=${items[${left}]}
437 items[${left}]=${tmp}
438 left=$(( ${left} + 1 ))
439 done
440 echo ${items[@]}
441 __versionator_shopt_toggle off
442 }
443
444 __versionator__test_version_compare() {
445 __versionator_shopt_toggle on
446 local lt=1 eq=2 gt=3 p q
447
448 __versionator__test_version_compare_t() {
449 version_compare "${1}" "${3}"
450 local r=$?
451 [[ ${r} -eq ${2} ]] || echo "FAIL: ${@} (got ${r} exp ${2})"
452 }
453
454 echo "
455 0 $lt 1
456 1 $lt 2
457 2 $gt 1
458 2 $eq 2
459 0 $eq 0
460 10 $lt 20
461 68 $eq 068
462 068 $gt 67
463 068 $lt 69
464
465 1.0 $lt 2.0
466 2.0 $eq 2.0
467 2.0 $gt 1.0
468
469 1.0 $gt 0.0
470 0.0 $eq 0.0
471 0.0 $lt 1.0
472
473 0.1 $lt 0.2
474 0.2 $eq 0.2
475 0.3 $gt 0.2
476
477 1.2 $lt 2.1
478 2.1 $gt 1.2
479
480 1.2.3 $lt 1.2.4
481 1.2.4 $gt 1.2.3
482
483 1.2.0 $eq 1.2
484 1.2.1 $gt 1.2
485 1.2 $lt 1.2.1
486
487 1.2b $eq 1.2b
488 1.2b $lt 1.2c
489 1.2b $gt 1.2a
490 1.2b $gt 1.2
491 1.2 $lt 1.2a
492
493 1.3 $gt 1.2a
494 1.3 $lt 1.3a
495
496 1.0_alpha7 $lt 1.0_beta7
497 1.0_beta $lt 1.0_pre
498 1.0_pre5 $lt 1.0_rc2
499 1.0_rc2 $lt 1.0
500
501 1.0_p1 $gt 1.0
502 1.0_p1-r1 $gt 1.0_p1
503
504 1.0_alpha6-r1 $gt 1.0_alpha6
505 1.0_beta6-r1 $gt 1.0_alpha6-r2
506
507 1.0_pre1 $lt 1.0-p1
508
509 1.0p $gt 1.0_p1
510 1.0r $gt 1.0-r1
511 1.6.15 $gt 1.6.10-r2
512 1.6.10-r2 $lt 1.6.15
513
514 " | while read a b c ; do
515 [[ -z "${a}${b}${c}" ]] && continue;
516 __versionator__test_version_compare_t "${a}" "${b}" "${c}"
517 done
518
519
520 for q in "alpha beta pre rc=${lt};${gt}" "p r=${gt};${lt}" ; do
521 for p in ${q%%=*} ; do
522 local c=${q##*=}
523 local alt=${c%%;*} agt=${c##*;}
524 __versionator__test_version_compare_t "1.0" $agt "1.0_${p}"
525 __versionator__test_version_compare_t "1.0" $agt "1.0_${p}1"
526 __versionator__test_version_compare_t "1.0" $agt "1.0_${p}068"
527
528 __versionator__test_version_compare_t "2.0_${p}" $alt "2.0"
529 __versionator__test_version_compare_t "2.0_${p}1" $alt "2.0"
530 __versionator__test_version_compare_t "2.0_${p}068" $alt "2.0"
531
532 __versionator__test_version_compare_t "1.0_${p}" $eq "1.0_${p}"
533 __versionator__test_version_compare_t "0.0_${p}" $lt "0.0_${p}1"
534 __versionator__test_version_compare_t "666_${p}3" $gt "666_${p}"
535
536 __versionator__test_version_compare_t "1_${p}7" $lt "1_${p}8"
537 __versionator__test_version_compare_t "1_${p}7" $eq "1_${p}7"
538 __versionator__test_version_compare_t "1_${p}7" $gt "1_${p}6"
539 __versionator__test_version_compare_t "1_${p}09" $eq "1_${p}9"
540 done
541 done
542
543 for p in "-r" "_p" ; do
544 __versionator__test_version_compare_t "7.2${p}1" $lt "7.2${p}2"
545 __versionator__test_version_compare_t "7.2${p}2" $gt "7.2${p}1"
546 __versionator__test_version_compare_t "7.2${p}3" $gt "7.2${p}2"
547 __versionator__test_version_compare_t "7.2${p}2" $lt "7.2${p}3"
548 done
549 __versionator_shopt_toggle off
550 }

  ViewVC Help
Powered by ViewVC 1.1.20