1
# Copyright (C) 2005, 2006, 2008, 2009, 2010 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
"""Export a Tree to a non-versioned directory.
24
from bzrlib import errors, osutils
25
from bzrlib.export import _export_iter_entries
26
from bzrlib.filters import (
28
filtered_output_bytes,
30
from bzrlib.trace import mutter
33
def dir_exporter(tree, dest, root, subdir, filtered=False):
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
42
:note: If the export fails, the destination directory will be
43
left in a half-assed state.
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.
62
for dp, ie in _export_iter_entries(tree, subdir):
63
fullpath = osutils.pathjoin(dest, dp)
65
to_fetch.append((ie.file_id, (dp, tree.is_executable(ie.file_id))))
66
elif ie.kind == "directory":
68
elif ie.kind == "symlink":
70
symlink_target = tree.get_symlink_target(ie.file_id)
71
os.symlink(symlink_target, fullpath)
73
raise errors.BzrError(
74
"Failed to create symlink %r -> %r, error: %s"
75
% (fullpath, symlink_target, e))
77
raise errors.BzrError("don't know how to export {%s} of kind %r" %
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)