14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
"""Export functionality, which can take a Tree and create a different representation.
19
Such as non-controlled directories, tarfiles, zipfiles, etc.
17
"""Export trees to tarballs, non-controlled directories, zipfiles, etc.
20
from __future__ import absolute_import
23
import bzrlib.errors as errors
25
# Maps format name => export function
27
# Maps filename extensions => export format name
28
_exporter_extensions = {}
30
def register_exporter(format, extensions, func, override=False):
31
"""Register an exporter.
33
:param format: This is the name of the format, such as 'tgz' or 'zip'
34
:param extensions: Extensions which should be used in the case that a
35
format was not explicitly specified.
36
:type extensions: List
37
:param func: The function. It will be called with (tree, dest, root)
38
:param override: Whether to override an object which already exists.
39
Frequently plugins will want to provide functionality
40
until it shows up in mainline, so the default is False.
42
global _exporters, _exporter_extensions
44
if (format not in _exporters) or override:
45
_exporters[format] = func
47
for ext in extensions:
48
if (ext not in _exporter_extensions) or override:
49
_exporter_extensions[ext] = format
52
def register_lazy_exporter(scheme, extensions, module, funcname):
53
"""Register lazy-loaded exporter function.
55
When requesting a specific type of export, load the respective path.
57
def _loader(tree, dest, root, subdir, filtered, per_file_timestamps):
58
mod = __import__(module, globals(), locals(), [funcname])
59
func = getattr(mod, funcname)
60
return func(tree, dest, root, subdir, filtered=filtered,
61
per_file_timestamps=per_file_timestamps)
62
register_exporter(scheme, extensions, _loader)
65
def export(tree, dest, format=None, root=None, subdir=None, filtered=False,
66
per_file_timestamps=False):
35
def export(tree, dest, format=None, root=None, subdir=None,
36
per_file_timestamps=False, fileobj=None):
67
37
"""Export the given Tree to the specific destination.
69
39
:param tree: A Tree (such as RevisionTree) to export
80
50
:param subdir: A starting directory within the tree. None means to export
81
51
the entire tree, and anything else should specify the relative path to
82
52
a directory to start exporting from.
83
:param filtered: If True, content filtering is applied to the
85
:param per_file_timestamps: Whether to use the timestamp stored in the
86
tree rather than now(). This will do a revision lookup
53
:param per_file_timestamps: Whether to use the timestamp stored in the
54
tree rather than now(). This will do a revision lookup
87
55
for every file so will be significantly slower.
56
:param fileobj: Optional file object to use
89
global _exporters, _exporter_extensions
92
for ext in _exporter_extensions:
93
if dest.endswith(ext):
94
format = _exporter_extensions[ext]
58
if format is None and dest is not None:
59
format = guess_format(dest)
97
61
# Most of the exporters will just have to call
98
62
# this function anyway, so why not do it for them
100
64
root = get_root_name(dest)
102
if format not in _exporters:
103
raise errors.NoSuchExportFormat(format)
106
return _exporters[format](tree, dest, root, subdir, filtered=filtered,
107
per_file_timestamps=per_file_timestamps)
66
if not per_file_timestamps:
67
force_mtime = time.time()
68
if getattr(tree, '_repository', None):
70
force_mtime = tree._repository.get_revision(
71
tree.get_revision_id()).timestamp
72
except errors.NoSuchRevision:
77
trace.mutter('export version %r', tree)
80
# TODO(jelmer): If the tree is remote (e.g. HPSS, Git Remote),
81
# then we should stream a tar file and unpack that on the fly.
82
with tree.lock_read():
83
for unused in dir_exporter_generator(tree, dest, root, subdir,
88
with tree.lock_read():
89
chunks = tree.archive(format, dest, root=root,
90
subdir=subdir, force_mtime=force_mtime)
93
getattr(sys.stdout, 'buffer', sys.stdout).write(chunk)
94
elif fileobj is not None:
98
with open(dest, 'wb') as f:
103
def guess_format(filename, default='dir'):
104
"""Guess the export format based on a file name.
106
:param filename: Filename to guess from
107
:param default: Default format to fall back to
110
format = archive.format_registry.get_format_from_filename(filename)
112
116
def get_root_name(dest):
113
117
"""Get just the root name for an export.
115
>>> get_root_name('../mytest.tar')
117
>>> get_root_name('mytar.tar')
119
>>> get_root_name('mytar.tar.bz2')
121
>>> get_root_name('tar.tar.tar.tgz')
123
>>> get_root_name('bzr-0.0.5.tar.gz')
125
>>> get_root_name('bzr-0.0.5.zip')
127
>>> get_root_name('bzr-0.0.5')
129
>>> get_root_name('a/long/path/mytar.tgz')
131
>>> get_root_name('../parent/../dir/other.tbz2')
134
120
global _exporter_extensions
122
# Exporting to -/foo doesn't make sense so use relative paths.
135
124
dest = os.path.basename(dest)
136
for ext in _exporter_extensions:
125
for ext in archive.format_registry.extensions:
137
126
if dest.endswith(ext):
138
127
return dest[:-len(ext)]
142
def _export_iter_entries(tree, subdir):
131
def _export_iter_entries(tree, subdir, skip_special=True):
143
132
"""Iter the entries for tree suitable for exporting.
145
134
:param tree: A tree object.
146
135
:param subdir: None or the path of an entry to start exporting from.
136
:param skip_special: Whether to skip .bzr files.
137
:return: iterator over tuples with final path, tree path and inventory
138
entry for each entry to export
152
subdir_id = inv.path2id(subdir)
153
if subdir_id is not None:
154
subdir_object = inv[subdir_id]
155
# XXX: subdir is path not an id, so NoSuchId isn't proper error
157
raise errors.NoSuchId(tree, subdir)
158
if subdir_object is not None and subdir_object.kind != 'directory':
159
yield subdir_object.name, subdir_object
162
entries = inv.iter_entries(subdir_object)
164
entries.next() # skip root
165
for entry in entries:
142
if subdir is not None:
143
subdir = subdir.rstrip('/')
144
entries = tree.iter_entries_by_dir()
145
for path, entry in entries:
166
149
# The .bzr* namespace is reserved for "magic" files like
167
150
# .bzrignore and .bzrrules - do not export these
168
if entry[0].startswith(".bzr"):
171
if not tree.has_filename(entry[0]):
174
if not tree.has_filename(os.path.join(subdir, entry[0])):
179
register_lazy_exporter(None, [], 'bzrlib.export.dir_exporter', 'dir_exporter')
180
register_lazy_exporter('dir', [], 'bzrlib.export.dir_exporter', 'dir_exporter')
181
register_lazy_exporter('tar', ['.tar'], 'bzrlib.export.tar_exporter', 'tar_exporter')
182
register_lazy_exporter('tgz', ['.tar.gz', '.tgz'], 'bzrlib.export.tar_exporter', 'tgz_exporter')
183
register_lazy_exporter('tbz2', ['.tar.bz2', '.tbz2'], 'bzrlib.export.tar_exporter', 'tbz_exporter')
184
register_lazy_exporter('zip', ['.zip'], 'bzrlib.export.zip_exporter', 'zip_exporter')
151
if skip_special and path.startswith(".bzr"):
154
if entry.kind == 'directory':
156
final_path = entry.name
157
elif subdir is not None:
158
if path.startswith(subdir + '/'):
159
final_path = path[len(subdir) + 1:]
164
if not tree.has_filename(path):
167
yield final_path, path, entry
170
def dir_exporter_generator(tree, dest, root, subdir=None,
171
force_mtime=None, fileobj=None):
172
"""Return a generator that exports this tree to a new directory.
174
`dest` should either not exist or should be empty. If it does not exist it
175
will be created holding the contents of this tree.
177
:note: If the export fails, the destination directory will be
178
left in an incompletely exported state: export is not transactional.
183
if e.errno == errno.EEXIST:
184
# check if directory empty
185
if os.listdir(dest) != []:
186
raise errors.BzrError(
187
"Can't export tree to non-empty directory.")
190
# Iterate everything, building up the files we will want to export, and
191
# creating the directories and symlinks that we need.
192
# This tracks (file_id, (destination_path, executable))
193
# This matches the api that tree.iter_files_bytes() wants
194
# Note in the case of revision trees, this does trigger a double inventory
195
# lookup, hopefully it isn't too expensive.
197
for dp, tp, ie in _export_iter_entries(tree, subdir):
198
file_id = getattr(ie, 'file_id', None)
199
fullpath = osutils.pathjoin(dest, dp)
200
if ie.kind == "file":
201
to_fetch.append((tp, (dp, tp, file_id)))
202
elif ie.kind in ("directory", "tree-reference"):
204
elif ie.kind == "symlink":
206
symlink_target = tree.get_symlink_target(tp)
207
os.symlink(symlink_target, fullpath)
209
raise errors.BzrError(
210
"Failed to create symlink %r -> %r, error: %s"
211
% (fullpath, symlink_target, e))
213
raise errors.BzrError("don't know how to export {%s} of kind %r" %
217
# The data returned here can be in any order, but we've already created all
219
flags = os.O_CREAT | os.O_TRUNC | os.O_WRONLY | getattr(os, 'O_BINARY', 0)
220
for (relpath, treepath, file_id), chunks in tree.iter_files_bytes(to_fetch):
221
fullpath = osutils.pathjoin(dest, relpath)
222
# We set the mode and let the umask sort out the file info
224
if tree.is_executable(treepath):
226
with os.fdopen(os.open(fullpath, flags, mode), 'wb') as out:
227
out.writelines(chunks)
228
if force_mtime is not None:
231
mtime = tree.get_file_mtime(treepath)
232
os.utime(fullpath, (mtime, mtime))