madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

c410dcb18b66f9074bd01c38a471468f4cd4fd8d
[code/mbuild.git] / sbuild-wrapper.sh
1 #!/bin/sh
2 #
3 #  - automate builds with sbuild and dinstall the result
4 #
5 # Copyright (c) 2008 martin f. krafft <madduck@debian.org>
6 # Released under the terms of the Artistic Licence 2.0.
7 #
8 set -eu
9
10 ME="${0##*/}"
11
12 BUILDDIR="$(mktemp -dt $ME.XXXXXXXX)"
13 trap "cd /tmp && rm -r '$BUILDDIR'" 0
14
15 BUNDLE_DIR="${TMPDIR:-/tmp}"
16 TARGET_DIR="$PWD"
17 copy_back_files() {
18   echo "Copying files destined for $1 back to $TARGET_DIR..."
19   dcmd cp -v "$2" "$TARGET_DIR"
20 }
21 dinstall() {
22   copy_back_files "$@"
23 }
24 print_bundle_location() {
25   echo "Bundle available at $@ ."
26 }
27
28 KEYID=
29
30 MBUILDRCS="/etc/mbuild/rc $HOME/.mbuildrc $HOME/.mbuild/rc"
31 for rc in $MBUILDRCS; do
32   [ -r "$rc" ] && . "$rc"
33 done
34
35 if [ -z "$KEYID" ]; then
36   echo "E: \$KEYID is not defined in rc file." >&2
37   exit 1
38 fi
39
40 DEB_BUILD_ARCH="$(dpkg-architecture -qDEB_BUILD_ARCH)"
41
42 about()
43 {
44   echo "$ME -- wrapper for sbuild"
45   echo "Copyright © martin f. krafft <madduck@debian.org>"
46   echo "Released under the terms of the Artistic Licence 2.0"
47 }
48
49 usage()
50 {
51   about
52   echo
53   echo "Usage: $ME [options] [sbuild_options] file_source.changes"
54   echo
55   echo "Valid options are:"
56   cat <<-_eof | column -s\& -t
57         --dist & specify the target distribution (default: from changes file)
58         --arch & specify the target architecture (default: $DEB_BUILD_ARCH)
59         -sa & pass --force-orig-source to sbuild
60         -B & do a binary-only build
61         -h|--help & show this output.
62         -V|--version & show version information.
63         _eof
64 }
65
66 binonly=0
67 sbuild_args=
68 schangesfile=
69 arch=
70 for opt in "$@"; do
71   case "$opt" in
72     -h|--help) usage; exit 0;;
73     -V|--version) about; exit 0;;
74     --arch=*) arch="${opt#--arch=}";;
75     -D) sbuild_args="${sbuild_args:+$sbuild_args }$opt";;
76     -sa) sbuild_args="${sbuild_args:+$sbuild_args }--force-orig-source";;
77     -B) binonly=1;;
78     --*) sbuild_args="${sbuild_args:+$sbuild_args }$opt";;
79     *_source.changes)
80       if [ -z "$schangesfile" ]; then
81         if [ -f "$opt" ] && [ -r "$opt" ]; then
82           schangesfile="$opt"
83         else
84           echo "E: file does not exist: $opt" >&2
85           exit 1
86         fi
87       else
88         echo "E: cannot pass more than one source changes file: $opt" >&2
89         exit 1
90       fi
91       ;;
92     *)
93       echo "E: unknown option: $opt" >&2
94       exit 1
95       ;;
96   esac
97 done
98
99 if [ -z "$schangesfile" ]; then
100   usage
101   exit 1
102 fi
103
104 [ -z "$arch" ] && arch="$DEB_BUILD_ARCH"
105
106 dist="$(sed -ne 's,Distribution: ,,p' "$schangesfile")"
107
108 schangesfile_basename="${schangesfile##*/}"
109 packagename="${schangesfile_basename%%_*}"
110 packagenameversion="${schangesfile_basename%_*}"
111 debversion="${packagenameversion#*_}"
112 upstream_version="${debversion%-*}"
113 dscfile_basename="$packagenameversion".dsc
114
115 case "$schangesfile" in
116   */*) cd "${schangesfile%/*}";;
117   *) :;;
118 esac
119 dcmd cp "${schangesfile##*/}" "$BUILDDIR"
120 schangesfile="$schangesfile_basename"
121
122 cd "$BUILDDIR"
123
124 dcmd "$dscfile_basename" | while read f; do
125   test -f "$f" && continue
126
127   case "$f" in
128     *.tar.gz|*.tar.bz2|*.tar.lzma|*.tar.xz)
129       last_version="$(apt-cache madison $packagename | sed -rne "s,.+(\<${upstream_version}[^[:space:]]+).*,\1,p")"
130       echo "I: fetching missing tarball for $upstream_version (from $last_version)…" >&2
131       apt-get --tar-only --only-source source "${packagename}=${last_version}"
132       ;;
133     *)
134       echo "E: I do not know how to obtain the file: $f" >&2
135       exit 1
136       ;;
137   esac
138 done
139
140 [ "$binonly" = 0 ] && sbuild_args="${sbuild_args:+$sbuild_args }--source --arch-all"
141
142 sbuild $sbuild_args --arch="$arch" --dist="$dist" --keyid=$KEYID \
143   "$dscfile_basename"
144
145 changesfile="${packagenameversion}_${arch}.changes"
146
147 #mergechanges "$schangesfile" "$changesfile" > temp-combined.changes
148 #mv temp-combined.changes "$changesfile"
149 #changestool "$changesfile" updatechecksums
150
151 #sign_changes_file "$changesfile"
152
153 dinstall "$dist" "$changesfile"
154
155 DATESTR="$(date +%Y.%m.%d.%H%M%S)"
156 BUILD_PREFIX="${packagenameversion}_${arch}.${DATESTR}"
157
158 mv current "${BUILD_PREFIX}".buildlog
159 rm -f current-*
160
161 bundle="${BUNDLE_DIR}/${BUILD_PREFIX}".bundle.tar
162 tar -chf "$bundle" *
163 chmod 644 "$bundle"
164
165 rm -r "$BUILDDIR"
166 trap - 0
167
168 print_bundle_location "$bundle"
169
170 exit 0