From 880ae81d3c485a54c744cb75f3faa4adf8cea802 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Wed, 15 Feb 2017 15:27:51 -0500 Subject: makechrootpkg: Avoid having code floating around outside of a function. 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. --- makechrootpkg.in | 238 ++++++++++++++++++++++++++++--------------------------- 1 file changed, 123 insertions(+), 115 deletions(-) diff --git a/makechrootpkg.in b/makechrootpkg.in index afd14f6..7ff2fd7 100644 --- a/makechrootpkg.in +++ b/makechrootpkg.in @@ -14,26 +14,28 @@ m4_include(lib/common.sh) shopt -s nullglob -default_makepkg_args=(-s --noconfirm -L --holdver) -makepkg_args=("${default_makepkg_args[@]}") -repack=false -update_first=false -clean_first=false -install_pkg= -run_namcap=false -temp_chroot=false -chrootdir= -passeddir= -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 + install_pkg= + run_namcap=false + temp_chroot=false + chrootdir= + passeddir= + 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} +} usage() { echo "Usage: ${0##*/} [options] -r [--] [makepkg args]" @@ -457,114 +459,120 @@ move_products() { } # }}} -orig_argv=("$@") - -while getopts 'hcur:I:l:nTD:d:' 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+="-$$" ;; - h|*) usage ;; - esac -done +main() { + init_variables + + orig_argv=("$@") + + while getopts 'hcur:I:l:nTD:d:' 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+="-$$" ;; + h|*) usage ;; + esac + done -[[ ! -f PKGBUILD && -z "${install_pkgs[*]}" ]] && die 'This must be run in a directory containing a PKGBUILD.' + [[ ! -f PKGBUILD && -z "${install_pkgs[*]}" ]] && die 'This must be run in a directory containing a PKGBUILD.' -check_root "$0" "${orig_argv[@]}" + check_root "$0" "${orig_argv[@]}" -# 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" + # 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 + if [[ ${copy:0:1} = / ]]; then + copydir=$copy + else + copydir="$chrootdir/$copy" + fi -# Pass all arguments after -- right to makepkg -makepkg_args+=("${@:$OPTIND}") + # 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 -# 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 -if [[ -n $SUDO_USER ]]; then - eval "USER_HOME=~$SUDO_USER" -else - USER_HOME=$HOME -fi + umask 0022 -umask 0022 + load_vars "$USER_HOME/.makepkg.conf" + load_vars /etc/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 -# 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" -# 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 [[ ! -d $copydir ]] || $clean_first; then - sync_chroot "$chrootdir" "$copy" -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 -$update_first && arch-nspawn "$copydir" \ + download_sources "$copydir" "$src_owner" + + prepare_chroot "$copydir" "$USER_HOME" "$repack" + + if arch-nspawn "$copydir" \ + --bind-ro="$PWD:/startdir_host" \ + --bind-ro="$SRCDEST:/srcdest_host" \ + "${bindmounts_ro[@]}" "${bindmounts_rw[@]}" \ + /chrootprepare "${makepkg_args[@]}" && + arch-nspawn "$copydir" \ + --bind-ro="$PWD:/startdir_host" \ + --bind-ro="$SRCDEST:/srcdest_host" \ "${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-ro="$PWD:/startdir_host" \ - --bind-ro="$SRCDEST:/srcdest_host" \ - "${bindmounts_ro[@]}" "${bindmounts_rw[@]}" \ - /chrootprepare "${makepkg_args[@]}" && - arch-nspawn "$copydir" \ - --bind-ro="$PWD:/startdir_host" \ - --bind-ro="$SRCDEST:/srcdest_host" \ - "${bindmounts_ro[@]}" "${bindmounts_rw[@]}" \ - /chrootbuild "${makepkg_args[@]}" -then - move_products "$copydir" "$src_owner" -else - (( ret += 1 )) -fi - -$temp_chroot && delete_chroot "$copydir" - -if (( ret != 0 )); then - if $temp_chroot; then - die "Build failed" + /chrootbuild "${makepkg_args[@]}" + then + move_products "$copydir" "$src_owner" else - die "Build failed, check %s/build" "$copydir" + (( ret += 1 )) fi -else - true -fi + + $temp_chroot && delete_chroot "$copydir" + + if (( ret != 0 )); then + if $temp_chroot; then + die "Build failed" + else + die "Build failed, check %s/build" "$copydir" + fi + else + true + fi +} + +main "$@" -- cgit v1.2.3-70-g09d2