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

Contents of /eclass/versionator.eclass

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.7 - (hide annotations) (download)
Sat May 21 04:56:20 2005 UTC (9 years, 7 months ago) by ciaranm
Branch: MAIN
Changes since 1.6: +247 -87 lines
updated to do better comparisons, bug #87157

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

  ViewVC Help
Powered by ViewVC 1.1.20