From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from pigeon.gentoo.org ([208.92.234.80] helo=lists.gentoo.org) by finch.gentoo.org with esmtp (Exim 4.60) (envelope-from ) id 1RaxHH-0002VO-R1 for garchives@archives.gentoo.org; Wed, 14 Dec 2011 22:26:20 +0000 Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 9625921C2EC; Wed, 14 Dec 2011 22:26:04 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) by pigeon.gentoo.org (Postfix) with ESMTP id 734FD21C2E6 for ; Wed, 14 Dec 2011 22:25:22 +0000 (UTC) Received: from vapier.localnet (localhost [127.0.0.1]) by smtp.gentoo.org (Postfix) with ESMTP id AAC121B4038 for ; Wed, 14 Dec 2011 22:25:21 +0000 (UTC) From: Mike Frysinger Organization: wh0rd.org To: gentoo-dev@lists.gentoo.org Subject: [gentoo-dev] estack_{push,pop}: cool new helpers or over engineering? Date: Wed, 14 Dec 2011 17:25:21 -0500 User-Agent: KMail/1.13.7 (Linux/3.1.0-atsc; KDE/4.6.5; x86_64; ; ) 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: multipart/signed; boundary="nextPart4435302.pBAeTAOonX"; protocol="application/pgp-signature"; micalg=pgp-sha1 Content-Transfer-Encoding: 7bit Message-Id: <201112141725.22148.vapier@gentoo.org> X-Archives-Salt: dd022478-2610-4458-9593-b87a8d2c8035 X-Archives-Hash: 292f1ea0625364fbc68c261297c2b63c --nextPart4435302.pBAeTAOonX Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable i needed to temporarily modify the umask in some vcs eclasses. rather than open coding the umask saving/restoring, i decided to re-use the eshopts_{push,pop} logic so the umask can be pushed/popped easily. the resulting code was mostly copy & paste the same, and the stack maintena= nce ends up drowning out the meat of the stuff i care about -- screwing with the umask. so to that end, i added a set of generic stack helpers: estack_{push,pop}. then i rewrote eshopts_{push,pop} and based eumask_{push,pop} on top of that. what do people think ? good stuff, or am i trying too hard ? =2Dmike =2D-- eutils.eclass 14 Dec 2011 17:36:18 -0000 1.372 +++ eutils.eclass 14 Dec 2011 22:23:02 -0000 @@ -100,6 +100,51 @@ esvn_clean() { find "$@" -type d -name '.svn' -prune -print0 | xargs -0 rm -rf } =20 +# @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 +# estask_push mystack 1 2 3 4 5 +# while i=3D$(estack_pop mystack) ; do +# echo ${i} +# done +# @CODE +estack_push() { + [[ $# -eq 0 ]] && die "estack_push: incorrect # of arguments" + local stack_name=3D"__ESTACK_$1__" ; shift + eval ${stack_name}+=3D\( \"\$@\" \) +} + +# @FUNCTION: estack_pop +# @USAGE: +# @DESCRIPTION: +# Pop a single item off the specified stack and return 0. If no more +# items are available, return 1. See estack_push for more info. +estack_pop() { + if [[ $# -ne 1 ]] ; then + # Would like to call `die` here, but people will usually + # be calling this in a subshell; e.g. + # val=3D$(estack_pop foo) + eerror "estack_pop: incorrect # of arguments" + return 1 + fi + + local stack_name=3D"__ESTACK_$1__" ; shift + eval local i=3D\${#${stack_name}[@]} + # Don't warn -- let the caller interpret this as a failure + # or as normal behavior (akin to `shift`) + [[ $(( --i )) -eq -1 ]] && return 1 + + eval local s=3D\"\${${stack_name}[${i}]}\" + eval unset ${stack_name}[${i}] + echo "${s}" +} + # @FUNCTION: eshopts_push # @USAGE: [options to `set` or `shopt`] # @DESCRIPTION: @@ -126,15 +171,14 @@ esvn_clean() { eshopts_push() { # have to assume __ESHOPTS_SAVE__ isn't screwed with # as a `declare -a` here will reset its value =2D local i=3D${#__ESHOPTS_SAVE__[@]} if [[ $1 =3D=3D -[su] ]] ; then =2D __ESHOPTS_SAVE__[$i]=3D$(shopt -p) + estack_push eshopts "$(shopt -p)" [[ $# -eq 0 ]] && return 0 shopt "$@" || die "eshopts_push: bad options to shopt: $*" else =2D __ESHOPTS_SAVE__[$i]=3D$- + estack_push eshopts $- [[ $# -eq 0 ]] && return 0 set "$@" || die "eshopts_push: bad options to set: $*" fi @@ -144,19 +188,36 @@ eshopts_push() { # Restore the shell options to the state saved with the corresponding # eshopts_push call. See that function for more details. eshopts_pop() { =2D [[ $# -ne 0 ]] && die "eshopts_pop takes no arguments" =2D local i=3D$(( ${#__ESHOPTS_SAVE__[@]} - 1 )) =2D [[ ${i} -eq -1 ]] && die "eshopts_{push,pop}: unbalanced pair" =2D local s=3D${__ESHOPTS_SAVE__[$i]} =2D unset __ESHOPTS_SAVE__[$i] + local s + s=3D$(estack_pop eshopts) || die # do not merge with `local` above if [[ ${s} =3D=3D "shopt -"* ]] ; then eval "${s}" || die "eshopts_pop: sanity: invalid shopt options: ${s}" else set +$- || die "eshopts_pop: sanity: invalid shell settings: $-" set -${s} || die "eshopts_pop: sanity: unable to restore saved shell s= ettings: ${s}" fi } =20 +# @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() { + local s + s=3D$(estack_pop eumask) || die # do not merge with `local` above + umask ${s} || die "${FUNCNAME}: sanity: could not restore umask: ${s}" +} + # @VARIABLE: EPATCH_SOURCE # @DESCRIPTION: # Default directory to search for patches. --nextPart4435302.pBAeTAOonX Content-Type: application/pgp-signature; name=signature.asc Content-Description: This is a digitally signed message part. -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.17 (GNU/Linux) iQIcBAABAgAGBQJO6SJSAAoJEEFjO5/oN/WBGKQQAJAbogK1nc0l0VdnEHizf04S VRbNxkkYpPgU7jw0kTHQkLDggrQSGLHWHcejSXimDVmT34kovyvOH0htIe4QZKQo uOxrsp0EcwkH0TsYEugpCKC7LHzNaL+TlnLVwpouQ7bwI+qgwsP/ZWZTBg/VA90Z /F33RnasQ0MIZCWaNEQt+bIpLmVRkreXYymYjFjEz2IBU3NwMd2EB3G+UZ1357lb vwYlQhqadb/nlh+EMNWXInogjFfnC5Vt3488RSTd+6u5mKl2SXit+zt+VcRsbNX1 GhZQy8TVZWR9PI1anegdt8PGYPmJguR62HeGuU8iMwN3w7NwKpnT9XvxT22ks4We c57pzUHHpT3bu/2XLXFiqDpgvqcrwauBaUgsdAKEHFLs4CRFuLpSxGZiq2rmqYcf wkcZHfgoX3dtj+4atd+8cVOwZT5cjwyLPho5t75kz/vhKdVtdwpBYsvnteXUhFNT 4/h8j1oBDrTZRR0yWaIP3S9BMk4fU96luejJoHOLPF9Yy8feQn44p8xcJAOlJ3eN HEaqfAQSzFzHDCQjPh8bJFO4dMw9pF56GlVpgJyySCXAQuJIb5BtRvh7hFBRWpLJ FfyhyUQ9OmKyAFTWLFvV1X/i6LaMzzYIWvmHSw2neDBp/IquLmA2pIaUxiU+wyVT gyGcTmoXD26pwRVh7Kxu =21xU -----END PGP SIGNATURE----- --nextPart4435302.pBAeTAOonX--