From patchwork Wed Apr 5 19:35:58 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luke Shumaker X-Patchwork-Id: 90 Return-Path: Delivered-To: patchwork@archlinux.org Received: from nymeria.archlinux.org by nymeria.archlinux.org (Dovecot) with LMTP id efTBFzpH5ViTagAAtiB/HQ for ; Wed, 05 Apr 2017 21:36:26 +0200 Received: from nymeria.archlinux.org (localhost.localdomain [127.0.0.1]) by nymeria.archlinux.org (Postfix) with ESMTP id CF00E406FB; Wed, 5 Apr 2017 21:36:24 +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=ham 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; Wed, 5 Apr 2017 21:36:24 +0200 (CEST) Received: from luna.archlinux.org (luna.archlinux.org [127.0.0.1]) by luna.archlinux.org (Postfix) with ESMTP id 06C9F2C0F5; Wed, 5 Apr 2017 19:36:24 +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 408FA2C0EA for ; Wed, 5 Apr 2017 19:36:22 +0000 (UTC) Received: from mav.lukeshu.com (team4272.com [IPv6:2001:19f0:5c00:8069:5400:ff:fe26:6a86]) by luna.archlinux.org (Postfix) with ESMTPS for ; Wed, 5 Apr 2017 19:36:22 +0000 (UTC) Received: from build64-par (unknown [IPv6:2601:803:202:9275:21f:e2ff:fe4d:191b]) by mav.lukeshu.com (Postfix) with ESMTPSA id E3F1C83EB8; Wed, 5 Apr 2017 15:36:20 -0400 (EDT) From: lukeshu@lukeshu.com To: arch-projects@lists.archlinux.org Date: Wed, 5 Apr 2017 15:35:58 -0400 Message-Id: <20170405193603.22277-3-lukeshu@lukeshu.com> X-Mailer: git-send-email 2.12.1 In-Reply-To: <20170405193603.22277-1-lukeshu@lukeshu.com> References: <20170405193603.22277-1-lukeshu@lukeshu.com> Subject: [arch-projects] [devtools] [PATCH 2/7] makechrootpkg: Have functions be more function-y. 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: 237 Status: X-Keywords: Content-Length: 5527 From: Luke Shumaker Rather than them simply being named blocks of code with braces around them. That is: have them take things via arguments rather than global variables. Specific notes: - create_chroot->sync_chroot: I pulled out locking the destination chroot; getting that lock is now the caller's responsibility. It still handles locking the source chroot though. I pulled the `if [[ ! -d $copydir ]] || $clean_first;` check out; it is now the caller's responsibility to use that check when deciding if to call sync_chroot. However, when pulling that check out, I left it as `if true;`, to keep an indentation level. This patch has had to be rebased/merged many times, and changing the indentation is a sure way to make that go less smoothly; I'm not going to re-indent this block until I see the check removed in the git.archlinux.org/devtools.git repository. - install_packages: 1. Receive the list of packages as arguments, rather than a global variable. 2. Make the caller responsible for looking at PKGBUILD. From the name and arguments, one would never expect it to look at PKGBUILD. --- makechrootpkg.in | 88 +++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 75 insertions(+), 13 deletions(-) diff --git a/makechrootpkg.in b/makechrootpkg.in index 5329f76..bdcaa88 100644 --- a/makechrootpkg.in +++ b/makechrootpkg.in @@ -76,6 +76,14 @@ usage() { } # {{{ functions +# Usage: load_vars $makepkg_conf +# Globals: +# - SRCDEST +# - SRCPKGDEST +# - PKGDEST +# - LOGDEST +# - MAKEFLAGS +# - PACKAGER load_vars() { local makepkg_conf="$1" var @@ -88,11 +96,23 @@ load_vars() { return 0 } -create_chroot() { - # Lock the chroot we want to use. We'll keep this lock until we exit. - lock 9 "$copydir.lock" "Locking chroot copy [%s]" "$copy" +# Usage: sync_chroot $CHROOTDIR/$CHROOT <$CHROOTCOPY|$copydir> +sync_chroot() { + local chrootdir=$1 + local copy=$2 + local copydir='' + if [[ ${copy:0:1} = / ]]; then + copydir=$copy + else + copydir="$chrootdir/$copy" + fi + + if [[ "$chrootdir/root" -ef "$copydir" ]]; then + error 'Cannot sync copy with itself: %s' "$copydir" + return 1 + fi - if [[ ! -d $copydir ]] || $clean_first; then + if true; then # indent for rebasing/merging purposes # Get a read lock on the root chroot to make # sure we don't clone a half-updated chroot slock 8 "$chrootdir/root.lock" "Locking clean chroot" @@ -117,7 +137,11 @@ create_chroot() { touch "$copydir" } -clean_temporary() { +# Usage: delete_chroot $copydir [$copy] +delete_chroot() { + local copydir=$1 + local copy=${1:-$2} + stat_busy "Removing temporary copy [%s]" "$copy" if is_btrfs "$chrootdir" && ! mountpoint -q "$copydir"; then btrfs subvolume delete "$copydir" >/dev/null || @@ -133,7 +157,11 @@ clean_temporary() { stat_done } +# Usage: install_packages $copydir $pkgs... install_packages() { + local copydir=$1 + local install_pkgs=("${@:2}") + local -a pkgnames local ret @@ -145,11 +173,19 @@ install_packages() { ret=$? rm -- "${pkgnames[@]/#/$copydir/root/}" - # If there is no PKGBUILD we are done - [[ -f PKGBUILD ]] || exit $ret + return $ret } +# Usage: prepare_chroot $copydir $HOME $repack $run_namcap +# Globals: +# - MAKEFLAGS +# - PACKAGER prepare_chroot() { + local copydir=$1 + local USER_HOME=$2 + local repack=$3 + local run_namcap=$4 + $repack || rm -rf "$copydir/build" local builduser_uid="${SUDO_UID:-$UID}" @@ -216,7 +252,14 @@ _chrootnamcap() { done } +# Usage: download_sources $copydir $src_owner +# Globals: +# - SRCDEST +# - USER download_sources() { + local copydir=$1 + local src_owner=$2 + local builddir="$(mktemp -d)" chmod 1777 "$builddir" @@ -235,7 +278,15 @@ download_sources() { rm -rf $builddir } +# Usage: move_products $copydir $owner +# Globals: +# - PKGDEST +# - LOGDEST move_products() { + local copydir=$1 + local src_owner=$2 + + local pkgfile for pkgfile in "$copydir"/pkgdest/*; do chown "$src_owner" "$pkgfile" mv "$pkgfile" "$PKGDEST" @@ -246,6 +297,7 @@ move_products() { fi done + local l for l in "$copydir"/logdest/*; do [[ $l == */logpipe.* ]] && continue chown "$src_owner" "$l" @@ -321,17 +373,27 @@ load_vars /etc/makepkg.conf [[ -d $SRCPKGDEST ]] || SRCPKGDEST=$PWD [[ -d $LOGDEST ]] || LOGDEST=$PWD -create_chroot +# 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 -[[ -n ${install_pkgs[*]} ]] && install_packages +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 +download_sources "$copydir" "$src_owner" -prepare_chroot +prepare_chroot "$copydir" "$USER_HOME" "$repack" if arch-nspawn "$copydir" \ --bind="$PWD:/startdir" \ @@ -339,12 +401,12 @@ if arch-nspawn "$copydir" \ "${bindmounts_ro[@]}" "${bindmounts_rw[@]}" \ /chrootbuild then - move_products + move_products "$copydir" "$src_owner" else (( ret += 1 )) fi -$temp_chroot && clean_temporary +$temp_chroot && delete_chroot "$copydir" "$copy" if (( ret != 0 )); then if $temp_chroot; then