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: 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: 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
### 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
}
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
07-19-2010, 07:43 PM
Mike Frysinger
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
07-19-2010, 07:53 PM
Alec Warner
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
07-19-2010, 08:07 PM
David Leverton
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:
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
}
07-19-2010, 08:30 PM
Mike Frysinger
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
07-19-2010, 08:45 PM
David Leverton
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.
07-19-2010, 09:11 PM
Mike Frysinger
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
07-19-2010, 09:44 PM
David Leverton
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.