1
# Copyright (C) 2005-2010 Canonical Ltd
1
# Copyright (C) 2005 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
13
13
# You should have received a copy of the GNU General Public License
14
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
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
17
"""Export functionality, which can take a Tree and create a different representation.
19
19
Such as non-controlled directories, tarfiles, zipfiles, etc.
22
from bzrlib.trace import mutter
23
24
import bzrlib.errors as errors
31
32
"""Register an exporter.
33
34
: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
:param extensions: Extensions which should be used in the case that a
35
36
format was not explicitly specified.
36
37
:type extensions: List
37
38
:param func: The function. It will be called with (tree, dest, root)
42
43
global _exporters, _exporter_extensions
44
if (format not in _exporters) or override:
45
if not _exporters.has_key(format) or override:
45
46
_exporters[format] = func
47
48
for ext in extensions:
48
if (ext not in _exporter_extensions) or override:
49
if not _exporter_extensions.has_key(ext) or override:
49
50
_exporter_extensions[ext] = format
55
56
When requesting a specific type of export, load the respective path.
57
def _loader(tree, dest, root, subdir, filtered, per_file_timestamps):
58
def _loader(tree, dest, root):
58
59
mod = __import__(module, globals(), locals(), [funcname])
59
60
func = getattr(mod, funcname)
60
return func(tree, dest, root, subdir, filtered=filtered,
61
per_file_timestamps=per_file_timestamps)
61
return func(tree, dest, root)
62
62
register_exporter(scheme, extensions, _loader)
65
def export(tree, dest, format=None, root=None, subdir=None, filtered=False,
66
per_file_timestamps=False):
65
def export(tree, dest, format=None, root=None):
67
66
"""Export the given Tree to the specific destination.
69
68
:param tree: A Tree (such as RevisionTree) to export
71
70
:param format: The format (dir, zip, etc), if None, it will check the
72
71
extension on dest, looking for a match
73
72
:param root: The root location inside the format.
74
It is common practise to have zipfiles and tarballs
73
It is common practise to have zipfiles and tarballs
75
74
extract into a subdirectory, rather than into the
76
75
current working directory.
77
76
If root is None, the default root will be
78
77
selected as the destination without its
80
:param subdir: A starting directory within the tree. None means to export
81
the entire tree, and anything else should specify the relative path to
82
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
87
for every file so will be significantly slower.
89
80
global _exporters, _exporter_extensions
100
91
root = get_root_name(dest)
102
if format not in _exporters:
93
if not _exporters.has_key(format):
103
94
raise errors.NoSuchExportFormat(format)
106
return _exporters[format](tree, dest, root, subdir, filtered=filtered,
107
per_file_timestamps=per_file_timestamps)
95
return _exporters[format](tree, dest, root)
112
98
def get_root_name(dest):
113
99
"""Get just the root name for an export.
115
>>> get_root_name('../mytest.tar')
117
101
>>> get_root_name('mytar.tar')
119
103
>>> get_root_name('mytar.tar.bz2')
142
def _export_iter_entries(tree, subdir):
143
"""Iter the entries for tree suitable for exporting.
145
:param tree: A tree object.
146
:param subdir: None or the path of an entry to start exporting from.
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:
166
# The .bzr* namespace is reserved for "magic" files like
167
# .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
126
register_lazy_exporter(None, [], 'bzrlib.export.dir_exporter', 'dir_exporter')
180
127
register_lazy_exporter('dir', [], 'bzrlib.export.dir_exporter', 'dir_exporter')
181
128
register_lazy_exporter('tar', ['.tar'], 'bzrlib.export.tar_exporter', 'tar_exporter')