Linux Archive

Linux Archive (http://www.linux-archive.org/)
-   Gentoo Development (http://www.linux-archive.org/gentoo-development/)
-   -   versionator.eclass: convert to eshopts_{push,pop} (http://www.linux-archive.org/gentoo-development/401005-versionator-eclass-convert-eshopts_-push-pop.html)

Mike Frysinger 07-18-2010 09:29 PM

versionator.eclass: convert to eshopts_{push,pop}
 
now that eutils.eclass contains a common version of safely handling shopts
settings, we can drop the local versionator.eclass code that was handling this
and convert it to eutils. seems to work for me, but i rarely use this eclass.
-mike

--- versionator.eclass 18 Jul 2010 21:24:33 -0000 1.16
+++ versionator.eclass 18 Jul 2010 21:29:21 -0000
@@ -25,27 +25,7 @@
# version_is_at_least want have
# which may be buggy, so use with caution.

-# Quick function to toggle the shopts required for some functions on and off
-# Used because we can't set extglob in global scope anymore (QA Violation)
-__versionator_shopt_toggle() {
- VERSIONATOR_RECURSION=${VERSIONATOR_RECURSION:-0}
- case "$1" in
- "on")
- if [[ $VERSIONATOR_RECURSION -lt 1 ]] ; then
- VERSIONATOR_OLD_EXTGLOB=$(shopt -p extglob)
- shopt -s extglob
- fi
- VERSIONATOR_RECURSION=$(( $VERSIONATOR_RECURSION + 1 ))
- ;;
- "off")
- VERSIONATOR_RECURSION=$(( $VERSIONATOR_RECURSION - 1 ))
- if [[ $VERSIONATOR_RECURSION -lt 1 ]] ; then
- eval $VERSIONATOR_OLD_EXTGLOB
- fi
- ;;
- esac
- return 0
-}
+inherit eutils

# @FUNCTION: get_all_version_components
# @USAGE: [version]
@@ -58,7 +38,7 @@ __versionator_shopt_toggle() {
# 20040905 -> 20040905
# 3.0c-r1 -> 3 . 0 c - r1
get_all_version_components() {
- __versionator_shopt_toggle on
+ eshopts_push -s extglob
local ver_str=${1:-${PV}} result result_idx=0
result=( )

@@ -66,7 +46,7 @@ get_all_version_components() {
# times.
if [[ "${VERSIONATOR_CACHE_VER_STR}" == "${ver_str}" ]] ; then
echo ${VERSIONATOR_CACHE_RESULT}
- __versionator_shopt_toggle off
+ eshopts_pop
return
fi
export VERSIONATOR_CACHE_VER_STR="${ver_str}"
@@ -106,7 +86,7 @@ get_all_version_components() {

export VERSIONATOR_CACHE_RESULT="${result[@]}"
echo ${result[@]}
- __versionator_shopt_toggle off
+ eshopts_pop
}

# @FUNCTION: get_version_components
@@ -120,11 +100,11 @@ get_all_version_components() {
# 20040905 -> 20040905
# 3.0c-r1 -> 3 0 c r1
get_version_components() {
- __versionator_shopt_toggle on
+ eshopts_push -s extglob
local c="$(get_all_version_components "${1:-${PV}}")"
c=( ${c[@]//[-._]/ } )
echo ${c[@]}
- __versionator_shopt_toggle off
+ eshopts_pop
}

# @FUNCTION: get_major_version
@@ -137,11 +117,11 @@ get_version_components() {
# 20040905 -> 20040905
# 3.0c-r1 -> 3
get_major_version() {
- __versionator_shopt_toggle on
+ eshopts_push -s extglob
local c
c=( $(get_all_version_components "${1:-${PV}}" ) )
echo ${c[0]}
- __versionator_shopt_toggle off
+ eshopts_pop
}

# @FUNCTION: get_version_component_range
@@ -153,7 +133,7 @@ get_major_version() {
# 1-2 1.2.3 -> 1.2
# 2- 1.2.3 -> 2.3
get_version_component_range() {
- __versionator_shopt_toggle on
+ eshopts_push -s extglob
local c v="${2:-${PV}}" range="${1}" range_start range_end i=-1 j=0
c=( $(get_all_version_components ${v} ) )
range_start="${range%-*}" ; range_start="${range_start:-1}"
@@ -161,17 +141,17 @@ get_version_component_range() {

while (( j < ${range_start} )) ; do
i=$(($i + 1))
- [[ $i -gt ${#c[@]} ]] && __versionator_shopt_toggle off && return
+ [[ $i -gt ${#c[@]} ]] && eshopts_pop && return
[[ -n "${c[${i}]//[-._]}" ]] && j=$(($j + 1))
done

while (( j <= ${range_end} )) ; do
echo -n ${c[$i]}
- [[ $i -gt ${#c[@]} ]] && __versionator_shopt_toggle off && return
+ [[ $i -gt ${#c[@]} ]] && eshopts_pop && return
[[ -n "${c[${i}]//[-._]}" ]] && j=$(($j + 1))
i=$(($i + 1))
done
- __versionator_shopt_toggle off
+ eshopts_pop
}

# @FUNCTION: get_after_major_version
@@ -185,9 +165,9 @@ get_version_component_range() {
# 20040905 -> (empty string)
# 3.0c-r1 -> 0c-r1
get_after_major_version() {
- __versionator_shopt_toggle on
+ eshopts_push -s extglob
echo $(get_version_component_range 2- "${1:-${PV}}" )
- __versionator_shopt_toggle off
+ eshopts_pop
}

# @FUNCTION: replace_version_separator
@@ -201,7 +181,7 @@ get_after_major_version() {
# Rather than being a number, $1 can be a separator character such as '-', '.'
# or '_'. In this case, the first separator of this kind is selected.
replace_version_separator() {
- __versionator_shopt_toggle on
+ eshopts_push -s extglob
local w i c found=0 v="${3:-${PV}}"
w=${1:-1}
c=( $(get_all_version_components ${v} ) )
@@ -226,7 +206,7 @@ replace_version_separator() {
fi
c=${c[@]}
echo ${c// }
- __versionator_shopt_toggle off
+ eshopts_pop
}

# @FUNCTION: replace_all_version_separators
@@ -235,12 +215,12 @@ replace_version_separator() {
# Replace all version separators in $2 (defaults to $PV) with $1.
# '_' 1b.2.3 -> 1b_2_3
replace_all_version_separators() {
- __versionator_shopt_toggle on
+ eshopts_push -s extglob
local c
c=( $(get_all_version_components "${2:-${PV}}" ) )
c="${c[@]//[-._]/$1}"
echo ${c// }
- __versionator_shopt_toggle off
+ eshopts_pop
}

# @FUNCTION: delete_version_separator
@@ -254,9 +234,9 @@ replace_all_version_separators() {
# Rather than being a number, $1 can be a separator character such as '-', '.'
# or '_'. In this case, the first separator of this kind is deleted.
delete_version_separator() {
- __versionator_shopt_toggle on
+ eshopts_push -s extglob
replace_version_separator "${1}" "" "${2}"
- __versionator_shopt_toggle off
+ eshopts_pop
}

# @FUNCTION: delete_all_version_separators
@@ -265,9 +245,9 @@ delete_version_separator() {
# Delete all version separators in $1 (defaults to $PV).
# 1b.2.3 -> 1b23
delete_all_version_separators() {
- __versionator_shopt_toggle on
+ eshopts_push -s extglob
replace_all_version_separators "" "${1}"
- __versionator_shopt_toggle off
+ eshopts_pop
}

# @FUNCTION: get_version_component_count
@@ -277,11 +257,11 @@ delete_all_version_separators() {
# 1.0.1 -> 3
# 3.0c-r1 -> 4
get_version_component_count() {
- __versionator_shopt_toggle on
+ eshopts_push -s extglob
local a
a=( $(get_version_components "${1:-${PV}}" ) )
echo ${#a[@]}
- __versionator_shopt_toggle off
+ eshopts_pop
}

# @FUNCTION: get_last_version_component_index
@@ -292,9 +272,9 @@ get_version_component_count() {
# 1.0.1 -> 3
# 3.0c-r1 -> 4
get_last_version_component_index() {
- __versionator_shopt_toggle on
+ eshopts_push -s extglob
echo $(( $(get_version_component_count "${1:-${PV}}" ) - 1 ))
- __versionator_shopt_toggle off
+ eshopts_pop
}

# @FUNCTION: version_is_at_least
@@ -304,25 +284,25 @@ get_last_version_component_index() {
# only. May not be reliable, be sure to do very careful testing before actually
# using this.
version_is_at_least() {
- __versionator_shopt_toggle on
+ eshopts_push -s extglob
local want_s="$1" have_s="${2:-${PVR}}" r
version_compare "${want_s}" "${have_s}"
r=$?
case $r in
1|2)
- __versionator_shopt_toggle off
+ eshopts_pop
return 0
;;
3)
- __versionator_shopt_toggle off
+ eshopts_pop
return 1
;;
*)
- __versionator_shopt_toggle off
+ eshopts_pop
die "versionator compare bug [atleast, ${want_s}, ${have_s}, ${r}]"
;;
esac
- __versionator_shopt_toggle off
+ eshopts_pop
}

# @FUNCTION: version_compare
@@ -333,7 +313,7 @@ version_is_at_least() {
# return 3. You probably want version_is_at_least rather than this function.
# May not be very reliable. Test carefully before using this.
version_compare() {
- __versionator_shopt_toggle on
+ eshopts_push -s extglob
local ver_a=${1} ver_b=${2} parts_a parts_b cur_idx_a=0 cur_idx_b=0
parts_a=( $(get_all_version_components "${ver_a}" ) )
parts_b=( $(get_all_version_components "${ver_b}" ) )
@@ -379,8 +359,8 @@ version_compare() {
[[ -z ${cur_tok_b} ]] && cur_tok_b=0

# compare
- [[ ${cur_tok_a} -lt ${cur_tok_b} ]] && __versionator_shopt_toggle off && return 1
- [[ ${cur_tok_a} -gt ${cur_tok_b} ]] && __versionator_shopt_toggle off && return 3
+ [[ ${cur_tok_a} -lt ${cur_tok_b} ]] && eshopts_pop && return 1
+ [[ ${cur_tok_a} -gt ${cur_tok_b} ]] && eshopts_pop && return 3
done

### number parts equal. compare letter parts.
@@ -401,8 +381,8 @@ version_compare() {
fi

# compare
- [[ ${letter_a} < ${letter_b} ]] && __versionator_shopt_toggle off && return 1
- [[ ${letter_a} > ${letter_b} ]] && __versionator_shopt_toggle off && return 3
+ [[ ${letter_a} < ${letter_b} ]] && eshopts_pop && return 1
+ [[ ${letter_a} > ${letter_b} ]] && eshopts_pop && return 3

### letter parts equal. compare suffixes in order.
local suffix rule part r_lt r_gt
@@ -427,19 +407,19 @@ version_compare() {

[[ -z ${suffix_a} ]] && [[ -z ${suffix_b} ]] && continue

- [[ -z ${suffix_a} ]] && __versionator_shopt_toggle off && return ${r_gt}
- [[ -z ${suffix_b} ]] && __versionator_shopt_toggle off && return ${r_lt}
+ [[ -z ${suffix_a} ]] && eshopts_pop && return ${r_gt}
+ [[ -z ${suffix_b} ]] && eshopts_pop && return ${r_lt}

# avoid octal problems
suffix_a=${suffix_a##+(0)} ; suffix_a=${suffix_a:-0}
suffix_b=${suffix_b##+(0)} ; suffix_b=${suffix_b:-0}

- [[ ${suffix_a} -lt ${suffix_b} ]] && __versionator_shopt_toggle off && return 1
- [[ ${suffix_a} -gt ${suffix_b} ]] && __versionator_shopt_toggle off && return 3
+ [[ ${suffix_a} -lt ${suffix_b} ]] && eshopts_pop && return 1
+ [[ ${suffix_a} -gt ${suffix_b} ]] && eshopts_pop && return 3
done

### no differences.
- __versionator_shopt_toggle off
+ eshopts_pop
return 2
}

@@ -450,7 +430,7 @@ version_compare() {
# algorithm for simplicity, so don't call it with more than a few dozen items.
# Uses version_compare, so be careful.
version_sort() {
- __versionator_shopt_toggle on
+ eshopts_push -s extglob
local items= left=0
items=( $@ )
while [[ ${left} -lt ${#items[@]} ]] ; do
@@ -467,7 +447,7 @@ version_sort() {
left=$(( ${left} + 1 ))
done
echo ${items[@]}
- __versionator_shopt_toggle off
+ eshopts_pop
}

# @FUNCTION: version_format_string
@@ -489,7 +469,7 @@ version_format_string() {
}

__versionator__test_version_compare() {
- __versionator_shopt_toggle on
+ eshopts_push -s extglob
local lt=1 eq=2 gt=3 p q

__versionator__test_version_compare_t() {
@@ -593,5 +573,5 @@ __versionator__test_version_compare() {
__versionator__test_version_compare_t "7.2${p}3" $gt "7.2${p}2"
__versionator__test_version_compare_t "7.2${p}2" $lt "7.2${p}3"
done
- __versionator_shopt_toggle off
+ eshopts_pop
}

Alec Warner 07-19-2010 03:17 AM

versionator.eclass: convert to eshopts_{push,pop}
 
Can we do away with all the extra foo && return bullshit and just set a trap?

trap "eshopts pop" RETURN

?

On Sun, Jul 18, 2010 at 2:29 PM, Mike Frysinger <vapier@gentoo.org> wrote:
> now that eutils.eclass contains a common version of safely handling shopts
> settings, we can drop the local versionator.eclass code that was handling this
> and convert it to eutils. *seems to work for me, but i rarely use this eclass.
> -mike
>
> --- versionator.eclass *18 Jul 2010 21:24:33 -0000 * * *1.16
> +++ versionator.eclass *18 Jul 2010 21:29:21 -0000
> @@ -25,27 +25,7 @@
> *# * * version_is_at_least * * * * * * want * * *have
> *# *which may be buggy, so use with caution.
>
> -# Quick function to toggle the shopts required for some functions on and off
> -# Used because we can't set extglob in global scope anymore (QA Violation)
> -__versionator_shopt_toggle() {
> - * * * VERSIONATOR_RECURSION=${VERSIONATOR_RECURSION:-0}
> - * * * case "$1" in
> - * * * * * * * "on")
> - * * * * * * * * * * * if [[ $VERSIONATOR_RECURSION -lt 1 ]] ; *then
> - * * * * * * * * * * * * * * * VERSIONATOR_OLD_EXTGLOB=$(shopt -p extglob)
> - * * * * * * * * * * * * * * * shopt -s extglob
> - * * * * * * * * * * * fi
> - * * * * * * * * * * * VERSIONATOR_RECURSION=$(( $VERSIONATOR_RECURSION + 1 ))
> - * * * * * * * * * * * ;;
> - * * * * * * * "off")
> - * * * * * * * * * * * VERSIONATOR_RECURSION=$(( $VERSIONATOR_RECURSION - 1 ))
> - * * * * * * * * * * * if [[ $VERSIONATOR_RECURSION -lt 1 ]] ; then
> - * * * * * * * * * * * * * * * eval $VERSIONATOR_OLD_EXTGLOB
> - * * * * * * * * * * * fi
> - * * * * * * * * * * * ;;
> - * * * esac
> - * * * return 0
> -}
> +inherit eutils
>
> *# @FUNCTION: get_all_version_components
> *# @USAGE: [version]
> @@ -58,7 +38,7 @@ __versionator_shopt_toggle() {
> *# * * 20040905 * *-> *20040905
> *# * * 3.0c-r1 * * -> *3 . 0 c - r1
> *get_all_version_components() {
> - * * * __versionator_shopt_toggle on
> + * * * eshopts_push -s extglob
> * * * *local ver_str=${1:-${PV}} result result_idx=0
> * * * *result=( )
>
> @@ -66,7 +46,7 @@ get_all_version_components() {
> * * * *# times.
> * * * *if [[ "${VERSIONATOR_CACHE_VER_STR}" == "${ver_str}" ]] ; then
> * * * * * * * *echo ${VERSIONATOR_CACHE_RESULT}
> - * * * * * * * __versionator_shopt_toggle off
> + * * * * * * * eshopts_pop
> * * * * * * * *return
> * * * *fi
> * * * *export VERSIONATOR_CACHE_VER_STR="${ver_str}"
> @@ -106,7 +86,7 @@ get_all_version_components() {
>
> * * * *export VERSIONATOR_CACHE_RESULT="${result[@]}"
> * * * *echo ${result[@]}
> - * * * __versionator_shopt_toggle off
> + * * * eshopts_pop
> *}
>
> *# @FUNCTION: get_version_components
> @@ -120,11 +100,11 @@ get_all_version_components() {
> *# * * 20040905 * *-> *20040905
> *# * * 3.0c-r1 * * -> *3 0 c r1
> *get_version_components() {
> - * * * __versionator_shopt_toggle on
> + * * * eshopts_push -s extglob
> * * * *local c="$(get_all_version_components "${1:-${PV}}")"
> * * * *c=( ${c[@]//[-._]/ } )
> * * * *echo ${c[@]}
> - * * * __versionator_shopt_toggle off
> + * * * eshopts_pop
> *}
>
> *# @FUNCTION: get_major_version
> @@ -137,11 +117,11 @@ get_version_components() {
> *# * * 20040905 * *-> *20040905
> *# * * 3.0c-r1 * * -> *3
> *get_major_version() {
> - * * * __versionator_shopt_toggle on
> + * * * eshopts_push -s extglob
> * * * *local c
> * * * *c=( $(get_all_version_components "${1:-${PV}}" ) )
> * * * *echo ${c[0]}
> - * * * __versionator_shopt_toggle off
> + * * * eshopts_pop
> *}
>
> *# @FUNCTION: get_version_component_range
> @@ -153,7 +133,7 @@ get_major_version() {
> *# * *1-2 * *1.2.3 * * * -> 1.2
> *# * *2- * * 1.2.3 * * * -> 2.3
> *get_version_component_range() {
> - * * * __versionator_shopt_toggle on
> + * * * eshopts_push -s extglob
> * * * *local c v="${2:-${PV}}" range="${1}" range_start range_end i=-1 j=0
> * * * *c=( $(get_all_version_components ${v} ) )
> * * * *range_start="${range%-*}" ; range_start="${range_start:-1}"
> @@ -161,17 +141,17 @@ get_version_component_range() {
>
> * * * *while (( j < ${range_start} )) ; do
> * * * * * * * *i=$(($i + 1))
> - * * * * * * * [[ $i -gt ${#c[@]} ]] && __versionator_shopt_toggle off && return
> + * * * * * * * [[ $i -gt ${#c[@]} ]] && eshopts_pop && return
> * * * * * * * *[[ -n "${c[${i}]//[-._]}" ]] && j=$(($j + 1))
> * * * *done
>
> * * * *while (( j <= ${range_end} )) ; do
> * * * * * * * *echo -n ${c[$i]}
> - * * * * * * * [[ $i -gt ${#c[@]} ]] && __versionator_shopt_toggle off && return
> + * * * * * * * [[ $i -gt ${#c[@]} ]] && eshopts_pop && return
> * * * * * * * *[[ -n "${c[${i}]//[-._]}" ]] && j=$(($j + 1))
> * * * * * * * *i=$(($i + 1))
> * * * *done
> - * * * __versionator_shopt_toggle off
> + * * * eshopts_pop
> *}
>
> *# @FUNCTION: get_after_major_version
> @@ -185,9 +165,9 @@ get_version_component_range() {
> *# * * 20040905 * *-> *(empty string)
> *# * * 3.0c-r1 * * -> *0c-r1
> *get_after_major_version() {
> - * * * __versionator_shopt_toggle on
> + * * * eshopts_push -s extglob
> * * * *echo $(get_version_component_range 2- "${1:-${PV}}" )
> - * * * __versionator_shopt_toggle off
> + * * * eshopts_pop
> *}
>
> *# @FUNCTION: replace_version_separator
> @@ -201,7 +181,7 @@ get_after_major_version() {
> *# Rather than being a number, $1 can be a separator character such as '-', '.'
> *# or '_'. In this case, the first separator of this kind is selected.
> *replace_version_separator() {
> - * * * __versionator_shopt_toggle on
> + * * * eshopts_push -s extglob
> * * * *local w i c found=0 v="${3:-${PV}}"
> * * * *w=${1:-1}
> * * * *c=( $(get_all_version_components ${v} ) )
> @@ -226,7 +206,7 @@ replace_version_separator() {
> * * * *fi
> * * * *c=${c[@]}
> * * * *echo ${c// }
> - * * * __versionator_shopt_toggle off
> + * * * eshopts_pop
> *}
>
> *# @FUNCTION: replace_all_version_separators
> @@ -235,12 +215,12 @@ replace_version_separator() {
> *# Replace all version separators in $2 (defaults to $PV) with $1.
> *# * * '_' 1b.2.3 * * * *-> 1b_2_3
> *replace_all_version_separators() {
> - * * * __versionator_shopt_toggle on
> + * * * eshopts_push -s extglob
> * * * *local c
> * * * *c=( $(get_all_version_components "${2:-${PV}}" ) )
> * * * *c="${c[@]//[-._]/$1}"
> * * * *echo ${c// }
> - * * * __versionator_shopt_toggle off
> + * * * eshopts_pop
> *}
>
> *# @FUNCTION: delete_version_separator
> @@ -254,9 +234,9 @@ replace_all_version_separators() {
> *# Rather than being a number, $1 can be a separator character such as '-', '.'
> *# or '_'. In this case, the first separator of this kind is deleted.
> *delete_version_separator() {
> - * * * __versionator_shopt_toggle on
> + * * * eshopts_push -s extglob
> * * * *replace_version_separator "${1}" "" "${2}"
> - * * * __versionator_shopt_toggle off
> + * * * eshopts_pop
> *}
>
> *# @FUNCTION: delete_all_version_separators
> @@ -265,9 +245,9 @@ delete_version_separator() {
> *# Delete all version separators in $1 (defaults to $PV).
> *# * * 1b.2.3 * * * *-> 1b23
> *delete_all_version_separators() {
> - * * * __versionator_shopt_toggle on
> + * * * eshopts_push -s extglob
> * * * *replace_all_version_separators "" "${1}"
> - * * * __versionator_shopt_toggle off
> + * * * eshopts_pop
> *}
>
> *# @FUNCTION: get_version_component_count
> @@ -277,11 +257,11 @@ delete_all_version_separators() {
> *# * * 1.0.1 * * * -> *3
> *# * * 3.0c-r1 * * -> *4
> *get_version_component_count() {
> - * * * __versionator_shopt_toggle on
> + * * * eshopts_push -s extglob
> * * * *local a
> * * * *a=( $(get_version_components "${1:-${PV}}" ) )
> * * * *echo ${#a[@]}
> - * * * __versionator_shopt_toggle off
> + * * * eshopts_pop
> *}
>
> *# @FUNCTION: get_last_version_component_index
> @@ -292,9 +272,9 @@ get_version_component_count() {
> *# * * 1.0.1 * * * -> *3
> *# * * 3.0c-r1 * * -> *4
> *get_last_version_component_index() {
> - * * * __versionator_shopt_toggle on
> + * * * eshopts_push -s extglob
> * * * *echo $(( $(get_version_component_count "${1:-${PV}}" ) - 1 ))
> - * * * __versionator_shopt_toggle off
> + * * * eshopts_pop
> *}
>
> *# @FUNCTION: version_is_at_least
> @@ -304,25 +284,25 @@ get_last_version_component_index() {
> *# only. May not be reliable, be sure to do very careful testing before actually
> *# using this.
> *version_is_at_least() {
> - * * * __versionator_shopt_toggle on
> + * * * eshopts_push -s extglob
> * * * *local want_s="$1" have_s="${2:-${PVR}}" r
> * * * *version_compare "${want_s}" "${have_s}"
> * * * *r=$?
> * * * *case $r in
> * * * * * * * *1|2)
> - * * * * * * * * * * * __versionator_shopt_toggle off
> + * * * * * * * * * * * eshopts_pop
> * * * * * * * * * * * *return 0
> * * * * * * * * * * * *;;
> * * * * * * * *3)
> - * * * * * * * * * * * __versionator_shopt_toggle off
> + * * * * * * * * * * * eshopts_pop
> * * * * * * * * * * * *return 1
> * * * * * * * * * * * *;;
> * * * * * * * **)
> - * * * * * * * * * * * __versionator_shopt_toggle off
> + * * * * * * * * * * * eshopts_pop
> * * * * * * * * * * * *die "versionator compare bug [atleast, ${want_s}, ${have_s}, ${r}]"
> * * * * * * * * * * * *;;
> * * * *esac
> - * * * __versionator_shopt_toggle off
> + * * * eshopts_pop
> *}
>
> *# @FUNCTION: version_compare
> @@ -333,7 +313,7 @@ version_is_at_least() {
> *# return 3. You probably want version_is_at_least rather than this function.
> *# May not be very reliable. Test carefully before using this.
> *version_compare() {
> - * * * __versionator_shopt_toggle on
> + * * * eshopts_push -s extglob
> * * * *local ver_a=${1} ver_b=${2} parts_a parts_b cur_idx_a=0 cur_idx_b=0
> * * * *parts_a=( $(get_all_version_components "${ver_a}" ) )
> * * * *parts_b=( $(get_all_version_components "${ver_b}" ) )
> @@ -379,8 +359,8 @@ version_compare() {
> * * * * * * * *[[ -z ${cur_tok_b} ]] && cur_tok_b=0
>
> * * * * * * * *# compare
> - * * * * * * * [[ ${cur_tok_a} -lt ${cur_tok_b} ]] && __versionator_shopt_toggle off && return 1
> - * * * * * * * [[ ${cur_tok_a} -gt ${cur_tok_b} ]] && __versionator_shopt_toggle off && return 3
> + * * * * * * * [[ ${cur_tok_a} -lt ${cur_tok_b} ]] && eshopts_pop && return 1
> + * * * * * * * [[ ${cur_tok_a} -gt ${cur_tok_b} ]] && eshopts_pop && return 3
> * * * *done
>
> * * * *### number parts equal. compare letter parts.
> @@ -401,8 +381,8 @@ version_compare() {
> * * * *fi
>
> * * * *# compare
> - * * * [[ ${letter_a} < ${letter_b} ]] && __versionator_shopt_toggle off && return 1
> - * * * [[ ${letter_a} > ${letter_b} ]] && __versionator_shopt_toggle off && return 3
> + * * * [[ ${letter_a} < ${letter_b} ]] && eshopts_pop && return 1
> + * * * [[ ${letter_a} > ${letter_b} ]] && eshopts_pop && return 3
>
> * * * *### letter parts equal. compare suffixes in order.
> * * * *local suffix rule part r_lt r_gt
> @@ -427,19 +407,19 @@ version_compare() {
>
> * * * * * * * *[[ -z ${suffix_a} ]] && [[ -z ${suffix_b} ]] && continue
>
> - * * * * * * * [[ -z ${suffix_a} ]] && __versionator_shopt_toggle off && return ${r_gt}
> - * * * * * * * [[ -z ${suffix_b} ]] && __versionator_shopt_toggle off && return ${r_lt}
> + * * * * * * * [[ -z ${suffix_a} ]] && eshopts_pop && return ${r_gt}
> + * * * * * * * [[ -z ${suffix_b} ]] && eshopts_pop && return ${r_lt}
>
> * * * * * * * *# avoid octal problems
> * * * * * * * *suffix_a=${suffix_a##+(0)} ; suffix_a=${suffix_a:-0}
> * * * * * * * *suffix_b=${suffix_b##+(0)} ; suffix_b=${suffix_b:-0}
>
> - * * * * * * * [[ ${suffix_a} -lt ${suffix_b} ]] && __versionator_shopt_toggle off && return 1
> - * * * * * * * [[ ${suffix_a} -gt ${suffix_b} ]] && __versionator_shopt_toggle off && return 3
> + * * * * * * * [[ ${suffix_a} -lt ${suffix_b} ]] && eshopts_pop && return 1
> + * * * * * * * [[ ${suffix_a} -gt ${suffix_b} ]] && eshopts_pop && return 3
> * * * *done
>
> * * * *### no differences.
> - * * * __versionator_shopt_toggle off
> + * * * eshopts_pop
> * * * *return 2
> *}
>
> @@ -450,7 +430,7 @@ version_compare() {
> *# algorithm for simplicity, so don't call it with more than a few dozen items.
> *# Uses version_compare, so be careful.
> *version_sort() {
> - * * * __versionator_shopt_toggle on
> + * * * eshopts_push -s extglob
> * * * *local items= left=0
> * * * *items=( $@ )
> * * * *while [[ ${left} -lt ${#items[@]} ]] ; do
> @@ -467,7 +447,7 @@ version_sort() {
> * * * * * * * *left=$(( ${left} + 1 ))
> * * * *done
> * * * *echo ${items[@]}
> - * * * __versionator_shopt_toggle off
> + * * * eshopts_pop
> *}
>
> *# @FUNCTION: version_format_string
> @@ -489,7 +469,7 @@ version_format_string() {
> *}
>
> *__versionator__test_version_compare() {
> - * * * __versionator_shopt_toggle on
> + * * * eshopts_push -s extglob
> * * * *local lt=1 eq=2 gt=3 p q
>
> * * * *__versionator__test_version_compare_t() {
> @@ -593,5 +573,5 @@ __versionator__test_version_compare() {
> * * * * * * * *__versionator__test_version_compare_t "7.2${p}3" $gt "7.2${p}2"
> * * * * * * * *__versionator__test_version_compare_t "7.2${p}2" $lt "7.2${p}3"
> * * * *done
> - * * * __versionator_shopt_toggle off
> + * * * eshopts_pop
> *}
>
>

Ciaran McCreesh 07-19-2010 07:38 AM

versionator.eclass: convert to eshopts_{push,pop}
 
On Sun, 18 Jul 2010 20:17:45 -0700
Alec Warner <antarus@gentoo.org> wrote:
> Can we do away with all the extra foo && return bullshit and just set
> a trap?
>
> trap "eshopts pop" RETURN
>
> ?

That strikes me as a horribly fragile way of doing things that's bound
to come back and screw things up at some point...

--
Ciaran McCreesh

Mike Frysinger 07-19-2010 07:43 PM

versionator.eclass: convert to eshopts_{push,pop}
 
On Monday, July 19, 2010 03:38:39 Ciaran McCreesh wrote:
> On Sun, 18 Jul 2010 20:17:45 -0700 Alec Warner wrote:
> > Can we do away with all the extra foo && return bullshit and just set
> > a trap?
> >
> > trap "eshopts pop" RETURN
> >
> > ?
>
> That strikes me as a horribly fragile way of doing things that's bound
> to come back and screw things up at some point...

nifty in theory, but i'm inclined to agree with Ciaran
-mike

Alec Warner 07-19-2010 07:53 PM

versionator.eclass: convert to eshopts_{push,pop}
 
On Mon, Jul 19, 2010 at 12:43 PM, Mike Frysinger <vapier@gentoo.org> wrote:
> On Monday, July 19, 2010 03:38:39 Ciaran McCreesh wrote:
>> On Sun, 18 Jul 2010 20:17:45 -0700 Alec Warner wrote:
>> > Can we do away with all the extra foo && return bullshit and just set
>> > a trap?
>> >
>> > trap "eshopts pop" RETURN
>> >
>> > *?
>>
>> That strikes me as a horribly fragile way of doing things that's bound
>> to come back and screw things up at some point...
>
> nifty in theory, but i'm inclined to agree with Ciaran
> -mike
>

no worries, please proceed with your patch ;)

-A

David Leverton 07-19-2010 08:07 PM

versionator.eclass: convert to eshopts_{push,pop}
 
On 19 July 2010 20:43, Mike Frysinger <vapier@gentoo.org> wrote:
> On Monday, July 19, 2010 03:38:39 Ciaran McCreesh wrote:
>> On Sun, 18 Jul 2010 20:17:45 -0700 Alec Warner wrote:
>> > Can we do away with all the extra foo && return bullshit and just set
>> > a trap?
>> >
>> > trap "eshopts pop" RETURN
>> >
>> > *?
>>
>> That strikes me as a horribly fragile way of doing things that's bound
>> to come back and screw things up at some point...
>
> nifty in theory, but i'm inclined to agree with Ciaran
> -mike

Is something like the below function too hideous (not massively
tested, but it seems to work)? Usage is something like:

[dleverton@shiny-one ~] $ foo() { shopt -p extglob; }
[dleverton@shiny-one ~] $ eshopts_need foo -s extglob
[dleverton@shiny-one ~] $ shopt -p extglob
shopt -u extglob
[dleverton@shiny-one ~] $ foo
shopt -s extglob
[dleverton@shiny-one ~] $ shopt -p extglob
shopt -u extglob

eshopts_need() {
[[ $# -ge 1 ]] || die "eshopts_need needs at least one argument"
local func=$1
shift
local opts=( "${@}" )
if [[ $1 == -[su] ]] ; then
eval "_eshopts_need_shopt_$(declare -f $func)"
eval "$func() {
local old=$(shopt -p)
$(declare -p opts)
shopt "${opts[@]}"
_eshopts_need_shopt_$func "$@"
local status=$?
eval "$old"
return $status
}"
else
eval "_eshopts_need_set_$(declare -f $func)"
eval "$func() {
local old=$-
$(declare -p opts)
set "${opts[@]}"
_eshopts_need_set_$func "$@"
local status=$?
set +$-
set -$old
return $status
}"
fi
}

Mike Frysinger 07-19-2010 08:30 PM

versionator.eclass: convert to eshopts_{push,pop}
 
On Monday, July 19, 2010 16:07:56 David Leverton wrote:
> On 19 July 2010 20:43, Mike Frysinger <vapier@gentoo.org> wrote:
> > On Monday, July 19, 2010 03:38:39 Ciaran McCreesh wrote:
> >> On Sun, 18 Jul 2010 20:17:45 -0700 Alec Warner wrote:
> >> > Can we do away with all the extra foo && return bullshit and just set
> >> > a trap?
> >> >
> >> > trap "eshopts pop" RETURN
> >> >
> >> > ?
> >>
> >> That strikes me as a horribly fragile way of doing things that's bound
> >> to come back and screw things up at some point...
> >
> > nifty in theory, but i'm inclined to agree with Ciaran
>
> Is something like the below function too hideous (not massively
> tested, but it seems to work)? Usage is something like:
>
> [dleverton@shiny-one ~] $ foo() { shopt -p extglob; }
> [dleverton@shiny-one ~] $ eshopts_need foo -s extglob

i imagine this might be useful in some scenarios, but i think the more common
usage is to enable things inline. otherwise, the exported API would need to
be wrapped internally like:
get_all_version_components() {
eshopts_need _get_all_version_components -s extglob
}

and at this point, it's hard to say this is better than just doing:
get_all_version_components() {
eshopts_push -s extglob
_get_all_version_components
eshopts_pop
}

... or the existing code we have now. although, the method we have now also
allows for disabling of shopts before calling `die`. not sure if that's
important, but i think it's better to disable before all exit/termination
points.

so unless their is a consumer now we can point to in the tree, i'm inclined to
leave this alone.

> eshopts_need() {
> [[ $# -ge 1 ]] || die "eshopts_need needs at least one argument"
> local func=$1
> shift
> local opts=( "${@}" )
> if [[ $1 == -[su] ]] ; then
> eval "_eshopts_need_shopt_$(declare -f $func)"
> eval "$func() {
> local old=$(shopt -p)
> $(declare -p opts)
> shopt "${opts[@]}"
> _eshopts_need_shopt_$func "$@"
> local status=$?
> eval "$old"
> return $status
> }"

there are already eshopts_push/pop funcs that accomodate more things than the
usage here, but i imagine you did it this way just to make testing in your
shell easier
-mike

David Leverton 07-19-2010 08:45 PM

versionator.eclass: convert to eshopts_{push,pop}
 
On 19 July 2010 21:30, Mike Frysinger <vapier@gentoo.org> wrote:
> i imagine this might be useful in some scenarios, but i think the more common
> usage is to enable things inline. *otherwise, the exported API would need to
> be wrapped internally like:
> get_all_version_components() {
> * * * *eshopts_need _get_all_version_components -s extglob
> }

It's meant to work to just define get_all_version_components with the
assumption that extglob will be enabled, and then call eshopts_need in
global scope right after.

> although, the method we have now also
> allows for disabling of shopts before calling `die`. *not sure if that's
> important, but i think it's better to disable before all exit/termination
> points.

That's a reasonable point.

> so unless their is a consumer now we can point to in the tree, i'm inclined to
> leave this alone.

Fair enough.

> there are already eshopts_push/pop funcs that accomodate more things than the
> usage here, but i imagine you did it this way just to make testing in your
> shell easier
> -mike

It was supposed to support everything the existing functions do (not
everything demonstrated in the example for brevity), but I might have
missed something.

Mike Frysinger 07-19-2010 09:11 PM

versionator.eclass: convert to eshopts_{push,pop}
 
On Monday, July 19, 2010 16:45:22 David Leverton wrote:
> On 19 July 2010 21:30, Mike Frysinger <vapier@gentoo.org> wrote:
> > i imagine this might be useful in some scenarios, but i think the more
> > common usage is to enable things inline. otherwise, the exported API
> > would need to be wrapped internally like:
> > get_all_version_components() {
> > eshopts_need _get_all_version_components -s extglob
> > }
>
> It's meant to work to just define get_all_version_components with the
> assumption that extglob will be enabled, and then call eshopts_need in
> global scope right after.

you mean the people who want to use get_all_version_components would have to
change their invocation to go through eshops_need ? otherwise i dont follow
what you mean.

> > there are already eshopts_push/pop funcs that accomodate more things than
> > the usage here, but i imagine you did it this way just to make testing
> > in your shell easier
>
> It was supposed to support everything the existing functions do (not
> everything demonstrated in the example for brevity), but I might have
> missed something.

yeah, i missed the point of the branch. but still, i'd prefer we keep the
logic in one place so when a bug is found, only one place needs fixing.
-mike

David Leverton 07-19-2010 09:44 PM

versionator.eclass: convert to eshopts_{push,pop}
 
On 19 July 2010 22:11, Mike Frysinger <vapier@gentoo.org> wrote:
> you mean the people who want to use get_all_version_components would have to
> change their invocation to go through eshops_need ? *otherwise i dont follow
> what you mean.

You define the function, then call eshopts_need immediately
afterwards, and it sets up the wrapper automatically. You can then
call get_all_version_components as normal.


All times are GMT. The time now is 06:54 AM.

VBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.