/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
5809.3.2 by Aaron Bentley
Support PreviewTree.has_filename.
1
# Copyright (C) 2005-2011 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
5967.6.1 by Martin Pool
pep8 cleanup
17
"""Export trees to tarballs, non-controlled directories, zipfiles, etc.
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
18
"""
19
6379.6.7 by Jelmer Vernooij
Move importing from future until after doc string, otherwise the doc string will disappear.
20
from __future__ import absolute_import
21
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
22
import os
5718.5.1 by Jelmer Vernooij
per_file_timestamp -> force_mtime.
23
import time
6006.3.6 by Martin Pool
Deprecate export(filtered=True)
24
import warnings
25
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
26
from .. import (
5436.2.1 by Andrew Bennetts
Add bzrlib.pyutils, which has get_named_object, a wrapper around __import__.
27
    errors,
28
    pyutils,
5718.5.4 by Jelmer Vernooij
fix timestamp in tgz files.
29
    trace,
5436.2.1 by Andrew Bennetts
Add bzrlib.pyutils, which has get_named_object, a wrapper around __import__.
30
    )
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
31
32
# Maps format name => export function
33
_exporters = {}
34
# Maps filename extensions => export format name
35
_exporter_extensions = {}
36
5967.6.1 by Martin Pool
pep8 cleanup
37
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
38
def register_exporter(format, extensions, func, override=False):
39
    """Register an exporter.
40
41
    :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
42
    :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.
43
                       format was not explicitly specified.
44
    :type extensions: List
45
    :param func: The function. It will be called with (tree, dest, root)
46
    :param override: Whether to override an object which already exists.
47
                     Frequently plugins will want to provide functionality
48
                     until it shows up in mainline, so the default is False.
49
    """
50
    global _exporters, _exporter_extensions
51
1963.2.1 by Robey Pointer
remove usage of has_key()
52
    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.
53
        _exporters[format] = func
54
55
    for ext in extensions:
1963.2.1 by Robey Pointer
remove usage of has_key()
56
        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.
57
            _exporter_extensions[ext] = format
58
59
60
def register_lazy_exporter(scheme, extensions, module, funcname):
61
    """Register lazy-loaded exporter function.
62
63
    When requesting a specific type of export, load the respective path.
64
    """
6006.3.7 by Martin Pool
Remove duplicated content-filtering code from exporters
65
    def _loader(tree, dest, root, subdir, force_mtime, fileobj):
5436.2.1 by Andrew Bennetts
Add bzrlib.pyutils, which has get_named_object, a wrapper around __import__.
66
        func = pyutils.get_named_object(module, funcname)
6006.3.7 by Martin Pool
Remove duplicated content-filtering code from exporters
67
        return func(tree, dest, root, subdir, force_mtime=force_mtime,
68
            fileobj=fileobj)
5967.6.1 by Martin Pool
pep8 cleanup
69
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
70
    register_exporter(scheme, extensions, _loader)
5967.6.1 by Martin Pool
pep8 cleanup
71
72
5952.1.18 by geoffreyfishing at gmail
Fixed line ending problems.
73
def get_export_generator(tree, dest=None, format=None, root=None, subdir=None,
5952.2.1 by Vincent Ladeuil
PEP8 tweaks, lines too long, spaces at end of lines.
74
                         filtered=False, per_file_timestamps=False,
75
                         fileobj=None):
76
    """Returns a generator that exports the given tree.
5967.6.1 by Martin Pool
pep8 cleanup
77
5952.2.1 by Vincent Ladeuil
PEP8 tweaks, lines too long, spaces at end of lines.
78
    The generator is expected to yield None while exporting the tree while the
79
    actual export is written to ``fileobj``.
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
80
81
    :param tree: A Tree (such as RevisionTree) to export
5952.2.1 by Vincent Ladeuil
PEP8 tweaks, lines too long, spaces at end of lines.
82
83
    :param dest: The destination where the files, etc should be put
84
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
85
    :param format: The format (dir, zip, etc), if None, it will check the
5952.2.1 by Vincent Ladeuil
PEP8 tweaks, lines too long, spaces at end of lines.
86
        extension on dest, looking for a match
87
88
    :param root: The root location inside the format.  It is common practise to
89
        have zipfiles and tarballs extract into a subdirectory, rather than
90
        into the current working directory.  If root is None, the default root
91
        will be selected as the destination without its extension.
92
3613.2.1 by Robert Collins
Teach export how to export a subdirectory. (Robert Collins)
93
    :param subdir: A starting directory within the tree. None means to export
94
        the entire tree, and anything else should specify the relative path to
95
        a directory to start exporting from.
5952.2.1 by Vincent Ladeuil
PEP8 tweaks, lines too long, spaces at end of lines.
96
97
    :param filtered: If True, content filtering is applied to the exported
6006.3.6 by Martin Pool
Deprecate export(filtered=True)
98
        files.  Deprecated in favour of passing a ContentFilterTree
99
        as the source.
5952.2.1 by Vincent Ladeuil
PEP8 tweaks, lines too long, spaces at end of lines.
100
101
    :param per_file_timestamps: Whether to use the timestamp stored in the tree
102
        rather than now(). This will do a revision lookup for every file so
103
        will be significantly slower.
104
5952.1.6 by geoffreyfishing at gmail
Created get_export_generator.
105
    :param fileobj: Optional file object to use
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
106
    """
107
    global _exporters, _exporter_extensions
108
5952.1.6 by geoffreyfishing at gmail
Created get_export_generator.
109
    if format is None and dest is not None:
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
110
        for ext in _exporter_extensions:
111
            if dest.endswith(ext):
112
                format = _exporter_extensions[ext]
113
                break
114
115
    # Most of the exporters will just have to call
116
    # this function anyway, so why not do it for them
117
    if root is None:
118
        root = get_root_name(dest)
119
1963.2.1 by Robey Pointer
remove usage of has_key()
120
    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.
121
        raise errors.NoSuchExportFormat(format)
5718.5.1 by Jelmer Vernooij
per_file_timestamp -> force_mtime.
122
123
    if not per_file_timestamps:
124
        force_mtime = time.time()
125
    else:
126
        force_mtime = None
127
5718.5.4 by Jelmer Vernooij
fix timestamp in tgz files.
128
    trace.mutter('export version %r', tree)
129
6006.3.6 by Martin Pool
Deprecate export(filtered=True)
130
    if filtered:
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
131
        from breezy.filter_tree import ContentFilterTree
6006.3.6 by Martin Pool
Deprecate export(filtered=True)
132
        warnings.warn(
133
            "passing filtered=True to export is deprecated in bzr 2.4",
134
            stacklevel=2)
135
        tree = ContentFilterTree(tree, tree._content_filter_stack)
6006.3.7 by Martin Pool
Remove duplicated content-filtering code from exporters
136
        # We don't want things re-filtered by the specific exporter.
137
        filtered = False
138
6754.8.4 by Jelmer Vernooij
Use new context stuff.
139
    with tree.lock_read():
6006.3.6 by Martin Pool
Deprecate export(filtered=True)
140
        for _ in _exporters[format](
6006.3.7 by Martin Pool
Remove duplicated content-filtering code from exporters
141
            tree, dest, root, subdir,
6006.3.6 by Martin Pool
Deprecate export(filtered=True)
142
            force_mtime=force_mtime, fileobj=fileobj):
5952.1.16 by geoffreyfishing at gmail
Moved unlock function into finally block.
143
            yield
5952.1.15 by geoffreyfishing at gmail
Major code cleanup.
144
145
5967.6.1 by Martin Pool
pep8 cleanup
146
def export(tree, dest, format=None, root=None, subdir=None, filtered=False,
5952.1.18 by geoffreyfishing at gmail
Fixed line ending problems.
147
           per_file_timestamps=False, fileobj=None):
5952.1.6 by geoffreyfishing at gmail
Created get_export_generator.
148
    """Export the given Tree to the specific destination.
149
150
    :param tree: A Tree (such as RevisionTree) to export
151
    :param dest: The destination where the files,etc should be put
152
    :param format: The format (dir, zip, etc), if None, it will check the
153
                   extension on dest, looking for a match
154
    :param root: The root location inside the format.
155
                 It is common practise to have zipfiles and tarballs
156
                 extract into a subdirectory, rather than into the
157
                 current working directory.
158
                 If root is None, the default root will be
159
                 selected as the destination without its
160
                 extension.
161
    :param subdir: A starting directory within the tree. None means to export
162
        the entire tree, and anything else should specify the relative path to
163
        a directory to start exporting from.
164
    :param filtered: If True, content filtering is applied to the
6006.3.6 by Martin Pool
Deprecate export(filtered=True)
165
        files exported.  Deprecated in favor of passing an ContentFilterTree.
5967.6.1 by Martin Pool
pep8 cleanup
166
    :param per_file_timestamps: Whether to use the timestamp stored in the
167
        tree rather than now(). This will do a revision lookup
5952.1.6 by geoffreyfishing at gmail
Created get_export_generator.
168
        for every file so will be significantly slower.
169
    :param fileobj: Optional file object to use
170
    """
5967.6.1 by Martin Pool
pep8 cleanup
171
    for _ in get_export_generator(tree, dest, format, root, subdir, filtered,
5952.1.18 by geoffreyfishing at gmail
Fixed line ending problems.
172
                                  per_file_timestamps, fileobj):
5952.1.6 by geoffreyfishing at gmail
Created get_export_generator.
173
        pass
174
5967.6.1 by Martin Pool
pep8 cleanup
175
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
176
def get_root_name(dest):
177
    """Get just the root name for an export.
178
179
    """
180
    global _exporter_extensions
5718.5.18 by Jelmer Vernooij
Don't export to '-', but rather to ''.
181
    if dest == '-':
182
        # Exporting to -/foo doesn't make sense so use relative paths.
183
        return ''
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
184
    dest = os.path.basename(dest)
185
    for ext in _exporter_extensions:
186
        if dest.endswith(ext):
187
            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.
188
    return dest
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
189
190
5718.5.8 by Jelmer Vernooij
add skip_special option.
191
def _export_iter_entries(tree, subdir, skip_special=True):
3613.2.2 by Robert Collins
Refactor exporters to remove obvious duplication to a helper function.
192
    """Iter the entries for tree suitable for exporting.
193
194
    :param tree: A tree object.
4988.10.2 by michal
bzr export won't fail on symlink exporting
195
    :param subdir: None or the path of an entry to start exporting from.
5718.5.8 by Jelmer Vernooij
add skip_special option.
196
    :param skip_special: Whether to skip .bzr files.
6331.2.1 by Jelmer Vernooij
Consistently pass tree path when exporting.
197
    :return: iterator over tuples with final path, tree path and inventory
198
        entry for each entry to export
3613.2.2 by Robert Collins
Refactor exporters to remove obvious duplication to a helper function.
199
    """
5809.3.1 by Aaron Bentley
Switch to iter_entries_by_dir.
200
    if subdir == '':
201
        subdir = None
202
    if subdir is not None:
203
        subdir = subdir.rstrip('/')
204
    entries = tree.iter_entries_by_dir()
6634.2.1 by Martin
Apply 2to3 next fixer and make compatible
205
    next(entries)  # skip root
5809.3.1 by Aaron Bentley
Switch to iter_entries_by_dir.
206
    for path, entry in entries:
3613.2.2 by Robert Collins
Refactor exporters to remove obvious duplication to a helper function.
207
        # The .bzr* namespace is reserved for "magic" files like
208
        # .bzrignore and .bzrrules - do not export these
5809.3.1 by Aaron Bentley
Switch to iter_entries_by_dir.
209
        if skip_special and path.startswith(".bzr"):
3613.2.2 by Robert Collins
Refactor exporters to remove obvious duplication to a helper function.
210
            continue
5809.3.1 by Aaron Bentley
Switch to iter_entries_by_dir.
211
        if path == subdir:
212
            if entry.kind == 'directory':
213
                continue
214
            final_path = entry.name
215
        elif subdir is not None:
216
            if path.startswith(subdir + '/'):
217
                final_path = path[len(subdir) + 1:]
218
            else:
4010.2.1 by James Westby
Handle files that are not present in the tree when exporting (#174539)
219
                continue
220
        else:
5809.3.1 by Aaron Bentley
Switch to iter_entries_by_dir.
221
            final_path = path
222
        if not tree.has_filename(path):
223
            continue
5967.6.1 by Martin Pool
pep8 cleanup
224
6331.2.1 by Jelmer Vernooij
Consistently pass tree path when exporting.
225
        yield final_path, path, entry
3613.2.2 by Robert Collins
Refactor exporters to remove obvious duplication to a helper function.
226
227
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
228
register_lazy_exporter(None, [], 'breezy.export.dir_exporter',
229
                       'dir_exporter_generator')
230
register_lazy_exporter('dir', [], 'breezy.export.dir_exporter',
231
                       'dir_exporter_generator')
232
register_lazy_exporter('tar', ['.tar'], 'breezy.export.tar_exporter',
5952.2.1 by Vincent Ladeuil
PEP8 tweaks, lines too long, spaces at end of lines.
233
                       'plain_tar_exporter_generator')
5967.6.1 by Martin Pool
pep8 cleanup
234
register_lazy_exporter('tgz', ['.tar.gz', '.tgz'],
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
235
                       'breezy.export.tar_exporter',
5952.2.1 by Vincent Ladeuil
PEP8 tweaks, lines too long, spaces at end of lines.
236
                       'tgz_exporter_generator')
237
register_lazy_exporter('tbz2', ['.tar.bz2', '.tbz2'],
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
238
                       'breezy.export.tar_exporter', 'tbz_exporter_generator')
239
register_lazy_exporter('tlzma', ['.tar.lzma'], 'breezy.export.tar_exporter',
5952.2.1 by Vincent Ladeuil
PEP8 tweaks, lines too long, spaces at end of lines.
240
                       'tar_lzma_exporter_generator')
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
241
register_lazy_exporter('txz', ['.tar.xz'], 'breezy.export.tar_exporter',
5957.3.3 by Vincent Ladeuil
Fix typo.
242
                       'tar_xz_exporter_generator')
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
243
register_lazy_exporter('zip', ['.zip'], 'breezy.export.zip_exporter',
5952.2.1 by Vincent Ladeuil
PEP8 tweaks, lines too long, spaces at end of lines.
244
                       'zip_exporter_generator')