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

Contents of /eclass/versionator.eclass

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.14 - (show annotations) (download)
Sun Jun 1 01:30:31 2008 UTC (6 years, 3 months ago) by vapier
Branch: MAIN
Changes since 1.13: +55 -27 lines
add eclass documentation by mren #210316

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

  ViewVC Help
Powered by ViewVC 1.1.20