[PATCH] Add zip format to export command

Goffredo Baroncelli kreijack at alice.it
Sat Nov 12 09:06:44 GMT 2005


Hi all,

the patch below add the zip format to the export command. 

This patch adds also two features which I need in the web
interface which I am developping:

- the additional parameter filter_func permits to select 
the files which will be archived in order to export only subdir. 

- the 'dest' parameter in the functions tar_exporter or
zip_exporter can be a file object or a stream ( as StringIO ).

If this patch will be added, I can reuse 
this code in my web interface in order to permit the export
of the repository in .tar.gz or .zip format.

Please apply

Goffredo
-----------------


Some examples:

$ ./bzr export /tmp/pippo3.tgz
$ ./bzr export /tmp/pippo3.zip
$ ./bzr export /tmp/pippo3

$ file /tmp/pippo3*
/tmp/pippo3:     directory
/tmp/pippo3.tgz: gzip compressed data, was "pippo3.tar", max compression
/tmp/pippo3.zip: Zip archive data, at least v2.0 to extract

$ find /tmp/pippo3 -type f | wc -l
256
$ tar tzf /tmp/pippo3.tgz | grep -v "/$" | wc -l
256
$ unzip -l /tmp/pippo3.zip | tail
     2156  11-09-05 23:47   pippo3/tools/convertfile.py
     2084  11-09-05 23:47   pippo3/tools/convertinv.py
     2868  11-09-05 23:47   pippo3/tools/history2revfiles.py
    15446  11-09-05 23:47   pippo3/tools/http_client.py
      320  11-09-05 23:47   pippo3/tools/trace-revisions
     2717  11-09-05 23:47   pippo3/tools/weavebench.py
      584  11-09-05 23:47   pippo3/tools/weavemerge.sh
    10186  11-09-05 23:47   pippo3/tutorial.txt
 --------                   -------
  1999510                   256 files


--------------------

=== modified file 'bzrlib/builtins.py'
--- bzrlib/builtins.py
+++ bzrlib/builtins.py
@@ -1119,8 +1119,11 @@
                 format = "tgz"
             elif ext in (".tar.bz2", ".tbz2"):
                 format = "tbz2"
+            elif ext in (".zip",):
+                format = "zip"
             else:
                 format = "dir"
+
         t.export(dest, format, root)



=== modified file 'bzrlib/tree.py'
--- bzrlib/tree.py
+++ bzrlib/tree.py
@@ -328,7 +328,7 @@
             if dest.endswith(end):
                 return dest[:-len(end)]

-    def tar_exporter(tree, dest, root, compression=None):
+    def tar_exporter(tree, dest, root, filter_func=None, compression=None):
         """Export this tree to a new tar file.

         `dest` will be created holding the contents of this tree; if it
@@ -340,12 +340,16 @@
         if root is None:
             root = get_root_name(dest)
         try:
-            ball = tarfile.open(dest, 'w:' + compression)
+            if isinstance(dest, basestring):
+                ball = tarfile.open(dest, 'w:' + compression)
+            else:
+                ball = tarfile.open("none", 'w:' + compression, dest)
         except tarfile.CompressionError, e:
             raise BzrError(str(e))
         mutter('export version %r' % tree)
         inv = tree.inventory
         for dp, ie in inv.iter_entries():
+            if filter_func and filter_func( dp,ie ): continue
             mutter("  export {%s} kind %s to %s" % (ie.file_id, ie.kind, dest))
             item, fileobj = ie.get_tar_item(root, dp, now, tree)
             ball.addfile(item, fileobj)
@@ -360,3 +364,32 @@
     def tbz_exporter(tree, dest, root):
         tar_exporter(tree, dest, root, compression='bz2')
     exporters['tbz2'] = tbz_exporter
+
+def zip_exporter( tree, dest, root, filter_func=None ):
+    """export this tree to a zip file"""
+
+    from time import time
+    import zipfile
+    now = time()
+
+    if root is None:
+        root = get_root_name(dest)
+
+    try:
+        ball = zipfile.ZipFile(dest, 'w', zipfile.ZIP_DEFLATED)
+    except zipfile.BadZipfile, e:
+        raise BzrError(str(e))
+
+    mutter('export version %r' % tree)
+
+    inv = tree.inventory
+    for dp, ie in inv.iter_entries():
+        if filter_func and filter_func( dp,ie ): continue
+        mutter("  export {%s} kind %s to %s" % (ie.file_id, ie.kind, dest))
+        # FIXME: we have to handle also directories and symlinks
+        if ie.kind <> "file": continue
+        item, fileobj = ie.get_tar_item(root, dp, now, tree)
+        ball.writestr(str(item.name), fileobj.read())
+    ball.close()
+
+exporters['zip'] = zip_exporter


-- 
gpg key@ keyserver.linux.it: Goffredo Baroncelli (ghigo) <kreijack AT inwind.it>
Key fingerprint = CE3C 7E01 6782 30A3 5B87  87C0 BB86 505C 6B2A CFF9
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : https://lists.ubuntu.com/archives/bazaar/attachments/20051112/ce147c3f/attachment.pgp 


More information about the bazaar mailing list