Rev 3615: Refactor exporters to remove obvious duplication to a helper function. in http://people.ubuntu.com/~robertc/baz2.0/export

Robert Collins robertc at robertcollins.net
Fri Aug 8 07:59:53 BST 2008


At http://people.ubuntu.com/~robertc/baz2.0/export

------------------------------------------------------------
revno: 3615
revision-id: robertc at robertcollins.net-20080808065948-io0c6pfo305ss5vj
parent: robertc at robertcollins.net-20080808052558-b3xkprmd19buq12s
committer: Robert Collins <robertc at robertcollins.net>
branch nick: export
timestamp: Fri 2008-08-08 16:59:48 +1000
message:
  Refactor exporters to remove obvious duplication to a helper function.
modified:
  bzrlib/export/__init__.py      __init__.py-20051114235828-1ba62cb4062304e6
  bzrlib/export/dir_exporter.py  dir_exporter.py-20051114235828-b51397f56bc7b117
  bzrlib/export/tar_exporter.py  tar_exporter.py-20051114235828-1f6349a2f090a5d0
  bzrlib/export/zip_exporter.py  zip_exporter.py-20051114235828-8f57f954fba6497e
=== modified file 'bzrlib/export/__init__.py'
--- a/bzrlib/export/__init__.py	2008-08-08 05:25:58 +0000
+++ b/bzrlib/export/__init__.py	2008-08-08 06:59:48 +0000
@@ -132,6 +132,28 @@
     return dest
 
 
+def _export_iter_entries(tree, subdir):
+    """Iter the entries for tree suitable for exporting.
+
+    :param tree: A tree object.
+    :param subdir: None or the path of a directory to start exporting from.
+    """
+    inv = tree.inventory
+    if subdir is None:
+        subdir_id = None
+    else:
+        subdir_id = inv.path2id(subdir)
+    entries = inv.iter_entries(subdir_id)
+    if subdir is None:
+        entries.next() # skip root
+    for entry in entries:
+        # The .bzr* namespace is reserved for "magic" files like
+        # .bzrignore and .bzrrules - do not export these
+        if entry[0].startswith(".bzr"):
+            continue
+        yield entry
+
+
 register_lazy_exporter(None, [], 'bzrlib.export.dir_exporter', 'dir_exporter')
 register_lazy_exporter('dir', [], 'bzrlib.export.dir_exporter', 'dir_exporter')
 register_lazy_exporter('tar', ['.tar'], 'bzrlib.export.tar_exporter', 'tar_exporter')

=== modified file 'bzrlib/export/dir_exporter.py'
--- a/bzrlib/export/dir_exporter.py	2008-08-08 05:25:58 +0000
+++ b/bzrlib/export/dir_exporter.py	2008-08-08 06:59:48 +0000
@@ -21,6 +21,7 @@
 import os
 
 from bzrlib import errors, osutils
+from bzrlib.export import _export_iter_entries
 from bzrlib.trace import mutter
 
 
@@ -36,22 +37,9 @@
     :note: If the export fails, the destination directory will be
            left in a half-assed state.
     """
+    mutter('export version %r', tree)
     os.mkdir(dest)
-    mutter('export version %r', tree)
-    inv = tree.inventory
-    if subdir is None:
-        subdir_id = None
-    else:
-        subdir_id = inv.path2id(subdir)
-    entries = inv.iter_entries(subdir_id)
-    if subdir is None:
-        entries.next() # skip root
-    for dp, ie in entries:
-        # The .bzr* namespace is reserved for "magic" files like
-        # .bzrignore and .bzrrules - do not export these
-        if dp.startswith(".bzr"):
-            continue
-        
+    for dp, ie in _export_iter_entries(tree, subdir):
         fullpath = osutils.pathjoin(dest, dp)
         if ie.kind == "file":
             fileobj = tree.get_file(ie.file_id)

=== modified file 'bzrlib/export/tar_exporter.py'
--- a/bzrlib/export/tar_exporter.py	2008-08-08 05:25:58 +0000
+++ b/bzrlib/export/tar_exporter.py	2008-08-08 06:59:48 +0000
@@ -23,6 +23,7 @@
 import time
 
 from bzrlib import errors, export, osutils
+from bzrlib.export import _export_iter_entries
 from bzrlib.trace import mutter
 
 
@@ -32,6 +33,7 @@
     `dest` will be created holding the contents of this tree; if it
     already exists, it will be clobbered, like with "tar -c".
     """
+    mutter('export version %r', tree)
     now = time.time()
     compression = str(compression or '')
     if dest == '-':
@@ -42,21 +44,7 @@
         if root is None:
             root = export.get_root_name(dest)
         ball = tarfile.open(dest, 'w:' + compression)
-    mutter('export version %r', tree)
-    inv = tree.inventory
-    if subdir is None:
-        subdir_id = None
-    else:
-        subdir_id = inv.path2id(subdir)
-    entries = inv.iter_entries(subdir_id)
-    if subdir is None:
-        entries.next() # skip root
-    for dp, ie in entries:
-        # The .bzr* namespace is reserved for "magic" files like
-        # .bzrignore and .bzrrules - do not export these
-        if dp.startswith(".bzr"):
-            continue
-
+    for dp, ie in _export_iter_entries(tree, subdir):
         filename = osutils.pathjoin(root, dp).encode('utf8')
         item = tarfile.TarInfo(filename)
         item.mtime = now

=== modified file 'bzrlib/export/zip_exporter.py'
--- a/bzrlib/export/zip_exporter.py	2008-08-08 05:25:58 +0000
+++ b/bzrlib/export/zip_exporter.py	2008-08-08 06:59:48 +0000
@@ -19,11 +19,13 @@
 
 import os
 import stat
+import time
 import zipfile
 
 from bzrlib import (
     osutils,
     )
+from bzrlib.export import _export_iter_entries
 from bzrlib.trace import mutter
 
 
@@ -41,30 +43,15 @@
     `dest` will be created holding the contents of this tree; if it
     already exists, it will be overwritten".
     """
-    import time
+    mutter('export version %r', tree)
 
     now = time.localtime()[:6]
-    mutter('export version %r', tree)
 
     compression = zipfile.ZIP_DEFLATED
     zipf = zipfile.ZipFile(dest, "w", compression)
 
-    inv = tree.inventory
-
     try:
-        if subdir is None:
-            subdir_id = None
-        else:
-            subdir_id = inv.path2id(subdir)
-        entries = inv.iter_entries(subdir_id)
-        if subdir is None:
-            entries.next() # skip root
-        for dp, ie in entries:
-            # The .bzr* namespace is reserved for "magic" files like
-            # .bzrignore and .bzrrules - do not export these
-            if dp.startswith(".bzr"):
-                continue
-
+        for dp, ie in _export_iter_entries(tree, subdir):
             file_id = ie.file_id
             mutter("  export {%s} kind %s to %s", file_id, ie.kind, dest)
 




More information about the bazaar-commits mailing list