forked from projectbuendia/buendia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mkdeb
executable file
·111 lines (99 loc) · 3.89 KB
/
mkdeb
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
#!/bin/bash
# Copyright 2015 The Project Buendia Authors
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy
# of the License at: http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software distrib-
# uted under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
# OR CONDITIONS OF ANY KIND, either express or implied. See the License for
# specific language governing permissions and limitations under the License.
set -e
output="$1"; shift
control="$1"; shift
if [ "$1" = "" ]; then
cat <<EOF
Usage: $0 <output.deb> <control-dir> <data-dir> [<data-dir> ...]
Builds a Debian package at the specified output path, given the files to
install arranged under the given <data-dir> and the control files in
<control-dir>. If more than one <data-dir> is specified, all files under
them are included; you must ensure that their contents do not collide.
If <control-dir> contains any files named *.template, they are rendered
(e.g. foo.template renders to produce foo) by performing bash-style parameter
expansion, command substitution, and arithmetic expansion. These are
all the shell expansions triggered by the "\$" character: \$VAR and \${VAR}
expand to environment variables, \$(command) expands to the output of a
shell command, and \$((expression)) evaluates an arithmetic expression.
EOF
exit 1
fi
tmp=/tmp/make_package.$$
trap 'rm -rf $tmp' EXIT
rm -rf $tmp
mkdir -p $tmp
TAR=$(which gtar || which tar) # we prefer GNU tar for the --owner flag
# If --owner and --group are supported, use them.
touch $tmp/dummy.txt
if $TAR cvf $tmp/dummy.tar --owner=0 --group=0 $tmp/dummy.txt 2>/dev/null; then
function append_to_tar_with_uid_0() {
dest="$1"
input_root="$2"
$TAR rvf "$dest" --owner=0 --group=0 -C "$input_root" .
}
else
# If --format=mtree is supported, use that to set owners and groups.
if $TAR cf - --format=mtree $tmp | grep -q '/dummy.txt.*type=file'; then
function append_to_tar_with_uid_0() {
dest="$1"
input_root="$2"
$TAR cf - --format=mtree -C "$input_root" . | \
sed -e 's/ .* \(type=[a-z]*\).*/ uid=0 gid=0 \1/' | \
$TAR rvf "$dest" -C "$input_root" @-
}
else
echo "$TAR doesn't support setting file owners or groups."
echo "Please install GNU tar."
exit 1
fi
fi
# Copy the control files into a temporary directory.
mkdir -p $tmp/control
cp -pr $control/* $tmp/control
# Apply bash expansion to any *.template files.
for i in $tmp/control/*.template; do
if [ -f $i ]; then
result=${i%.template}
cp $i $result # copy permissions
cat <(echo 'cat <<EOF') $i <(echo 'EOF') | bash > $result
# Copy the timestamps from the original file, so that the resulting
# data.tar remains exactly identical if none of the input files have
# changed. This lets us use diffdeb to detect meaningful changes.
touch -r $i $result
rm $i
fi
done
# Set each directory's timestamp to the timestamp of the newest file within,
# giving deterministic timetsamps so that the resulting data.tar remains
# exactly identical if none of the input files have changed.
(
unset CLICOLOR
unset CLICOLOR_FORCE
unset LSCOLORS
unset LS_COLORS
for dir in $(find $tmp -type d | sort -r); do
cd $dir
touch -r "$(command ls -1t | head -1)" .
done
)
# Pack up the Debian package.
append_to_tar_with_uid_0 $tmp/control.tar $tmp/control
while [ -n "$1" ]; do
append_to_tar_with_uid_0 $tmp/data.tar "$1"
shift
done
gzip $tmp/control.tar $tmp/data.tar
echo 2.0 > $tmp/debian-binary
mkdir -p $(dirname "$output")
ar -r $output $tmp/debian-binary $tmp/control.tar.gz $tmp/data.tar.gz
echo "$output OK ($(echo $(wc -c < "$output")) bytes)"