~ubuntu-branches/debian/squeeze/devscripts/squeeze

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
#!/usr/bin/perl -w
# vim:sw=4:sta:

#   dget - Download Debian source and binary packages
#   Copyright (C) 2005 Christoph Berg <myon@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

# 2005-10-04 cb: initial release
# 2005-12-11 cb: -x option, update documentation
# 2005-12-31 cb: -b, -q options, use getopt

use strict;
use IO::File;
use Digest::MD5;
use Getopt::Long;

# global variables

my $found_dsc;
# use curl if installed, wget otherwise
my $wget = -e "/usr/bin/curl" ? "curl" : "wget";
my $opt;
my $backup_dir = "backup";

# functions

sub usage {
    die "usage: $0 [-bqx] package-url.dsc/changes package package=version ...\n";
}

sub wget {
    my ($file, $url) = @_;
    my @cmd = ($wget);
    push @cmd, ($wget eq "wget" ? "-q" : "-s") if $opt->{quiet};
    push @cmd, ($wget eq "wget" ? "--no-check-certificate" : "--insecure") if $opt->{insecure};
    push @cmd, ($wget eq "wget" ? "-O" : "-o");
    system @cmd, $file, $url;
    return $? >> 8;
}

sub backup_or_unlink {
    my $file = shift;
    return unless -e $file;
    if ($opt->{backup}) {
	unless (-d $backup_dir) {
	    mkdir $backup_dir or die "mkdir $backup_dir: $!";
	}
	rename $file, "$backup_dir/$file" or die "rename $file $backup_dir/$file: $!";
    } else {
	unlink $file or die "unlink $file: $!";
    }
}

# some files both are in .dsc and .changes, download only once
my %seen;
sub get_file {
    my ($dir, $file, $md5sum) = @_;
    return if $seen{$file};

    if ($md5sum eq "unlink") {
	backup_or_unlink($file);
    }

    if (-e $file) {
	my $md5 = Digest::MD5->new;
	my $fh5 = new IO::File($file) or die "$file: $!";
	my $md5sum_new = Digest::MD5->new->addfile($fh5)->hexdigest();
	close $fh5;
	if (not $md5sum or ($md5sum_new eq $md5sum)) {
	    print "$0: using existing $file\n";
	} else {
	    print "$0: md5sum for $file does not match\n";
	    backup_or_unlink($file);
	}
    }

    unless (-e $file) {
	print "$0: retrieving $dir/$file\n";
	if (wget($file, "$dir/$file")) {
	    warn "$0: $wget $file $dir/$file failed\n";
	    unlink $file;
	    return 0;
	}
    }

    if ($file =~ /\.(?:changes|dsc)$/) {
	parse_file($dir, $file);
    }
    if ($file =~ /\.dsc$/) {
	$found_dsc = $file;
    }

    $seen{$file} = 1;
    return 1;
}

sub parse_file {
    my ($dir, $file) = @_;

    my $fh = new IO::File($file);
    open $fh, $file or die "$file: $!";
    while (<$fh>) {
	if (/^ ([0-9a-f]{32}) (?:\S+ )*(\S+)$/) {
	    get_file($dir, $2, $1) or return;;
	}
    }
    close $fh;
}

# we reinvent "apt-get -d install" here, without requiring root
# (and we do not download dependencies)
sub apt_get {
    my ($package, $version) = @_;

    my $qpackage = quotemeta($package);
    my $qversion = quotemeta($version) if $version;
    my @hosts;

    my $apt = new IO::File("LC_ALL=C apt-cache policy $package |") or die "$!";
    OUTER: while (<$apt>) {
	if (not $version and /^  Candidate: (.+)/) {
	    $version = $1;
	    $qversion = quotemeta($version);
	}
	if ($qversion and /^ [ *]{3} ($qversion) 0/) {
	    while (<$apt>) {
		last OUTER unless /^        (?:\d+) (\S+)/;
		push @hosts, $1;
	    }
	}
    }
    close $apt;
    unless ($version) {
	die "$0: $package has no installation candidate\n";
    }
    unless (@hosts) {
	die "$0: no hostnames in apt-cache policy $package for $version found\n";
    }

    $qversion =~ s/^([^:]+:)/($1)?/;
    $qversion =~ s/-([^.-]+)$/-$1(\.0\.\\d+)?\$/; # BinNMU: -x -> -x.0.1
    $qversion =~ s/-([^.-]+\.[^.-]+)$/-$1(\.\\d+)?\$/; # -x.y -> -x.y.1

    $apt = new IO::File("LC_ALL=C apt-cache show $package |") or die "$!";
    my ($v, $p, $filename, $md5sum);
    while (<$apt>) {
	if (/^Package: $qpackage$/) {
	    $p = $package;
	}
	if (/^Version: $qversion$/) {
	    $v = $version;
	}
	if (/^Filename: (.*)/) {
	    $filename = $1;
	}
	if (/^MD5sum: (.*)/) {
	    $md5sum = $1;
	}
	if (/^Description:/) { # we assume this is the last field
	    if ($p and $v and $filename) {
		last;
	    }
	    undef $p;
	    undef $v;
	    undef $filename;
	    undef $md5sum;
	}
    }
    close $apt;

    unless ($filename) {
	die "$0: no filename for $package ($version) found\n";
    }

    # find deb lines matching the hosts in the policy output
    $apt = new IO::File("/etc/apt/sources.list") or die "/etc/apt/sources.list: $!";
    my @repositories;
    my $host_re = '(?:' . (join '|', map { quotemeta; } @hosts) . ')';
    while (<$apt>) {
	if (/^\s*deb\s*($host_re\S+)/) {
	    push @repositories, $1;
	}
    }
    close $apt;
    unless (@repositories) {
	die "no repository found in /etc/apt/sources.list";
    }

    # try each repository in turn
    foreach my $repository (@repositories) {
	my ($dir, $file) = ($repository, $filename);
	if ($filename =~ /(.*)\/([^\/]*)$/) {
	    ($dir, $file) = ("$repository/$1", $2);
	}

	get_file($dir, $file, $md5sum) and exit 0;
    }
    exit 1;
}

# main program

Getopt::Long::config('bundling');
unless (GetOptions(
    '-h'	=>  \$opt->{'help'},
    '--help'	=>  \$opt->{'help'},
    '-b'	=>  \$opt->{'backup'},
    '--backup'	=>  \$opt->{'backup'},
    '-q'	=>  \$opt->{'quiet'},
    '--quiet'	=>  \$opt->{'quiet'},
    '-x'	=>  \$opt->{'unpack_source'},
    '--extract'	=>  \$opt->{'unpack_source'},
    '--insecure'	=> \$opt->{'insecure'},
)) {
    usage();
}

usage() if !@ARGV or $opt->{help};

for my $arg (@ARGV) {
    $found_dsc = "";

    if ($arg =~ /^((?:copy|file|ftp|http|rsh|rsync|ssh|www).*)\/([^\/]+\.\w+)$/) {
	get_file($1, $2, "unlink");
	if ($found_dsc and $opt->{unpack_source}) {
	    system 'dpkg-source', '-x', $found_dsc;
	}

    } elsif ($arg =~ /^[a-z0-9.+-]{2,}$/) {
	apt_get($arg);

    } elsif ($arg =~ /^([a-z0-9.+-]{2,})=([a-zA-Z0-9.:~+-]+)$/) {
	apt_get($1, $2);

    } else {
	usage();
    }
}

=pod

=head1 NAME

dget -- Download Debian source and binary packages

=head1 SYNOPSIS

=over

=item B<dget> [B<-bqx>] I<URL>

=item B<dget> [B<-bq>] I<package>

=item B<dget> [B<-bq>] I<package>=I<version>

=back

=head1 DESCRIPTION

B<dget> downloads Debian packages. In the first form, B<dget> acts as a source
package-aware form of wget; it fetches the given URL and recursively any files
referenced, if the URL points to a .dsc or .changes file. When the B<-x> option
is given, the downloaded source is unpacked by B<dpkg-source>.

In the second and third form, B<dget> downloads a binary package from the
Debian mirror configured in /etc/apt/sources.list. Unlike B<apt-get install
-d>, it does not require root privileges, writes to the current directory, and
does not download dependencies.

Before downloading referenced files in .dsc and .changes files, and before
downloading binary packages, if any of the files already exist, md5sums are
compared to avoid wasting bandwidth. Download backends used are B<curl> and
B<wget>, looked for in that order.

B<dget> was written to make it easier to retrieve source packages from the web
for sponsor uploads. For checking the package with B<debdiff>, the last binary
version is available via B<dget> I<package>, the last source version via
B<apt-get source> I<package>.

=head1 OPTIONS

B<-b> move files that would be overwritten to B<./backup>.

B<-q> suppress wget/curl output.

B<-x> run B<dpkg-source -x> on the downloaded source package.

B<--insecure> allow ssl connections to untrusted hosts.

=head1 BUGS

B<dget> I<package> should be implemented in B<apt-get install -d>.

=head1 AUTHOR

=over

=item Christoph Berg <myon@debian.org>

=back

=head1 SEE ALSO

apt-get(1), debdiff(1), dpkg-source(1), curl(1), wget(1).