13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
17
"""Export a Tree to a non-versioned directory.
23
24
from bzrlib import errors, osutils
24
25
from bzrlib.export import _export_iter_entries
26
from bzrlib.filters import (
28
filtered_output_bytes,
25
30
from bzrlib.trace import mutter
28
def dir_exporter(tree, dest, root, subdir):
33
def dir_exporter(tree, dest, root, subdir, filtered=False):
29
34
"""Export this tree to a new directory.
31
36
`dest` should not exist, and will be created holding the
38
43
left in a half-assed state.
40
45
mutter('export version %r', tree)
49
if e.errno == errno.EEXIST:
50
# check if directory empty
51
if os.listdir(dest) != []:
52
raise errors.BzrError("Can't export tree to non-empty directory.")
55
# Iterate everything, building up the files we will want to export, and
56
# creating the directories and symlinks that we need.
57
# This tracks (file_id, (destination_path, executable))
58
# This matches the api that tree.iter_files_bytes() wants
59
# Note in the case of revision trees, this does trigger a double inventory
60
# lookup, hopefully it isn't too expensive.
42
62
for dp, ie in _export_iter_entries(tree, subdir):
43
63
fullpath = osutils.pathjoin(dest, dp)
44
64
if ie.kind == "file":
45
fileobj = tree.get_file(ie.file_id)
46
osutils.pumpfile(fileobj, file(fullpath, 'wb'))
47
if tree.is_executable(ie.file_id):
48
os.chmod(fullpath, 0755)
65
to_fetch.append((ie.file_id, (dp, tree.is_executable(ie.file_id))))
49
66
elif ie.kind == "directory":
51
68
elif ie.kind == "symlink":
53
os.symlink(ie.symlink_target, fullpath)
70
symlink_target = tree.get_symlink_target(ie.file_id)
71
os.symlink(symlink_target, fullpath)
55
73
raise errors.BzrError(
56
74
"Failed to create symlink %r -> %r, error: %s"
57
% (fullpath, self.symlink_target, e))
75
% (fullpath, symlink_target, e))
59
77
raise errors.BzrError("don't know how to export {%s} of kind %r" %
60
78
(ie.file_id, ie.kind))
79
# The data returned here can be in any order, but we've already created all
81
flags = os.O_CREAT | os.O_TRUNC | os.O_WRONLY | getattr(os, 'O_BINARY', 0)
82
for (relpath, executable), chunks in tree.iter_files_bytes(to_fetch):
84
filters = tree._content_filter_stack(relpath)
85
context = ContentFilterContext(relpath, tree, ie)
86
chunks = filtered_output_bytes(chunks, filters, context)
87
fullpath = osutils.pathjoin(dest, relpath)
88
# We set the mode and let the umask sort out the file info
92
out = os.fdopen(os.open(fullpath, flags, mode), 'wb')
94
out.writelines(chunks)