20
20
from __future__ import absolute_import
32
# Maps format name => export function
34
# Maps filename extensions => export format name
35
_exporter_extensions = {}
38
def register_exporter(format, extensions, func, override=False):
39
"""Register an exporter.
41
:param format: This is the name of the format, such as 'tgz' or 'zip'
42
:param extensions: Extensions which should be used in the case that a
43
format was not explicitly specified.
44
:type extensions: List
45
:param func: The function. It will be called with (tree, dest, root)
46
:param override: Whether to override an object which already exists.
47
Frequently plugins will want to provide functionality
48
until it shows up in mainline, so the default is False.
50
global _exporters, _exporter_extensions
52
if (format not in _exporters) or override:
53
_exporters[format] = func
55
for ext in extensions:
56
if (ext not in _exporter_extensions) or override:
57
_exporter_extensions[ext] = format
60
def register_lazy_exporter(scheme, extensions, module, funcname):
61
"""Register lazy-loaded exporter function.
63
When requesting a specific type of export, load the respective path.
65
def _loader(tree, dest, root, subdir, force_mtime, fileobj):
66
func = pyutils.get_named_object(module, funcname)
67
return func(tree, dest, root, subdir, force_mtime=force_mtime,
70
register_exporter(scheme, extensions, _loader)
73
def get_export_generator(tree, dest=None, format=None, root=None, subdir=None,
74
filtered=False, per_file_timestamps=False,
76
"""Returns a generator that exports the given tree.
78
The generator is expected to yield None while exporting the tree while the
79
actual export is written to ``fileobj``.
81
:param tree: A Tree (such as RevisionTree) to export
83
:param dest: The destination where the files, etc should be put
85
:param format: The format (dir, zip, etc), if None, it will check the
86
extension on dest, looking for a match
88
:param root: The root location inside the format. It is common practise to
89
have zipfiles and tarballs extract into a subdirectory, rather than
90
into the current working directory. If root is None, the default root
91
will be selected as the destination without its extension.
93
:param subdir: A starting directory within the tree. None means to export
94
the entire tree, and anything else should specify the relative path to
95
a directory to start exporting from.
97
:param filtered: If True, content filtering is applied to the exported
98
files. Deprecated in favour of passing a ContentFilterTree
101
:param per_file_timestamps: Whether to use the timestamp stored in the tree
102
rather than now(). This will do a revision lookup for every file so
103
will be significantly slower.
105
:param fileobj: Optional file object to use
107
global _exporters, _exporter_extensions
109
if format is None and dest is not None:
110
for ext in _exporter_extensions:
111
if dest.endswith(ext):
112
format = _exporter_extensions[ext]
115
# Most of the exporters will just have to call
116
# this function anyway, so why not do it for them
118
root = get_root_name(dest)
120
if format not in _exporters:
121
raise errors.NoSuchExportFormat(format)
123
if not per_file_timestamps:
124
force_mtime = time.time()
128
trace.mutter('export version %r', tree)
131
from breezy.filter_tree import ContentFilterTree
133
"passing filtered=True to export is deprecated in bzr 2.4",
135
tree = ContentFilterTree(tree, tree._content_filter_stack)
136
# We don't want things re-filtered by the specific exporter.
139
with tree.lock_read():
140
for _ in _exporters[format](
141
tree, dest, root, subdir,
142
force_mtime=force_mtime, fileobj=fileobj):
146
def export(tree, dest, format=None, root=None, subdir=None, filtered=False,
36
def export(tree, dest, format=None, root=None, subdir=None,
147
37
per_file_timestamps=False, fileobj=None):
148
38
"""Export the given Tree to the specific destination.
161
51
:param subdir: A starting directory within the tree. None means to export
162
52
the entire tree, and anything else should specify the relative path to
163
53
a directory to start exporting from.
164
:param filtered: If True, content filtering is applied to the
165
files exported. Deprecated in favor of passing an ContentFilterTree.
166
54
:param per_file_timestamps: Whether to use the timestamp stored in the
167
55
tree rather than now(). This will do a revision lookup
168
56
for every file so will be significantly slower.
169
57
:param fileobj: Optional file object to use
171
for _ in get_export_generator(tree, dest, format, root, subdir, filtered,
172
per_file_timestamps, fileobj):
59
if format is None and dest is not None:
60
format = guess_format(dest)
62
# Most of the exporters will just have to call
63
# this function anyway, so why not do it for them
65
root = get_root_name(dest)
67
if not per_file_timestamps:
68
force_mtime = time.time()
72
trace.mutter('export version %r', tree)
75
# TODO(jelmer): If the tree is remote (e.g. HPSS, Git Remote),
76
# then we should stream a tar file and unpack that on the fly.
77
with tree.lock_read():
78
for unused in dir_exporter_generator(tree, dest, root, subdir,
83
with tree.lock_read():
84
chunks = archive.create_archive(format, tree, dest, root, subdir,
88
sys.stdout.write(chunk)
89
elif fileobj is not None:
93
with open(dest, 'wb') as f:
98
def guess_format(filename, default='dir'):
99
"""Guess the export format based on a file name.
101
:param filename: Filename to guess from
102
:param default: Default format to fall back to
105
format = archive.format_registry.get_format_from_filename(filename)
176
111
def get_root_name(dest):
227
162
yield final_path, path, entry
230
register_lazy_exporter(None, [], 'breezy.export.dir_exporter',
231
'dir_exporter_generator')
232
register_lazy_exporter('dir', [], 'breezy.export.dir_exporter',
233
'dir_exporter_generator')
234
register_lazy_exporter('tar', ['.tar'], 'breezy.export.tar_exporter',
235
'plain_tar_exporter_generator')
236
register_lazy_exporter('tgz', ['.tar.gz', '.tgz'],
237
'breezy.export.tar_exporter',
238
'tgz_exporter_generator')
239
register_lazy_exporter('tbz2', ['.tar.bz2', '.tbz2'],
240
'breezy.export.tar_exporter', 'tbz_exporter_generator')
241
register_lazy_exporter('tlzma', ['.tar.lzma'], 'breezy.export.tar_exporter',
242
'tar_lzma_exporter_generator')
243
register_lazy_exporter('txz', ['.tar.xz'], 'breezy.export.tar_exporter',
244
'tar_xz_exporter_generator')
245
register_lazy_exporter('zip', ['.zip'], 'breezy.export.zip_exporter',
246
'zip_exporter_generator')
165
def dir_exporter_generator(tree, dest, root, subdir=None,
166
force_mtime=None, fileobj=None):
167
"""Return a generator that exports this tree to a new directory.
169
`dest` should either not exist or should be empty. If it does not exist it
170
will be created holding the contents of this tree.
172
:note: If the export fails, the destination directory will be
173
left in an incompletely exported state: export is not transactional.
178
if e.errno == errno.EEXIST:
179
# check if directory empty
180
if os.listdir(dest) != []:
181
raise errors.BzrError(
182
"Can't export tree to non-empty directory.")
185
# Iterate everything, building up the files we will want to export, and
186
# creating the directories and symlinks that we need.
187
# This tracks (file_id, (destination_path, executable))
188
# This matches the api that tree.iter_files_bytes() wants
189
# Note in the case of revision trees, this does trigger a double inventory
190
# lookup, hopefully it isn't too expensive.
192
for dp, tp, ie in _export_iter_entries(tree, subdir):
193
fullpath = osutils.pathjoin(dest, dp)
194
if ie.kind == "file":
195
to_fetch.append((tp, (dp, tp, ie.file_id)))
196
elif ie.kind in ("directory", "tree-reference"):
198
elif ie.kind == "symlink":
200
symlink_target = tree.get_symlink_target(tp, ie.file_id)
201
os.symlink(symlink_target, fullpath)
203
raise errors.BzrError(
204
"Failed to create symlink %r -> %r, error: %s"
205
% (fullpath, symlink_target, e))
207
raise errors.BzrError("don't know how to export {%s} of kind %r" %
211
# The data returned here can be in any order, but we've already created all
213
flags = os.O_CREAT | os.O_TRUNC | os.O_WRONLY | getattr(os, 'O_BINARY', 0)
214
for (relpath, treepath, file_id), chunks in tree.iter_files_bytes(to_fetch):
215
fullpath = osutils.pathjoin(dest, relpath)
216
# We set the mode and let the umask sort out the file info
218
if tree.is_executable(treepath, file_id):
220
with os.fdopen(os.open(fullpath, flags, mode), 'wb') as out:
221
out.writelines(chunks)
222
if force_mtime is not None:
225
mtime = tree.get_file_mtime(treepath, file_id)
226
os.utime(fullpath, (mtime, mtime))