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