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 trees to tarballs, non-controlled directories, zipfiles, etc.
33
def export(tree, dest, format=None, root=None, subdir=None,
34
per_file_timestamps=False, fileobj=None):
35
"""Export the given Tree to the specific destination.
37
:param tree: A Tree (such as RevisionTree) to export
38
:param dest: The destination where the files,etc should be put
39
:param format: The format (dir, zip, etc), if None, it will check the
40
extension on dest, looking for a match
41
:param root: The root location inside the format.
42
It is common practise to have zipfiles and tarballs
43
extract into a subdirectory, rather than into the
44
current working directory.
45
If root is None, the default root will be
46
selected as the destination without its
48
:param subdir: A starting directory within the tree. None means to export
49
the entire tree, and anything else should specify the relative path to
50
a directory to start exporting from.
51
:param per_file_timestamps: Whether to use the timestamp stored in the
52
tree rather than now(). This will do a revision lookup
53
for every file so will be significantly slower.
54
:param fileobj: Optional file object to use
56
if format is None and dest is not None:
57
format = guess_format(dest)
59
# Most of the exporters will just have to call
60
# this function anyway, so why not do it for them
62
root = get_root_name(dest)
64
if not per_file_timestamps:
65
force_mtime = time.time()
66
if getattr(tree, '_repository', None):
68
force_mtime = tree._repository.get_revision(
69
tree.get_revision_id()).timestamp
70
except errors.NoSuchRevision:
75
trace.mutter('export version %r', tree)
78
# TODO(jelmer): If the tree is remote (e.g. HPSS, Git Remote),
79
# then we should stream a tar file and unpack that on the fly.
80
with tree.lock_read():
81
for unused in dir_exporter_generator(tree, dest, root, subdir,
86
with tree.lock_read():
87
chunks = tree.archive(format, dest, root=root,
88
subdir=subdir, force_mtime=force_mtime)
91
getattr(sys.stdout, 'buffer', sys.stdout).write(chunk)
92
elif fileobj is not None:
96
with open(dest, 'wb') as f:
101
def guess_format(filename, default='dir'):
102
"""Guess the export format based on a file name.
104
:param filename: Filename to guess from
105
:param default: Default format to fall back to
108
format = archive.format_registry.get_format_from_filename(filename)
114
def get_root_name(dest):
115
"""Get just the root name for an export.
118
global _exporter_extensions
120
# Exporting to -/foo doesn't make sense so use relative paths.
122
dest = os.path.basename(dest)
123
for ext in archive.format_registry.extensions:
124
if dest.endswith(ext):
125
return dest[:-len(ext)]
129
def _export_iter_entries(tree, subdir, skip_special=True):
130
"""Iter the entries for tree suitable for exporting.
132
:param tree: A tree object.
133
:param subdir: None or the path of an entry to start exporting from.
134
:param skip_special: Whether to skip .bzr files.
135
:return: iterator over tuples with final path, tree path and inventory
136
entry for each entry to export
140
if subdir is not None:
141
subdir = subdir.rstrip('/')
142
entries = tree.iter_entries_by_dir()
143
for path, entry in entries:
147
# The .bzr* namespace is reserved for "magic" files like
148
# .bzrignore and .bzrrules - do not export these
149
if skip_special and path.startswith(".bzr"):
152
if entry.kind == 'directory':
154
final_path = entry.name
155
elif subdir is not None:
156
if path.startswith(subdir + '/'):
157
final_path = path[len(subdir) + 1:]
162
if not tree.has_filename(path):
165
yield final_path, path, entry
168
def dir_exporter_generator(tree, dest, root, subdir=None,
169
force_mtime=None, fileobj=None):
170
"""Return a generator that exports this tree to a new directory.
172
`dest` should either not exist or should be empty. If it does not exist it
173
will be created holding the contents of this tree.
175
:note: If the export fails, the destination directory will be
176
left in an incompletely exported state: export is not transactional.
181
if e.errno == errno.EEXIST:
182
# check if directory empty
183
if os.listdir(dest) != []:
184
raise errors.BzrError(
185
"Can't export tree to non-empty directory.")
188
# Iterate everything, building up the files we will want to export, and
189
# creating the directories and symlinks that we need.
190
# This tracks (file_id, (destination_path, executable))
191
# This matches the api that tree.iter_files_bytes() wants
192
# Note in the case of revision trees, this does trigger a double inventory
193
# lookup, hopefully it isn't too expensive.
195
for dp, tp, ie in _export_iter_entries(tree, subdir):
196
file_id = getattr(ie, 'file_id', None)
197
fullpath = osutils.pathjoin(dest, dp)
198
if ie.kind == "file":
199
to_fetch.append((tp, (dp, tp, file_id)))
200
elif ie.kind in ("directory", "tree-reference"):
202
elif ie.kind == "symlink":
204
symlink_target = tree.get_symlink_target(tp)
205
os.symlink(symlink_target, fullpath)
207
raise errors.BzrError(
208
"Failed to create symlink %r -> %r, error: %s"
209
% (fullpath, symlink_target, e))
211
raise errors.BzrError("don't know how to export {%s} of kind %r" %
215
# The data returned here can be in any order, but we've already created all
217
flags = os.O_CREAT | os.O_TRUNC | os.O_WRONLY | getattr(os, 'O_BINARY', 0)
218
for (relpath, treepath, file_id), chunks in tree.iter_files_bytes(to_fetch):
219
fullpath = osutils.pathjoin(dest, relpath)
220
# We set the mode and let the umask sort out the file info
222
if tree.is_executable(treepath):
224
with os.fdopen(os.open(fullpath, flags, mode), 'wb') as out:
225
out.writelines(chunks)
226
if force_mtime is not None:
229
mtime = tree.get_file_mtime(treepath)
230
os.utime(fullpath, (mtime, mtime))