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

Contents of /eclass/versionator.eclass

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.8 - (hide annotations) (download)
Wed Jul 6 20:20:04 2005 UTC (9 years, 2 months ago) by agriffis
Branch: MAIN
Changes since 1.7: +1 -2 lines
Remove ECLASS= and INHERITED= which haven't been necessary for a long time now

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

  ViewVC Help
Powered by ViewVC 1.1.20