[Pkg-bazaar-commits] ./bzr/unstable r849: - Put files inside an exported tarball into a top-level directory rather than

Martin Pool mbp at sourcefrog.net
Fri Apr 10 08:13:40 UTC 2009


------------------------------------------------------------
revno: 849
committer: Martin Pool <mbp at sourcefrog.net>
timestamp: Thu 2005-07-07 12:07:03 +1000
message:
  - Put files inside an exported tarball into a top-level directory rather than 
    dumping them into the current directory.  
modified:
  NEWS
  bzrlib/commands.py
  bzrlib/tree.py
-------------- next part --------------
=== modified file 'NEWS'
--- a/NEWS	2005-07-04 05:27:11 +0000
+++ b/NEWS	2005-07-07 02:07:03 +0000
@@ -12,6 +12,11 @@
 
   CHANGES:
 
+    * When exporting to a tarball with ``bzr export --format tgz``, put 
+      everything under a top directory rather than dumping it into the
+      current directory.   This can be overridden with the ``--root`` 
+      option.  Patch from William Dod? and John Meinel.
+
     * New ``bzr upgrade`` command to upgrade the format of a branch,
       replacing ``bzr check --update``.
 

=== modified file 'bzrlib/commands.py'
--- a/bzrlib/commands.py	2005-07-06 05:32:32 +0000
+++ b/bzrlib/commands.py	2005-07-07 02:07:03 +0000
@@ -1042,18 +1042,20 @@
     If no revision is specified this exports the last committed revision.
 
     Format may be an "exporter" name, such as tar, tgz, tbz2.  If none is
-    given, exports to a directory (equivalent to --format=dir)."""
+    given, exports to a directory (equivalent to --format=dir).
+
+    Root may be the top directory for tar, tgz and tbz2 formats."""
     # TODO: list known exporters
     takes_args = ['dest']
-    takes_options = ['revision', 'format']
-    def run(self, dest, revision=None, format='dir'):
+    takes_options = ['revision', 'format', 'root']
+    def run(self, dest, revision=None, format='dir', root=None):
         b = find_branch('.')
         if revision == None:
             rh = b.revision_history()[-1]
         else:
             rh = b.lookup_revision(int(revision))
         t = b.revision_tree(rh)
-        t.export(dest, format)
+        t.export(dest, format, root)
 
 
 class cmd_cat(Command):
@@ -1354,6 +1356,7 @@
     'email':                  None,
     'update':                 None,
     'long':                   None,
+    'root':                   str,
     }
 
 SHORT_OPTIONS = {

=== modified file 'bzrlib/tree.py'
--- a/bzrlib/tree.py	2005-06-28 03:02:31 +0000
+++ b/bzrlib/tree.py	2005-07-07 02:07:03 +0000
@@ -18,6 +18,7 @@
 """
 
 from osutils import pumpfile, appendpath, fingerprint_file
+import os
 
 from bzrlib.trace import mutter, note
 from bzrlib.errors import BzrError
@@ -93,13 +94,14 @@
         pumpfile(self.get_file(fileid), sys.stdout)
         
         
-    def export(self, dest, format='dir'):
+    def export(self, dest, format='dir', root=None):
         """Export this tree."""
         try:
             exporter = exporters[format]
         except KeyError:
+            from bzrlib.errors import BzrCommandError
             raise BzrCommandError("export format %r not supported" % format)
-        exporter(self, dest)
+        exporter(self, dest, root)
 
 
 
@@ -223,7 +225,7 @@
 ######################################################################
 # export
 
-def dir_exporter(tree, dest):
+def dir_exporter(tree, dest, root):
     """Export this tree to a new directory.
 
     `dest` should not exist, and will be created holding the
@@ -256,7 +258,29 @@
 except ImportError:
     pass
 else:
-    def tar_exporter(tree, dest, compression=None):
+    def get_root_name(dest):
+        """Get just the root name for a tarball.
+
+        >>> get_root_name('mytar.tar')
+        'mytar'
+        >>> get_root_name('mytar.tar.bz2')
+        'mytar'
+        >>> get_root_name('tar.tar.tar.tgz')
+        'tar.tar.tar'
+        >>> get_root_name('bzr-0.0.5.tar.gz')
+        'bzr-0.0.5'
+        >>> get_root_name('a/long/path/mytar.tgz')
+        'mytar'
+        >>> get_root_name('../parent/../dir/other.tbz2')
+        'other'
+        """
+        endings = ['.tar', '.tar.gz', '.tgz', '.tar.bz2', '.tbz2']
+        dest = os.path.basename(dest)
+        for end in endings:
+            if dest.endswith(end):
+                return dest[:-len(end)]
+
+    def tar_exporter(tree, dest, root, compression=None):
         """Export this tree to a new tar file.
 
         `dest` will be created holding the contents of this tree; if it
@@ -265,6 +289,8 @@
         from time import time
         now = time()
         compression = str(compression or '')
+        if root is None:
+            root = get_root_name(dest)
         try:
             ball = tarfile.open(dest, 'w:' + compression)
         except tarfile.CompressionError, e:
@@ -273,7 +299,7 @@
         inv = tree.inventory
         for dp, ie in inv.iter_entries():
             mutter("  export {%s} kind %s to %s" % (ie.file_id, ie.kind, dest))
-            item = tarfile.TarInfo(dp)
+            item = tarfile.TarInfo(os.path.join(root, dp))
             # TODO: would be cool to actually set it to the timestamp of the
             # revision it was last changed
             item.mtime = now
@@ -296,12 +322,12 @@
         ball.close()
     exporters['tar'] = tar_exporter
 
-    def tgz_exporter(tree, dest):
-        tar_exporter(tree, dest, compression='gz')
+    def tgz_exporter(tree, dest, root):
+        tar_exporter(tree, dest, root, compression='gz')
     exporters['tgz'] = tgz_exporter
 
-    def tbz_exporter(tree, dest):
-        tar_exporter(tree, dest, compression='bz2')
+    def tbz_exporter(tree, dest, root):
+        tar_exporter(tree, dest, root, compression='bz2')
     exporters['tbz2'] = tbz_exporter
 
 



More information about the Pkg-bazaar-commits mailing list