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

Contents of /eclass/versionator.eclass

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.23 - (hide annotations) (download)
Tue Dec 27 17:55:12 2011 UTC (2 years, 8 months ago) by fauli
Branch: MAIN
Changes since 1.22: +2 -2 lines
Update copyright years in headers

1 fauli 1.23 # Copyright 1999-2011 Gentoo Foundation
2 ciaranm 1.1 # Distributed under the terms of the GNU General Public License v2
3 fauli 1.23 # $Header: /var/cvsroot/gentoo-x86/eclass/versionator.eclass,v 1.22 2011/12/15 05:10:25 vapier Exp $
4 vapier 1.14
5     # @ECLASS: versionator.eclass
6     # @MAINTAINER:
7 vapier 1.20 # Jonathan Callen <abcd@gentoo.org>
8     # base-system@gentoo.org
9 vapier 1.14 # @BLURB: functions which simplify manipulation of ${PV} and similar version strings
10     # @DESCRIPTION:
11 ciaranm 1.1 # This eclass provides functions which simplify manipulating $PV and similar
12     # variables. Most functions default to working with $PV, although other
13     # values can be used.
14 vapier 1.14 # @EXAMPLE:
15 ciaranm 1.1 # Simple Example 1: $PV is 1.2.3b, we want 1_2.3b:
16     # MY_PV=$(replace_version_separator 1 '_' )
17     #
18     # Simple Example 2: $PV is 1.4.5, we want 1:
19     # MY_MAJORV=$(get_major_version )
20     #
21 ciaranm 1.3 # Rather than being a number, the index parameter can be a separator character
22     # such as '-', '.' or '_'. In this case, the first separator of this kind is
23     # selected.
24 ciaranm 1.2 #
25     # There's also:
26     # version_is_at_least want have
27 vapier 1.14 # which may be buggy, so use with caution.
28 ciaranm 1.1
29 vapier 1.21 if [[ ${___ECLASS_ONCE_VERSIONATOR} != "recur -_+^+_- spank" ]] ; then
30     ___ECLASS_ONCE_VERSIONATOR="recur -_+^+_- spank"
31    
32 vapier 1.17 inherit eutils
33 ciaranm 1.1
34 vapier 1.14 # @FUNCTION: get_all_version_components
35     # @USAGE: [version]
36     # @DESCRIPTION:
37 ciaranm 1.1 # Split up a version string into its component parts. If no parameter is
38     # supplied, defaults to $PV.
39     # 0.8.3 -> 0 . 8 . 3
40     # 7c -> 7 c
41     # 3.0_p2 -> 3 . 0 _ p2
42     # 20040905 -> 20040905
43     # 3.0c-r1 -> 3 . 0 c - r1
44     get_all_version_components() {
45 vapier 1.17 eshopts_push -s extglob
46 abcd 1.18 local ver_str=${1:-${PV}} result
47     result=()
48 ciaranm 1.1
49     # sneaky cache trick cache to avoid having to parse the same thing several
50     # times.
51 abcd 1.18 if [[ ${VERSIONATOR_CACHE_VER_STR} == ${ver_str} ]] ; then
52 ciaranm 1.1 echo ${VERSIONATOR_CACHE_RESULT}
53 vapier 1.17 eshopts_pop
54 ciaranm 1.1 return
55     fi
56 abcd 1.18 export VERSIONATOR_CACHE_VER_STR=${ver_str}
57 ciaranm 1.1
58 abcd 1.18 while [[ -n $ver_str ]] ; do
59     case "${ver_str::1}" in
60 ciaranm 1.1 # number: parse whilst we have a number
61     [[:digit:]])
62 abcd 1.18 result+=("${ver_str%%[^[:digit:]]*}")
63     ver_str=${ver_str##+([[:digit:]])}
64 ciaranm 1.1 ;;
65    
66     # separator: single character
67     [-_.])
68 abcd 1.18 result+=("${ver_str::1}")
69     ver_str=${ver_str:1}
70 ciaranm 1.1 ;;
71    
72     # letter: grab the letters plus any following numbers
73     [[:alpha:]])
74 abcd 1.18 local not_match=${ver_str##+([[:alpha:]])*([[:digit:]])}
75     # Can't say "${ver_str::-${#not_match}}" in Bash 3.2
76     result+=("${ver_str::${#ver_str} - ${#not_match}}")
77     ver_str=${not_match}
78 ciaranm 1.1 ;;
79    
80     # huh?
81     *)
82 abcd 1.18 result+=("${ver_str::1}")
83     ver_str=${ver_str:1}
84 ciaranm 1.1 ;;
85     esac
86     done
87    
88 abcd 1.18 export VERSIONATOR_CACHE_RESULT=${result[*]}
89 ciaranm 1.1 echo ${result[@]}
90 vapier 1.17 eshopts_pop
91 ciaranm 1.1 }
92    
93 vapier 1.14 # @FUNCTION: get_version_components
94     # @USAGE: [version]
95     # @DESCRIPTION:
96 ciaranm 1.1 # Get the important version components, excluding '.', '-' and '_'. Defaults to
97     # $PV if no parameter is supplied.
98     # 0.8.3 -> 0 8 3
99     # 7c -> 7 c
100     # 3.0_p2 -> 3 0 p2
101     # 20040905 -> 20040905
102     # 3.0c-r1 -> 3 0 c r1
103     get_version_components() {
104 abcd 1.18 local c=$(get_all_version_components "${1:-${PV}}")
105     echo ${c//[-._]/ }
106 ciaranm 1.1 }
107    
108 vapier 1.14 # @FUNCTION: get_major_version
109     # @USAGE: [version]
110     # @DESCRIPTION:
111 ciaranm 1.1 # 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 abcd 1.18 local c=($(get_all_version_components "${1:-${PV}}"))
119 ciaranm 1.1 echo ${c[0]}
120     }
121    
122 vapier 1.14 # @FUNCTION: get_version_component_range
123 vapier 1.19 # @USAGE: <range> [version]
124 vapier 1.14 # @DESCRIPTION:
125 ciaranm 1.1 # Get a particular component or range of components from the version. If no
126     # version parameter is supplied, defaults to $PV.
127     # 1 1.2.3 -> 1
128     # 1-2 1.2.3 -> 1.2
129     # 2- 1.2.3 -> 2.3
130     get_version_component_range() {
131 vapier 1.17 eshopts_push -s extglob
132 abcd 1.18 local c v="${2:-${PV}}" range="${1}" range_start range_end
133     local -i i=-1 j=0
134     c=($(get_all_version_components "${v}"))
135     range_start=${range%-*}; range_start=${range_start:-1}
136     range_end=${range#*-} ; range_end=${range_end:-${#c[@]}}
137    
138     while ((j < range_start)); do
139     i+=1
140     ((i > ${#c[@]})) && eshopts_pop && return
141     [[ -n "${c[i]//[-._]}" ]] && j+=1
142 ciaranm 1.1 done
143    
144 abcd 1.18 while ((j <= range_end)); do
145     echo -n ${c[i]}
146     ((i > ${#c[@]})) && eshopts_pop && return
147     [[ -n "${c[i]//[-._]}" ]] && j+=1
148     i+=1
149 ciaranm 1.1 done
150 vapier 1.17 eshopts_pop
151 ciaranm 1.1 }
152    
153 vapier 1.14 # @FUNCTION: get_after_major_version
154     # @USAGE: [version]
155     # @DESCRIPTION:
156 ciaranm 1.1 # Get everything after the major version and its separator (if present) of a
157     # value. Defaults to $PV if no parameter is supplied.
158     # 0.8.3 -> 8.3
159     # 7c -> c
160     # 3.0_p2 -> 0_p2
161     # 20040905 -> (empty string)
162     # 3.0c-r1 -> 0c-r1
163     get_after_major_version() {
164 abcd 1.18 echo $(get_version_component_range 2- "${1:-${PV}}")
165 ciaranm 1.1 }
166    
167 opfer 1.15 # @FUNCTION: replace_version_separator
168 vapier 1.14 # @USAGE: <search> <replacement> [subject]
169     # @DESCRIPTION:
170 ciaranm 1.1 # Replace the $1th separator with $2 in $3 (defaults to $PV if $3 is not
171     # supplied). If there are fewer than $1 separators, don't change anything.
172     # 1 '_' 1.2.3 -> 1_2.3
173     # 2 '_' 1.2.3 -> 1.2_3
174     # 1 '_' 1b-2.3 -> 1b_2.3
175 ciaranm 1.3 # Rather than being a number, $1 can be a separator character such as '-', '.'
176     # or '_'. In this case, the first separator of this kind is selected.
177 ciaranm 1.1 replace_version_separator() {
178 vapier 1.17 eshopts_push -s extglob
179 abcd 1.18 local w c v="${3:-${PV}}"
180     declare -i i found=0
181 ciaranm 1.3 w=${1:-1}
182 abcd 1.18 c=($(get_all_version_components ${v}))
183     if [[ ${w} != *[[:digit:]]* ]] ; then
184 ciaranm 1.3 # it's a character, not an index
185 abcd 1.18 for ((i = 0; i < ${#c[@]}; i++)); do
186     if [[ ${c[i]} == ${w} ]]; then
187     c[i]=${2}
188 ciaranm 1.1 break
189     fi
190 ciaranm 1.3 done
191     else
192 abcd 1.18 for ((i = 0; i < ${#c[@]}; i++)); do
193     if [[ -n "${c[i]//[^-._]}" ]]; then
194     found+=1
195     if ((found == w)); then
196     c[i]=${2}
197 ciaranm 1.3 break
198     fi
199     fi
200     done
201     fi
202 abcd 1.18 c=${c[*]}
203 ciaranm 1.1 echo ${c// }
204 vapier 1.17 eshopts_pop
205 ciaranm 1.1 }
206    
207 vapier 1.14 # @FUNCTION: replace_all_version_separators
208     # @USAGE: <replacement> [subject]
209     # @DESCRIPTION:
210 ciaranm 1.1 # Replace all version separators in $2 (defaults to $PV) with $1.
211     # '_' 1b.2.3 -> 1b_2_3
212     replace_all_version_separators() {
213 abcd 1.18 local c=($(get_all_version_components "${2:-${PV}}"))
214     c=${c[@]//[-._]/$1}
215 ciaranm 1.1 echo ${c// }
216     }
217    
218 vapier 1.14 # @FUNCTION: delete_version_separator
219     # @USAGE: <search> [subject]
220     # @DESCRIPTION:
221 ciaranm 1.4 # Delete the $1th separator in $2 (defaults to $PV if $2 is not supplied). If
222 ciaranm 1.3 # there are fewer than $1 separators, don't change anything.
223     # 1 1.2.3 -> 12.3
224     # 2 1.2.3 -> 1.23
225     # 1 1b-2.3 -> 1b2.3
226     # Rather than being a number, $1 can be a separator character such as '-', '.'
227     # or '_'. In this case, the first separator of this kind is deleted.
228     delete_version_separator() {
229     replace_version_separator "${1}" "" "${2}"
230     }
231    
232 vapier 1.14 # @FUNCTION: delete_all_version_separators
233     # @USAGE: [subject]
234     # @DESCRIPTION:
235 ciaranm 1.3 # Delete all version separators in $1 (defaults to $PV).
236 ciaranm 1.5 # 1b.2.3 -> 1b23
237 ciaranm 1.3 delete_all_version_separators() {
238 ciaranm 1.5 replace_all_version_separators "" "${1}"
239 ciaranm 1.3 }
240    
241 vapier 1.14 # @FUNCTION: get_version_component_count
242     # @USAGE: [version]
243     # @DESCRIPTION:
244 kugelfang 1.10 # How many version components are there in $1 (defaults to $PV)?
245     # 1.0.1 -> 3
246     # 3.0c-r1 -> 4
247     get_version_component_count() {
248 abcd 1.18 local a=($(get_version_components "${1:-${PV}}"))
249 kugelfang 1.10 echo ${#a[@]}
250     }
251    
252 vapier 1.14 # @FUNCTION: get_last_version_component_index
253     # @USAGE: [version]
254     # @DESCRIPTION:
255 kugelfang 1.10 # What is the index of the last version component in $1 (defaults to $PV)?
256     # Equivalent to get_version_component_count - 1.
257 abcd 1.18 # 1.0.1 -> 2
258     # 3.0c-r1 -> 3
259 kugelfang 1.10 get_last_version_component_index() {
260 abcd 1.18 echo $(($(get_version_component_count "${1:-${PV}}" ) - 1))
261 kugelfang 1.10 }
262    
263 vapier 1.14 # @FUNCTION: version_is_at_least
264     # @USAGE: <want> [have]
265     # @DESCRIPTION:
266 ciaranm 1.2 # Is $2 (defaults to $PVR) at least version $1? Intended for use in eclasses
267 ciaranm 1.7 # only. May not be reliable, be sure to do very careful testing before actually
268 vapier 1.14 # using this.
269 ciaranm 1.2 version_is_at_least() {
270 ciaranm 1.7 local want_s="$1" have_s="${2:-${PVR}}" r
271     version_compare "${want_s}" "${have_s}"
272     r=$?
273     case $r in
274     1|2)
275     return 0
276     ;;
277     3)
278     return 1
279     ;;
280     *)
281     die "versionator compare bug [atleast, ${want_s}, ${have_s}, ${r}]"
282     ;;
283     esac
284     }
285    
286 vapier 1.14 # @FUNCTION: version_compare
287     # @USAGE: <A> <B>
288     # @DESCRIPTION:
289     # Takes two parameters (A, B) which are versions. If A is an earlier version
290     # than B, returns 1. If A is identical to B, return 2. If A is later than B,
291 ciaranm 1.7 # return 3. You probably want version_is_at_least rather than this function.
292     # May not be very reliable. Test carefully before using this.
293     version_compare() {
294 vapier 1.17 eshopts_push -s extglob
295 abcd 1.18 local ver_a=${1} ver_b=${2} parts_a parts_b
296     local cur_tok_a cur_tok_b num_part_a num_part_b
297     local -i cur_idx_a=0 cur_idx_b=0 prev_idx_a prev_idx_b
298 ciaranm 1.7 parts_a=( $(get_all_version_components "${ver_a}" ) )
299     parts_b=( $(get_all_version_components "${ver_b}" ) )
300    
301     ### compare number parts.
302 abcd 1.18 local -i inf_loop=0
303     while true; do
304     inf_loop+=1
305     ((inf_loop > 20)) && \
306 ciaranm 1.7 die "versionator compare bug [numbers, ${ver_a}, ${ver_b}]"
307    
308 abcd 1.18 # Store the current index to test later
309     prev_idx_a=cur_idx_a
310     prev_idx_b=cur_idx_b
311    
312 ciaranm 1.7 # grab the current number components
313 abcd 1.18 cur_tok_a=${parts_a[cur_idx_a]}
314     cur_tok_b=${parts_b[cur_idx_b]}
315 ciaranm 1.7
316     # number?
317     if [[ -n ${cur_tok_a} ]] && [[ -z ${cur_tok_a//[[:digit:]]} ]] ; then
318 abcd 1.18 cur_idx_a+=1
319     [[ ${parts_a[cur_idx_a]} == . ]] \
320     && cur_idx_a+=1
321 ciaranm 1.7 else
322 abcd 1.18 cur_tok_a=
323 ciaranm 1.7 fi
324    
325     if [[ -n ${cur_tok_b} ]] && [[ -z ${cur_tok_b//[[:digit:]]} ]] ; then
326 abcd 1.18 cur_idx_b+=1
327     [[ ${parts_b[cur_idx_b]} == . ]] \
328     && cur_idx_b+=1
329 ciaranm 1.7 else
330 abcd 1.18 cur_tok_b=
331 ciaranm 1.7 fi
332    
333     # done with number components?
334 abcd 1.18 [[ -z ${cur_tok_a} && -z ${cur_tok_b} ]] && break
335 ciaranm 1.7
336 abcd 1.18 # if a component is blank, then it is the lesser value
337     [[ -z ${cur_tok_a} ]] && eshopts_pop && return 1
338     [[ -z ${cur_tok_b} ]] && eshopts_pop && return 3
339    
340     # According to PMS, if we are *not* in the first number part, and either
341     # token begins with "0", then we use a different algorithm (that
342     # effectively does floating point comparison)
343     if (( prev_idx_a != 0 && prev_idx_b != 0 )) \
344     && [[ ${cur_tok_a} == 0* || ${cur_tok_b} == 0* ]] ; then
345    
346     # strip trailing zeros
347     cur_tok_a=${cur_tok_a%%+(0)}
348     cur_tok_b=${cur_tok_b%%+(0)}
349    
350     # do a *string* comparison of the resulting values: 2 > 11
351     [[ ${cur_tok_a} < ${cur_tok_b} ]] && eshopts_pop && return 1
352     [[ ${cur_tok_a} > ${cur_tok_b} ]] && eshopts_pop && return 3
353     else
354     # to avoid going into octal mode, strip any leading zeros. otherwise
355     # bash will throw a hissy fit on versions like 6.3.068.
356     cur_tok_a=${cur_tok_a##+(0)}
357     cur_tok_b=${cur_tok_b##+(0)}
358    
359     # now if a component is blank, it was originally 0 -- make it so
360     : ${cur_tok_a:=0}
361     : ${cur_tok_b:=0}
362    
363     # compare
364     ((cur_tok_a < cur_tok_b)) && eshopts_pop && return 1
365     ((cur_tok_a > cur_tok_b)) && eshopts_pop && return 3
366     fi
367 ciaranm 1.2 done
368    
369 ciaranm 1.7 ### number parts equal. compare letter parts.
370     local letter_a=
371 abcd 1.18 letter_a=${parts_a[cur_idx_a]}
372     if [[ ${#letter_a} -eq 1 && -z ${letter_a/[a-z]} ]] ; then
373     cur_idx_a+=1
374 ciaranm 1.7 else
375 abcd 1.18 letter_a=@
376 ciaranm 1.7 fi
377    
378     local letter_b=
379 abcd 1.18 letter_b=${parts_b[cur_idx_b]}
380     if [[ ${#letter_b} -eq 1 && -z ${letter_b/[a-z]} ]] ; then
381     cur_idx_b+=1
382 ciaranm 1.7 else
383 abcd 1.18 letter_b=@
384 ciaranm 1.7 fi
385    
386     # compare
387 vapier 1.17 [[ ${letter_a} < ${letter_b} ]] && eshopts_pop && return 1
388     [[ ${letter_a} > ${letter_b} ]] && eshopts_pop && return 3
389 ciaranm 1.7
390     ### letter parts equal. compare suffixes in order.
391 abcd 1.18 inf_loop=0
392     while true ; do
393     inf_loop+=1
394     ((inf_loop > 20)) && \
395     die "versionator compare bug [numbers, ${ver_a}, ${ver_b}]"
396     [[ ${parts_a[cur_idx_a]} == _ ]] && ((cur_idx_a++))
397     [[ ${parts_b[cur_idx_b]} == _ ]] && ((cur_idx_b++))
398    
399     cur_tok_a=${parts_a[cur_idx_a]}
400     cur_tok_b=${parts_b[cur_idx_b]}
401     num_part_a=0
402     num_part_b=0
403    
404     if has ${cur_tok_a%%+([0-9])} "alpha" "beta" "pre" "rc" "p"; then
405     cur_idx_a+=1
406     num_part_a=${cur_tok_a##+([a-z])}
407     # I don't like octal
408     num_part_a=${num_part_a##+(0)}
409     : ${num_part_a:=0}
410     cur_tok_a=${cur_tok_a%%+([0-9])}
411     else
412     cur_tok_a=
413     fi
414 ciaranm 1.7
415 abcd 1.18 if has ${cur_tok_b%%+([0-9])} alpha beta pre rc p; then
416     cur_idx_b+=1
417     num_part_b=${cur_tok_b##+([a-z])}
418     # I still don't like octal
419     num_part_b=${num_part_b##+(0)}
420     : ${num_part_b:=0}
421     cur_tok_b=${cur_tok_b%%+([0-9])}
422     else
423     cur_tok_b=
424     fi
425 ciaranm 1.2
426 abcd 1.18 if [[ ${cur_tok_a} != ${cur_tok_b} ]]; then
427     local suffix
428     for suffix in alpha beta pre rc "" p; do
429     [[ ${cur_tok_a} == ${suffix} ]] && eshopts_pop && return 1
430     [[ ${cur_tok_b} == ${suffix} ]] && eshopts_pop && return 3
431     done
432     elif [[ -z ${cur_tok_a} && -z ${cur_tok_b} ]]; then
433     break
434     else
435     ((num_part_a < num_part_b)) && eshopts_pop && return 1
436     ((num_part_a > num_part_b)) && eshopts_pop && return 3
437     fi
438     done
439 ciaranm 1.2
440 abcd 1.18 # At this point, the only thing that should be left is the -r# part
441     [[ ${parts_a[cur_idx_a]} == - ]] && ((cur_idx_a++))
442     [[ ${parts_b[cur_idx_b]} == - ]] && ((cur_idx_b++))
443    
444     # Sanity check
445     if [[ ${parts_a[cur_idx_a]/r+([0-9])} || ${parts_b[cur_idx_b]/r+([0-9])} ]]; then
446     die "versionator compare bug [revisions, ${ver_a}, ${ver_b}]"
447     fi
448 ciaranm 1.2
449 abcd 1.18 num_part_a=${parts_a[cur_idx_a]#r}
450     num_part_a=${num_part_a##+(0)}
451     : ${num_part_a:=0}
452     num_part_b=${parts_b[cur_idx_b]#r}
453     num_part_b=${num_part_b##+(0)}
454     : ${num_part_b:=0}
455 ciaranm 1.2
456 abcd 1.18 ((num_part_a < num_part_b)) && eshopts_pop && return 1
457     ((num_part_a > num_part_b)) && eshopts_pop && return 3
458 ciaranm 1.7
459     ### no differences.
460 vapier 1.17 eshopts_pop
461 ciaranm 1.7 return 2
462     }
463    
464 vapier 1.14 # @FUNCTION: version_sort
465     # @USAGE: <version> [more versions...]
466     # @DESCRIPTION:
467 ciaranm 1.7 # Returns its parameters sorted, highest version last. We're using a quadratic
468     # algorithm for simplicity, so don't call it with more than a few dozen items.
469     # Uses version_compare, so be careful.
470     version_sort() {
471 vapier 1.17 eshopts_push -s extglob
472 abcd 1.18 local items=
473     local -i left=0
474     items=("$@")
475     while ((left < ${#items[@]})); do
476     local -i lowest_idx=left
477     local -i idx=lowest_idx+1
478     while ((idx < ${#items[@]})); do
479     version_compare "${items[lowest_idx]}" "${items[idx]}"
480     [[ $? -eq 3 ]] && lowest_idx=idx
481     idx+=1
482 ciaranm 1.7 done
483 abcd 1.18 local tmp=${items[lowest_idx]}
484     items[lowest_idx]=${items[left]}
485     items[left]=${tmp}
486     left+=1
487 ciaranm 1.7 done
488     echo ${items[@]}
489 vapier 1.17 eshopts_pop
490 ciaranm 1.7 }
491    
492 vapier 1.16 # @FUNCTION: version_format_string
493     # @USAGE: <format> [version]
494     # @DESCRIPTION:
495     # Reformat complicated version strings. The first argument is the string
496     # to reformat with while the rest of the args are passed on to the
497     # get_version_components function. You should make sure to single quote
498 abcd 1.18 # the first argument since it'll have variables that get delayed expansions.
499 vapier 1.16 # @EXAMPLE:
500     # P="cow-hat-1.2.3_p4"
501     # MY_P=$(version_format_string '${PN}_source_$1_$2-$3_$4')
502     # Now MY_P will be: cow-hat_source_1_2-3_p4
503     version_format_string() {
504     local fstr=$1
505     shift
506     set -- $(get_version_components "$@")
507     eval echo "${fstr}"
508     }
509    
510 vapier 1.21 fi

  ViewVC Help
Powered by ViewVC 1.1.20