def export_tarball_generator(tree, ball, root, subdir=None, force_mtime=None): """Export tree contents to a tarball. :returns: A generator that will repeatedly produce None as each file is emitted. The entire generator must be consumed to complete writing the file. :param tree: Tree to export :param ball: Tarball to export to; it will be closed when writing is complete. :param subdir: Sub directory to export :param force_mtime: Option mtime to force, instead of using tree timestamps. """ try: for final_path, tree_path, entry in _export_iter_entries(tree, subdir): (item, fileobj) = prepare_tarball_item(tree, root, final_path, tree_path, entry, force_mtime) ball.addfile(item, fileobj) yield finally: ball.close()
def export_tarball_generator(tree, ball, root, subdir=None, force_mtime=None): """Export tree contents to a tarball. :returns: A generator that will repeatedly produce None as each file is emitted. The entire generator must be consumed to complete writing the file. :param tree: Tree to export :param ball: Tarball to export to; it will be closed when writing is complete. :param subdir: Sub directory to export :param force_mtime: Option mtime to force, instead of using tree timestamps. """ try: for final_path, tree_path, entry in _export_iter_entries(tree, subdir): (item, fileobj) = prepare_tarball_item( tree, root, final_path, tree_path, entry, force_mtime) ball.addfile(item, fileobj) yield finally: ball.close()
except OSError, e: if e.errno == errno.EEXIST: # check if directory empty if os.listdir(dest) != []: raise errors.BzrError( "Can't export tree to non-empty directory.") else: raise # Iterate everything, building up the files we will want to export, and # creating the directories and symlinks that we need. # This tracks (file_id, (destination_path, executable)) # This matches the api that tree.iter_files_bytes() wants # Note in the case of revision trees, this does trigger a double inventory # lookup, hopefully it isn't too expensive. to_fetch = [] for dp, tp, ie in _export_iter_entries(tree, subdir): fullpath = osutils.pathjoin(dest, dp) if ie.kind == "file": to_fetch.append((ie.file_id, (dp, tp, ie.file_id))) elif ie.kind == "directory": os.mkdir(fullpath) elif ie.kind == "symlink": try: symlink_target = tree.get_symlink_target(ie.file_id, tp) os.symlink(symlink_target, fullpath) except OSError, e: raise errors.BzrError( "Failed to create symlink %r -> %r, error: %s" % (fullpath, symlink_target, e)) else: raise errors.BzrError("don't know how to export {%s} of kind %r" %
def zip_exporter_generator(tree, dest, root, subdir=None, force_mtime=None, fileobj=None): """ 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". """ compression = zipfile.ZIP_DEFLATED if fileobj is not None: dest = fileobj elif dest == "-": dest = sys.stdout zipf = zipfile.ZipFile(dest, "w", compression) try: for dp, tp, ie in _export_iter_entries(tree, subdir): file_id = ie.file_id mutter(" export {%s} kind %s to %s", file_id, ie.kind, dest) # zipfile.ZipFile switches all paths to forward # slashes anyway, so just stick with that. if force_mtime is not None: mtime = force_mtime else: mtime = tree.get_file_mtime(ie.file_id, tp) date_time = time.localtime(mtime)[:6] filename = osutils.pathjoin(root, dp).encode('utf8') if ie.kind == "file": zinfo = zipfile.ZipInfo( filename=filename, date_time=date_time) zinfo.compress_type = compression zinfo.external_attr = _FILE_ATTR content = tree.get_file_text(file_id, tp) zipf.writestr(zinfo, content) elif ie.kind == "directory": # Directories must contain a trailing slash, to indicate # to the zip routine that they are really directories and # not just empty files. zinfo = zipfile.ZipInfo( filename=filename + '/', date_time=date_time) zinfo.compress_type = compression zinfo.external_attr = _DIR_ATTR zipf.writestr(zinfo, '') elif ie.kind == "symlink": zinfo = zipfile.ZipInfo( filename=(filename + '.lnk'), date_time=date_time) zinfo.compress_type = compression zinfo.external_attr = _FILE_ATTR zipf.writestr(zinfo, tree.get_symlink_target(file_id, tp)) yield zipf.close() except UnicodeEncodeError: zipf.close() os.remove(dest) from bzrlib.errors import BzrError raise BzrError("Can't export non-ascii filenames to zip")
os.mkdir(dest) except OSError, e: if e.errno == errno.EEXIST: # check if directory empty if os.listdir(dest) != []: raise errors.BzrError("Can't export tree to non-empty directory.") else: raise # Iterate everything, building up the files we will want to export, and # creating the directories and symlinks that we need. # This tracks (file_id, (destination_path, executable)) # This matches the api that tree.iter_files_bytes() wants # Note in the case of revision trees, this does trigger a double inventory # lookup, hopefully it isn't too expensive. to_fetch = [] for dp, tp, ie in _export_iter_entries(tree, subdir): fullpath = osutils.pathjoin(dest, dp) if ie.kind == "file": to_fetch.append((ie.file_id, (dp, tp, ie.file_id))) elif ie.kind == "directory": os.mkdir(fullpath) elif ie.kind == "symlink": try: symlink_target = tree.get_symlink_target(ie.file_id, tp) os.symlink(symlink_target, fullpath) except OSError, e: raise errors.BzrError("Failed to create symlink %r -> %r, error: %s" % (fullpath, symlink_target, e)) else: raise errors.BzrError("don't know how to export {%s} of kind %r" % (ie.file_id, ie.kind)) yield
def zip_exporter_generator(tree, dest, root, subdir=None, force_mtime=None, fileobj=None): """ 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". """ compression = zipfile.ZIP_DEFLATED if fileobj is not None: dest = fileobj elif dest == "-": dest = sys.stdout zipf = zipfile.ZipFile(dest, "w", compression) try: for dp, tp, ie in _export_iter_entries(tree, subdir): file_id = ie.file_id mutter(" export {%s} kind %s to %s", file_id, ie.kind, dest) # zipfile.ZipFile switches all paths to forward # slashes anyway, so just stick with that. if force_mtime is not None: mtime = force_mtime else: mtime = tree.get_file_mtime(ie.file_id, tp) date_time = time.localtime(mtime)[:6] filename = osutils.pathjoin(root, dp).encode('utf8') if ie.kind == "file": zinfo = zipfile.ZipInfo(filename=filename, date_time=date_time) zinfo.compress_type = compression zinfo.external_attr = _FILE_ATTR content = tree.get_file_text(file_id, tp) zipf.writestr(zinfo, content) elif ie.kind == "directory": # Directories must contain a trailing slash, to indicate # to the zip routine that they are really directories and # not just empty files. zinfo = zipfile.ZipInfo(filename=filename + '/', date_time=date_time) zinfo.compress_type = compression zinfo.external_attr = _DIR_ATTR zipf.writestr(zinfo, '') elif ie.kind == "symlink": zinfo = zipfile.ZipInfo(filename=(filename + '.lnk'), date_time=date_time) zinfo.compress_type = compression zinfo.external_attr = _FILE_ATTR zipf.writestr(zinfo, tree.get_symlink_target(file_id, tp)) yield zipf.close() except UnicodeEncodeError: zipf.close() os.remove(dest) from bzrlib.errors import BzrError raise BzrError("Can't export non-ascii filenames to zip")