[Pkg-bazaar-commits] r192 ./bzr-builddeb/trunk: * Don't complain when repacking the tarball if the target exists, but is the

James Westby jw+debian at jameswestby.net
Wed Oct 31 21:07:48 UTC 2007


------------------------------------------------------------
revno: 192
committer: James Westby <jw+debian at jameswestby.net>
branch nick: trunk
timestamp: Wed 2007-10-31 21:07:48 +0000
message:
  * Don't complain when repacking the tarball if the target exists, but is the
    same as the source. Only .tar.gz can be considered identical.
modified:
  __init__.py
  debian/changelog
  repack_tarball.py
  tests/test_repack_tarball.py
  tests/test_repack_tarball_extra.py
-------------- next part --------------
=== modified file '__init__.py'
--- a/__init__.py	2007-10-31 19:17:23 +0000
+++ b/__init__.py	2007-10-31 21:07:48 +0000
@@ -26,7 +26,11 @@
 import subprocess
 
 from bzrlib.commands import Command, register_command
-from bzrlib.errors import BzrCommandError, NoWorkingTree, NotBranchError
+from bzrlib.errors import (BzrCommandError,
+                           NoWorkingTree,
+                           NotBranchError,
+                           FileExists,
+                           )
 from bzrlib.option import Option
 from bzrlib.trace import info, warning
 from bzrlib.transport import get_transport
@@ -362,7 +366,13 @@
     orig_dir = os.path.join(tree.basedir, orig_dir)
 
     dest_name = tarball_name(package, version)
-    repack_tarball(path, dest_name, target_dir=orig_dir)
+    try:
+      repack_tarball(path, dest_name, target_dir=orig_dir)
+    except FileExists:
+      raise BzrCommandError("The target file %s already exists, and is either "
+                            "different to the new upstream tarball, or they "
+                            "are of different formats. Either delete the target "
+                            "file, or use it as the argument to import.")
     filename = os.path.join(orig_dir, dest_name)
 
     try:

=== modified file 'debian/changelog'
--- a/debian/changelog	2007-10-31 19:58:28 +0000
+++ b/debian/changelog	2007-10-31 21:07:48 +0000
@@ -11,8 +11,10 @@
   * Improve the error message when the upstream tag is not found to actually
     give the name of the expected tag.
   * Allow the last upstream not to be on the mainline during merge-upstream.
+  * Don't complain when repacking the tarball if the target exists, but is the
+    same as the source. Only .tar.gz can be considered identical.
 
- -- James Westby <jw+debian at jameswestby.net>  Wed, 31 Oct 2007 19:57:29 +0000
+ -- James Westby <jw+debian at jameswestby.net>  Wed, 31 Oct 2007 21:06:58 +0000
 
 bzr-builddeb (0.91) unstable; urgency=low
 

=== modified file 'repack_tarball.py'
--- a/repack_tarball.py	2007-10-29 18:00:52 +0000
+++ b/repack_tarball.py	2007-10-31 21:07:48 +0000
@@ -22,6 +22,7 @@
 import os
 import tarfile
 import bz2
+import sha
 
 from bzrlib.errors import (
                            FileExists,
@@ -56,8 +57,8 @@
   :warning: .zip files are currently unsupported.
   :throws NoSuchFile: if orig_name doesn't exist.
   :throws NotADirectory: if target_dir exists and is not a directory.
-  :throws FileExists: if the target filename (after considering target_dir
-                      exists.
+  :throws FileExists: if the target filename (after considering target_dir)
+                      exists, and is not identical to the source.
   :throes BzrCommandError: if the source isn't supported for repacking.
   """
   if target_dir is not None:
@@ -68,7 +69,21 @@
         raise NotADirectory(target_dir)
     new_name = os.path.join(target_dir, new_name)
   if os.path.exists(new_name):
-    raise FileExists(new_name)
+    if not orig_name.endswith('.tar.gz'):
+      raise FileExists(new_name)
+    f = open(orig_name)
+    try:
+      orig_sha = sha.sha(f.read()).hexdigest()
+    finally:
+      f.close()
+    f = open(new_name)
+    try:
+      new_sha = sha.sha(f.read()).hexdigest()
+    finally:
+      f.close()
+    if orig_sha != new_sha:
+      raise FileExists(new_name)
+    return
   if os.path.isdir(orig_name):
     tar = tarfile.open(new_name, 'w:gz')
     try:
@@ -111,3 +126,4 @@
       trans_file.close()
 
 # vim: ts=2 sts=2 sw=2
+

=== modified file 'tests/test_repack_tarball.py'
--- a/tests/test_repack_tarball.py	2007-10-29 18:00:52 +0000
+++ b/tests/test_repack_tarball.py	2007-10-31 21:07:48 +0000
@@ -19,6 +19,7 @@
 #
 
 import os
+import shutil
 import tarfile
 
 from bzrlib.errors import (NoSuchFile,

=== modified file 'tests/test_repack_tarball_extra.py'
--- a/tests/test_repack_tarball_extra.py	2007-10-29 18:00:52 +0000
+++ b/tests/test_repack_tarball_extra.py	2007-10-31 21:07:48 +0000
@@ -18,12 +18,52 @@
 #    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 #
 
+import os
+import shutil
+import tarfile
+
 from repack_tarball import repack_tarball
 
-from bzrlib.errors import BzrCommandError
+from bzrlib.errors import BzrCommandError, FileExists
 from bzrlib.tests import TestCaseInTempDir
 
 
+def touch(filename):
+  f = open(filename, 'w')
+  try:
+    f.write(' ')
+  finally:
+    f.close()
+
+
+def create_basedir(basedir, files):
+  """Create the basedir that the source can be built from"""
+  os.mkdir(basedir)
+  for filename in [os.path.join(basedir, file) for file in files]:
+    if filename.endswith('/'):
+      os.mkdir(filename)
+    else:
+      touch(filename)
+
+
+def make_new_upstream_tarball(tarball):
+  tar = tarfile.open(tarball, 'w:gz')
+  try:
+    tar.add('package-0.2')
+  finally:
+    tar.close()
+  shutil.rmtree('package-0.2')
+
+
+def make_new_upstream_tarball_bz2(tarball):
+  tar = tarfile.open(tarball, 'w:bz2')
+  try:
+    tar.add('package-0.2')
+  finally:
+    tar.close()
+  shutil.rmtree('package-0.2')
+
+
 class TestRepackTarballExtra(TestCaseInTempDir):
 
   def test_repack_tarball_errors_unkown_format(self):
@@ -34,4 +74,42 @@
     self.assertRaises(BzrCommandError, repack_tarball, old_tarball,
                       'package_0.2.orig.tar.gz')
 
+  def test_conditional_repack_tarball_different(self):
+    tarball_name = 'package-0.2.tar.gz'
+    create_basedir('package-0.2/', files=['README'])
+    make_new_upstream_tarball(tarball_name)
+    target_dir = 'target'
+    os.mkdir(target_dir)
+    create_basedir('package-0.2/', files=['README', 'NEWS'])
+    make_new_upstream_tarball(os.path.join(target_dir, tarball_name))
+    self.assertRaises(FileExists, repack_tarball, tarball_name,
+        tarball_name, target_dir=target_dir)
+    self.failUnlessExists(tarball_name)
+    self.failUnlessExists(os.path.join(target_dir, tarball_name))
+
+  def test_conditional_repack_tarball_same(self):
+    tarball_name = 'package-0.2.tar.gz'
+    create_basedir('package-0.2/', files=['README'])
+    make_new_upstream_tarball(tarball_name)
+    target_dir = 'target'
+    os.mkdir(target_dir)
+    shutil.copy(tarball_name, target_dir)
+    repack_tarball(tarball_name, tarball_name, target_dir=target_dir)
+    self.failUnlessExists(tarball_name)
+    self.failUnlessExists(os.path.join(target_dir, tarball_name))
+
+  def test_conditional_repack_different_formats(self):
+    tarball_name = 'package-0.2.tar.gz'
+    bz2_tarball_name = 'package-0.2.tar.bz2'
+    create_basedir('package-0.2/', files=['README'])
+    make_new_upstream_tarball_bz2(bz2_tarball_name)
+    target_dir = 'target'
+    os.mkdir(target_dir)
+    create_basedir('package-0.2/', files=['README'])
+    make_new_upstream_tarball(os.path.join(target_dir, tarball_name))
+    self.assertRaises(FileExists, repack_tarball, bz2_tarball_name,
+        tarball_name, target_dir=target_dir)
+    self.failUnlessExists(bz2_tarball_name)
+    self.failUnlessExists(os.path.join(target_dir, tarball_name))
+
 # vim: ts=2 sts=2 sw=2



More information about the Pkg-bazaar-commits mailing list