1
# Copyright (C) 2005-2011 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 breezy.tree.Tree to a new or empty directory."""
19
from __future__ import absolute_import
24
from .. import errors, osutils
25
from ..export import _export_iter_entries
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.
32
`dest` should either not exist or should be empty. If it does not exist it
33
will be created holding the contents of this tree.
35
:param fileobj: Is not used in this exporter
37
:note: If the export fails, the destination directory will be
38
left in an incompletely exported state: export is not transactional.
43
if e.errno == errno.EEXIST:
44
# check if directory empty
45
if os.listdir(dest) != []:
46
raise errors.BzrError(
47
"Can't export tree to non-empty directory.")
50
# Iterate everything, building up the files we will want to export, and
51
# creating the directories and symlinks that we need.
52
# This tracks (file_id, (destination_path, executable))
53
# This matches the api that tree.iter_files_bytes() wants
54
# Note in the case of revision trees, this does trigger a double inventory
55
# lookup, hopefully it isn't too expensive.
57
for dp, tp, ie in _export_iter_entries(tree, subdir):
58
fullpath = osutils.pathjoin(dest, dp)
60
to_fetch.append((tp, (dp, tp, ie.file_id)))
61
elif ie.kind in ("directory", "tree-reference"):
63
elif ie.kind == "symlink":
65
symlink_target = tree.get_symlink_target(tp, ie.file_id)
66
os.symlink(symlink_target, fullpath)
68
raise errors.BzrError(
69
"Failed to create symlink %r -> %r, error: %s"
70
% (fullpath, symlink_target, e))
72
raise errors.BzrError("don't know how to export {%s} of kind %r" %
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)
79
for (relpath, treepath, file_id), chunks in tree.iter_files_bytes(to_fetch):
80
fullpath = osutils.pathjoin(dest, relpath)
81
# We set the mode and let the umask sort out the file info
83
if tree.is_executable(treepath, file_id):
85
with os.fdopen(os.open(fullpath, flags, mode), 'wb') as out:
86
out.writelines(chunks)
87
if force_mtime is not None:
90
mtime = tree.get_file_mtime(treepath, file_id)
91
os.utime(fullpath, (mtime, mtime))