17
17
"""Export a bzrlib.tree.Tree to a new or empty directory."""
19
from __future__ import absolute_import
23
24
from bzrlib import errors, osutils
24
25
from bzrlib.export import _export_iter_entries
25
from bzrlib.filters import (
27
filtered_output_bytes,
29
from bzrlib.trace import mutter
32
def dir_exporter(tree, dest, root, subdir, filtered=False,
33
per_file_timestamps=False):
34
"""Export this tree to a new directory.
28
def dir_exporter_generator(tree, dest, root, subdir=None,
29
force_mtime=None, fileobj=None):
30
"""Return a generator that exports this tree to a new directory.
36
32
`dest` should either not exist or should be empty. If it does not exist it
37
33
will be created holding the contents of this tree.
35
:param fileobj: Is not used in this exporter
39
37
:note: If the export fails, the destination directory will be
40
38
left in an incompletely exported state: export is not transactional.
42
mutter('export version %r', tree)
46
43
if e.errno == errno.EEXIST:
47
44
# check if directory empty
48
45
if os.listdir(dest) != []:
49
raise errors.BzrError("Can't export tree to non-empty directory.")
46
raise errors.BzrError(
47
"Can't export tree to non-empty directory.")
52
50
# Iterate everything, building up the files we will want to export, and
56
54
# Note in the case of revision trees, this does trigger a double inventory
57
55
# lookup, hopefully it isn't too expensive.
59
for dp, ie in _export_iter_entries(tree, subdir):
57
for dp, tp, ie in _export_iter_entries(tree, subdir):
60
58
fullpath = osutils.pathjoin(dest, dp)
61
59
if ie.kind == "file":
62
to_fetch.append((ie.file_id, (dp, tree.is_executable(ie.file_id))))
60
to_fetch.append((ie.file_id, (dp, tp, ie.file_id)))
63
61
elif ie.kind == "directory":
65
63
elif ie.kind == "symlink":
67
symlink_target = tree.get_symlink_target(ie.file_id)
65
symlink_target = tree.get_symlink_target(ie.file_id, tp)
68
66
os.symlink(symlink_target, fullpath)
70
68
raise errors.BzrError(
71
69
"Failed to create symlink %r -> %r, error: %s"
72
70
% (fullpath, symlink_target, e))
74
72
raise errors.BzrError("don't know how to export {%s} of kind %r" %
75
73
(ie.file_id, ie.kind))
76
76
# The data returned here can be in any order, but we've already created all
78
78
flags = os.O_CREAT | os.O_TRUNC | os.O_WRONLY | getattr(os, 'O_BINARY', 0)
80
for (relpath, executable), chunks in tree.iter_files_bytes(to_fetch):
82
filters = tree._content_filter_stack(relpath)
83
context = ContentFilterContext(relpath, tree, ie)
84
chunks = filtered_output_bytes(chunks, filters, context)
79
for (relpath, treepath, file_id), chunks in tree.iter_files_bytes(to_fetch):
85
80
fullpath = osutils.pathjoin(dest, relpath)
86
81
# We set the mode and let the umask sort out the file info
83
if tree.is_executable(file_id, treepath):
90
85
out = os.fdopen(os.open(fullpath, flags, mode), 'wb')
92
87
out.writelines(chunks)
95
if per_file_timestamps:
96
mtime = tree.get_file_mtime(tree.path2id(relpath), relpath)
90
if force_mtime is not None:
93
mtime = tree.get_file_mtime(file_id, treepath)
99
94
os.utime(fullpath, (mtime, mtime))