]> git.madduck.net Git - code/mbuild.git/blob - sbuild-wrapper.sh

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:

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