~bzr-pqm/bzr/bzr.dev

1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
1
# Copyright (C) 2005 Canonical Ltd
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
2
#
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
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.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
7
#
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
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.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
12
#
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
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
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
16
17
"""Export functionality, which can take a Tree and create a different representation.
18
19
Such as non-controlled directories, tarfiles, zipfiles, etc.
20
"""
21
22
from bzrlib.trace import mutter
23
import os
24
import bzrlib.errors as errors
25
26
# Maps format name => export function
27
_exporters = {}
28
# Maps filename extensions => export format name
29
_exporter_extensions = {}
30
31
def register_exporter(format, extensions, func, override=False):
32
    """Register an exporter.
33
34
    :param format: This is the name of the format, such as 'tgz' or 'zip'
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
35
    :param extensions: Extensions which should be used in the case that a
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
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.
42
    """
43
    global _exporters, _exporter_extensions
44
1963.2.1 by Robey Pointer
remove usage of has_key()
45
    if (format not in _exporters) or override:
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
46
        _exporters[format] = func
47
48
    for ext in extensions:
1963.2.1 by Robey Pointer
remove usage of has_key()
49
        if (ext not in _exporter_extensions) or override:
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
50
            _exporter_extensions[ext] = format
51
52
53
def register_lazy_exporter(scheme, extensions, module, funcname):
54
    """Register lazy-loaded exporter function.
55
56
    When requesting a specific type of export, load the respective path.
57
    """
3368.2.41 by Ian Clatworthy
1st cut merge of bzr.dev r3907
58
    def _loader(tree, dest, root, subdir, filtered):
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
59
        mod = __import__(module, globals(), locals(), [funcname])
60
        func = getattr(mod, funcname)
3368.2.41 by Ian Clatworthy
1st cut merge of bzr.dev r3907
61
        return func(tree, dest, root, subdir, filtered=filtered)
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
62
    register_exporter(scheme, extensions, _loader)
63
64
3368.2.41 by Ian Clatworthy
1st cut merge of bzr.dev r3907
65
def export(tree, dest, format=None, root=None, subdir=None, filtered=False):
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
66
    """Export the given Tree to the specific destination.
67
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.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
73
                 It is common practise to have zipfiles and tarballs
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
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
78
                 extension.
3613.2.1 by Robert Collins
Teach export how to export a subdirectory. (Robert Collins)
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.
3368.2.32 by Ian Clatworthy
add --filters to export command
82
    :param filtered: If True, content filtering is applied to the
83
                     files exported.
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
84
    """
85
    global _exporters, _exporter_extensions
86
87
    if format is None:
88
        for ext in _exporter_extensions:
89
            if dest.endswith(ext):
90
                format = _exporter_extensions[ext]
91
                break
92
93
    # Most of the exporters will just have to call
94
    # this function anyway, so why not do it for them
95
    if root is None:
96
        root = get_root_name(dest)
97
1963.2.1 by Robey Pointer
remove usage of has_key()
98
    if format not in _exporters:
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
99
        raise errors.NoSuchExportFormat(format)
2947.2.1 by Robert Collins
(robertc) Fix export to lock the repository. (Robert Collins)
100
    tree.lock_read()
101
    try:
3368.2.41 by Ian Clatworthy
1st cut merge of bzr.dev r3907
102
        return _exporters[format](tree, dest, root, subdir, filtered=filtered)
2947.2.1 by Robert Collins
(robertc) Fix export to lock the repository. (Robert Collins)
103
    finally:
104
        tree.unlock()
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
105
106
107
def get_root_name(dest):
108
    """Get just the root name for an export.
109
2024.2.3 by John Arbash Meinel
Move out export tests from test_too_much, refactor
110
    >>> get_root_name('../mytest.tar')
111
    'mytest'
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
112
    >>> get_root_name('mytar.tar')
113
    'mytar'
114
    >>> get_root_name('mytar.tar.bz2')
115
    'mytar'
116
    >>> get_root_name('tar.tar.tar.tgz')
117
    'tar.tar.tar'
118
    >>> get_root_name('bzr-0.0.5.tar.gz')
119
    'bzr-0.0.5'
120
    >>> get_root_name('bzr-0.0.5.zip')
121
    'bzr-0.0.5'
122
    >>> get_root_name('bzr-0.0.5')
123
    'bzr-0.0.5'
124
    >>> get_root_name('a/long/path/mytar.tgz')
125
    'mytar'
126
    >>> get_root_name('../parent/../dir/other.tbz2')
127
    'other'
128
    """
129
    global _exporter_extensions
130
    dest = os.path.basename(dest)
131
    for ext in _exporter_extensions:
132
        if dest.endswith(ext):
133
            return dest[:-len(ext)]
1185.31.13 by John Arbash Meinel
Updated the test to also test zip exports. Fixed some small bugs exposed by test suite.
134
    return dest
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
135
136
3613.2.2 by Robert Collins
Refactor exporters to remove obvious duplication to a helper function.
137
def _export_iter_entries(tree, subdir):
138
    """Iter the entries for tree suitable for exporting.
139
140
    :param tree: A tree object.
141
    :param subdir: None or the path of a directory to start exporting from.
142
    """
143
    inv = tree.inventory
144
    if subdir is None:
145
        subdir_id = None
146
    else:
147
        subdir_id = inv.path2id(subdir)
148
    entries = inv.iter_entries(subdir_id)
149
    if subdir is None:
150
        entries.next() # skip root
151
    for entry in entries:
152
        # The .bzr* namespace is reserved for "magic" files like
153
        # .bzrignore and .bzrrules - do not export these
154
        if entry[0].startswith(".bzr"):
155
            continue
4010.2.1 by James Westby
Handle files that are not present in the tree when exporting (#174539)
156
        if subdir is None:
157
            if not tree.has_filename(entry[0]):
158
                continue
159
        else:
160
            if not tree.has_filename(os.path.join(subdir, entry[0])):
161
                continue
3613.2.2 by Robert Collins
Refactor exporters to remove obvious duplication to a helper function.
162
        yield entry
163
164
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
165
register_lazy_exporter(None, [], 'bzrlib.export.dir_exporter', 'dir_exporter')
166
register_lazy_exporter('dir', [], 'bzrlib.export.dir_exporter', 'dir_exporter')
167
register_lazy_exporter('tar', ['.tar'], 'bzrlib.export.tar_exporter', 'tar_exporter')
168
register_lazy_exporter('tgz', ['.tar.gz', '.tgz'], 'bzrlib.export.tar_exporter', 'tgz_exporter')
1185.31.13 by John Arbash Meinel
Updated the test to also test zip exports. Fixed some small bugs exposed by test suite.
169
register_lazy_exporter('tbz2', ['.tar.bz2', '.tbz2'], 'bzrlib.export.tar_exporter', 'tbz_exporter')
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
170
register_lazy_exporter('zip', ['.zip'], 'bzrlib.export.zip_exporter', 'zip_exporter')
171