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