[PATCH] Add zip format to export command
Goffredo Baroncelli
kreijack at alice.it
Sun Nov 13 00:34:23 GMT 2005
On Saturday 12 November 2005 17:14, you (Alexander Belchenko) wrote:
> Goffredo Baroncelli wrote:
> > the patch below add the zip format to the export command.
>
> It's funny. I'm already do similar job for my Windows version. But in
> the end I decide to move zip exporter in to plugin. You can find my
> plugin here:
>
> http://bzr.onembedding.com/bzr.win/plugins/index.htm
Good idea implementing the zip format exporter as plugin.
I read your code, and I think that it can be simplified removing the class
cmd_zip_export, and using the classic command bzr export
Moreover I added the support for empty directory, and symlink. The symlinks
are simulated creating a file with a .lnk extension which contains the
link [ but there will be some problems if you have a link and a file ending
in .lnk with the same name .... ]
ghigo at therra:~/bazaar/bzr.dev$ ./bzr plugins
[...]
/home/ghigo/bazaar/bzr.dev/bzrlib/plugins/zipexport
Zip exporter
ghigo at therra:~/bazaar/test$ ls -l empty-dir/
total 0
ghigo at therra:~/bazaar/test$ ls -l link-to-dir*
lrwxrwxrwx 1 ghigo ghigo 4 2005-10-31 18:47 link-to-dir -> dirb
ghigo at therra:~/bazaar/test$ ../bzr.dev/bzr export --format zip /tmp/test.zip
ghigo at therra:~/bazaar/test$ unzip -l /tmp/test.zip
Archive: /tmp/test.zip
Length Date Time Name
-------- ---- ---- ----
[...]
0 11-13-05 01:19 test/empty-dir/
[...]
4 11-13-05 01:19 test/link-to-dir.lnk
-------- -------
72 12 files
> For me the bad thing about zip format is no support for unicode names.
> I'm thinking about use additional command line options for specify
> filename encoding for non-ascii file names. It may be useful for some
> cases when user definitely know what file encodings is. Something like this:
>
> bzr zip-export --fe=cp866 dest.zip
> ^^^^ this is file name encoding option
>
> Another limitation -- no symlink or empty directory support (latter only
> limitation of python zipfile library) -- probably main reason why zip is
> not included into mainline bzr.dev.
>
The symlink is a more general problem: we need a way to convert a
symlink into a generic file for the OS which doesn't support symlink.
Think about a unix repository handled by a windows system... And also we
need a way to convert a file.lnk in a symlink...
The zip export is a more simple case.
Filename encoding is a mess; even tough you know which encoding is at the export
phase, you don't will know the encoding at the unzip phase...
> Alexander
Goffredo
--------------------------
# (c) Alexander Belchenko, 2005
# - 2005-11-13: Goffredo Baroncelli <kreijack AT inwind DOT it>
# add support for fake symlink and empty directory
"""\
Zip exporter
"""
import os
from bzrlib.errors import BzrError
from bzrlib.tree import exporters
def zip_exporter(tree, dest, root):
""" Export this tree to a new zip file.
`dest` will be created holding the contents of this tree; if it
already exists, it will be overwritten".
"""
import time
import zipfile
from bzrlib.trace import mutter
now = time.localtime()[:6]
mutter('export version %r' % tree)
compression = zipfile.ZIP_DEFLATED
zipf = zipfile.ZipFile(dest, "w", compression)
inv = tree.inventory
try:
for dp, ie in inv.iter_entries():
file_id = ie.file_id
mutter(" export {%s} kind %s to %s" % (file_id, ie.kind, dest))
if ie.kind == "file":
zinfo = zipfile.ZipInfo(
filename=str(os.path.join(root, dp)),
date_time=now)
zinfo.compress_type = compression
zipf.writestr(zinfo, tree.get_file_text(file_id))
elif ie.kind == "directory":
zinfo = zipfile.ZipInfo(
filename=str(os.path.join(root, dp)+os.sep),
date_time=now)
zinfo.compress_type = compression
zipf.writestr(zinfo,'')
elif ie.kind == "symlink":
zinfo = zipfile.ZipInfo(
filename=str(os.path.join(root, dp+".lnk")),
date_time=now)
zinfo.compress_type = compression
zipf.writestr(zinfo, ie.symlink_target)
zipf.close()
except UnicodeEncodeError:
zipf.close()
os.remove(dest)
raise BzrError("Can't export non-ascii filenames to zip")
exporters['zip'] = zip_exporter
--
gpg key@ keyserver.linux.it: Goffredo Baroncelli (ghigo) <kreijack @ 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/20051113/c1f6c9d8/attachment.pgp
More information about the bazaar
mailing list