~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/export/zip_exporter.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2011-06-07 17:21:50 UTC
  • mfrom: (5957.3.3 trunk)
  • Revision ID: pqm@pqm.ubuntu.com-20110607172150-b8tdisesgnjxk7ku
(vila) Exporting may now be done with a generator (Geoff)

Show diffs side-by-side

added added

removed removed

Lines of Context:
44
44
_DIR_ATTR = stat.S_IFDIR | ZIP_DIRECTORY_BIT | DIR_PERMISSIONS
45
45
 
46
46
 
47
 
def zip_exporter(tree, dest, root, subdir=None, filtered=False, force_mtime=None):
 
47
def zip_exporter_generator(tree, dest, root, subdir=None, filtered=False, 
 
48
                 force_mtime=None, fileobj=None):
48
49
    """ Export this tree to a new zip file.
49
50
 
50
51
    `dest` will be created holding the contents of this tree; if it
52
53
    """
53
54
 
54
55
    compression = zipfile.ZIP_DEFLATED
55
 
    if dest == "-":
 
56
    if fileobj is not None:
 
57
        dest = fileobj
 
58
    elif dest == "-":
56
59
        dest = sys.stdout
57
60
    zipf = zipfile.ZipFile(dest, "w", compression)
58
61
    try:
100
103
                zinfo.compress_type = compression
101
104
                zinfo.external_attr = _FILE_ATTR
102
105
                zipf.writestr(zinfo, tree.get_symlink_target(file_id))
 
106
            yield
103
107
 
104
108
        zipf.close()
105
109
 
108
112
        os.remove(dest)
109
113
        from bzrlib.errors import BzrError
110
114
        raise BzrError("Can't export non-ascii filenames to zip")
 
115
 
 
116
 
 
117
def zip_exporter(tree, dest, root, subdir=None, filtered=False,
 
118
                 force_mtime=None, fileobj=None):
 
119
    for _ in zip_exporter_generator(tree, dest, root, subdir, filtered,
 
120
                                    force_mtime, fileobj):
 
121
        pass