Gentoo Logo
Gentoo Spaceship




Note: Due to technical difficulties, the Archives are currently not up to date. GMANE provides an alternative service for most mailing lists.
c.f. bug 424647
List Archive: gentoo-dev
Navigation:
Lists: gentoo-dev: < Prev By Thread Next > < Prev By Date Next >
Headers:
To: gentoo-dev@g.o
From: Alec Warner <antarus@g.o>
Subject: Re: versionator.eclass: convert to eshopts_{push,pop}
Date: Sun, 18 Jul 2010 20:17:45 -0700
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@g.o> 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
>  }
>
>


Replies:
Re: versionator.eclass: convert to eshopts_{push,pop}
-- Ciaran McCreesh
References:
versionator.eclass: convert to eshopts_{push,pop}
-- Mike Frysinger
Navigation:
Lists: gentoo-dev: < Prev By Thread Next > < Prev By Date Next >
Previous by thread:
versionator.eclass: convert to eshopts_{push,pop}
Next by thread:
Re: versionator.eclass: convert to eshopts_{push,pop}
Previous by date:
Re: Re: Upcoming Council meeting on July 26th, 1900 UTC
Next by date:
Re: versionator.eclass: convert to eshopts_{push,pop}


Updated Jun 29, 2012

Summary: Archive of the gentoo-dev mailing list.

Donate to support our development efforts.

Copyright 2001-2013 Gentoo Foundation, Inc. Questions, Comments? Contact us.