From patchwork Wed Apr 5 23:06:10 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luke Shumaker X-Patchwork-Id: 97 Return-Path: Delivered-To: patchwork@archlinux.org Received: from nymeria.archlinux.org by nymeria.archlinux.org (Dovecot) with LMTP id aZhzInp45VjtMgAAtiB/HQ for ; Thu, 06 Apr 2017 01:06:34 +0200 Received: from nymeria.archlinux.org (localhost.localdomain [127.0.0.1]) by nymeria.archlinux.org (Postfix) with ESMTP id 89A004065F; Thu, 6 Apr 2017 01:06:31 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on nymeria.archlinux.org X-Spam-Level: X-Spam-Status: No, score=-4.2 required=2.5 tests=BAYES_00,RCVD_IN_DNSWL_MED shortcircuit=no autolearn=unavailable autolearn_force=no version=3.4.1 Received: from luna.archlinux.org (luna.archlinux.org [IPv6:2a01:4f8:160:3033::2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by nymeria.archlinux.org (Postfix) with ESMTPS; Thu, 6 Apr 2017 01:06:31 +0200 (CEST) Received: from luna.archlinux.org (luna.archlinux.org [127.0.0.1]) by luna.archlinux.org (Postfix) with ESMTP id 4DD0B2C09A; Wed, 5 Apr 2017 23:06:31 +0000 (UTC) Authentication-Results: luna.archlinux.org; dkim=none Received: from luna.archlinux.org (luna.archlinux.org [127.0.0.1]) by luna.archlinux.org (Postfix) with ESMTP id 838C52C096 for ; Wed, 5 Apr 2017 23:06:27 +0000 (UTC) Received: from mav.lukeshu.com (mav.lukeshu.com [104.207.138.63]) by luna.archlinux.org (Postfix) with ESMTPS for ; Wed, 5 Apr 2017 23:06:27 +0000 (UTC) Received: from build64-par (unknown [IPv6:2601:803:202:9275:21f:e2ff:fe4d:191b]) by mav.lukeshu.com (Postfix) with ESMTPSA id 44E5883EB8; Wed, 5 Apr 2017 19:06:26 -0400 (EDT) From: lukeshu@lukeshu.com To: arch-projects@lists.archlinux.org Date: Wed, 5 Apr 2017 19:06:10 -0400 Message-Id: <20170405230610.17372-2-lukeshu@lukeshu.com> X-Mailer: git-send-email 2.12.1 In-Reply-To: <20170405230610.17372-1-lukeshu@lukeshu.com> References: <20170405224251.GB612@rampage> <20170405230610.17372-1-lukeshu@lukeshu.com> Subject: [arch-projects] [devtools] [PATCH] makechrootpkg: Avoid having code floating around outside of a function. X-BeenThere: arch-projects@archlinux.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Arch Linux projects development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Arch Linux projects development discussion Cc: Luke Shumaker Errors-To: arch-projects-bounces@archlinux.org Sender: "arch-projects" X-UID: 250 Status: X-Keywords: Content-Length: 7617 From: Luke Shumaker This means wrapping variable initialization in init_variables(), and the main program routine in main(). I did NOT put `shopt -s nullglob` in to a function. It make make sense to move init_variables() down into the main() function, instead of having it as a separate function up top (if this done, then the `-g` flag passed to `declare` in init_variables() can be dropped). However, in interest of keeping the `diff -w` small, and merges/rebases simpler, this isn't done here. --- makechrootpkg.in | 252 ++++++++++++++++++++++++++++--------------------------- 1 file changed, 130 insertions(+), 122 deletions(-) diff --git a/makechrootpkg.in b/makechrootpkg.in index f6764cb..a221f7c 100644 --- a/makechrootpkg.in +++ b/makechrootpkg.in @@ -15,26 +15,28 @@ m4_include(lib/archroot.sh) shopt -s nullglob -default_makepkg_args=(-s --noconfirm -L --holdver) -makepkg_args=("${default_makepkg_args[@]}") -repack=false -update_first=false -clean_first=false -run_namcap=false -temp_chroot=false -chrootdir= -passeddir= -makepkg_user= -declare -a install_pkgs -declare -i ret=0 - -bindmounts_ro=() -bindmounts_rw=() - -copy=$USER -[[ -n ${SUDO_USER:-} ]] && copy=$SUDO_USER -[[ -z "$copy" || $copy = root ]] && copy=copy -src_owner=${SUDO_USER:-$USER} +init_variables() { + default_makepkg_args=(-s --noconfirm -L --holdver) + makepkg_args=("${default_makepkg_args[@]}") + repack=false + update_first=false + clean_first=false + run_namcap=false + temp_chroot=false + chrootdir= + passeddir= + makepkg_user= + declare -ga install_pkgs + declare -gi ret=0 + + bindmounts_ro=() + bindmounts_rw=() + + copy=$USER + [[ -n ${SUDO_USER:-} ]] && copy=$SUDO_USER + [[ -z "$copy" || $copy = root ]] && copy=copy + src_owner=${SUDO_USER:-$USER} +} usage() { echo "Usage: ${0##*/} [options] -r [--] [makepkg args]" @@ -308,109 +310,115 @@ move_products() { } # }}} -while getopts 'hcur:I:l:nTD:d:U:' arg; do - case "$arg" in - c) clean_first=true ;; - D) bindmounts_ro+=(--bind-ro="$OPTARG") ;; - d) bindmounts_rw+=(--bind="$OPTARG") ;; - u) update_first=true ;; - r) passeddir="$OPTARG" ;; - I) install_pkgs+=("$OPTARG") ;; - l) copy="$OPTARG" ;; - n) run_namcap=true; makepkg_args+=(-i) ;; - T) temp_chroot=true; copy+="-$$" ;; - U) makepkg_user="$OPTARG" ;; - h|*) usage ;; - esac -done - -[[ ! -f PKGBUILD && -z "${install_pkgs[*]}" ]] && die 'This must be run in a directory containing a PKGBUILD.' -[[ -n $makepkg_user && -z $(id -u "$makepkg_user") ]] && die 'Invalid makepkg user.' - -check_root - -# Canonicalize chrootdir, getting rid of trailing / -chrootdir=$(readlink -e "$passeddir") -[[ ! -d $chrootdir ]] && die "No chroot dir defined, or invalid path '%s'" "$passeddir" -[[ ! -d $chrootdir/root ]] && die "Missing chroot dir root directory. Try using: mkarchroot %s/root base-devel" "$chrootdir" - -if [[ ${copy:0:1} = / ]]; then - copydir=$copy -else - copydir="$chrootdir/$copy" -fi - -# Pass all arguments after -- right to makepkg -makepkg_args+=("${@:$OPTIND}") - -# See if -R was passed to makepkg -for arg in "${@:OPTIND}"; do - case ${arg%%=*} in - -*R*|--repackage) - repack=true - break 2 - ;; - esac -done - -if [[ -n $SUDO_USER ]]; then - eval "USER_HOME=~$SUDO_USER" -else - USER_HOME=$HOME -fi - -umask 0022 - -load_vars "${XDG_CONFIG_HOME:-$USER_HOME/.config}/pacman/makepkg.conf" || load_vars "$USER_HOME/.makepkg.conf" -load_vars /etc/makepkg.conf - -# Use PKGBUILD directory if these don't exist -[[ -d $PKGDEST ]] || PKGDEST=$PWD -[[ -d $SRCDEST ]] || SRCDEST=$PWD -[[ -d $SRCPKGDEST ]] || SRCPKGDEST=$PWD -[[ -d $LOGDEST ]] || LOGDEST=$PWD - -# Lock the chroot we want to use. We'll keep this lock until we exit. -lock 9 "$copydir.lock" "Locking chroot copy [%s]" "$copy" - -if [[ ! -d $copydir ]] || $clean_first; then - sync_chroot "$chrootdir" "$copy" -fi - -$update_first && arch-nspawn "$copydir" \ +main() { + init_variables + + while getopts 'hcur:I:l:nTD:d:U:' arg; do + case "$arg" in + c) clean_first=true ;; + D) bindmounts_ro+=(--bind-ro="$OPTARG") ;; + d) bindmounts_rw+=(--bind="$OPTARG") ;; + u) update_first=true ;; + r) passeddir="$OPTARG" ;; + I) install_pkgs+=("$OPTARG") ;; + l) copy="$OPTARG" ;; + n) run_namcap=true; makepkg_args+=(-i) ;; + T) temp_chroot=true; copy+="-$$" ;; + U) makepkg_user="$OPTARG" ;; + h|*) usage ;; + esac + done + + [[ ! -f PKGBUILD && -z "${install_pkgs[*]}" ]] && die 'This must be run in a directory containing a PKGBUILD.' + [[ -n $makepkg_user && -z $(id -u "$makepkg_user") ]] && die 'Invalid makepkg user.' + + check_root + + # Canonicalize chrootdir, getting rid of trailing / + chrootdir=$(readlink -e "$passeddir") + [[ ! -d $chrootdir ]] && die "No chroot dir defined, or invalid path '%s'" "$passeddir" + [[ ! -d $chrootdir/root ]] && die "Missing chroot dir root directory. Try using: mkarchroot %s/root base-devel" "$chrootdir" + + if [[ ${copy:0:1} = / ]]; then + copydir=$copy + else + copydir="$chrootdir/$copy" + fi + + # Pass all arguments after -- right to makepkg + makepkg_args+=("${@:$OPTIND}") + + # See if -R was passed to makepkg + for arg in "${@:OPTIND}"; do + case ${arg%%=*} in + -*R*|--repackage) + repack=true + break 2 + ;; + esac + done + + if [[ -n $SUDO_USER ]]; then + eval "USER_HOME=~$SUDO_USER" + else + USER_HOME=$HOME + fi + + umask 0022 + + load_vars "${XDG_CONFIG_HOME:-$USER_HOME/.config}/pacman/makepkg.conf" || load_vars "$USER_HOME/.makepkg.conf" + load_vars /etc/makepkg.conf + + # Use PKGBUILD directory if these don't exist + [[ -d $PKGDEST ]] || PKGDEST=$PWD + [[ -d $SRCDEST ]] || SRCDEST=$PWD + [[ -d $SRCPKGDEST ]] || SRCPKGDEST=$PWD + [[ -d $LOGDEST ]] || LOGDEST=$PWD + + # Lock the chroot we want to use. We'll keep this lock until we exit. + lock 9 "$copydir.lock" "Locking chroot copy [%s]" "$copy" + + if [[ ! -d $copydir ]] || $clean_first; then + sync_chroot "$chrootdir" "$copy" + fi + + $update_first && arch-nspawn "$copydir" \ + "${bindmounts_ro[@]}" "${bindmounts_rw[@]}" \ + pacman -Syu --noconfirm + + if [[ -n ${install_pkgs[*]:-} ]]; then + install_packages "$copydir" "${install_pkgs[@]}" + ret=$? + # If there is no PKGBUILD we have done + [[ -f PKGBUILD ]] || return $ret + fi + + download_sources "$copydir" "$src_owner" + + prepare_chroot "$copydir" "$USER_HOME" "$repack" + + if arch-nspawn "$copydir" \ + --bind="$PWD:/startdir" \ + --bind="$SRCDEST:/srcdest" \ "${bindmounts_ro[@]}" "${bindmounts_rw[@]}" \ - pacman -Syu --noconfirm + /chrootbuild "${makepkg_args[@]}" + then + move_products "$copydir" "$src_owner" + else + (( ret += 1 )) + fi -if [[ -n ${install_pkgs[*]:-} ]]; then - install_packages "$copydir" "${install_pkgs[@]}" - ret=$? - # If there is no PKGBUILD we have done - [[ -f PKGBUILD ]] || return $ret -fi - -download_sources "$copydir" "$src_owner" - -prepare_chroot "$copydir" "$USER_HOME" "$repack" - -if arch-nspawn "$copydir" \ - --bind="$PWD:/startdir" \ - --bind="$SRCDEST:/srcdest" \ - "${bindmounts_ro[@]}" "${bindmounts_rw[@]}" \ - /chrootbuild "${makepkg_args[@]}" -then - move_products "$copydir" "$src_owner" -else - (( ret += 1 )) -fi - -$temp_chroot && delete_chroot "$copydir" "$copy" - -if (( ret != 0 )); then - if $temp_chroot; then - die "Build failed" + $temp_chroot && delete_chroot "$copydir" "$copy" + + if (( ret != 0 )); then + if $temp_chroot; then + die "Build failed" + else + die "Build failed, check %s/build" "$copydir" + fi else - die "Build failed, check %s/build" "$copydir" + true fi -else - true -fi +} + +main "$@"