From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by finch.gentoo.org (Postfix) with ESMTPS id 2A5F8139694 for ; Sun, 12 Mar 2017 10:13:49 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id D6DA9E0D34; Sun, 12 Mar 2017 10:13:40 +0000 (UTC) Received: from smtp.gentoo.org (woodpecker.gentoo.org [IPv6:2001:470:ea4a:1:5054:ff:fec7:86e4]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id 5D74DE0D1A for ; Sun, 12 Mar 2017 10:13:40 +0000 (UTC) Received: from localhost.localdomain (d202-252.icpnet.pl [109.173.202.252]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) (Authenticated sender: mgorny) by smtp.gentoo.org (Postfix) with ESMTPSA id 86653340AD2; Sun, 12 Mar 2017 10:13:38 +0000 (UTC) From: =?UTF-8?q?Micha=C5=82=20G=C3=B3rny?= To: gentoo-dev@lists.gentoo.org Cc: =?UTF-8?q?Micha=C5=82=20G=C3=B3rny?= Subject: [gentoo-dev] [PATCH v3] estack.eclass: Split estack* logic from eutils Date: Sun, 12 Mar 2017 11:13:32 +0100 Message-Id: <20170312101332.10442-1-mgorny@gentoo.org> X-Mailer: git-send-email 2.12.0 In-Reply-To: <20170311135058.3223-1-mgorny@gentoo.org> References: <20170311135058.3223-1-mgorny@gentoo.org> Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-dev@lists.gentoo.org Reply-to: gentoo-dev@lists.gentoo.org MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Archives-Salt: 636f5e03-fc91-4492-b298-c67af283241c X-Archives-Hash: aac1beef7527ddf89b8ebb9abafc0c59 Split the estack_* and related functions from eutils into a dedicated estack.eclass. Those functions have significant complexity and are not used frequently, therefore they benefit from having a separate file and an explicit dedicated maintainer. The new eclass is implicitly inherited by eutils to preserve compatibility. However, the inherit will be removed in EAPI 7, and the ebuilds should switch to using estack directly. Thanks to Ulrich Müller for doing the research on this. // v3: also rename&update tests --- eclass/estack.eclass | 217 +++++++++++++++++++++ eclass/eutils.eclass | 210 +------------------- .../tests/{eutils_eshopts.sh => estack_eshopts.sh} | 4 +- .../tests/{eutils_estack.sh => estack_estack.sh} | 4 +- eclass/tests/{eutils_evar.sh => estack_evar.sh} | 4 +- 5 files changed, 230 insertions(+), 209 deletions(-) create mode 100644 eclass/estack.eclass rename eclass/tests/{eutils_eshopts.sh => estack_eshopts.sh} (93%) rename eclass/tests/{eutils_estack.sh => estack_estack.sh} (93%) rename eclass/tests/{eutils_evar.sh => estack_evar.sh} (96%) diff --git a/eclass/estack.eclass b/eclass/estack.eclass new file mode 100644 index 000000000000..19c388f3d8d2 --- /dev/null +++ b/eclass/estack.eclass @@ -0,0 +1,217 @@ +# Copyright 1999-2017 Gentoo Foundation +# Distributed under the terms of the GNU General Public License v2 + +# @ECLASS: estack.eclass +# @MAINTAINER: +# base-system@gentoo.org +# @BLURB: stack-like value storage support +# @DESCRIPTION: +# Support for storing values on stack-like variables. + +if [[ -z ${_ESTACK_ECLASS} ]]; then + +# @FUNCTION: estack_push +# @USAGE: [items to push] +# @DESCRIPTION: +# Push any number of items onto the specified stack. Pick a name that +# is a valid variable (i.e. stick to alphanumerics), and push as many +# items as you like onto the stack at once. +# +# The following code snippet will echo 5, then 4, then 3, then ... +# @CODE +# estack_push mystack 1 2 3 4 5 +# while estack_pop mystack i ; do +# echo "${i}" +# done +# @CODE +estack_push() { + [[ $# -eq 0 ]] && die "estack_push: incorrect # of arguments" + local stack_name="_ESTACK_$1_" ; shift + eval ${stack_name}+=\( \"\$@\" \) +} + +# @FUNCTION: estack_pop +# @USAGE: [variable] +# @DESCRIPTION: +# Pop a single item off the specified stack. If a variable is specified, +# the popped item is stored there. If no more items are available, return +# 1, else return 0. See estack_push for more info. +estack_pop() { + [[ $# -eq 0 || $# -gt 2 ]] && die "estack_pop: incorrect # of arguments" + + # We use the fugly _estack_xxx var names to avoid collision with + # passing back the return value. If we used "local i" and the + # caller ran `estack_pop ... i`, we'd end up setting the local + # copy of "i" rather than the caller's copy. The _estack_xxx + # garbage is preferable to using $1/$2 everywhere as that is a + # bit harder to read. + local _estack_name="_ESTACK_$1_" ; shift + local _estack_retvar=$1 ; shift + eval local _estack_i=\${#${_estack_name}\[@\]} + # Don't warn -- let the caller interpret this as a failure + # or as normal behavior (akin to `shift`) + [[ $(( --_estack_i )) -eq -1 ]] && return 1 + + if [[ -n ${_estack_retvar} ]] ; then + eval ${_estack_retvar}=\"\${${_estack_name}\[${_estack_i}\]}\" + fi + eval unset \"${_estack_name}\[${_estack_i}\]\" +} + +# @FUNCTION: evar_push +# @USAGE: [more vars to save] +# @DESCRIPTION: +# This let's you temporarily modify a variable and then restore it (including +# set vs unset semantics). Arrays are not supported at this time. +# +# This is meant for variables where using `local` does not work (such as +# exported variables, or only temporarily changing things in a func). +# +# For example: +# @CODE +# evar_push LC_ALL +# export LC_ALL=C +# ... do some stuff that needs LC_ALL=C set ... +# evar_pop +# +# # You can also save/restore more than one var at a time +# evar_push BUTTERFLY IN THE SKY +# ... do stuff with the vars ... +# evar_pop # This restores just one var, SKY +# ... do more stuff ... +# evar_pop 3 # This pops the remaining 3 vars +# @CODE +evar_push() { + local var val + for var ; do + [[ ${!var+set} == "set" ]] \ + && val=${!var} \ + || val="unset_76fc3c462065bb4ca959f939e6793f94" + estack_push evar "${var}" "${val}" + done +} + +# @FUNCTION: evar_push_set +# @USAGE: [new value to store] +# @DESCRIPTION: +# This is a handy shortcut to save and temporarily set a variable. If a value +# is not specified, the var will be unset. +evar_push_set() { + local var=$1 + evar_push ${var} + case $# in + 1) unset ${var} ;; + 2) printf -v "${var}" '%s' "$2" ;; + *) die "${FUNCNAME}: incorrect # of args: $*" ;; + esac +} + +# @FUNCTION: evar_pop +# @USAGE: [number of vars to restore] +# @DESCRIPTION: +# Restore the variables to the state saved with the corresponding +# evar_push call. See that function for more details. +evar_pop() { + local cnt=${1:-bad} + case $# in + 0) cnt=1 ;; + 1) isdigit "${cnt}" || die "${FUNCNAME}: first arg must be a number: $*" ;; + *) die "${FUNCNAME}: only accepts one arg: $*" ;; + esac + + local var val + while (( cnt-- )) ; do + estack_pop evar val || die "${FUNCNAME}: unbalanced push" + estack_pop evar var || die "${FUNCNAME}: unbalanced push" + [[ ${val} == "unset_76fc3c462065bb4ca959f939e6793f94" ]] \ + && unset ${var} \ + || printf -v "${var}" '%s' "${val}" + done +} + +# @FUNCTION: eshopts_push +# @USAGE: [options to `set` or `shopt`] +# @DESCRIPTION: +# Often times code will want to enable a shell option to change code behavior. +# Since changing shell options can easily break other pieces of code (which +# assume the default state), eshopts_push is used to (1) push the current shell +# options onto a stack and (2) pass the specified arguments to set. +# +# If the first argument is '-s' or '-u', we assume you want to call `shopt` +# rather than `set` as there are some options only available via that. +# +# A common example is to disable shell globbing so that special meaning/care +# may be used with variables/arguments to custom functions. That would be: +# @CODE +# eshopts_push -o noglob +# for x in ${foo} ; do +# if ...some check... ; then +# eshopts_pop +# return 0 +# fi +# done +# eshopts_pop +# @CODE +eshopts_push() { + if [[ $1 == -[su] ]] ; then + estack_push eshopts "$(shopt -p)" + [[ $# -eq 0 ]] && return 0 + shopt "$@" || die "${FUNCNAME}: bad options to shopt: $*" + else + estack_push eshopts $- + [[ $# -eq 0 ]] && return 0 + set "$@" || die "${FUNCNAME}: bad options to set: $*" + fi +} + +# @FUNCTION: eshopts_pop +# @USAGE: +# @DESCRIPTION: +# Restore the shell options to the state saved with the corresponding +# eshopts_push call. See that function for more details. +eshopts_pop() { + local s + estack_pop eshopts s || die "${FUNCNAME}: unbalanced push" + if [[ ${s} == "shopt -"* ]] ; then + eval "${s}" || die "${FUNCNAME}: sanity: invalid shopt options: ${s}" + else + set +$- || die "${FUNCNAME}: sanity: invalid shell settings: $-" + set -${s} || die "${FUNCNAME}: sanity: unable to restore saved shell settings: ${s}" + fi +} + +# @FUNCTION: eumask_push +# @USAGE: +# @DESCRIPTION: +# Set the umask to the new value specified while saving the previous +# value onto a stack. Useful for temporarily changing the umask. +eumask_push() { + estack_push eumask "$(umask)" + umask "$@" || die "${FUNCNAME}: bad options to umask: $*" +} + +# @FUNCTION: eumask_pop +# @USAGE: +# @DESCRIPTION: +# Restore the previous umask state. +eumask_pop() { + [[ $# -eq 0 ]] || die "${FUNCNAME}: we take no options" + local s + estack_pop eumask s || die "${FUNCNAME}: unbalanced push" + umask ${s} || die "${FUNCNAME}: sanity: could not restore umask: ${s}" +} + +# @FUNCTION: isdigit +# @USAGE: [more numbers] +# @DESCRIPTION: +# Return true if all arguments are numbers. +isdigit() { + local d + for d ; do + [[ ${d:-bad} == *[!0-9]* ]] && return 1 + done + return 0 +} + +_ESTACK_ECLASS=1 +fi #_ESTACK_ECLASS diff --git a/eclass/eutils.eclass b/eclass/eutils.eclass index ac6a4854d17b..79ec00042a3f 100644 --- a/eclass/eutils.eclass +++ b/eclass/eutils.eclass @@ -19,6 +19,13 @@ _EUTILS_ECLASS=1 inherit multilib toolchain-funcs +# implicitly inherited (now split) eclasses +case ${EAPI:-0} in +0|1|2|3|4|5|6) + inherit estack + ;; +esac + # @FUNCTION: eqawarn # @USAGE: [message] # @DESCRIPTION: @@ -63,209 +70,6 @@ egit_clean() { find "$@" -type d -name '.git*' -prune -print0 | xargs -0 rm -rf } -# @FUNCTION: estack_push -# @USAGE: [items to push] -# @DESCRIPTION: -# Push any number of items onto the specified stack. Pick a name that -# is a valid variable (i.e. stick to alphanumerics), and push as many -# items as you like onto the stack at once. -# -# The following code snippet will echo 5, then 4, then 3, then ... -# @CODE -# estack_push mystack 1 2 3 4 5 -# while estack_pop mystack i ; do -# echo "${i}" -# done -# @CODE -estack_push() { - [[ $# -eq 0 ]] && die "estack_push: incorrect # of arguments" - local stack_name="_ESTACK_$1_" ; shift - eval ${stack_name}+=\( \"\$@\" \) -} - -# @FUNCTION: estack_pop -# @USAGE: [variable] -# @DESCRIPTION: -# Pop a single item off the specified stack. If a variable is specified, -# the popped item is stored there. If no more items are available, return -# 1, else return 0. See estack_push for more info. -estack_pop() { - [[ $# -eq 0 || $# -gt 2 ]] && die "estack_pop: incorrect # of arguments" - - # We use the fugly _estack_xxx var names to avoid collision with - # passing back the return value. If we used "local i" and the - # caller ran `estack_pop ... i`, we'd end up setting the local - # copy of "i" rather than the caller's copy. The _estack_xxx - # garbage is preferable to using $1/$2 everywhere as that is a - # bit harder to read. - local _estack_name="_ESTACK_$1_" ; shift - local _estack_retvar=$1 ; shift - eval local _estack_i=\${#${_estack_name}\[@\]} - # Don't warn -- let the caller interpret this as a failure - # or as normal behavior (akin to `shift`) - [[ $(( --_estack_i )) -eq -1 ]] && return 1 - - if [[ -n ${_estack_retvar} ]] ; then - eval ${_estack_retvar}=\"\${${_estack_name}\[${_estack_i}\]}\" - fi - eval unset \"${_estack_name}\[${_estack_i}\]\" -} - -# @FUNCTION: evar_push -# @USAGE: [more vars to save] -# @DESCRIPTION: -# This let's you temporarily modify a variable and then restore it (including -# set vs unset semantics). Arrays are not supported at this time. -# -# This is meant for variables where using `local` does not work (such as -# exported variables, or only temporarily changing things in a func). -# -# For example: -# @CODE -# evar_push LC_ALL -# export LC_ALL=C -# ... do some stuff that needs LC_ALL=C set ... -# evar_pop -# -# # You can also save/restore more than one var at a time -# evar_push BUTTERFLY IN THE SKY -# ... do stuff with the vars ... -# evar_pop # This restores just one var, SKY -# ... do more stuff ... -# evar_pop 3 # This pops the remaining 3 vars -# @CODE -evar_push() { - local var val - for var ; do - [[ ${!var+set} == "set" ]] \ - && val=${!var} \ - || val="unset_76fc3c462065bb4ca959f939e6793f94" - estack_push evar "${var}" "${val}" - done -} - -# @FUNCTION: evar_push_set -# @USAGE: [new value to store] -# @DESCRIPTION: -# This is a handy shortcut to save and temporarily set a variable. If a value -# is not specified, the var will be unset. -evar_push_set() { - local var=$1 - evar_push ${var} - case $# in - 1) unset ${var} ;; - 2) printf -v "${var}" '%s' "$2" ;; - *) die "${FUNCNAME}: incorrect # of args: $*" ;; - esac -} - -# @FUNCTION: evar_pop -# @USAGE: [number of vars to restore] -# @DESCRIPTION: -# Restore the variables to the state saved with the corresponding -# evar_push call. See that function for more details. -evar_pop() { - local cnt=${1:-bad} - case $# in - 0) cnt=1 ;; - 1) isdigit "${cnt}" || die "${FUNCNAME}: first arg must be a number: $*" ;; - *) die "${FUNCNAME}: only accepts one arg: $*" ;; - esac - - local var val - while (( cnt-- )) ; do - estack_pop evar val || die "${FUNCNAME}: unbalanced push" - estack_pop evar var || die "${FUNCNAME}: unbalanced push" - [[ ${val} == "unset_76fc3c462065bb4ca959f939e6793f94" ]] \ - && unset ${var} \ - || printf -v "${var}" '%s' "${val}" - done -} - -# @FUNCTION: eshopts_push -# @USAGE: [options to `set` or `shopt`] -# @DESCRIPTION: -# Often times code will want to enable a shell option to change code behavior. -# Since changing shell options can easily break other pieces of code (which -# assume the default state), eshopts_push is used to (1) push the current shell -# options onto a stack and (2) pass the specified arguments to set. -# -# If the first argument is '-s' or '-u', we assume you want to call `shopt` -# rather than `set` as there are some options only available via that. -# -# A common example is to disable shell globbing so that special meaning/care -# may be used with variables/arguments to custom functions. That would be: -# @CODE -# eshopts_push -o noglob -# for x in ${foo} ; do -# if ...some check... ; then -# eshopts_pop -# return 0 -# fi -# done -# eshopts_pop -# @CODE -eshopts_push() { - if [[ $1 == -[su] ]] ; then - estack_push eshopts "$(shopt -p)" - [[ $# -eq 0 ]] && return 0 - shopt "$@" || die "${FUNCNAME}: bad options to shopt: $*" - else - estack_push eshopts $- - [[ $# -eq 0 ]] && return 0 - set "$@" || die "${FUNCNAME}: bad options to set: $*" - fi -} - -# @FUNCTION: eshopts_pop -# @USAGE: -# @DESCRIPTION: -# Restore the shell options to the state saved with the corresponding -# eshopts_push call. See that function for more details. -eshopts_pop() { - local s - estack_pop eshopts s || die "${FUNCNAME}: unbalanced push" - if [[ ${s} == "shopt -"* ]] ; then - eval "${s}" || die "${FUNCNAME}: sanity: invalid shopt options: ${s}" - else - set +$- || die "${FUNCNAME}: sanity: invalid shell settings: $-" - set -${s} || die "${FUNCNAME}: sanity: unable to restore saved shell settings: ${s}" - fi -} - -# @FUNCTION: eumask_push -# @USAGE: -# @DESCRIPTION: -# Set the umask to the new value specified while saving the previous -# value onto a stack. Useful for temporarily changing the umask. -eumask_push() { - estack_push eumask "$(umask)" - umask "$@" || die "${FUNCNAME}: bad options to umask: $*" -} - -# @FUNCTION: eumask_pop -# @USAGE: -# @DESCRIPTION: -# Restore the previous umask state. -eumask_pop() { - [[ $# -eq 0 ]] || die "${FUNCNAME}: we take no options" - local s - estack_pop eumask s || die "${FUNCNAME}: unbalanced push" - umask ${s} || die "${FUNCNAME}: sanity: could not restore umask: ${s}" -} - -# @FUNCTION: isdigit -# @USAGE: [more numbers] -# @DESCRIPTION: -# Return true if all arguments are numbers. -isdigit() { - local d - for d ; do - [[ ${d:-bad} == *[!0-9]* ]] && return 1 - done - return 0 -} - # @VARIABLE: EPATCH_SOURCE # @DESCRIPTION: # Default directory to search for patches. diff --git a/eclass/tests/eutils_eshopts.sh b/eclass/tests/estack_eshopts.sh similarity index 93% rename from eclass/tests/eutils_eshopts.sh rename to eclass/tests/estack_eshopts.sh index 2e9bbb6d4612..606a17cfb053 100755 --- a/eclass/tests/eutils_eshopts.sh +++ b/eclass/tests/estack_eshopts.sh @@ -1,10 +1,10 @@ #!/bin/bash -# Copyright 1999-2015 Gentoo Foundation +# Copyright 1999-2017 Gentoo Foundation # Distributed under the terms of the GNU General Public License v2 source tests-common.sh -inherit eutils +inherit estack test-it() { local s0 s1 s2 diff --git a/eclass/tests/eutils_estack.sh b/eclass/tests/estack_estack.sh similarity index 93% rename from eclass/tests/eutils_estack.sh rename to eclass/tests/estack_estack.sh index c1fecf323133..4845243d3ae4 100755 --- a/eclass/tests/eutils_estack.sh +++ b/eclass/tests/estack_estack.sh @@ -1,10 +1,10 @@ #!/bin/bash -# Copyright 1999-2015 Gentoo Foundation +# Copyright 1999-2017 Gentoo Foundation # Distributed under the terms of the GNU General Public License v2 source tests-common.sh -inherit eutils +inherit estack tbegin "initial stack state" estack_pop teststack diff --git a/eclass/tests/eutils_evar.sh b/eclass/tests/estack_evar.sh similarity index 96% rename from eclass/tests/eutils_evar.sh rename to eclass/tests/estack_evar.sh index f8db340f723d..29badba0079e 100755 --- a/eclass/tests/eutils_evar.sh +++ b/eclass/tests/estack_evar.sh @@ -1,10 +1,10 @@ #!/bin/bash -# Copyright 1999-2015 Gentoo Foundation +# Copyright 1999-2017 Gentoo Foundation # Distributed under the terms of the GNU General Public License v2 source tests-common.sh -inherit eutils +inherit estack tbegin "simple push/pop" VAR=1 -- 2.12.0