/[gentoo-x86]/eclass/eutils.eclass
Gentoo

Diff of /eclass/eutils.eclass

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.372 Revision 1.373
1# Copyright 1999-2011 Gentoo Foundation 1# Copyright 1999-2011 Gentoo Foundation
2# Distributed under the terms of the GNU General Public License v2 2# Distributed under the terms of the GNU General Public License v2
3# $Header: /var/cvsroot/gentoo-x86/eclass/eutils.eclass,v 1.372 2011/12/14 17:36:18 vapier Exp $ 3# $Header: /var/cvsroot/gentoo-x86/eclass/eutils.eclass,v 1.373 2011/12/16 23:38:41 vapier Exp $
4 4
5# @ECLASS: eutils.eclass 5# @ECLASS: eutils.eclass
6# @MAINTAINER: 6# @MAINTAINER:
7# base-system@gentoo.org 7# base-system@gentoo.org
8# @BLURB: many extra (but common) functions that are used in ebuilds 8# @BLURB: many extra (but common) functions that are used in ebuilds
96# Remove .svn directories recursiveley. Useful when a source tarball contains 96# Remove .svn directories recursiveley. Useful when a source tarball contains
97# internal Subversion directories. Defaults to $PWD. 97# internal Subversion directories. Defaults to $PWD.
98esvn_clean() { 98esvn_clean() {
99 [[ -z $* ]] && set -- . 99 [[ -z $* ]] && set -- .
100 find "$@" -type d -name '.svn' -prune -print0 | xargs -0 rm -rf 100 find "$@" -type d -name '.svn' -prune -print0 | xargs -0 rm -rf
101}
102
103# @FUNCTION: estack_push
104# @USAGE: <stack> [items to push]
105# @DESCRIPTION:
106# Push any number of items onto the specified stack. Pick a name that
107# is a valid variable (i.e. stick to alphanumerics), and push as many
108# items as you like onto the stack at once.
109#
110# The following code snippet will echo 5, then 4, then 3, then ...
111# @CODE
112# estack_push mystack 1 2 3 4 5
113# while estack_pop mystack i ; do
114# echo "${i}"
115# done
116# @CODE
117estack_push() {
118 [[ $# -eq 0 ]] && die "estack_push: incorrect # of arguments"
119 local stack_name="__ESTACK_$1__" ; shift
120 eval ${stack_name}+=\( \"\$@\" \)
121}
122
123# @FUNCTION: estack_pop
124# @USAGE: <stack> [variable]
125# @DESCRIPTION:
126# Pop a single item off the specified stack. If a variable is specified,
127# the popped item is stored there. If no more items are available, return
128# 1, else return 0. See estack_push for more info.
129estack_pop() {
130 [[ $# -eq 0 || $# -gt 2 ]] && die "estack_pop: incorrect # of arguments"
131
132 # We use the fugly __estack_xxx var names to avoid collision with
133 # passing back the return value. If we used "local i" and the
134 # caller ran `estack_pop ... i`, we'd end up setting the local
135 # copy of "i" rather than the caller's copy. The __estack_xxx
136 # garbage is preferable to using $1/$2 everywhere as that is a
137 # bit harder to read.
138 local __estack_name="__ESTACK_$1__" ; shift
139 local __estack_retvar=$1 ; shift
140 eval local __estack_i=\${#${__estack_name}[@]}
141 # Don't warn -- let the caller interpret this as a failure
142 # or as normal behavior (akin to `shift`)
143 [[ $(( --__estack_i )) -eq -1 ]] && return 1
144
145 if [[ -n ${__estack_retvar} ]] ; then
146 eval ${__estack_retvar}=\"\${${__estack_name}[${__estack_i}]}\"
147 fi
148 eval unset ${__estack_name}[${__estack_i}]
101} 149}
102 150
103# @FUNCTION: eshopts_push 151# @FUNCTION: eshopts_push
104# @USAGE: [options to `set` or `shopt`] 152# @USAGE: [options to `set` or `shopt`]
105# @DESCRIPTION: 153# @DESCRIPTION:
124# eshopts_pop 172# eshopts_pop
125# @CODE 173# @CODE
126eshopts_push() { 174eshopts_push() {
127 # have to assume __ESHOPTS_SAVE__ isn't screwed with 175 # have to assume __ESHOPTS_SAVE__ isn't screwed with
128 # as a `declare -a` here will reset its value 176 # as a `declare -a` here will reset its value
129 local i=${#__ESHOPTS_SAVE__[@]}
130 if [[ $1 == -[su] ]] ; then 177 if [[ $1 == -[su] ]] ; then
131 __ESHOPTS_SAVE__[$i]=$(shopt -p) 178 estack_push eshopts "$(shopt -p)"
132 [[ $# -eq 0 ]] && return 0 179 [[ $# -eq 0 ]] && return 0
133 shopt "$@" || die "eshopts_push: bad options to shopt: $*" 180 shopt "$@" || die "${FUNCNAME}: bad options to shopt: $*"
134 else 181 else
135 __ESHOPTS_SAVE__[$i]=$- 182 estack_push eshopts $-
136 [[ $# -eq 0 ]] && return 0 183 [[ $# -eq 0 ]] && return 0
137 set "$@" || die "eshopts_push: bad options to set: $*" 184 set "$@" || die "${FUNCNAME}: bad options to set: $*"
138 fi 185 fi
139} 186}
140 187
141# @FUNCTION: eshopts_pop 188# @FUNCTION: eshopts_pop
142# @USAGE: 189# @USAGE:
143# @DESCRIPTION: 190# @DESCRIPTION:
144# Restore the shell options to the state saved with the corresponding 191# Restore the shell options to the state saved with the corresponding
145# eshopts_push call. See that function for more details. 192# eshopts_push call. See that function for more details.
146eshopts_pop() { 193eshopts_pop() {
147 [[ $# -ne 0 ]] && die "eshopts_pop takes no arguments" 194 local s
148 local i=$(( ${#__ESHOPTS_SAVE__[@]} - 1 )) 195 estack_pop eshopts s || die "${FUNCNAME}: unbalanced push"
149 [[ ${i} -eq -1 ]] && die "eshopts_{push,pop}: unbalanced pair"
150 local s=${__ESHOPTS_SAVE__[$i]}
151 unset __ESHOPTS_SAVE__[$i]
152 if [[ ${s} == "shopt -"* ]] ; then 196 if [[ ${s} == "shopt -"* ]] ; then
153 eval "${s}" || die "eshopts_pop: sanity: invalid shopt options: ${s}" 197 eval "${s}" || die "${FUNCNAME}: sanity: invalid shopt options: ${s}"
154 else 198 else
155 set +$- || die "eshopts_pop: sanity: invalid shell settings: $-" 199 set +$- || die "${FUNCNAME}: sanity: invalid shell settings: $-"
156 set -${s} || die "eshopts_pop: sanity: unable to restore saved shell settings: ${s}" 200 set -${s} || die "${FUNCNAME}: sanity: unable to restore saved shell settings: ${s}"
157 fi 201 fi
202}
203
204# @FUNCTION: eumask_push
205# @USAGE: <new umask>
206# @DESCRIPTION:
207# Set the umask to the new value specified while saving the previous
208# value onto a stack. Useful for temporarily changing the umask.
209eumask_push() {
210 estack_push eumask "$(umask)"
211 umask "$@" || die "${FUNCNAME}: bad options to umask: $*"
212}
213
214# @FUNCTION: eumask_pop
215# @USAGE:
216# @DESCRIPTION:
217# Restore the previous umask state.
218eumask_pop() {
219 local s
220 estack_pop eumask s || die "${FUNCNAME}: unbalanced push"
221 umask ${s} || die "${FUNCNAME}: sanity: could not restore umask: ${s}"
158} 222}
159 223
160# @VARIABLE: EPATCH_SOURCE 224# @VARIABLE: EPATCH_SOURCE
161# @DESCRIPTION: 225# @DESCRIPTION:
162# Default directory to search for patches. 226# Default directory to search for patches.

Legend:
Removed from v.1.372  
changed lines
  Added in v.1.373

  ViewVC Help
Powered by ViewVC 1.1.20