1 |
Can we do away with all the extra foo && return bullshit and just set a trap? |
2 |
|
3 |
trap "eshopts pop" RETURN |
4 |
|
5 |
? |
6 |
|
7 |
On Sun, Jul 18, 2010 at 2:29 PM, Mike Frysinger <vapier@g.o> wrote: |
8 |
> now that eutils.eclass contains a common version of safely handling shopts |
9 |
> settings, we can drop the local versionator.eclass code that was handling this |
10 |
> and convert it to eutils. seems to work for me, but i rarely use this eclass. |
11 |
> -mike |
12 |
> |
13 |
> --- versionator.eclass 18 Jul 2010 21:24:33 -0000 1.16 |
14 |
> +++ versionator.eclass 18 Jul 2010 21:29:21 -0000 |
15 |
> @@ -25,27 +25,7 @@ |
16 |
> # version_is_at_least want have |
17 |
> # which may be buggy, so use with caution. |
18 |
> |
19 |
> -# Quick function to toggle the shopts required for some functions on and off |
20 |
> -# Used because we can't set extglob in global scope anymore (QA Violation) |
21 |
> -__versionator_shopt_toggle() { |
22 |
> - VERSIONATOR_RECURSION=${VERSIONATOR_RECURSION:-0} |
23 |
> - case "$1" in |
24 |
> - "on") |
25 |
> - if [[ $VERSIONATOR_RECURSION -lt 1 ]] ; then |
26 |
> - VERSIONATOR_OLD_EXTGLOB=$(shopt -p extglob) |
27 |
> - shopt -s extglob |
28 |
> - fi |
29 |
> - VERSIONATOR_RECURSION=$(( $VERSIONATOR_RECURSION + 1 )) |
30 |
> - ;; |
31 |
> - "off") |
32 |
> - VERSIONATOR_RECURSION=$(( $VERSIONATOR_RECURSION - 1 )) |
33 |
> - if [[ $VERSIONATOR_RECURSION -lt 1 ]] ; then |
34 |
> - eval $VERSIONATOR_OLD_EXTGLOB |
35 |
> - fi |
36 |
> - ;; |
37 |
> - esac |
38 |
> - return 0 |
39 |
> -} |
40 |
> +inherit eutils |
41 |
> |
42 |
> # @FUNCTION: get_all_version_components |
43 |
> # @USAGE: [version] |
44 |
> @@ -58,7 +38,7 @@ __versionator_shopt_toggle() { |
45 |
> # 20040905 -> 20040905 |
46 |
> # 3.0c-r1 -> 3 . 0 c - r1 |
47 |
> get_all_version_components() { |
48 |
> - __versionator_shopt_toggle on |
49 |
> + eshopts_push -s extglob |
50 |
> local ver_str=${1:-${PV}} result result_idx=0 |
51 |
> result=( ) |
52 |
> |
53 |
> @@ -66,7 +46,7 @@ get_all_version_components() { |
54 |
> # times. |
55 |
> if [[ "${VERSIONATOR_CACHE_VER_STR}" == "${ver_str}" ]] ; then |
56 |
> echo ${VERSIONATOR_CACHE_RESULT} |
57 |
> - __versionator_shopt_toggle off |
58 |
> + eshopts_pop |
59 |
> return |
60 |
> fi |
61 |
> export VERSIONATOR_CACHE_VER_STR="${ver_str}" |
62 |
> @@ -106,7 +86,7 @@ get_all_version_components() { |
63 |
> |
64 |
> export VERSIONATOR_CACHE_RESULT="${result[@]}" |
65 |
> echo ${result[@]} |
66 |
> - __versionator_shopt_toggle off |
67 |
> + eshopts_pop |
68 |
> } |
69 |
> |
70 |
> # @FUNCTION: get_version_components |
71 |
> @@ -120,11 +100,11 @@ get_all_version_components() { |
72 |
> # 20040905 -> 20040905 |
73 |
> # 3.0c-r1 -> 3 0 c r1 |
74 |
> get_version_components() { |
75 |
> - __versionator_shopt_toggle on |
76 |
> + eshopts_push -s extglob |
77 |
> local c="$(get_all_version_components "${1:-${PV}}")" |
78 |
> c=( ${c[@]//[-._]/ } ) |
79 |
> echo ${c[@]} |
80 |
> - __versionator_shopt_toggle off |
81 |
> + eshopts_pop |
82 |
> } |
83 |
> |
84 |
> # @FUNCTION: get_major_version |
85 |
> @@ -137,11 +117,11 @@ get_version_components() { |
86 |
> # 20040905 -> 20040905 |
87 |
> # 3.0c-r1 -> 3 |
88 |
> get_major_version() { |
89 |
> - __versionator_shopt_toggle on |
90 |
> + eshopts_push -s extglob |
91 |
> local c |
92 |
> c=( $(get_all_version_components "${1:-${PV}}" ) ) |
93 |
> echo ${c[0]} |
94 |
> - __versionator_shopt_toggle off |
95 |
> + eshopts_pop |
96 |
> } |
97 |
> |
98 |
> # @FUNCTION: get_version_component_range |
99 |
> @@ -153,7 +133,7 @@ get_major_version() { |
100 |
> # 1-2 1.2.3 -> 1.2 |
101 |
> # 2- 1.2.3 -> 2.3 |
102 |
> get_version_component_range() { |
103 |
> - __versionator_shopt_toggle on |
104 |
> + eshopts_push -s extglob |
105 |
> local c v="${2:-${PV}}" range="${1}" range_start range_end i=-1 j=0 |
106 |
> c=( $(get_all_version_components ${v} ) ) |
107 |
> range_start="${range%-*}" ; range_start="${range_start:-1}" |
108 |
> @@ -161,17 +141,17 @@ get_version_component_range() { |
109 |
> |
110 |
> while (( j < ${range_start} )) ; do |
111 |
> i=$(($i + 1)) |
112 |
> - [[ $i -gt ${#c[@]} ]] && __versionator_shopt_toggle off && return |
113 |
> + [[ $i -gt ${#c[@]} ]] && eshopts_pop && return |
114 |
> [[ -n "${c[${i}]//[-._]}" ]] && j=$(($j + 1)) |
115 |
> done |
116 |
> |
117 |
> while (( j <= ${range_end} )) ; do |
118 |
> echo -n ${c[$i]} |
119 |
> - [[ $i -gt ${#c[@]} ]] && __versionator_shopt_toggle off && return |
120 |
> + [[ $i -gt ${#c[@]} ]] && eshopts_pop && return |
121 |
> [[ -n "${c[${i}]//[-._]}" ]] && j=$(($j + 1)) |
122 |
> i=$(($i + 1)) |
123 |
> done |
124 |
> - __versionator_shopt_toggle off |
125 |
> + eshopts_pop |
126 |
> } |
127 |
> |
128 |
> # @FUNCTION: get_after_major_version |
129 |
> @@ -185,9 +165,9 @@ get_version_component_range() { |
130 |
> # 20040905 -> (empty string) |
131 |
> # 3.0c-r1 -> 0c-r1 |
132 |
> get_after_major_version() { |
133 |
> - __versionator_shopt_toggle on |
134 |
> + eshopts_push -s extglob |
135 |
> echo $(get_version_component_range 2- "${1:-${PV}}" ) |
136 |
> - __versionator_shopt_toggle off |
137 |
> + eshopts_pop |
138 |
> } |
139 |
> |
140 |
> # @FUNCTION: replace_version_separator |
141 |
> @@ -201,7 +181,7 @@ get_after_major_version() { |
142 |
> # Rather than being a number, $1 can be a separator character such as '-', '.' |
143 |
> # or '_'. In this case, the first separator of this kind is selected. |
144 |
> replace_version_separator() { |
145 |
> - __versionator_shopt_toggle on |
146 |
> + eshopts_push -s extglob |
147 |
> local w i c found=0 v="${3:-${PV}}" |
148 |
> w=${1:-1} |
149 |
> c=( $(get_all_version_components ${v} ) ) |
150 |
> @@ -226,7 +206,7 @@ replace_version_separator() { |
151 |
> fi |
152 |
> c=${c[@]} |
153 |
> echo ${c// } |
154 |
> - __versionator_shopt_toggle off |
155 |
> + eshopts_pop |
156 |
> } |
157 |
> |
158 |
> # @FUNCTION: replace_all_version_separators |
159 |
> @@ -235,12 +215,12 @@ replace_version_separator() { |
160 |
> # Replace all version separators in $2 (defaults to $PV) with $1. |
161 |
> # '_' 1b.2.3 -> 1b_2_3 |
162 |
> replace_all_version_separators() { |
163 |
> - __versionator_shopt_toggle on |
164 |
> + eshopts_push -s extglob |
165 |
> local c |
166 |
> c=( $(get_all_version_components "${2:-${PV}}" ) ) |
167 |
> c="${c[@]//[-._]/$1}" |
168 |
> echo ${c// } |
169 |
> - __versionator_shopt_toggle off |
170 |
> + eshopts_pop |
171 |
> } |
172 |
> |
173 |
> # @FUNCTION: delete_version_separator |
174 |
> @@ -254,9 +234,9 @@ replace_all_version_separators() { |
175 |
> # 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 deleted. |
177 |
> delete_version_separator() { |
178 |
> - __versionator_shopt_toggle on |
179 |
> + eshopts_push -s extglob |
180 |
> replace_version_separator "${1}" "" "${2}" |
181 |
> - __versionator_shopt_toggle off |
182 |
> + eshopts_pop |
183 |
> } |
184 |
> |
185 |
> # @FUNCTION: delete_all_version_separators |
186 |
> @@ -265,9 +245,9 @@ delete_version_separator() { |
187 |
> # Delete all version separators in $1 (defaults to $PV). |
188 |
> # 1b.2.3 -> 1b23 |
189 |
> delete_all_version_separators() { |
190 |
> - __versionator_shopt_toggle on |
191 |
> + eshopts_push -s extglob |
192 |
> replace_all_version_separators "" "${1}" |
193 |
> - __versionator_shopt_toggle off |
194 |
> + eshopts_pop |
195 |
> } |
196 |
> |
197 |
> # @FUNCTION: get_version_component_count |
198 |
> @@ -277,11 +257,11 @@ delete_all_version_separators() { |
199 |
> # 1.0.1 -> 3 |
200 |
> # 3.0c-r1 -> 4 |
201 |
> get_version_component_count() { |
202 |
> - __versionator_shopt_toggle on |
203 |
> + eshopts_push -s extglob |
204 |
> local a |
205 |
> a=( $(get_version_components "${1:-${PV}}" ) ) |
206 |
> echo ${#a[@]} |
207 |
> - __versionator_shopt_toggle off |
208 |
> + eshopts_pop |
209 |
> } |
210 |
> |
211 |
> # @FUNCTION: get_last_version_component_index |
212 |
> @@ -292,9 +272,9 @@ get_version_component_count() { |
213 |
> # 1.0.1 -> 3 |
214 |
> # 3.0c-r1 -> 4 |
215 |
> get_last_version_component_index() { |
216 |
> - __versionator_shopt_toggle on |
217 |
> + eshopts_push -s extglob |
218 |
> echo $(( $(get_version_component_count "${1:-${PV}}" ) - 1 )) |
219 |
> - __versionator_shopt_toggle off |
220 |
> + eshopts_pop |
221 |
> } |
222 |
> |
223 |
> # @FUNCTION: version_is_at_least |
224 |
> @@ -304,25 +284,25 @@ get_last_version_component_index() { |
225 |
> # only. May not be reliable, be sure to do very careful testing before actually |
226 |
> # using this. |
227 |
> version_is_at_least() { |
228 |
> - __versionator_shopt_toggle on |
229 |
> + eshopts_push -s extglob |
230 |
> local want_s="$1" have_s="${2:-${PVR}}" r |
231 |
> version_compare "${want_s}" "${have_s}" |
232 |
> r=$? |
233 |
> case $r in |
234 |
> 1|2) |
235 |
> - __versionator_shopt_toggle off |
236 |
> + eshopts_pop |
237 |
> return 0 |
238 |
> ;; |
239 |
> 3) |
240 |
> - __versionator_shopt_toggle off |
241 |
> + eshopts_pop |
242 |
> return 1 |
243 |
> ;; |
244 |
> *) |
245 |
> - __versionator_shopt_toggle off |
246 |
> + eshopts_pop |
247 |
> die "versionator compare bug [atleast, ${want_s}, ${have_s}, ${r}]" |
248 |
> ;; |
249 |
> esac |
250 |
> - __versionator_shopt_toggle off |
251 |
> + eshopts_pop |
252 |
> } |
253 |
> |
254 |
> # @FUNCTION: version_compare |
255 |
> @@ -333,7 +313,7 @@ version_is_at_least() { |
256 |
> # return 3. You probably want version_is_at_least rather than this function. |
257 |
> # May not be very reliable. Test carefully before using this. |
258 |
> version_compare() { |
259 |
> - __versionator_shopt_toggle on |
260 |
> + eshopts_push -s extglob |
261 |
> local ver_a=${1} ver_b=${2} parts_a parts_b cur_idx_a=0 cur_idx_b=0 |
262 |
> parts_a=( $(get_all_version_components "${ver_a}" ) ) |
263 |
> parts_b=( $(get_all_version_components "${ver_b}" ) ) |
264 |
> @@ -379,8 +359,8 @@ version_compare() { |
265 |
> [[ -z ${cur_tok_b} ]] && cur_tok_b=0 |
266 |
> |
267 |
> # compare |
268 |
> - [[ ${cur_tok_a} -lt ${cur_tok_b} ]] && __versionator_shopt_toggle off && return 1 |
269 |
> - [[ ${cur_tok_a} -gt ${cur_tok_b} ]] && __versionator_shopt_toggle off && return 3 |
270 |
> + [[ ${cur_tok_a} -lt ${cur_tok_b} ]] && eshopts_pop && return 1 |
271 |
> + [[ ${cur_tok_a} -gt ${cur_tok_b} ]] && eshopts_pop && return 3 |
272 |
> done |
273 |
> |
274 |
> ### number parts equal. compare letter parts. |
275 |
> @@ -401,8 +381,8 @@ version_compare() { |
276 |
> fi |
277 |
> |
278 |
> # compare |
279 |
> - [[ ${letter_a} < ${letter_b} ]] && __versionator_shopt_toggle off && return 1 |
280 |
> - [[ ${letter_a} > ${letter_b} ]] && __versionator_shopt_toggle off && return 3 |
281 |
> + [[ ${letter_a} < ${letter_b} ]] && eshopts_pop && return 1 |
282 |
> + [[ ${letter_a} > ${letter_b} ]] && eshopts_pop && return 3 |
283 |
> |
284 |
> ### letter parts equal. compare suffixes in order. |
285 |
> local suffix rule part r_lt r_gt |
286 |
> @@ -427,19 +407,19 @@ version_compare() { |
287 |
> |
288 |
> [[ -z ${suffix_a} ]] && [[ -z ${suffix_b} ]] && continue |
289 |
> |
290 |
> - [[ -z ${suffix_a} ]] && __versionator_shopt_toggle off && return ${r_gt} |
291 |
> - [[ -z ${suffix_b} ]] && __versionator_shopt_toggle off && return ${r_lt} |
292 |
> + [[ -z ${suffix_a} ]] && eshopts_pop && return ${r_gt} |
293 |
> + [[ -z ${suffix_b} ]] && eshopts_pop && return ${r_lt} |
294 |
> |
295 |
> # avoid octal problems |
296 |
> suffix_a=${suffix_a##+(0)} ; suffix_a=${suffix_a:-0} |
297 |
> suffix_b=${suffix_b##+(0)} ; suffix_b=${suffix_b:-0} |
298 |
> |
299 |
> - [[ ${suffix_a} -lt ${suffix_b} ]] && __versionator_shopt_toggle off && return 1 |
300 |
> - [[ ${suffix_a} -gt ${suffix_b} ]] && __versionator_shopt_toggle off && return 3 |
301 |
> + [[ ${suffix_a} -lt ${suffix_b} ]] && eshopts_pop && return 1 |
302 |
> + [[ ${suffix_a} -gt ${suffix_b} ]] && eshopts_pop && return 3 |
303 |
> done |
304 |
> |
305 |
> ### no differences. |
306 |
> - __versionator_shopt_toggle off |
307 |
> + eshopts_pop |
308 |
> return 2 |
309 |
> } |
310 |
> |
311 |
> @@ -450,7 +430,7 @@ version_compare() { |
312 |
> # algorithm for simplicity, so don't call it with more than a few dozen items. |
313 |
> # Uses version_compare, so be careful. |
314 |
> version_sort() { |
315 |
> - __versionator_shopt_toggle on |
316 |
> + eshopts_push -s extglob |
317 |
> local items= left=0 |
318 |
> items=( $@ ) |
319 |
> while [[ ${left} -lt ${#items[@]} ]] ; do |
320 |
> @@ -467,7 +447,7 @@ version_sort() { |
321 |
> left=$(( ${left} + 1 )) |
322 |
> done |
323 |
> echo ${items[@]} |
324 |
> - __versionator_shopt_toggle off |
325 |
> + eshopts_pop |
326 |
> } |
327 |
> |
328 |
> # @FUNCTION: version_format_string |
329 |
> @@ -489,7 +469,7 @@ version_format_string() { |
330 |
> } |
331 |
> |
332 |
> __versionator__test_version_compare() { |
333 |
> - __versionator_shopt_toggle on |
334 |
> + eshopts_push -s extglob |
335 |
> local lt=1 eq=2 gt=3 p q |
336 |
> |
337 |
> __versionator__test_version_compare_t() { |
338 |
> @@ -593,5 +573,5 @@ __versionator__test_version_compare() { |
339 |
> __versionator__test_version_compare_t "7.2${p}3" $gt "7.2${p}2" |
340 |
> __versionator__test_version_compare_t "7.2${p}2" $lt "7.2${p}3" |
341 |
> done |
342 |
> - __versionator_shopt_toggle off |
343 |
> + eshopts_pop |
344 |
> } |
345 |
> |
346 |
> |