~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tree.py

Merging Alexander's zip export patch

Show diffs side-by-side

added added

removed removed

Lines of Context:
360
360
    def tbz_exporter(tree, dest, root):
361
361
        tar_exporter(tree, dest, root, compression='bz2')
362
362
    exporters['tbz2'] = tbz_exporter
 
363
 
 
364
 
 
365
def zip_exporter(tree, dest, root):
 
366
    """ Export this tree to a new zip file.
 
367
 
 
368
    `dest` will be created holding the contents of this tree; if it
 
369
    already exists, it will be overwritten".
 
370
    """
 
371
    import time
 
372
    import zipfile
 
373
 
 
374
    now = time.localtime()[:6]
 
375
    mutter('export version %r', tree)
 
376
 
 
377
    compression = zipfile.ZIP_DEFLATED
 
378
    zipf = zipfile.ZipFile(dest, "w", compression)
 
379
 
 
380
    inv = tree.inventory
 
381
 
 
382
    try:
 
383
        for dp, ie in inv.iter_entries():
 
384
 
 
385
            file_id = ie.file_id
 
386
            mutter("  export {%s} kind %s to %s", file_id, ie.kind, dest)
 
387
 
 
388
            if ie.kind == "file": 
 
389
                zinfo = zipfile.ZipInfo(
 
390
                            filename=str(os.path.join(root, dp)),
 
391
                            date_time=now)
 
392
                zinfo.compress_type = compression
 
393
                zipf.writestr(zinfo, tree.get_file_text(file_id))
 
394
            elif ie.kind == "directory":
 
395
                zinfo = zipfile.ZipInfo(
 
396
                            filename=str(os.path.join(root, dp)+os.sep),
 
397
                            date_time=now)
 
398
                zinfo.compress_type = compression
 
399
                zipf.writestr(zinfo,'')
 
400
            elif ie.kind == "symlink":
 
401
                zinfo = zipfile.ZipInfo(
 
402
                            filename=str(os.path.join(root, dp+".lnk")),
 
403
                            date_time=now)
 
404
                zinfo.compress_type = compression
 
405
                zipf.writestr(zinfo, ie.symlink_target)
 
406
 
 
407
        zipf.close()
 
408
 
 
409
    except UnicodeEncodeError:
 
410
        zipf.close()
 
411
        os.remove(dest)
 
412
        from bzrlib.errors import BzrError
 
413
        raise BzrError("Can't export non-ascii filenames to zip")
 
414
#/def zip_exporter(tree, dest, root):
 
415
 
 
416
exporters["zip"] = zip_exporter