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

Diff of /eclass/versionator.eclass

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.13 Revision 1.14
1# Copyright 1999-2004 Gentoo Foundation 1# Copyright 1999-2008 Gentoo Foundation
2# Distributed under the terms of the GNU General Public License v2 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 $ 3# $Header: /var/cvsroot/gentoo-x86/eclass/versionator.eclass,v 1.14 2008/06/01 01:30:31 vapier Exp $
4# 4
5# Original Author: Ciaran McCreesh <ciaranm@gentoo.org> 5# @ECLASS: versionator.eclass
6# 6# @MAINTAINER:
7# base-system@gentoo.org
8# @BLURB: functions which simplify manipulation of ${PV} and similar version strings
9# @DESCRIPTION:
7# This eclass provides functions which simplify manipulating $PV and similar 10# This eclass provides functions which simplify manipulating $PV and similar
8# variables. Most functions default to working with $PV, although other 11# variables. Most functions default to working with $PV, although other
9# values can be used. 12# values can be used.
10# 13# @EXAMPLE:
11# Simple Example 1: $PV is 1.2.3b, we want 1_2.3b: 14# Simple Example 1: $PV is 1.2.3b, we want 1_2.3b:
12# MY_PV=$(replace_version_separator 1 '_' ) 15# MY_PV=$(replace_version_separator 1 '_' )
13# 16#
14# Simple Example 2: $PV is 1.4.5, we want 1: 17# Simple Example 2: $PV is 1.4.5, we want 1:
15# MY_MAJORV=$(get_major_version ) 18# 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# 19#
31# Rather than being a number, the index parameter can be a separator character 20# 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 21# such as '-', '.' or '_'. In this case, the first separator of this kind is
33# selected. 22# selected.
34# 23#
35# There's also: 24# There's also:
36# version_is_at_least want have 25# version_is_at_least want have
37# which may be buggy, so use with caution. 26# which may be buggy, so use with caution.
38 27
39# Quick function to toggle the shopts required for some functions on and off 28# 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) 29# Used because we can't set extglob in global scope anymore (QA Violation)
41__versionator_shopt_toggle() { 30__versionator_shopt_toggle() {
42 VERSIONATOR_RECURSION=${VERSIONATOR_RECURSION:-0} 31 VERSIONATOR_RECURSION=${VERSIONATOR_RECURSION:-0}
56 ;; 45 ;;
57 esac 46 esac
58 return 0 47 return 0
59} 48}
60 49
50# @FUNCTION: get_all_version_components
51# @USAGE: [version]
52# @DESCRIPTION:
61# Split up a version string into its component parts. If no parameter is 53# Split up a version string into its component parts. If no parameter is
62# supplied, defaults to $PV. 54# supplied, defaults to $PV.
63# 0.8.3 -> 0 . 8 . 3 55# 0.8.3 -> 0 . 8 . 3
64# 7c -> 7 c 56# 7c -> 7 c
65# 3.0_p2 -> 3 . 0 _ p2 57# 3.0_p2 -> 3 . 0 _ p2
115 export VERSIONATOR_CACHE_RESULT="${result[@]}" 107 export VERSIONATOR_CACHE_RESULT="${result[@]}"
116 echo ${result[@]} 108 echo ${result[@]}
117 __versionator_shopt_toggle off 109 __versionator_shopt_toggle off
118} 110}
119 111
112# @FUNCTION: get_version_components
113# @USAGE: [version]
114# @DESCRIPTION:
120# Get the important version components, excluding '.', '-' and '_'. Defaults to 115# Get the important version components, excluding '.', '-' and '_'. Defaults to
121# $PV if no parameter is supplied. 116# $PV if no parameter is supplied.
122# 0.8.3 -> 0 8 3 117# 0.8.3 -> 0 8 3
123# 7c -> 7 c 118# 7c -> 7 c
124# 3.0_p2 -> 3 0 p2 119# 3.0_p2 -> 3 0 p2
130 c=( ${c[@]//[-._]/ } ) 125 c=( ${c[@]//[-._]/ } )
131 echo ${c[@]} 126 echo ${c[@]}
132 __versionator_shopt_toggle off 127 __versionator_shopt_toggle off
133} 128}
134 129
130# @FUNCTION: get_major_version
131# @USAGE: [version]
132# @DESCRIPTION:
135# Get the major version of a value. Defaults to $PV if no parameter is supplied. 133# Get the major version of a value. Defaults to $PV if no parameter is supplied.
136# 0.8.3 -> 0 134# 0.8.3 -> 0
137# 7c -> 7 135# 7c -> 7
138# 3.0_p2 -> 3 136# 3.0_p2 -> 3
139# 20040905 -> 20040905 137# 20040905 -> 20040905
144 c=( $(get_all_version_components "${1:-${PV}}" ) ) 142 c=( $(get_all_version_components "${1:-${PV}}" ) )
145 echo ${c[0]} 143 echo ${c[0]}
146 __versionator_shopt_toggle off 144 __versionator_shopt_toggle off
147} 145}
148 146
147# @FUNCTION: get_version_component_range
148# @USAGE: [version]
149# @DESCRIPTION:
149# Get a particular component or range of components from the version. If no 150# Get a particular component or range of components from the version. If no
150# version parameter is supplied, defaults to $PV. 151# version parameter is supplied, defaults to $PV.
151# 1 1.2.3 -> 1 152# 1 1.2.3 -> 1
152# 1-2 1.2.3 -> 1.2 153# 1-2 1.2.3 -> 1.2
153# 2- 1.2.3 -> 2.3 154# 2- 1.2.3 -> 2.3
171 i=$(($i + 1)) 172 i=$(($i + 1))
172 done 173 done
173 __versionator_shopt_toggle off 174 __versionator_shopt_toggle off
174} 175}
175 176
177# @FUNCTION: get_after_major_version
178# @USAGE: [version]
179# @DESCRIPTION:
176# Get everything after the major version and its separator (if present) of a 180# Get everything after the major version and its separator (if present) of a
177# value. Defaults to $PV if no parameter is supplied. 181# value. Defaults to $PV if no parameter is supplied.
178# 0.8.3 -> 8.3 182# 0.8.3 -> 8.3
179# 7c -> c 183# 7c -> c
180# 3.0_p2 -> 0_p2 184# 3.0_p2 -> 0_p2
184 __versionator_shopt_toggle on 188 __versionator_shopt_toggle on
185 echo $(get_version_component_range 2- "${1:-${PV}}" ) 189 echo $(get_version_component_range 2- "${1:-${PV}}" )
186 __versionator_shopt_toggle off 190 __versionator_shopt_toggle off
187} 191}
188 192
193# @FUNCTION: replace_version_sepaator
194# @USAGE: <search> <replacement> [subject]
195# @DESCRIPTION:
189# Replace the $1th separator with $2 in $3 (defaults to $PV if $3 is not 196# 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. 197# supplied). If there are fewer than $1 separators, don't change anything.
191# 1 '_' 1.2.3 -> 1_2.3 198# 1 '_' 1.2.3 -> 1_2.3
192# 2 '_' 1.2.3 -> 1.2_3 199# 2 '_' 1.2.3 -> 1.2_3
193# 1 '_' 1b-2.3 -> 1b_2.3 200# 1 '_' 1b-2.3 -> 1b_2.3
220 c=${c[@]} 227 c=${c[@]}
221 echo ${c// } 228 echo ${c// }
222 __versionator_shopt_toggle off 229 __versionator_shopt_toggle off
223} 230}
224 231
232# @FUNCTION: replace_all_version_separators
233# @USAGE: <replacement> [subject]
234# @DESCRIPTION:
225# Replace all version separators in $2 (defaults to $PV) with $1. 235# Replace all version separators in $2 (defaults to $PV) with $1.
226# '_' 1b.2.3 -> 1b_2_3 236# '_' 1b.2.3 -> 1b_2_3
227replace_all_version_separators() { 237replace_all_version_separators() {
228 __versionator_shopt_toggle on 238 __versionator_shopt_toggle on
229 local c 239 local c
231 c="${c[@]//[-._]/$1}" 241 c="${c[@]//[-._]/$1}"
232 echo ${c// } 242 echo ${c// }
233 __versionator_shopt_toggle off 243 __versionator_shopt_toggle off
234} 244}
235 245
246# @FUNCTION: delete_version_separator
247# @USAGE: <search> [subject]
248# @DESCRIPTION:
236# Delete the $1th separator in $2 (defaults to $PV if $2 is not supplied). If 249# 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. 250# there are fewer than $1 separators, don't change anything.
238# 1 1.2.3 -> 12.3 251# 1 1.2.3 -> 12.3
239# 2 1.2.3 -> 1.23 252# 2 1.2.3 -> 1.23
240# 1 1b-2.3 -> 1b2.3 253# 1 1b-2.3 -> 1b2.3
244 __versionator_shopt_toggle on 257 __versionator_shopt_toggle on
245 replace_version_separator "${1}" "" "${2}" 258 replace_version_separator "${1}" "" "${2}"
246 __versionator_shopt_toggle off 259 __versionator_shopt_toggle off
247} 260}
248 261
262# @FUNCTION: delete_all_version_separators
263# @USAGE: [subject]
264# @DESCRIPTION:
249# Delete all version separators in $1 (defaults to $PV). 265# Delete all version separators in $1 (defaults to $PV).
250# 1b.2.3 -> 1b23 266# 1b.2.3 -> 1b23
251delete_all_version_separators() { 267delete_all_version_separators() {
252 __versionator_shopt_toggle on 268 __versionator_shopt_toggle on
253 replace_all_version_separators "" "${1}" 269 replace_all_version_separators "" "${1}"
254 __versionator_shopt_toggle off 270 __versionator_shopt_toggle off
255} 271}
256 272
273# @FUNCTION: get_version_component_count
274# @USAGE: [version]
275# @DESCRIPTION:
257# How many version components are there in $1 (defaults to $PV)? 276# How many version components are there in $1 (defaults to $PV)?
258# 1.0.1 -> 3 277# 1.0.1 -> 3
259# 3.0c-r1 -> 4 278# 3.0c-r1 -> 4
260#
261get_version_component_count() { 279get_version_component_count() {
262 __versionator_shopt_toggle on 280 __versionator_shopt_toggle on
263 local a 281 local a
264 a=( $(get_version_components "${1:-${PV}}" ) ) 282 a=( $(get_version_components "${1:-${PV}}" ) )
265 echo ${#a[@]} 283 echo ${#a[@]}
266 __versionator_shopt_toggle off 284 __versionator_shopt_toggle off
267} 285}
268 286
287# @FUNCTION: get_last_version_component_index
288# @USAGE: [version]
289# @DESCRIPTION:
269# What is the index of the last version component in $1 (defaults to $PV)? 290# What is the index of the last version component in $1 (defaults to $PV)?
270# Equivalent to get_version_component_count - 1. 291# Equivalent to get_version_component_count - 1.
271# 1.0.1 -> 3 292# 1.0.1 -> 3
272# 3.0c-r1 -> 4 293# 3.0c-r1 -> 4
273#
274get_last_version_component_index() { 294get_last_version_component_index() {
275 __versionator_shopt_toggle on 295 __versionator_shopt_toggle on
276 echo $(( $(get_version_component_count "${1:-${PV}}" ) - 1 )) 296 echo $(( $(get_version_component_count "${1:-${PV}}" ) - 1 ))
277 __versionator_shopt_toggle off 297 __versionator_shopt_toggle off
278} 298}
279 299
300# @FUNCTION: version_is_at_least
301# @USAGE: <want> [have]
302# @DESCRIPTION:
280# Is $2 (defaults to $PVR) at least version $1? Intended for use in eclasses 303# 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 304# 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. 305# using this.
283version_is_at_least() { 306version_is_at_least() {
284 __versionator_shopt_toggle on 307 __versionator_shopt_toggle on
285 local want_s="$1" have_s="${2:-${PVR}}" r 308 local want_s="$1" have_s="${2:-${PVR}}" r
286 version_compare "${want_s}" "${have_s}" 309 version_compare "${want_s}" "${have_s}"
287 r=$? 310 r=$?
300 ;; 323 ;;
301 esac 324 esac
302 __versionator_shopt_toggle off 325 __versionator_shopt_toggle off
303} 326}
304 327
328# @FUNCTION: version_compare
329# @USAGE: <A> <B>
330# @DESCRIPTION:
305# Takes two parameters (a, b) which are versions. If a is an earlier version 331# 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, 332# than B, returns 1. If A is identical to B, return 2. If A is later than B,
307# return 3. You probably want version_is_at_least rather than this function. 333# return 3. You probably want version_is_at_least rather than this function.
308# May not be very reliable. Test carefully before using this. 334# May not be very reliable. Test carefully before using this.
309version_compare() { 335version_compare() {
310 __versionator_shopt_toggle on 336 __versionator_shopt_toggle on
311 local ver_a=${1} ver_b=${2} parts_a parts_b cur_idx_a=0 cur_idx_b=0 337 local ver_a=${1} ver_b=${2} parts_a parts_b cur_idx_a=0 cur_idx_b=0
415 ### no differences. 441 ### no differences.
416 __versionator_shopt_toggle off 442 __versionator_shopt_toggle off
417 return 2 443 return 2
418} 444}
419 445
446# @FUNCTION: version_sort
447# @USAGE: <version> [more versions...]
448# @DESCRIPTION:
420# Returns its parameters sorted, highest version last. We're using a quadratic 449# 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. 450# algorithm for simplicity, so don't call it with more than a few dozen items.
422# Uses version_compare, so be careful. 451# Uses version_compare, so be careful.
423version_sort() { 452version_sort() {
424 __versionator_shopt_toggle on 453 __versionator_shopt_toggle on
546 __versionator__test_version_compare_t "7.2${p}3" $gt "7.2${p}2" 575 __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" 576 __versionator__test_version_compare_t "7.2${p}2" $lt "7.2${p}3"
548 done 577 done
549 __versionator_shopt_toggle off 578 __versionator_shopt_toggle off
550} 579}
551

Legend:
Removed from v.1.13  
changed lines
  Added in v.1.14

  ViewVC Help
Powered by ViewVC 1.1.20