-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebutant
executable file
·514 lines (424 loc) · 13.2 KB
/
debutant
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
#!/bin/bash
#
# debutant - Debian packaging framework for binary blobs
# Copyright (C) 2015 - 2022 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH
# Author: Christopher Huhn <c.huhn@gsi.de>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
###################
#
# helper function definitions for recipes:
#
abort() {
echo "$*" >&2
exit 1
}
# Add a dependency to debian/control
# -b : Add a build-dependency
add_dependency() {
local depends="Depends"
if [ "$1" = "-b" ]; then
depends="Build-Depends"
shift
fi
for PKG in "$@"; do
if grep -q "^${depends}: .*" "$PKG_DIR/debian/control"; then
grep -q "^${depends}: .*\<$PKG\>" "$PKG_DIR/debian/control" \
|| sed -i -e "s/^\(${depends}: .*\)/\1, $PKG/" "$PKG_DIR/debian/control"
else
echo "${depends}: $PKG" >> "$PKG_DIR/debian/control"
fi
done
}
# install an initscript the debian way
# recommendation for custom init scripts:
# place in $RECIPE_DIR/debian/(init|default) and use add_debian_files
add_initscript() {
local init=$1
[ "$init" ] || init="$RECIPE_DIR/debian/init"
cp "$init" "$PKG_DIR/debian/init"
# also copy over the default file if found:
if [ -e "$(dirname $init)/default" ]; then
cp "$(dirname $init)/default" "$PKG_DIR/debian/default"
fi
# 'modern' initscripts make use of lsb functions:
#add_dependency "lsb-base (>= 3.2-14)" # versioned depends not supported by add_dependency yet
}
## DEPRECATED: use add_debian_files and $RECIPE_DIR/debian instead
add_install_scripts() {
for file in preinst postinst prerm postrm; do
[ -e "$RECIPE_DIR/$file" ] || continue
cp "$RECIPE_DIR/$file" "$PKG_DIR/debian/"
chmod a+rx "$PKG_DIR/debian/$file"
done
}
#
# load prepared packaging files from $RECIPE_DIR/debian
#
add_debian_files() {
if [ -z "$PACKAGE" ]; then
if [ "$1" ]; then
# read package name from the command line
PACKAGE=$1
elif [ -r "$PKG_DIR/debian/control" ]; then
# we already have a debian/control in place (e.g. from alien)
# and read the package name from there:
PACKAGE=$(sed -ne 's/^Source: //p' "$PKG_DIR/debian/control")
fi
fi
if [ "$PACKAGE" -a -d "$RECIPE_DIR/$PACKAGE.debian" ]; then
cp -br "$RECIPE_DIR/$PACKAGE.debian/." "$PKG_DIR/debian"
elif [ -d "$RECIPE_DIR/debian" ]; then
cp -br "$RECIPE_DIR/debian/." "$PKG_DIR/debian"
fi
}
#
# there are some lintian warnings we cannot do anything about
#
add_lintian_overrides() {
local package=$1
cat <<EOF >> "$PKG_DIR/debian/$package.lintian-overrides"
${package} binary: binary-or-shlib-defines-rpath
${package} binary: embedded-library
${package} binary: hardening-no-relro
${package} binary: shlib-with-non-pic-code
# we override dh_strip as it might break stuff:
${package} binary: unstripped-binary-or-object
EOF
}
# sometimes the debian architecture has to be set explicitly
# e.g. if we want to build i386 packages on amd64
build_architecture() {
DEB_HOST_ARCH=$1
[ "$DEB_HOST_ARCH" ] || DEB_HOST_ARCH=`dpkg-architecture -qDEB_BUILD_ARCH`
export DEB_HOST_ARCH
debian_control Architecture $DEB_HOST_ARCH
}
# add a build dependency to debian/control
build_depends() {
for PKG in "$@"; do
add_dependency -b "$PKG"
# TODO: install it if required ?!
done
}
# build the package
# adds a revision (default 1) unless --keep-revision is given
build_package() {
(
local current_version
cd "$PKG_DIR"
current_version=$(head -1 debian/changelog |
sed -e 's/.*(\([[:alnum:]:_+.~-]\+\)).*/\1/')
if [ "$1" != "--keep-revision" ]; then
if [ -z "$VERSION" ]; then
new_version=$current_version
else
new_version=$VERSION
fi
if [[ $new_version =~ '-' ]]; then
# change the existing revision suffix
new_version=${new_version//-*/-$REVISION}
else
# add a revision suffix
new_version=${new_version}-${REVISION}
fi
debchange='debchange'
[ "$PACKAGE" ] && debchange="$debchange --package $PACKAGE"
$debchange -v "$new_version" -b \
-D "$(lsb_release -cs)" \
--force-distribution \
"Build by debutant"
fi
if [ "$PBUILDER" ]; then
pdebuild --pbuilder cowbuilder
else
debuild -b -us -uc -a${DEB_HOST_ARCH}
fi
)
}
#
# test ths SHA256 sum of a file
# OK: rc 0
# Not OK or error: rc 1
check_sha256sum() {
sha256sum -c <<<"$2 $1"
}
cleanup() {
[ -d $BUILD_DIR ] && rm -r $BUILD_DIR
}
#
# create initial packaging files with dh_make
#
# TODO: add option to send -p packagename_version to dh_make
#
debut_dh_make() {
( cd ${PKG_DIR}; yes | dh_make -y -s --native "$@")
# modify rules file to override dh_strip
debian_rules
add_lintian_overrides
# remove README file dummies:
rm ${PKG_DIR}/debian/README*
cat <<EOF > ${PKG_DIR}/debian/README.Debian
This package was generated with debutant.
EOF
}
# change an option in debian/control
debian_control() {
local key=$1
local value="$2"
sed -i -e "s|^\($key:\) .*|\1 $value|" ${PKG_DIR}/debian/control
}
# install a generic debian/rules
debian_rules() {
[ -e "${PKG_DIR}/debian/rules" ] &&
cp "${PKG_DIR}/debian/rules" "${PKG_DIR}/debian/rules.orig"
cat > "${PKG_DIR}/debian/rules" <<EOF
#!/usr/bin/make -f
%:
EOF
# emacs tends to turn tabs into spaces, we'll clarify this:
/bin/echo -e "\tdh \$@" >> "${PKG_DIR}/debian/rules"
# stripping the provided binaries may or may not be a good idea
# and breaks building for foreign architectures
echo "override_dh_strip:" >> "${PKG_DIR}/debian/rules"
chmod a+rx "${PKG_DIR}/debian/rules"
}
# TODO: Not implemented yet: add an override to a dh-style debian/rules file
# (as created by debian_rules (see above) or dh_make
debian_rules_add_override() {
:
}
# add something to debian/install
debian_install() {
local pkg
if [ "$1" == "-p" ]; then
pkg=$2
shift 2
elif [ "$PACKAGE" ]; then
# read the package name from the global envar,
# but only if debian/$PACKAGE.install exists
# or debian/install does not exist
if [ -e "$PKG_DIR/debian/$PACKAGE.install" ] ||
[ ! -e "$PKG_DIR/debian/install" ]; then
pkg=$PACKAGE
fi
fi
echo "$@" >> "$PKG_DIR/debian/${pkg:+$pkg.}install"
}
# add something to debian/docs
debian_installdocs() {
echo "$@" >> "$PKG_DIR/debian/docs"
}
# add something to debian/examples
debian_installexamples() {
echo "$@" >> "$PKG_DIR/debian/examples"
}
# move pkg install scripts out of the way
# TODO: Add them to the pkg documentation
disable_install_scripts() {
SCRIPT_DIR=debutant/original_install_scripts
mkdir -vp "$PKG_DIR/$SCRIPT_DIR"
for script in preinst postinst prerm postrm; do
[ -r "${PKG_DIR}/debian/$script" ] \
&& mv -v "${PKG_DIR}/debian/$script" "$PKG_DIR/$SCRIPT_DIR"
done
debian_installdocs $SCRIPT_DIR/
}
# copy over the created debs:
drop_result() {
if [ "$1" ]; then
cp -v "$@" .
else
find "$BUILD_DIR" -maxdepth 1 \
-\( -name \*.deb -o -name \*.changes -o -name \*.build -\) \
-exec cp -v {} . \;
fi
}
# detect the archive type of the given archive and extract it
extract() {
local blob="$1"
local dir=$2
local mime
if [ ! -r "$blob" ]; then
[ -r "$BUILD_DIR/$blob" ] && blob=$BUILD_DIR/$blob
fi
[ "$dir" ] || dir=$BUILD_DIR
[ -d "$dir" ] || mkdir -p "$dir"
mime=$(mime_type "$blob")
case $mime in
application/gzip|application/x-gzip)
case $blob in
# TAR file:
*.tar.gz|*.tgz)
tar xfz "$blob" -C "$dir"
;;
# something else:
*)
gunzip -cd > "$dir/$(basename "$blob") .gz"
;;
esac
;;
application/x-tar)
tar xf "$blob" -C "$dir"
;;
application/zip)
unzip -d $dir $blob
;;
application/x-dosexec)
cabextract -d $dir $blob
;;
application/x-rpm)
(
archive=`readlink -f "$blob"`
cd $dir
fakeroot alien -dsg --scripts --verbose "$archive"
# TODO: replace alien comment in package description
# with debutant comment
# sed -i -e 's/(Converted from a rpm package by alien .*/Package created by debutant .../' */debian/control
)
;;
# Debian package: extract with dpkg-deb; metadata goes to DEBIAN/
application/x-debian-package|application/vnd.debian.binary-package|application/octet-stream)
# on Squeeze debs are application/octet-stream
if expr $blob : '.*\.deb' > /dev/null; then
dpkg-deb -x $blob $dir # normal package content
mkdir -p $dir/DEBIAN
dpkg-deb -e $blob $dir/DEBIAN # debian metadata
else
abort "Unknown archive type $mine for $blob: $mime"
fi
;;
*)
abort "Unknown archive type $mine for $blob: $mime"
;;
esac
}
fetch() {
local url=$1
# guess the filename from the Location: header of the HTTP response
# (to prevent downloaded files being called 'download' aso.
curl --silent --show-error --location --remote-name --remote-header-name \
-\# $url -w "%{filename_effective}"
}
#
# list all available recipes:
#
list_recipes() {
# TODO: Add a recipe description to the output
echo "Available recipes:"
for D in $RECIPE_DIRS; do
[ -d $D ] || continue
ls $D/*/recipe | xargs -n1 dirname | xargs -n1 basename | sort |
sed -e "s/^/ /"
done
}
# detect the mime type of a file
mime_type() {
file --brief --mime-type "$1"
}
prepare() {
# some preparations:
ME=$(whoami)
[ "$DEBEMAIL" ] || DEBEMAIL="$ME@$(hostname -f)"
[ "$DEBFULLNAME" ] ||
DEBFULLNAME=$(getent passwd $ME | cut -f 5 -d : | cut -f 1 -d,)
BUILD_DIR=$(mktemp -d -p .)
PKG_DIR="${BUILD_DIR}/${RECIPE_NAME}"
PACKAGE=$RECIPE_NAME
DEBIAN_VERSION="$(lsb_release -is | tr [:upper:] [:lower:])$(lsb_release -rs)"
}
usage() {
cat <<EOF
Usage: $(basename $0) [opts] <recipe>
Avalable options:
-d: debug output
-l: list available recipes
-p: use pbuilder for packaging
-r <revision>: Debian package revision (default: 1)
EOF
list_recipes
}
#export these functions to subshells (ie. our recipes):
export abort add_dependency cleanup debian_control debian_rules \
disable_install_scripts extract fetch prepare
#########
#
# minimal recipe invocation logic
#
# search path for recipes:
# ... /usr/local/lib/debutnt/recipes??, /var/lib/debutant/recipes???
RECIPE_DIRS="$(dirname $0)/recipes /usr/lib/debutant/recipes"
REVISION=1
while : ; do
case $1 in
-d|--debug)
DEBUG=1
;;
-h|--help)
usage
exit 0
;;
-l|--list)
list_recipes
exit 0
;;
-r|--revision)
REVISION=$2
shift
;;
-p|--pbuilder)
PBUILDER=1
;;
*)
break
;;
esac
shift
done
RECIPE_NAME=$1
shift
if [ -z "$RECIPE_NAME" ]; then
list_recipes
exit
fi
FOUND=
for D in $RECIPE_DIRS; do
[ -d "$D" ] || continue
if [ -e "$D/$RECIPE_NAME/recipe" ]; then
FOUND=1
set -e
[ $DEBUG ] && set -x
RECIPE_DIR="$(dirname $0)/recipes/$RECIPE_NAME"
# preparations are performed here, not inside recipes
prepare
# now we have a BUILD_DIR
# stage 1: extract and convert the archive
. "$RECIPE_DIR/recipe" "$@"
# now we have a PKG_DIR and put debian/... into it:
#dh_make ... #preseeded with recipe config?
#cp -b $RECIPE_DIR/debian/* $PKG_DIR/debian/
# stage 2: customize debian files:
#. $RECIPE_DIR/recipe package $@
# stage 3: we build the package and clean up
if [ $DEBUG ]; then
set +x
else
# cleanup:
[ -d "$BUILD_DIR" ] && rm -rf "$BUILD_DIR"
fi
break
fi
done
[ "$FOUND" ] || abort "No recipe found for '$RECIPE_NAME'"