1
# Copyright (C) 2005 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
"""Export functionality, which can take a Tree and create a different representation.
19
Such as non-controlled directories, tarfiles, zipfiles, etc.
22
from bzrlib.trace import mutter
24
import bzrlib.errors as errors
26
# Maps format name => export function
28
# Maps filename extensions => export format name
29
_exporter_extensions = {}
31
def register_exporter(format, extensions, func, override=False):
32
"""Register an exporter.
34
:param format: This is the name of the format, such as 'tgz' or 'zip'
35
:param extensions: Extensions which should be used in the case that a
36
format was not explicitly specified.
37
:type extensions: List
38
:param func: The function. It will be called with (tree, dest, root)
39
:param override: Whether to override an object which already exists.
40
Frequently plugins will want to provide functionality
41
until it shows up in mainline, so the default is False.
43
global _exporters, _exporter_extensions
45
if (format not in _exporters) or override:
46
_exporters[format] = func
48
for ext in extensions:
49
if (ext not in _exporter_extensions) or override:
50
_exporter_extensions[ext] = format
53
def register_lazy_exporter(scheme, extensions, module, funcname):
54
"""Register lazy-loaded exporter function.
56
When requesting a specific type of export, load the respective path.
58
def _loader(tree, dest, root, subdir):
59
mod = __import__(module, globals(), locals(), [funcname])
60
func = getattr(mod, funcname)
61
return func(tree, dest, root, subdir)
62
register_exporter(scheme, extensions, _loader)
65
def export(tree, dest, format=None, root=None, subdir=None):
66
"""Export the given Tree to the specific destination.
68
:param tree: A Tree (such as RevisionTree) to export
69
:param dest: The destination where the files,etc should be put
70
:param format: The format (dir, zip, etc), if None, it will check the
71
extension on dest, looking for a match
72
:param root: The root location inside the format.
73
It is common practise to have zipfiles and tarballs
74
extract into a subdirectory, rather than into the
75
current working directory.
76
If root is None, the default root will be
77
selected as the destination without its
79
:param subdir: A starting directory within the tree. None means to export
80
the entire tree, and anything else should specify the relative path to
81
a directory to start exporting from.
83
global _exporters, _exporter_extensions
86
for ext in _exporter_extensions:
87
if dest.endswith(ext):
88
format = _exporter_extensions[ext]
91
# Most of the exporters will just have to call
92
# this function anyway, so why not do it for them
94
root = get_root_name(dest)
96
if format not in _exporters:
97
raise errors.NoSuchExportFormat(format)
100
return _exporters[format](tree, dest, root, subdir)
105
def get_root_name(dest):
106
"""Get just the root name for an export.
108
>>> get_root_name('../mytest.tar')
110
>>> get_root_name('mytar.tar')
112
>>> get_root_name('mytar.tar.bz2')
114
>>> get_root_name('tar.tar.tar.tgz')
116
>>> get_root_name('bzr-0.0.5.tar.gz')
118
>>> get_root_name('bzr-0.0.5.zip')
120
>>> get_root_name('bzr-0.0.5')
122
>>> get_root_name('a/long/path/mytar.tgz')
124
>>> get_root_name('../parent/../dir/other.tbz2')
127
global _exporter_extensions
128
dest = os.path.basename(dest)
129
for ext in _exporter_extensions:
130
if dest.endswith(ext):
131
return dest[:-len(ext)]
135
def _export_iter_entries(tree, subdir):
136
"""Iter the entries for tree suitable for exporting.
138
:param tree: A tree object.
139
:param subdir: None or the path of a directory to start exporting from.
145
subdir_id = inv.path2id(subdir)
146
entries = inv.iter_entries(subdir_id)
148
entries.next() # skip root
149
for entry in entries:
150
# The .bzr* namespace is reserved for "magic" files like
151
# .bzrignore and .bzrrules - do not export these
152
if entry[0].startswith(".bzr"):
157
register_lazy_exporter(None, [], 'bzrlib.export.dir_exporter', 'dir_exporter')
158
register_lazy_exporter('dir', [], 'bzrlib.export.dir_exporter', 'dir_exporter')
159
register_lazy_exporter('tar', ['.tar'], 'bzrlib.export.tar_exporter', 'tar_exporter')
160
register_lazy_exporter('tgz', ['.tar.gz', '.tgz'], 'bzrlib.export.tar_exporter', 'tgz_exporter')
161
register_lazy_exporter('tbz2', ['.tar.bz2', '.tbz2'], 'bzrlib.export.tar_exporter', 'tbz_exporter')
162
register_lazy_exporter('zip', ['.zip'], 'bzrlib.export.zip_exporter', 'zip_exporter')