The following commit has been merged in the master branch:
commit 639565ec1f8146da236ad85a5240a5a8d25443e2
Author: James Vega <[email protected]>
Date:   Tue Apr 26 00:03:15 2011 -0400

    debsnap: Use sha1sum to check whether an existing file should be 
overwritten.
    
    Signed-off-by: James Vega <[email protected]>

diff --git a/debian/changelog b/debian/changelog
index 654df76..82a11e0 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -9,8 +9,11 @@ devscripts (2.10.73) UNRELEASED; urgency=low
     files under one top-level directory.  (Closes: #601945)
   * bts: Indicate version argument is optional for done command.  (Closes:
     #623567)
-  * debsnap: Add support for download binary packages.  Thanks to Timo Juhani
-    Lindfors for the patch.  (Closes: #587523)
+  * debsnap:
+    + Add support for downloading binary packages.  Thanks to Timo Juhani
+      Lindfors for the patch.  (Closes: #587523)
+    + Only download a file if it doesn't exist or its hashsum doesn't match
+      the one specified by snapshot.d.o.
 
   [ Benjamin Drung ]
   * Add myself to uploaders.
diff --git a/scripts/debsnap.pl b/scripts/debsnap.pl
index 6176748..954d235 100755
--- a/scripts/debsnap.pl
+++ b/scripts/debsnap.pl
@@ -162,6 +162,22 @@ sub read_conf
     $opt{baseurl} = $config_vars{DEBSNAP_BASE_URL};
 }
 
+sub have_file($$)
+{
+    my ($path, $hash) = @_;
+
+    if (-e $path) {
+       open(HASH, '-|', 'sha1sum', $path) || fatal "Can't run sha1sum: $!";
+       while (<HASH>) {
+           if (m/^([a-fA-F\d]{40}) /) {
+               close(HASH) || fatal "sha1sum problems: $! $?";
+               return $1 eq $hash;
+           }
+       }
+    }
+    return 0;
+}
+
 sub fatal($)
 {
     my ($pack, $file, $line);
@@ -200,13 +216,15 @@ $package eq '' && usage(1);
 
 $opt{binary} ||= $opt{architecture};
 
+my $baseurl;
 if ($opt{binary}) {
     $opt{destdir} ||= "binary-$package";
+    $baseurl = "$opt{baseurl}/mr/binary/$package/";
 } else {
     $opt{destdir} ||= "source-$package";
+    $baseurl = "$opt{baseurl}/mr/package/$package/";
 }
 
-my $baseurl = "$opt{baseurl}/mr/package/$package/";
 if (-d $opt{destdir}) {
     unless ($opt{force} || cwd() eq abs_path($opt{destdir})) {
        fatal "Destination dir $opt{destdir} already exists.\nPlease (re)move 
it first, or use --force to overwrite.";
@@ -214,14 +232,12 @@ if (-d $opt{destdir}) {
 }
 make_path($opt{destdir});
 
-if ($opt{binary}) {
-    $baseurl = "$opt{baseurl}/mr/binary/$package/";
-    
-    my $json_text = fetch_json_page($baseurl);
-    unless ($json_text && @{$json_text->{result}}) {
-       fatal "Unable to retrieve information for $package from $baseurl.";
-    }
+my $json_text = fetch_json_page($baseurl);
+unless ($json_text && @{$json_text->{result}}) {
+    fatal "Unable to retrieve information for $package from $baseurl.";
+}
 
+if ($opt{binary}) {
     foreach my $version (@{$json_text->{result}}) {
        if ($pkgversion) {
            next if ($version->{binary_version} <=> $pkgversion);
@@ -238,60 +254,51 @@ if ($opt{binary}) {
            if ($opt{architecture}) {
                next if ($result->{architecture} ne $opt{architecture});
            }
-           my $fileinfo = @{$src_json->{fileinfo}{$result->{hash}}}[0];
-           my $file_url = "$opt{baseurl}/file/$result->{hash}";
+           my $hash = $result->{hash};
+           my $fileinfo = @{$src_json->{fileinfo}{$hash}}[0];
+           my $file_url = "$opt{baseurl}/file/$hash";
            my $file_name = basename($fileinfo->{name});
-           verbose "Getting file $file_name: $file_url";
-           LWP::Simple::getstore($file_url, "$opt{destdir}/$file_name");
+           if (!have_file("$opt{destdir}/$file_name", $hash)) {
+               verbose "Getting file $file_name: $file_url";
+               LWP::Simple::getstore($file_url, "$opt{destdir}/$file_name");
+           }
        }
     }
-    if ($warnings) {
-       exit 2;
-    }
-    exit 0;
 }
-
-my $json_text = fetch_json_page($baseurl);
-unless ($json_text && @{$json_text->{result}}) {
-    fatal "Unable to retrieve information for $package from $baseurl.";
-}
-# Keep track of what's been downloaded so we don't download the same
-# orig.tar.gz multiple times
-my %fetched;
-# iterate over each available version in the JSON structure:
-foreach my $version (@{$json_text->{result}}) {
-    if ($pkgversion) {
-       next if ($version->{version} <=> $pkgversion);
-    }
-
-    my $src_json = 
fetch_json_page("$baseurl/$version->{version}/srcfiles?fileinfo=1");
-    unless ($src_json) {
-       warn "$progname: No source files found for $package version 
$version->{version}\n";
-       $warnings++;
-    }
-
-    foreach my $hash (keys %{$src_json->{fileinfo}}) {
-       my $fileinfo = $src_json->{fileinfo}{$hash};
-       my $file_name;
-       # fileinfo may match multiple files (e.g., orig tarball for iceweasel 
3.0.12)
-       foreach my $info (@$fileinfo) {
-           if ($info->{name} =~ m/^${package}/) {
-               $file_name = $info->{name};
-               last;
-           }
+else {
+    foreach my $version (@{$json_text->{result}}) {
+       if ($pkgversion) {
+           next if ($version->{version} <=> $pkgversion);
        }
-       unless ($file_name) {
-           warn "$progname: No files with hash $hash matched '${package}'\n";
+
+       my $src_json = 
fetch_json_page("$baseurl/$version->{version}/srcfiles?fileinfo=1");
+       unless ($src_json) {
+           warn "$progname: No source files found for $package version 
$version->{version}\n";
            $warnings++;
-           next;
        }
-       my $file_url = "$opt{baseurl}/file/$hash";
-       $file_name = basename($file_name);
-       if (!$fetched{$file_name}) {
-           verbose "Getting file $file_name: $file_url";
-           LWP::Simple::getstore($file_url, "$opt{destdir}/$file_name");
+
+       foreach my $hash (keys %{$src_json->{fileinfo}}) {
+           my $fileinfo = $src_json->{fileinfo}{$hash};
+           my $file_name;
+           # fileinfo may match multiple files (e.g., orig tarball for 
iceweasel 3.0.12)
+           foreach my $info (@$fileinfo) {
+               if ($info->{name} =~ m/^${package}/) {
+                   $file_name = $info->{name};
+                   last;
+               }
+           }
+           unless ($file_name) {
+               warn "$progname: No files with hash $hash matched 
'${package}'\n";
+               $warnings++;
+               next;
+           }
+           my $file_url = "$opt{baseurl}/file/$hash";
+           $file_name = basename($file_name);
+           if (!have_file("$opt{destdir}/$file_name", $hash)) {
+               verbose "Getting file $file_name: $file_url";
+               LWP::Simple::getstore($file_url, "$opt{destdir}/$file_name");
+           }
        }
-       $fetched{$file_name} = 1;
     }
 }
 

-- 
Git repository for devscripts


-- 
To unsubscribe, send mail to [email protected].

Reply via email to