1
# Copyright (C) 2005 Canonical Ltd
1
# Copyright (C) 2005, 2006, 2008, 2009, 2010 Canonical Ltd
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
"""Export a Tree to a non-versioned directory.
17
"""Export a bzrlib.tree.Tree to a new or empty directory."""
24
23
from bzrlib import errors, osutils
25
24
from bzrlib.export import _export_iter_entries
30
29
from bzrlib.trace import mutter
33
def dir_exporter(tree, dest, root, subdir, filtered=False):
32
def dir_exporter(tree, dest, root, subdir, filtered=False,
33
per_file_timestamps=False):
34
34
"""Export this tree to a new directory.
36
`dest` should not exist, and will be created holding the
37
contents of this tree.
39
TODO: To handle subdirectories we need to create the
36
`dest` should either not exist or should be empty. If it does not exist it
37
will be created holding the contents of this tree.
42
39
:note: If the export fails, the destination directory will be
43
left in a half-assed state.
40
left in an incompletely exported state: export is not transactional.
45
42
mutter('export version %r', tree)
52
49
raise errors.BzrError("Can't export tree to non-empty directory.")
52
# Iterate everything, building up the files we will want to export, and
53
# creating the directories and symlinks that we need.
54
# This tracks (file_id, (destination_path, executable))
55
# This matches the api that tree.iter_files_bytes() wants
56
# Note in the case of revision trees, this does trigger a double inventory
57
# lookup, hopefully it isn't too expensive.
55
59
for dp, ie in _export_iter_entries(tree, subdir):
56
60
fullpath = osutils.pathjoin(dest, dp)
57
61
if ie.kind == "file":
59
chunks = tree.get_file_lines(ie.file_id)
60
filters = tree._content_filter_stack(dp)
61
context = ContentFilterContext(dp, tree, ie)
62
contents = filtered_output_bytes(chunks, filters, context)
63
content = ''.join(contents)
64
fileobj = StringIO.StringIO(content)
66
fileobj = tree.get_file(ie.file_id)
67
osutils.pumpfile(fileobj, file(fullpath, 'wb'))
68
if tree.is_executable(ie.file_id):
69
os.chmod(fullpath, 0755)
62
to_fetch.append((ie.file_id, (dp, tree.is_executable(ie.file_id))))
70
63
elif ie.kind == "directory":
72
65
elif ie.kind == "symlink":
81
74
raise errors.BzrError("don't know how to export {%s} of kind %r" %
82
75
(ie.file_id, ie.kind))
76
# The data returned here can be in any order, but we've already created all
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)
85
fullpath = osutils.pathjoin(dest, relpath)
86
# We set the mode and let the umask sort out the file info
90
out = os.fdopen(os.open(fullpath, flags, mode), 'wb')
92
out.writelines(chunks)
95
if per_file_timestamps:
96
mtime = tree.get_file_mtime(tree.path2id(relpath), relpath)
99
os.utime(fullpath, (mtime, mtime))