280
######################################################################
283
def dir_exporter(tree, dest, root):
284
"""Export this tree to a new directory.
286
`dest` should not exist, and will be created holding the
287
contents of this tree.
289
TODO: To handle subdirectories we need to create the
292
:note: If the export fails, the destination directory will be
293
left in a half-assed state.
297
mutter('export version %r', tree)
299
for dp, ie in inv.iter_entries():
300
ie.put_on_disk(dest, dp, tree)
302
exporters['dir'] = dir_exporter
309
def get_root_name(dest):
310
"""Get just the root name for a tarball.
312
>>> get_root_name('mytar.tar')
314
>>> get_root_name('mytar.tar.bz2')
316
>>> get_root_name('tar.tar.tar.tgz')
318
>>> get_root_name('bzr-0.0.5.tar.gz')
320
>>> get_root_name('a/long/path/mytar.tgz')
322
>>> get_root_name('../parent/../dir/other.tbz2')
325
endings = ['.tar', '.tar.gz', '.tgz', '.tar.bz2', '.tbz2']
326
dest = os.path.basename(dest)
328
if dest.endswith(end):
329
return dest[:-len(end)]
331
def tar_exporter(tree, dest, root, compression=None):
332
"""Export this tree to a new tar file.
334
`dest` will be created holding the contents of this tree; if it
335
already exists, it will be clobbered, like with "tar -c".
337
from time import time
339
compression = str(compression or '')
341
root = get_root_name(dest)
343
ball = tarfile.open(dest, 'w:' + compression)
344
except tarfile.CompressionError, e:
345
raise BzrError(str(e))
346
mutter('export version %r', tree)
348
for dp, ie in inv.iter_entries():
349
mutter(" export {%s} kind %s to %s", ie.file_id, ie.kind, dest)
350
item, fileobj = ie.get_tar_item(root, dp, now, tree)
351
ball.addfile(item, fileobj)
354
exporters['tar'] = tar_exporter
356
def tgz_exporter(tree, dest, root):
357
tar_exporter(tree, dest, root, compression='gz')
358
exporters['tgz'] = tgz_exporter
360
def tbz_exporter(tree, dest, root):
361
tar_exporter(tree, dest, root, compression='bz2')
362
exporters['tbz2'] = tbz_exporter
365
def zip_exporter(tree, dest, root):
366
""" Export this tree to a new zip file.
368
`dest` will be created holding the contents of this tree; if it
369
already exists, it will be overwritten".
374
now = time.localtime()[:6]
375
mutter('export version %r', tree)
377
compression = zipfile.ZIP_DEFLATED
378
zipf = zipfile.ZipFile(dest, "w", compression)
383
for dp, ie in inv.iter_entries():
386
mutter(" export {%s} kind %s to %s", file_id, ie.kind, dest)
388
if ie.kind == "file":
389
zinfo = zipfile.ZipInfo(
390
filename=str(os.path.join(root, dp)),
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),
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")),
404
zinfo.compress_type = compression
405
zipf.writestr(zinfo, ie.symlink_target)
409
except UnicodeEncodeError:
412
from bzrlib.errors import BzrError
413
raise BzrError("Can't export non-ascii filenames to zip")
414
#/def zip_exporter(tree, dest, root):
416
exporters["zip"] = zip_exporter