Hi, here is an updated version of dget.
Christoph -- [EMAIL PROTECTED] | http://www.df7cb.de/
#!/usr/bin/perl -w # vim:sw=4:sta: # dget - Download Debian source and binary packages # Copyright (C) 2005 Christoph Berg <[EMAIL PROTECTED]> # # 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 use strict; use IO::File; use Digest::MD5; # global variables my $found_dsc; my $unpack_source = 0; # use curl if installed, wget otherwise my $wget = "wget -q -O"; $wget = "curl -s -f -o" if -e "/usr/bin/curl"; # functions sub usage { die "usage: $0 [-x] package-url.dsc/changes package package=version ...\n"; } # some files both are in .dsc and .changes, download only once my %seen; sub get_file { my ($dir, $file, $md5sum) = @_; return if $seen{$file}; $seen{$file} = 1; if ($md5sum eq "unlink") { 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"; unlink $file; } } unless (-e $file) { print "$0: retrieving $dir/$file\n"; system "$wget $file $dir/$file"; unless (-e $file and -s $file) { die "$0: $wget $file $dir/$file failed\n"; unlink $file; } } if ($file =~ /\.(?:changes|dsc)$/) { parse_file($dir, $file); } if ($file =~ /\.dsc$/) { $found_dsc = $file; } } sub parse_file { my ($dir, $file) = @_; my $fh = IO::File::new($file); open $fh, $file or die "$file: $!"; while (<$fh>) { if (/^ ([0-9a-f]{32}) (?:\S+ )*(\S+)$/) { get_file($dir, $2, $1); } } close $fh; } sub apt_get { my ($package, $version) = @_; my $qpackage = quotemeta($package); my $qversion = quotemeta($version); $qversion =~ s/^([^:]+:)/($1)?/; $qversion =~ s/-([^.-]+)$/-$1(\.0\.\\d+)?\$/; # BinNMU: -x -> -x.0.1 $qversion =~ s/-([^.-]+\.[^.-]+)$/-$1(\.\\d+)?\$/; # -x.y -> -x.y.1 my $apt = new IO::File("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"; } $apt = new IO::File("/etc/apt/sources.list") or die "/etc/apt/sources.list: $!"; my $repository; while (<$apt>) { if (/^\s*deb\s*(\S+)/) { $repository = $1; last; } } close $apt; unless ($repository) { die "no repository found in /etc/apt/sources.list"; } my ($dir, $file) = ($repository, $filename); if ($filename =~ /(.*)\/([^\/]*)$/) { ($dir, $file) = ("$repository/$1", $2); } get_file($dir, $file, $md5sum); } # main program usage() unless @ARGV; for my $arg (@ARGV) { if ($arg eq "-x") { $unpack_source = 1; next; } $found_dsc = ""; if ($arg =~ /^((?:http|www).*)\/([^\/]+\.\w+)$/) { my ($dir, $file) = ($1, $2); get_file($dir, $file, "unlink"); if ($found_dsc and $unpack_source) { system "dpkg-source -x $found_dsc"; } } elsif ($arg =~ /^[a-z0-9.+-]{2,}$/) { # we reinvent "apt-get -d install" here, without requiring root # (and we do not download dependencies) $ENV{LC_ALL} = 'C'; my $apt = new IO::File("apt-cache policy $arg |") or die "$!"; my $candidate; while (<$apt>) { if (/^ Candidate: (.+)/) { $candidate = $1; last; } } close $apt; unless ($candidate) { die "$0: $arg has no installation candidate\n"; } apt_get($arg, $candidate); } 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<-x>] I<URL> =item B<dget> I<package> =item B<dget> I<package>=I<version> =back =head1 DESCRIPTION B<dget> downloads Debian packages. In the first form, B<dget> acts as an 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<-x> run B<dpkg-source -x> on the downloaded source package. =head1 BUGS B<dget> I<package> should be implemented in B<apt-get install -d>. =head1 AUTHOR =over =item Christoph Berg <[EMAIL PROTECTED]> =back =head1 SEE ALSO debdiff(1), apt-get(1), curl(1), wget(1).
signature.asc
Description: Digital signature