[Pkg-bazaar-commits] r121 ./bzr-builddeb/people/jdw/merge_upstream: Add a function to repack a tarball or dir to a .tar.gz.

James Westby jw+debian at jameswestby.net
Sat Jun 23 10:31:23 UTC 2007


------------------------------------------------------------
revno: 121
committer: James Westby <jw+debian at jameswestby.net>
branch nick: merge_upstream
timestamp: Sat 2007-06-23 11:31:23 +0100
message:
  Add a function to repack a tarball or dir to a .tar.gz.
  
  When an import has been done the .orig.tar.gz is needed. This function is the
  first step towards that as it can do the repacking of most types. Currently
  zip is unhandled, as zipfile is horrible, but it will be handled eventually.
  
  Also add suoport for plain .tar archives. This now works for both
  merge_upstream and repack_tarball.
  
  Throw more specific errors for missing or present files.
added:
  repack_tarball.py
  tests/test_repack_tarball.py
modified:
  merge_upstream.py
  tests/__init__.py
  tests/test_builder.py
  tests/test_merge_upstream.py
-------------- next part --------------
=== added file 'repack_tarball.py'
--- a/repack_tarball.py	1970-01-01 00:00:00 +0000
+++ b/repack_tarball.py	2007-06-23 10:31:23 +0000
@@ -0,0 +1,84 @@
+#    repack_tarball.py -- Repack files/dirs in to tarballs.
+#    Copyright (C) 2007 James Westby <jw+debian at jameswestby.net>
+#
+#    This file is part of bzr-builddeb.
+#
+#    bzr-builddeb 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.
+#
+#    bzr-builddeb 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 bzr-builddeb; if not, write to the Free Software
+#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+#
+
+import gzip
+import os
+import shutil
+import tarfile
+import zipfile
+
+from bzrlib.errors import (NoSuchFile,
+                           FileExists,
+                           )
+
+
+def repack_tarball(orig_name, new_name):
+  """Repack the file/dir named to a .tar.gz with the chosen name.
+
+  This function takes a named file of either .tar.gz, .tar .tgz .tar.bz2 
+  or .zip type, or a directory, and creates the file named in the second
+  argument in .tar.gz format.
+
+  The source must exist, and the target cannot exist.
+  
+  :param orig_name: the curent name of the file/dir
+  :type orig_name: string
+  :param new_name: the desired name of the tarball
+  :type new_name: string
+  :return: None
+  :warning: .zip files are currently unsupported.
+  """
+  if not os.path.exists(orig_name):
+    raise NoSuchFile(orig_name)
+  if os.path.exists(new_name):
+    raise FileExists(new_name)
+  if os.path.isdir(orig_name):
+    tar = tarfile.open(new_name, 'w:gz')
+    try:
+      tar.add(orig_name, os.path.basename(orig_name))
+    finally:
+      tar.close()
+  else:
+    if orig_name.endswith('.tar.gz') or orig_name.endswith('.tgz'):
+      shutil.copyfile(orig_name, new_name)
+    elif orig_name.endswith('.tar'):
+      f = open(orig_name)
+      try:
+        gz = gzip.GzipFile(new_name, 'w')
+        try:
+          gz.write(f.read())
+        finally:
+          gz.close()
+      finally:
+        f.close()
+    elif orig_name.endswith('.tar.bz2'):
+      old_tar = tarfile.open(orig_name, 'r:bz2')
+      try:
+        new_tar = tarfile.open(new_name, 'w:gz')
+        try:
+          for old_member in old_tar.getmembers():
+            new_tar.addfile(old_member)
+        finally:
+          new_tar.close()
+      finally:
+        old_tar.close()
+  # TODO: handle zip files.
+  # TODO: complain on unrecognised formats and test for that.
+

=== added file 'tests/test_repack_tarball.py'
--- a/tests/test_repack_tarball.py	1970-01-01 00:00:00 +0000
+++ b/tests/test_repack_tarball.py	2007-06-23 10:31:23 +0000
@@ -0,0 +1,94 @@
+#    test_repack_tarball.py -- Testsuite for repacking of tarballs
+#    Copyright (C) 2007 James Westby <jw+debian at jameswestby.net>
+#    
+#    This file is part of bzr-builddeb.
+#
+#    bzr-builddeb 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.
+#
+#    bzr-builddeb 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 bzr-builddeb; if not, write to the Free Software
+#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+#
+
+import os
+import tarfile
+
+from bzrlib.errors import (NoSuchFile,
+                           FileExists,
+                           )
+from bzrlib.tests import TestCaseInTempDir
+
+from repack_tarball import repack_tarball
+
+
+def touch(filename):
+  f = open(filename, 'w')
+  try:
+    f.write(' ')
+  finally:
+    f.close()
+
+
+class TestRepackTarball(TestCaseInTempDir):
+
+  basedir = 'package-0.2/'
+  files = ['README']
+  bare_tarball_name = 'package-0.2.tar'
+  tgz_tarball_name = 'package-0.2.tgz'
+  tar_gz_tarball_name = 'package-0.2.tar.gz'
+  tar_bz2_tarball_name = 'package-0.2.tar.bz2'
+  new_tarball = 'package_0.2.orig.tar.gz'
+
+  def create_basedir(self):
+    """Create the basedir that the source can be built from"""
+    os.mkdir(self.basedir)
+    for filename in [os.path.join(self.basedir, file) for file in self.files]:
+      if filename.endswith('/'):
+        os.mkdir(filename)
+      else:
+        touch(filename)
+
+  def create_old_tarball(self):
+    self.create_basedir()
+    self.build_tarball()
+
+  def test_create_old_tarball(self):
+    self.create_old_tarball()
+    self.failUnlessExists(self.old_tarball)
+
+  def test_repack_tarball_non_extant(self):
+    self.assertRaises(NoSuchFile, repack_tarball, self.old_tarball,
+                      self.new_tarball)
+  
+  def test_repack_tarball_result_extant(self):
+    self.create_old_tarball()
+    touch(self.new_tarball)
+    self.assertRaises(FileExists, repack_tarball, self.old_tarball,
+                      self.new_tarball)
+
+  def test_repack_tarball_creates_new(self):
+    self.create_old_tarball()
+    repack_tarball(self.old_tarball, self.new_tarball)
+    self.failUnlessExists(self.old_tarball)
+    self.failUnlessExists(self.new_tarball)
+
+  def test_repack_tarball_contents(self):
+    self.create_old_tarball()
+    repack_tarball(self.old_tarball, self.new_tarball)
+    tar = tarfile.open(self.new_tarball, 'r:gz')
+    try:
+      members = tar.getnames()
+    finally:
+      tar.close()
+    self.assertEqual(members,
+                     [self.basedir] +
+                     [os.path.join(self.basedir, file) for file in self.files])
+

=== modified file 'merge_upstream.py'
--- a/merge_upstream.py	2007-06-23 00:10:29 +0000
+++ b/merge_upstream.py	2007-06-23 10:31:23 +0000
@@ -24,13 +24,14 @@
 import os
 from StringIO import StringIO
 
-from bzrlib.errors import BzrCommandError
+from bzrlib.errors import (BzrCommandError,
+                           NoSuchFile,
+                           )
 from bzrlib.plugins.bzrtools.upstream_import import (import_tar,
                                                      import_dir,
                                                      import_zip,
                                                      )
 
-# TODO: rename/repack tarball in to place.
 # TODO: drop requirement for revision of last upstream, use tags or something
 #       instead.
 # TODO: support using an explicit standalone upstream branch.
@@ -63,7 +64,7 @@
     if tree.changes_from(tree.basis_tree()).has_changed():
       raise BzrCommandError("Working tree has uncommitted changes.")
     if not os.path.exists(source):
-      raise BzrCommandError("%s does not exists" % source)
+      raise NoSuchFile(source)
     current_revision = tree.last_revision()
     revno, rev_id = old_revision.in_branch(tree.branch)
     if rev_id != tree.branch.last_revision():

=== modified file 'tests/__init__.py'
--- a/tests/__init__.py	2007-06-23 00:10:29 +0000
+++ b/tests/__init__.py	2007-06-23 10:31:23 +0000
@@ -71,6 +71,16 @@
     shutil.rmtree('package-0.2')
   return _make_upstream_tarball
 
+def make_new_upstream_tarball_bare(tarball):
+  def _make_upstream_tarball():
+    tar = tarfile.open(tarball, 'w')
+    try:
+      tar.add('package-0.2')
+    finally:
+      tar.close()
+    shutil.rmtree('package-0.2')
+  return _make_upstream_tarball
+
 tarball_functions = [('dir', make_new_upstream_dir, '../package-0.2'),
                      ('.tar.gz', make_new_upstream_tarball,
                       '../package-0.2.tar.gz'),
@@ -78,6 +88,8 @@
                       '../package-0.2.tar.bz2'),
                      ('.zip', make_new_upstream_tarball_zip,
                       '../package-0.2.zip'),
+                     ('.tar', make_new_upstream_tarball_bare,
+                      '../package-0.2.tar'),
                      ]
 
 
@@ -97,6 +109,26 @@
     return result
 
 
+class RepackTarballAdaptor(object):
+
+  def adapt(self, test):
+    result = TestSuite()
+    for (name, function, source) in tarball_functions:
+      # XXX: Zip files are horrible, but work out how to repack them.
+      if name == '.zip':
+        continue
+      new_test = deepcopy(test)
+      source = os.path.basename(source)
+      new_test.build_tarball = function(source)
+      new_test.old_tarball = source
+      def make_new_id():
+        new_id = '%s(%s)' % (test.id(), name)
+        return lambda: new_id
+      new_test.id = make_new_id()
+      result.addTest(new_test)
+    return result
+
+
 def test_suite():
     loader = TestUtil.TestLoader()
     suite = TestSuite()
@@ -117,6 +149,8 @@
 
     adapt_modules(['%s.test_merge_upstream' % __name__],
                   MergeUpstreamAdaptor(), loader, suite)
+    adapt_modules(['%s.test_repack_tarball' % __name__],
+                  RepackTarballAdaptor(), loader, suite)
 
     return suite
 

=== modified file 'tests/test_builder.py'
--- a/tests/test_builder.py	2007-06-20 23:33:32 +0000
+++ b/tests/test_builder.py	2007-06-23 10:31:23 +0000
@@ -1,4 +1,4 @@
-#    test_util.py -- Testsuite for builddeb builder.py
+#    test_builder.py -- Testsuite for builddeb builder.py
 #    Copyright (C) 2007 James Westby <jw+debian at jameswestby.net>
 #    
 #    This file is part of bzr-builddeb.

=== modified file 'tests/test_merge_upstream.py'
--- a/tests/test_merge_upstream.py	2007-06-22 22:52:45 +0000
+++ b/tests/test_merge_upstream.py	2007-06-23 10:31:23 +0000
@@ -23,6 +23,7 @@
 
 from bzrlib.errors import (BzrCommandError,
                            InvalidRevisionSpec,
+                           NoSuchFile,
                            )
 from bzrlib.revisionspec import RevisionSpec
 from bzrlib.tests import TestCaseWithTransport
@@ -101,7 +102,7 @@
     self.make_first_upstream_commit()
     old_upstream_revision = self.wt.branch.last_revision()
     self.make_first_debian_commit()
-    self.assertRaises(BzrCommandError, merge_upstream, self.wt, 'source',
+    self.assertRaises(NoSuchFile, merge_upstream, self.wt, 'source',
                       make_revspec(old_upstream_revision))
 
   def test_merge_upstream_handles_invalid_revision(self):



More information about the Pkg-bazaar-commits mailing list