summaryrefslogtreecommitdiffstats
path: root/scripts/dcmd.sh
blob: 927baf82491a50bba8132fc867855fe1de4ddc4a (plain)
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
#!/bin/sh
#
# dcmd: expand file lists of .dsc/.changes files in the command line
#
# Copyright (C) 2008 Romain Francoise <rfrancoise@debian.org>
# Copyright (C) 2008 Christoph Berg <myon@debian.org>
# Copyright (C) 2008 Adam D. Barratt <adsb@debian.org>
#
# 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 2 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, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA

# Usage:
#
# dcmd replaces any reference to a .dsc or .changes file in the command
# line with the list of files in its 'Files' section, plus the
# .dsc/.changes file itself.
#
# $ dcmd sha1sum rcs_5.7-23_amd64.changes
# f61254e2b61e483c0de2fc163321399bbbeb43f1  rcs_5.7-23.dsc
# 7a2b283b4c505d8272a756b230486a9232376771  rcs_5.7-23.diff.gz
# e3bac970a57a6b0b41c28c615f2919c931a6cb68  rcs_5.7-23_amd64.deb
# c531310b18773d943249cfaa8b539a9b6e14b8f4  rcs_5.7-23_amd64.changes
# $

PROGNAME=${0##*/}

version() {
    echo \
"This is $PROGNAME, from the Debian devscripts package, version ###VERSION###
This code is copyright 2008 by Romain Francoise, all rights reserved.
This program comes with ABSOLUTELY NO WARRANTY.
You are free to redistribute this code under the terms of the
GNU General Public License, version 2 or later."
}

usage() {
    printf "Usage: %s [options] [command] [dsc or changes file] [...]\n" $PROGNAME
}

endswith() {
    case $1 in
	*$2) return 0 ;;
	*) return 1;;
    esac
}

# Instead of parsing the file completely as the previous Python
# implementation did (using python-debian), let's just select lines
# that look like they might be part of the file list.
RE="^ [0-9a-f]{32} [0-9]+ ((([a-zA-Z0-9_.-]+/)?[a-zA-Z0-9_.-]+|-) ([a-zA-Z]+|-) )?(.*)$"

maybe_expand() {
    local dir
    local sedre
    if [ -e "$1" ] && (endswith "$1" .changes || endswith "$1" .dsc || endswith "$1" .buildinfo); then
	# Need to escape whatever separator is being used in sed expression so
	# it doesn't prematurely end the s command
	dir=$(dirname "$1" | sed 's/,/\\,/g')
	if [ "$(echo "$1" | cut -b1-2)" != "./" ]; then
	    sedre="\."
	fi
	sed --regexp-extended -n "s,$RE,$dir/\5,p" <"$1" | sed "s,^$sedre/,,"
    fi
}

DSC=1; BCHANGES=1; SCHANGES=1; ARCHDEB=1; INDEPDEB=1; TARBALL=1; DIFF=1
CHANGES=1; DEB=1; ARCHUDEB=1; INDEPUDEB=1; UDEB=1; BUILDINFO=1;
FILTERED=0; FAIL_MISSING=1
EXTRACT_PACKAGE_NAME=0
SORT=0
TAC=0

while [ $# -gt 0 ]; do
    TYPE=""
    case "$1" in
	--version|-v) version; exit 0;;
	--help|-h) usage; exit 0;;
	--no-fail-on-missing|-r) FAIL_MISSING=0;;
	--fail-on-missing) FAIL_MISSING=1;;
	--package|-p) EXTRACT_PACKAGE_NAME=1;;
	--sort|-s) SORT=1;;
	--tac|-t) TAC=1;;
	--) shift; break;;
	--no-*)
	    TYPE=${1#--no-}
	    case "$FILTERED" in
		1)  echo "$PROGNAME: Can't combine --foo and --no-foo options" >&2;
		    exit 1;;
		0)  FILTERED=-1;;
	    esac;;
	--**)
	    TYPE=${1#--}
	    case "$FILTERED" in
		-1) echo "$PROGNAME: Can't combine --foo and --no-foo options" >&2;
		    exit 1;;
		0)  FILTERED=1; DSC=0; BCHANGES=0; SCHANGES=0; CHANGES=0
		    ARCHDEB=0; INDEPDEB=0; DEB=0; ARCHUDEB=0; INDEPUDEB=0
		    UDEB=0; TARBALL=0; DIFF=0; BUILDINFO=0;;
	    esac;;
	*) break;;
    esac

    case "$TYPE" in
	"") ;;
	dsc) [ "$FILTERED" = "1" ] && DSC=1 || DSC=0;;
	buildinfo) [ "$FILTERED" = "1" ] && BUILDINFO=1 || BUILDINFO=0;;
	changes) [ "$FILTERED" = "1" ] &&
	    { BCHANGES=1; SCHANGES=1; CHANGES=1; } ||
	    { BCHANGES=0; SCHANGES=0; CHANGES=0; } ;;
	bchanges) [ "$FILTERED" = "1" ] && BCHANGES=1 || BCHANGES=0;;
	schanges) [ "$FILTERED" = "1" ] && SCHANGES=1 || SCHANGES=1;;
	deb) [ "$FILTERED" = "1" ] &&
	    { ARCHDEB=1; INDEPDEB=1; DEB=1; } ||
	    { ARCHDEB=0; INDEPDEB=0; DEB=0; };;
	archdeb) [ "$FILTERED" = "1" ] && ARCHDEB=1 || ARCHDEB=0;;
	indepdeb) [ "$FILTERED" = "1" ] && INDEPDEB=1 || INDEPDEB=0;;
	udeb) [ "$FILTERED" = "1" ] &&
	    { ARCHUDEB=1; INDEPUDEB=1; UDEB=1; } ||
	    { ARCHUDEB=0; INDEPUDEB=0; UDEB=0; };;
	archudeb) [ "$FILTERED" = "1" ] && ARCHUDEB=1 || ARCHUDEB=0;;
	indepudeb) [ "$FILTERED" = "1" ] && INDEPUDEB=1 || INDEPUDEB=0;;
	tar|orig) [ "$FILTERED" = "1" ] && TARBALL=1 || TARBALL=0;;
	diff|debtar) [ "$FILTERED" = "1" ] && DIFF=1 || DIFF=0;;
	*) echo "$PROGNAME: Unknown option '$1'" >&2; exit 1;;
    esac
    shift
done

cmd=
args=""
while [ $# -gt 0 ]; do
    arg="$1"
    shift
    temparg="$(maybe_expand "$arg")"
    if [ -z "$temparg" ]; then
	if [ -z "$cmd" ]; then
	    cmd="$arg"
	    continue
	fi
	# Not expanded, so simply add to argument list
	args="$args
$arg"
    else
	SEEN_INDEPDEB=0; SEEN_ARCHDEB=0; SEEN_SCHANGES=0; SEEN_BCHANGES=0
	SEEN_INDEPUDEB=0; SEEN_ARCHUDEB=0; SEEN_UDEB=0;
	SEEN_TARBALL=0; SEEN_DIFF=0; SEEN_DSC=0; SEEN_BUILDINFO=0;
	MISSING=0
	newarg=""
	# Output those items from the expanded list which were
	# requested, and record which files are contained in the list
	eval "$(echo "$temparg" | while read THISARG; do
	    if [ -z "$THISARG" ]; then
		# Skip
		:
	    elif endswith "$THISARG" _all.deb; then
		[ "$INDEPDEB" = "0" ] || echo "newarg=\"\$newarg
$THISARG\";"
		echo "SEEN_INDEPDEB=1;"
	    elif endswith "$THISARG" .deb; then
		[ "$ARCHDEB" = "0" ] || echo "newarg=\"\$newarg
$THISARG\";"
		echo "SEEN_ARCHDEB=1;"
	    elif endswith "$THISARG" _all.udeb; then
		[ "$INDEPUDEB" = "0" ] || echo "newarg=\"\$newarg
$THISARG\";"
		echo "SEEN_INDEPUDEB=1;"
	    elif endswith "$THISARG" .udeb; then
		[ "$ARCHUDEB" = "0" ] || echo "newarg=\"\$newarg
$THISARG\";"
		echo "SEEN_ARCHUDEB=1;"
	    elif endswith "$THISARG" .debian.tar.gz || \
		 endswith "$THISARG" .debian.tar.xz || \
		 endswith "$THISARG" .debian.tar.bz2; then
		[ "$DIFF" = "0" ] || echo "newarg=\"\$newarg
$THISARG\";"
		echo "SEEN_DIFF=1;"
	    elif endswith "$THISARG" .tar.bz2	|| \
		 endswith "$THISARG" .tar.gz	|| \
		 endswith "$THISARG" .tar.lzma	|| \
		 endswith "$THISARG" .tar.xz	|| \
		 endswith "$THISARG" .tar.zst	|| \
		 endswith "$THISARG" .tar.*.asc; then
		[ "$TARBALL" = "0" ] || echo "newarg=\"\$newarg
$THISARG\";"
		echo "SEEN_TARBALL=1;"
	    elif endswith "$THISARG" _source.changes; then
		[ "$SCHANGES" = "0" ] || echo "newarg=\"\$newarg
$THISARG\";"
		echo "SEEN_SCHANGES=1;"
	    elif endswith "$THISARG" .changes; then
		[ "$BCHANGES" = "0" ] || echo "newarg\"\$newarg
$THISARG\";"
		echo "SEEN_BCHANGES=1;"
	    elif endswith "$THISARG" .dsc; then
		[ "$DSC" = "0" ] || echo "newarg=\"\$newarg
$THISARG\";"
		echo "SEEN_DSC=1;"
	    elif endswith "$THISARG" .buildinfo; then
		[ "$BUILDINFO" = "0" ] || echo "newarg=\"\$newarg
$THISARG\";"
		echo "SEEN_BUILDINFO=1;"
	    elif endswith "$THISARG" .diff.gz; then
		[ "$DIFF" = "0" ] || echo "newarg=\"\$newarg
$THISARG\";"
		echo "SEEN_DIFF=1;"
	    elif [ "$FILTERED" != "1" ]; then
		# What is it? Output anyway
		echo "newarg=\"\$newarg
$THISARG\";"
	    fi
	done)"

	INCLUDEARG=1
	if endswith "$arg" _source.changes; then
	    [ "$SCHANGES" = "1" ] || INCLUDEARG=0
	    SEEN_SCHANGES=1
	elif endswith "$arg" .changes; then
	    [ "$BCHANGES" = "1" ] || INCLUDEARG=0
	    SEEN_BCHANGES=1
	elif endswith "$arg" .dsc; then
	    [ "$DSC" = "1" ] || INCLUDEARG=0
	    SEEN_DSC=1
	elif endswith "$arg" .buildinfo; then
	    [ "$BUILDINFO" = "1" ] || INCLUDEARG=0
	    SEEN_BUILDINFO=1
	fi

	if [ "$FAIL_MISSING" = "1" ] && [ "$FILTERED" = "1" ]; then
	    if [ "$CHANGES" = "1" ]; then
		if [ "$SEEN_SCHANGES" = "0" ] && [ "$SEEN_BCHANGES" = "0" ]; then
		    MISSING=1; echo "$arg: .changes fiie not found" >&2
		fi
	    else
		if [ "$SCHANGES" = "1" ] && [ "$SEEN_SCHANGES" = "0" ]; then
		    MISSING=1; echo "$arg: source .changes file not found" >&2
		fi
		if [ "$BCHANGES" = "1" ] && [ "$SEEN_BCHANGES" = "0" ]; then
		    MISSING=1; echo "$arg: binary .changes file not found" >&2
		fi
	    fi

	    if [ "$DEB" = "1" ]; then
		if  [ "$SEEN_INDEPDEB" = "0" ] && [ "$SEEN_ARCHDEB" = "0" ]; then
		    MISSING=1; echo "$arg: binary packages not found" >&2
		fi
	    else
		if [ "$INDEPDEB" = "1" ] && [ "$SEEN_INDEPDEB" = "0" ]; then
		    MISSING=1; echo "$arg: arch-indep packages not found" >&2
		fi
		if [ "$ARCHDEB" = "1" ] && [ "$SEEN_ARCHDEB" = "0" ]; then
		    MISSING=1; echo "$arg: arch-dep packages not found" >&2
		fi
	    fi

	    if [ "$UDEB" = "1" ]; then
		if [ "$SEEN_INDEPUDEB" = "0" ] && [ "$SEEN_ARCHUDEB" = "0" ]; then
		    MISSING=1; echo "$arg: udeb packages not found" >&2
		fi
	    else
		if [ "$INDEPUDEB" = "1" ] && [ "$SEEN_INDEPUDEB" = "0" ]; then
		    MISSING=1; echo "$arg: arch-indep udeb packages not found" >&2
		fi
		if [ "$ARCHUDEB" = "1" ] && [ "$SEEN_ARCHUDEB" = "0" ]; then
		    MISSING=1; echo "$arg: arch-dep udeb packages not found" >&2
		fi

	    fi

	    if [ "$BUILDINFO" = "1" ] && [ "$SEEN_BUILDINFO" = "0" ]; then
		MISSING=1; echo "$arg: .buildinfo file not found" >&2
	    fi
	    if [ "$DSC" = "1" ] && [ "$SEEN_DSC" = "0" ]; then
		MISSING=1; echo "$arg: .dsc file not found" >&2
	    fi
	    if [ "$TARBALL" = "1" ] && [ "$SEEN_TARBALL" = "0" ]; then
		MISSING=1; echo "$arg: upstream tar not found" >&2
	    fi
	    if [ "$DIFF" = "1" ] && [ "$SEEN_DIFF" = "0" ]; then
		MISSING=1; echo "$arg: Debian debian.tar/diff not found" >&2
	    fi

	    [ "$MISSING" = "0" ] || exit 1
	fi

	args="$args
$newarg"
	[ "$INCLUDEARG" = "0" ] || args="$args
$arg"
    fi
done

IFS='
'
if [ "$EXTRACT_PACKAGE_NAME" = "1" ]; then
    packages=""
    for arg in $args; do
        packages="$packages
$(echo "$arg" |sed s/_.*//)"
    done
    args="$packages"
fi
if [ "$SORT" = "1" ]; then
    args="$(echo "$args"| sort -)"
fi
if [ "$TAC" = "1" ]; then
    args="$(echo "$args"| tac -)"
fi
if [ -z "$cmd" ]; then
    for arg in $args; do
	echo $arg
    done
    exit 0
fi

exec $cmd $args