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
52
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.
55
62
for dp, ie in _export_iter_entries(tree, subdir):
56
63
fullpath = osutils.pathjoin(dest, dp)
57
64
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)
65
to_fetch.append((ie.file_id, (dp, tree.is_executable(ie.file_id))))
70
66
elif ie.kind == "directory":
72
68
elif ie.kind == "symlink":
81
77
raise errors.BzrError("don't know how to export {%s} of kind %r" %
82
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)