[devscripts] 02/02: Unify repackaging code, and add test cases

Joachim Breitner nomeata at moszumanska.debian.org
Sun Mar 16 11:47:08 UTC 2014


This is an automated email from the git hooks/post-receive script.

nomeata pushed a commit to branch master
in repository devscripts.

commit ae87e001b54ddb7f08ac9ee976e8b61c01cee8c1
Author: Joachim Breitner <nomeata at debian.org>
Date:   Sun Mar 16 12:46:30 2014 +0100

    Unify repackaging code, and add test cases
    
    Dpkg.Compression already provides functions to handle decompression and
    compression almost transparently, so use them.
---
 scripts/uscan.pl | 79 ++++++++++++++++++++++++++++----------------------------
 test/test_uscan  | 61 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 100 insertions(+), 40 deletions(-)

diff --git a/scripts/uscan.pl b/scripts/uscan.pl
index 80445aa..b4e2ddf 100755
--- a/scripts/uscan.pl
+++ b/scripts/uscan.pl
@@ -79,6 +79,7 @@ sub quoted_regex_replace ($);
 sub safe_replace ($$);
 sub get_main_source_dir($$$$);
 sub compress_archive($$$);
+sub decompress_archive($$);
 
 sub usage {
     print <<"EOF";
@@ -329,6 +330,12 @@ if ($opt_v) { version(); exit 0; }
 
 my $supported_compressions = compression_get_file_extension_regex();
 
+# This makes more sense in Dpkg:Compression
+my $tarbase_regex = qr/^(.*)\.(tar\.gz  |tgz
+			      |tar\.bz2 |tbz2?
+			      |tar.lzma |tlz(?:ma?)?
+			      |tar.xz   |txz)$/x;
+
 # Now we can set the other variables according to the command line options
 
 $destdir = $opt_destdir if defined $opt_destdir;
@@ -1446,42 +1453,6 @@ EOF
 		 or uscan_die("$progname warning: OpenPGP signature did not verify.\n");
     }
 
-    if ($repack and $newfile_base =~ /^(.*)\.(tar\.bz2|tbz2?)$/ and
-        $repack_compression !~ /^bz2$/ ) {
-	print "-- Repacking from bzip2 to $repack_compression\n" if $verbose;
-	my $newfile_base_compression = "$1.tar.$repack_compression";
-	my (undef, $fname) = tempfile(UNLINK => 1);
-	spawn(exec => ['bunzip2', '-c', "$destdir/$newfile_base"],
-	      to_file => $fname,
-	      wait_child => 1);
-	compress_archive("$fname", "$destdir/$newfile_base_compression", $repack_compression);
-	$newfile_base = $newfile_base_compression;
-    }
-
-    if ($repack and $newfile_base =~ /^(.*)\.(tar\.lzma|tlz(?:ma?)?)$/ and
-        $repack_compression !~ /^lzma$/ ) {
-	print "-- Repacking from lzma to $repack_compression\n" if $verbose;
-	my $newfile_base_compression = "$1.tar.$repack_compression";
-	my (undef, $fname) = tempfile(UNLINK => 1);
-	spawn(exec => ['xz', '-F', 'lzma', '-cd', "$destdir/$newfile_base"],
-	      to_file => $fname,
-	      wait_child => 1);
-	compress_archive("$fname", "$destdir/$newfile_base_compression", $repack_compression);
-	$newfile_base = $newfile_base_compression;
-    }
-
-    if ($repack and $newfile_base =~ /^(.*)\.(tar\.xz|txz)$/ and
-        $repack_compression !~ /^xz$/ ) {
-	print "-- Repacking from xz to $repack_compression\n" if $verbose;
-	my $newfile_base_compression = "$1.tar.$repack_compression";
-	my (undef, $fname) = tempfile(UNLINK => 1);
-	spawn(exec => ['xz', '-cd', "$destdir/$newfile_base"],
-	      to_file => $fname,
-	      wait_child => 1);
-	compress_archive("$fname", "$destdir/$newfile_base_compression", $repack_compression);
-	$newfile_base = $newfile_base_compression;
-    }
-
     if ($repack and $newfile_base =~ /^(.*)\.(zip|jar)$/) {
 	print "-- Repacking from zip to .tar.$repack_compression\n" if $verbose;
 
@@ -1507,12 +1478,26 @@ EOF
 	}
 	chdir($cwd);
 	$newfile_base = $newfile_base_compression;
+
+    } elsif ($repack) { # Repacking from tar to tar, so just change the compression
+	my $comp = compression_guess_from_filename($newfile_base);
+	unless ($comp) {
+	   uscan_die("Cannot determine compression method of $newfile_base");
+	}
+	if ($comp ne $repack_compression) {
+	    print "-- Repacking from $comp to $repack_compression\n" if $verbose;
+	    my ($tarbase) = ($newfile_base =~ $tarbase_regex);
+	    my $newfile_base_compression = "$1.tar.$repack_compression";
+
+	    my (undef, $fname) = tempfile(UNLINK => 1);
+	    decompress_archive("$destdir/$newfile_base", $fname);
+	    compress_archive("$fname", "$destdir/$newfile_base_compression", $repack_compression);
+	    $newfile_base = $newfile_base_compression;
+	}
     }
 
-    if ($newfile_base =~ /\.(tar\.gz|tgz
-			     |tar\.bz2|tbz2?
-			     |tar.lzma|tlz(?:ma?)?
-			     |tar.xz|txz)$/x) {
+
+    if ($newfile_base =~ $tarbase_regex){
 	my $filetype;
 	eval {
 	    spawn(exec => ['file', '-b', '-k', "$destdir/$newfile_base"],
@@ -2251,3 +2236,17 @@ sub compress_archive($$$) {
 	wait_child => 1);
     unlink $from_file;
 }
+
+sub decompress_archive($$) {
+    my ($from_file, $to_file) = @_;
+    my $comp = compression_guess_from_filename($from_file);
+    unless ($comp) {
+       uscan_die("Cannot determine compression method of $from_file");
+    }
+
+    my $cmd = compression_get_property($comp, 'decomp_prog');
+    spawn(exec => $cmd,
+	from_file => $from_file,
+	to_file => $to_file,
+	wait_child => 1);
+}
diff --git a/test/test_uscan b/test/test_uscan
index 64293fa..c350b2a 100755
--- a/test/test_uscan
+++ b/test/test_uscan
@@ -38,6 +38,67 @@ containsName(){
 
 . "${0%/*}/shunit2-helper-functions.sh"
 
+# The following tests do the following: (1) create a minimal Debian package
+# directory, containing minimal files debian/{changelog,watch,copyright},
+# (2) create a minimal repository, containing a tarball (built on the fly),
+# (3) start an HTTP server that works offline, using the SimpleHTTPServer
+# module of Python, and (4) run uscan inside that minimal universe.
+
+
+# The following function tests the --repack feature
+helperTestRepack() {
+    from_comp="$1"
+    to_comp="$2"
+    file_output="$3"
+
+    PKG=foo
+    PORT=8000
+    TMPDIR=$(mktemp -d)
+
+    mkdir -p $TMPDIR/$PKG/debian
+
+    cat <<END > $TMPDIR/$PKG/debian/watch
+version=3
+http://localhost:$PORT/$PKG-(\d).tar.$from_comp
+END
+
+    cat <<END > $TMPDIR/$PKG/debian/changelog
+$PKG (0-1) unstable; urgency=low
+
+  * Initial release
+
+ -- Joe Developer <jd at debian.org>  Mon, 02 Nov 2013 22:21:31 -0100
+END
+
+    mkdir -p $TMPDIR/repo/foo
+    touch $TMPDIR/repo/foo/content
+
+    ( cd $TMPDIR/repo ;
+      tar cfa $PKG-1.tar.$from_comp * ;
+      python -m SimpleHTTPServer $PORT &
+      echo $! > pid )
+
+    OUTPUT=$( (cd $TMPDIR/$PKG ; $COMMAND --verbose --repack --repack-compression=$to_comp) )
+
+    TARBALL=${PKG}_1.orig.tar.$to_comp
+    assertTrue 'pristine tarball is not created' "[ -f $TMPDIR/$TARBALL ]"
+    assertNotNull "pristine tarball is not $to_comp-compressed" \
+                  "$( file -L $TMPDIR/$TARBALL | grep "$file_output" )"
+    CONTENTS="$(tar atf $TMPDIR/$TARBALL)"
+    assertTrue 'file contents missing'	\
+                $(containsName "$CONTENTS" content)
+
+    cleanup
+
+}
+
+testRepackGZ_XZ() { helperTestRepack "gz" "xz" "XZ compressed data" ; }
+testRepackGZ_BZ2() { helperTestRepack "gz" "bz2" "bzip2 compressed data" ; }
+testRepackBZ2_GZ() { helperTestRepack "bz2" "gz" "gzip compressed data" ; }
+testRepackGZ_GZ() { helperTestRepack "gz" "gz" "gzip compressed data" ; }
+testRepackXZ_XZ() { helperTestRepack "xz" "xz" "XZ compressed data" ; }
+
+
 # The following function tests the Files-Excluded feature of uscan, which
 # allows the selective exclusion of files from the upstream tarball before
 # repacking it.

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/collab-maint/devscripts.git



More information about the devscripts-devel mailing list