#!/bin/bash # $Id: git-pbuilder,v 1.33 2014/08/28 21:39:15 eagle Exp $ # # git-pbuilder -- Wrapper around pbuilder for git-buildpackage # # Note that this script requires bash, not a POSIX shell, because it uses bash # arrays to handle GIT_PBUILDER_OPTIONS. It's otherwise quite difficult to # get the contents of that environment variable to undergo the correct amount # of shell expansion. # # Written by Russ Allbery # Based on the example in the git-buildpackage documentation # Copyright 2014 Russ Allbery # Copyright 2008, 2009, 2010, 2011, 2012, 2013 # The Board of Trustees of the Leland Stanford Junior University # # Permission to use, copy, modify, and distribute this software and its # documentation for any purpose and without fee is hereby granted, provided # that the above copyright notice appear in all copies and that both that # copyright notice and this permission notice appear in supporting # documentation, and that the name of Stanford University not be used in # advertising or publicity pertaining to distribution of the software without # specific, written prior permission. Stanford University makes no # representations about the suitability of this software for any purpose. It # is provided "as is" without express or implied warranty. # # THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED # WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. set -e # Helper function to quote an argument so that it's protected from the shell. # This is used when passing arguments through in --debbuildopts, since they'll # undergo another round of shell expansion. shell_quote () { echo "$1" | sed -e "s/'/'\"'\"'/g" -e "1 s/^/'/" -e "\$ s/\$/'/" } # Set default BUILDER, DIST, and ARCH based on the name we were invoked as. # This allows people to create symlinks like git-pbuilder-squeeze and # git-qemubuilder-squeeze-amd64 pointing to git-pbuilder and auto-detecting # the builder, distribution, and architecture from that. default_BUILDER=${0#*git-} default_DIST=${default_BUILDER#*-} default_BUILDER=${default_BUILDER%%-*} case $default_BUILDER in pbuilder|cowbuilder) default_BUILDER=cowbuilder ;; /*) default_BUILDER=cowbuilder ;; esac case $default_BUILDER in *builder) ;; *) default_BUILDER=cowbuilder ;; esac case $default_DIST in *builder) default_DIST= ;; *-*) default_ARCH=${default_DIST#*-} default_DIST=${default_DIST%%-*} ;; esac : ${default_BUILDER:=cowbuilder} # Set BUILDER, DIST, and ARCH, allowing existing settings to override. : ${BUILDER:=$default_BUILDER} : ${DIST:=$default_DIST} : ${ARCH:=$default_ARCH} # If DIST ends in -backports, strip that out of DIST and add it to EXT. if expr "$DIST" : '.*-backports$' >/dev/null; then DIST=${DIST%-backports} EXT="-backports" # The URL to the Debian backports repository to add to the chroot # configuration when created via this script for a distribution ending in # -backports. Backports was incorporated into the main mirrors as of # wheezy. case $DIST in squeeze|oldstable) BACKPORTS='http://backports.debian.org/debian-backports' ;; *) BACKPORTS='http://ftp.debian.org/debian' ;; esac else EXT= fi # Make sure we have the necessary tools. if [ ! -x /usr/sbin/"$BUILDER" ]; then echo "$BUILDER not found; you need to install the $BUILDER package" >&2 exit 1 fi # Default options come from the environment. Use eval to parse # GIT_PBUILDER_OPTIONS into an array since some arguments may have quoting. eval "OPTIONS=( $GIT_PBUILDER_OPTIONS )" OUTPUT_DIR="${GIT_PBUILDER_OUTPUT_DIR:-../}" # How we handle options depends on what type of builder we're using. Ignore # options if $GIT_PBUILDER_AUTOCONF is set to no. if [ no != "$GIT_PBUILDER_AUTOCONF" ] ; then case $BUILDER in pbuilder) # The root directory where different pbuilder --basepath # directories are found. git-pbuilder expects them to be named # base-.tgz. : ${PBUILDER_BASE:=/var/cache/pbuilder} # If DIST is set, use base-$DIST.tgz. If DIST is not set, the sid # chroot may be either base.tgz or base-sid.tgz. Try both. If # ARCH is set, use base-$DIST-$ARCH.tgz. : ${DIST:=sid} if [ -n "$ARCH" ] ; then BASE="$PBUILDER_BASE/base-$DIST$EXT-$ARCH.tgz" OPTIONS+=( --architecture "$ARCH" ) elif [ "$DIST" = 'sid' ] ; then if [ -f "$PBUILDER_BASE/base-sid.tgz" ] ; then BASE="$PBUILDER_BASE/base-sid.tgz" else BASE="$PBUILDER_BASE/base.tgz" fi else BASE="$PBUILDER_BASE/base-$DIST$EXT.tgz" fi OPTIONS+=( --basetgz "$BASE" ) # Make sure the base tarball exists. if [ ! -f "$BASE" ] && [ "$1" != "create" ]; then echo "Base tarball $BASE does not exist" >&2 exit 1 fi # Set --debian-etch-workaround if DIST is etch. Assume that # everything else is new enough that it will be fine. if [ "$DIST" = 'etch' ] || [ "$DIST" = 'ebo' ] ; then OPTIONS+=( --debian-etch-workaround ) fi ;; cowbuilder) # The root directory where different cowbuilder --basepath # directories are found. git-pbuilder expects them to be named # base-.cow. : ${COWBUILDER_BASE:=/var/cache/pbuilder} # If DIST is set, use base-$DIST.cow. If DIST is not set, the sid # chroot may be either base.cow or base-sid.cow. Try both. If # ARCH is set, use base-$DIST-$ARCH.cow. : ${DIST:=sid} if [ -n "$ARCH" ] ; then BASE="$COWBUILDER_BASE/base-$DIST$EXT-$ARCH.cow" OPTIONS+=( --architecture "$ARCH" ) elif [ "$DIST" = 'sid' ] ; then if [ -d "$COWBUILDER_BASE/base-sid.cow" ] ; then BASE="$COWBUILDER_BASE/base-sid.cow" else BASE="$COWBUILDER_BASE/base.cow" fi else BASE="$COWBUILDER_BASE/base-$DIST$EXT.cow" fi OPTIONS+=( --basepath "$BASE" ) # Make sure the base directory exists. if [ ! -d "$BASE" ] && [ "$1" != "create" ]; then echo "Base directory $BASE does not exist" >&2 exit 1 fi # Set --debian-etch-workaround if DIST is etch. Assume that # everything else is new enough that it will be fine. if [ "$DIST" = 'etch' ] || [ "$DIST" = 'ebo' ] ; then OPTIONS+=( --debian-etch-workaround ) fi ;; qemubuilder) # There always has to be an architecture for qemubuilder, and it # doesn't make much sense to default to the current architecture. # There's probably no good default, but this one at least makes # some sense. : ${DIST:=sid} : ${ARCH:=armel} # There has to be a configuration file matching our distribution # and architecture. QEMUCONFIG="/var/cache/pbuilder/$BUILDER-$ARCH-$DIST$EXT.conf" if [ ! -r "$CONFIG" ]; then echo "Cannot read configuration file $QEMUCONFIG" >&2 exit 1 fi OPTIONS+=( --config "$QEMUCONFIG" ) ;; *) echo "Unknown builder $BUILDER" >&2 exit 1 ;; esac fi # If the first argument to the script is update, create, or login, run the # builder with the corresponding option under sudo rather than proceeding. case $1 in update|create|login) action="$1" shift # Since we're running the builder under sudo, $HOME will change to root's # home directory and the user's .pbuilderrc won't be run. sudo -E would # fix this, but that requires special configuration in sudoers to allow # it. Instead, check if the user has a .pbuilderrc and, if so, explicitly # add it as a configuration file. if [ -f "$HOME/.pbuilderrc" ] ; then OPTIONS+=( --configfile "$HOME/.pbuilderrc" ) fi # Run the builder. if [ no = "$GIT_PBUILDER_AUTOCONF" ] ; then sudo "$BUILDER" --"$action" "${OPTIONS[@]}" "$@" else if [ "$EXT" = '-backports' ] ; then OTHERMIRROR="deb $BACKPORTS $DIST$EXT main" sudo "$BUILDER" --"$action" --distribution "$DIST" \ --othermirror "$OTHERMIRROR" "${OPTIONS[@]}" "$@" else sudo "$BUILDER" --"$action" --distribution "$DIST" "${OPTIONS[@]}" "$@" fi fi exit $? ;; *) if [ -z "$GBP_BUILD_DIR" ]; then echo "Warning: git-pbuilder should be run via git-buildpackage" >&2 fi ;; esac # Print out some information about what we're doing. building="Building with $BUILDER" if [ no = "$GIT_PBUILDER_AUTOCONF" ] ; then echo "$building" elif [ -n "$ARCH" ] ; then echo "$building for distribution $DIST$EXT, architecture $ARCH" else echo "$building for distribution $DIST$EXT" fi # Source package format 1.0 doesn't automatically exclude Git files, so we # want to add the appropriate flags to do that. But source package format 3.0 # does exclude by default and has many other ways of controlling those # exclusions that we don't want to tromp on. So we don't want to give any -i # or -I flags unless we're using source format 1.0. if [ ! -f debian/source/format ] || grep -qs '^1.0' debian/source/format ; then echo 'Source format 1.0 detected, adding exclude flags' DEBBUILDOPTS="-i'(?:^|/)\\.git(attributes)?(?:\$|/.*\$)' -I.git" else DEBBUILDOPTS='' fi # Add all of the additional arguments we got on the command line, but quote # them from the shell since they'll undergo another round of shell expansion # when the pbuilder runs debbuild. source_only=false for arg in "$@" ; do if [ x'-S' = x"$arg" ] ; then source_only=true fi DEBBUILDOPTS+=" $(shell_quote "$arg")" done # Now we can finally run pdebuild. The quoting here is tricky, but this # seems to pass everything through properly. if [ no = "$GIT_PBUILDER_AUTOCONF" ] ; then pdebuild --pbuilder "$BUILDER" --debbuildopts "$DEBBUILDOPTS" \ -- "${OPTIONS[@]}" else pdebuild --buildresult "$OUTPUT_DIR" --pbuilder "$BUILDER" \ --debbuildopts "$DEBBUILDOPTS" -- "${OPTIONS[@]}" fi status="$?" if [ -n "`ls ../*_source.changes`" ] && [ true != "$source_only" ] ; then rm ../*_source.changes fi exit "$status" # Documentation. Use a hack to hide this from the shell. Because of the # above exit line, this should never be executed. DOCS=<<__END_OF_DOCS__ =head1 NAME git-pbuilder - Wrapper around cowbuilder/qemubuilder for git-buildpackage =head1 SYNOPSIS DIST=I ARCH=I [BUILDER=(pbuilder|qemubuilder)] \ B I DIST=I ARCH=I [BUILDER=(pbuilder|qemubuilder)] \ B (update | create | login) I =head1 DESCRIPTION B is a wrapper around B intended for use by B. It configures B to use B by default, passes appropriate options to B, and sets the base path for B based on the environment variable DIST and, if set, the environment variable ARCH. B can be selected instead by setting the environment variable BUILDER to C, and B can be selected by setting BUILDER to C. By default, B assumes the target distribution is C, the same architecture as the B default, and uses F if it exists. If it doesn't, F is tried. If DIST is set, its value is the target distribution and F.cow> is used instead. If DIST is C or C, B<--debian-etch-workaround> is also passed to B. If ARCH is set, its value is the target architecture and F-I.cow> is used, with I being set to C if DIST was not set. If B is used as the builder, no base directory is used. Instead, B is invoked with the B<--config> option pointing to the file F-I.conf> If B is used as the builder, B instead looks for F by default and F if it doesn't exist. If DIST or ARCH are set, they are used to form the expected name of the tgz file in the same way as they're used to form the expected base directory for B. Similar to B, B<--debian-etch-workaround> is passed to B if from the DIST setting it looks like the target distribution is etch. If B is invoked via a name that starts with C, the part between the hyphens is taken to be the default name of the builder to use. However, C is mapped to B for backward compatibility; if you want to use B, you have to explicitly set BUILDER. The part after the last hyphen is taken to be the default distribution (if it contains no additional hyphen) or the default distribution followed by the default architecture (if it contains a hyphen). One can therefore create symlinks like C pointing to B and use that name when wanting to use a distribution of C, or C to use B to build for the C architecture and the C distribution. Explicit settings of BUILDER, DIST, or ARCH always override any guesses from the command name. Any arguments are passed as-is to B via the B<--debbuildopts> option to B. To pass arguments to the builder instead, put them in the environment variable GIT_PBUILDER_OPTIONS. To disable all attempts to discover the base path, tarball, or configuration file and set up the pbuilder options and instead rely on the settings in .pbuilderrc, set GIT_PBUILDER_AUTOCONF to C. Normally, one does not run this script directly. Instead, it's used as the builder script for B. To configure B to use it, add a stanza like: [DEFAULT] builder = /path/to/git-pbuilder in your F file (which can be F<.gbp.conf> in your home directory or at the top level of the repository, or F in the F<.git> directory). DIST and ARCH are read as an environment variable so that you can set it before running B without having to worry about passing parameters through B. Alternately, B may be called with an argument of C, C, or C. In this case, it calls B (or the configured builder as described above) using B and passes the corresponding command to the builder, using the same logic as above to determine the base directory and distribution. If the distribution (set in DIST) ends in C<-backports>, one of the following will be added as an B<--othermirror> parameter to the builder: deb http://ftp.debian.org/debian $DIST main deb http://backports.debian.org/debian-backports $DIST main The first will be used for most distributions, and the second for C or C. Any additional arguments to B are passed along to the builder. Due to how B works, invoking the builder with an action will not read the user's F<.pbuilderrc> by default, so in this case B will add an explicit B<--configfile> option pointing to the user's F<.pbuilderrc> if it exists. =head1 ENVIRONMENT =over 4 =item ARCH Sets the target architecture. For a B builder, this sets both the base path and is passed as the B<--architecture> option. With B, this controls the path to the configuration file. With B, this sets the tgz path and is passed as B<--architecture>. =item BUILDER Sets the builder to use. The only supported settings are C (the default), C, and C. =item COWBUILDER_BASE Set this environment variable to change the default location for the cowbuilder base directories (F). =item DIST Sets the target distribution. This is used primarily to determine the base path for B or B or the configuration file path for B, but it's also used to determine whether to pass B<--debian-etch-workaround> to B or B. =item GIT_PBUILDER_AUTOCONF If set to C, disable the logic that constructs the base path, tarball, or configuration file and all other logic to determine the options to pass to the builder. Instead, just run the configured builder and assume its configuration is handled elsewhere (such as in F<.pbuilderrc>). This also suppresses setting B<--buildresult>, so the user will need to ensure that the configuration still puts packages where B expects them. =item GIT_PBUILDER_OPTIONS Add additional options for the builder. These options are passed as-is to B, B, or B via B. The contents of this variable will undergo shell expansion, so any arguments containing shell metacharacters or whitespace need to be quoted in the value of the environment variable. =item GIT_PBUILDER_OUTPUT_DIR Where to put the result of the build. The default is C<..> (the parent directory). This setting is ignored if GIT_PBUILDER_AUTOCONF is set to C. =item PBUILDER_BASE Set this environment variable to change the defualt location for the pbuilder tgz files (F) when BUILDER is set to C. =back =head1 FILES =over 4 =item /var/cache/pbuilder/base-sid.cow =item /var/cache/pbuilder/base.cow The default C directories, searched for in that order, if neither DIST nor ARCH is set. =item /var/cache/pbuilder/base-sid-$ARCH.cow The C directory used if ARCH is set and DIST is not set. =item /var/cache/pbuilder/base-$DIST.cow The C directory used if DIST is set and ARCH is not. =item /var/cache/pbuilder/base-$DIST-$ARCH.cow The C directory used if DIST and ARCH are both set. =item /var/cache/pbuilder/base-sid.tgz =item /var/cache/pbuilder/base.tgz =item /var/cache/pbuilder/base-sid-$ARCH.tgz =item /var/cache/pbuilder/base-$DIST.tgz =item /var/cache/pbuilder/base-$DIST-$ARCH.tgz Similar to the above, the C path used for various settings of DIST and ARCH if BUILDER is set to C. =item /var/cache/pbuilder/qemubuilder-$ARCH-$DIST.conf The C file used. $ARCH defaults to C and $DIST defaults to C if not set. =back =head1 SEE ALSO cowbuilder(8), dpkg-buildpackage(1), git-buildpackage(1), pbuilder(8), pdebuild(1), qemubuilder(8), sudo(8) The latest version of this script is available from L. =head1 AUTHOR Russ Allbery =cut __END_OF_DOCS__