/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
4597.9.2 by Vincent Ladeuil
Merge bzr.dev into cleanup
1
# Copyright (C) 2005-2010 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
5436.2.1 by Andrew Bennetts
Add bzrlib.pyutils, which has get_named_object, a wrapper around __import__.
23
from bzrlib import (
24
    errors,
25
    pyutils,
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
33
def register_exporter(format, extensions, func, override=False):
34
    """Register an exporter.
35
36
    :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
37
    :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.
38
                       format was not explicitly specified.
39
    :type extensions: List
40
    :param func: The function. It will be called with (tree, dest, root)
41
    :param override: Whether to override an object which already exists.
42
                     Frequently plugins will want to provide functionality
43
                     until it shows up in mainline, so the default is False.
44
    """
45
    global _exporters, _exporter_extensions
46
1963.2.1 by Robey Pointer
remove usage of has_key()
47
    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.
48
        _exporters[format] = func
49
50
    for ext in extensions:
1963.2.1 by Robey Pointer
remove usage of has_key()
51
        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.
52
            _exporter_extensions[ext] = format
53
54
55
def register_lazy_exporter(scheme, extensions, module, funcname):
56
    """Register lazy-loaded exporter function.
57
58
    When requesting a specific type of export, load the respective path.
59
    """
5076.2.3 by Jelmer Vernooij
Review comments from Rob.
60
    def _loader(tree, dest, root, subdir, filtered, per_file_timestamps):
5436.2.1 by Andrew Bennetts
Add bzrlib.pyutils, which has get_named_object, a wrapper around __import__.
61
        func = pyutils.get_named_object(module, funcname)
5076.2.1 by Jelmer Vernooij
Add use_tree_timestamp argument to exporters.
62
        return func(tree, dest, root, subdir, filtered=filtered,
5076.2.3 by Jelmer Vernooij
Review comments from Rob.
63
                    per_file_timestamps=per_file_timestamps)
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
64
    register_exporter(scheme, extensions, _loader)
65
66
5076.2.1 by Jelmer Vernooij
Add use_tree_timestamp argument to exporters.
67
def export(tree, dest, format=None, root=None, subdir=None, filtered=False,
5076.2.3 by Jelmer Vernooij
Review comments from Rob.
68
           per_file_timestamps=False):
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
69
    """Export the given Tree to the specific destination.
70
71
    :param tree: A Tree (such as RevisionTree) to export
72
    :param dest: The destination where the files,etc should be put
73
    :param format: The format (dir, zip, etc), if None, it will check the
74
                   extension on dest, looking for a match
75
    :param root: The root location inside the format.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
76
                 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.
77
                 extract into a subdirectory, rather than into the
78
                 current working directory.
79
                 If root is None, the default root will be
80
                 selected as the destination without its
81
                 extension.
3613.2.1 by Robert Collins
Teach export how to export a subdirectory. (Robert Collins)
82
    :param subdir: A starting directory within the tree. None means to export
83
        the entire tree, and anything else should specify the relative path to
84
        a directory to start exporting from.
3368.2.32 by Ian Clatworthy
add --filters to export command
85
    :param filtered: If True, content filtering is applied to the
86
                     files exported.
5076.2.3 by Jelmer Vernooij
Review comments from Rob.
87
    :param per_file_timestamps: Whether to use the timestamp stored in the 
88
        tree rather than now(). This will do a revision lookup 
89
        for every file so will be significantly slower.
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
90
    """
91
    global _exporters, _exporter_extensions
92
93
    if format is None:
94
        for ext in _exporter_extensions:
95
            if dest.endswith(ext):
96
                format = _exporter_extensions[ext]
97
                break
98
99
    # Most of the exporters will just have to call
100
    # this function anyway, so why not do it for them
101
    if root is None:
102
        root = get_root_name(dest)
103
1963.2.1 by Robey Pointer
remove usage of has_key()
104
    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.
105
        raise errors.NoSuchExportFormat(format)
2947.2.1 by Robert Collins
(robertc) Fix export to lock the repository. (Robert Collins)
106
    tree.lock_read()
107
    try:
5076.2.1 by Jelmer Vernooij
Add use_tree_timestamp argument to exporters.
108
        return _exporters[format](tree, dest, root, subdir, filtered=filtered,
5076.2.3 by Jelmer Vernooij
Review comments from Rob.
109
                                  per_file_timestamps=per_file_timestamps)
2947.2.1 by Robert Collins
(robertc) Fix export to lock the repository. (Robert Collins)
110
    finally:
111
        tree.unlock()
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
112
113
114
def get_root_name(dest):
115
    """Get just the root name for an export.
116
2024.2.3 by John Arbash Meinel
Move out export tests from test_too_much, refactor
117
    >>> get_root_name('../mytest.tar')
118
    'mytest'
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
119
    >>> get_root_name('mytar.tar')
120
    'mytar'
121
    >>> get_root_name('mytar.tar.bz2')
122
    'mytar'
123
    >>> get_root_name('tar.tar.tar.tgz')
124
    'tar.tar.tar'
125
    >>> get_root_name('bzr-0.0.5.tar.gz')
126
    'bzr-0.0.5'
127
    >>> get_root_name('bzr-0.0.5.zip')
128
    'bzr-0.0.5'
129
    >>> get_root_name('bzr-0.0.5')
130
    'bzr-0.0.5'
131
    >>> get_root_name('a/long/path/mytar.tgz')
132
    'mytar'
133
    >>> get_root_name('../parent/../dir/other.tbz2')
134
    'other'
135
    """
136
    global _exporter_extensions
137
    dest = os.path.basename(dest)
138
    for ext in _exporter_extensions:
139
        if dest.endswith(ext):
140
            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.
141
    return dest
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
142
143
3613.2.2 by Robert Collins
Refactor exporters to remove obvious duplication to a helper function.
144
def _export_iter_entries(tree, subdir):
145
    """Iter the entries for tree suitable for exporting.
146
147
    :param tree: A tree object.
4988.10.2 by michal
bzr export won't fail on symlink exporting
148
    :param subdir: None or the path of an entry to start exporting from.
3613.2.2 by Robert Collins
Refactor exporters to remove obvious duplication to a helper function.
149
    """
150
    inv = tree.inventory
151
    if subdir is None:
4988.10.1 by michal
- bug #511987 fixed, export of single file
152
        subdir_object = None
4988.10.2 by michal
bzr export won't fail on symlink exporting
153
    else:
154
        subdir_id = inv.path2id(subdir)
155
        if subdir_id is not None:
156
            subdir_object = inv[subdir_id]
157
        # XXX: subdir is path not an id, so NoSuchId isn't proper error
158
        else:
159
            raise errors.NoSuchId(tree, subdir)
160
    if subdir_object is not None and subdir_object.kind != 'directory':
4988.10.1 by michal
- bug #511987 fixed, export of single file
161
        yield subdir_object.name, subdir_object
162
        return
4988.10.2 by michal
bzr export won't fail on symlink exporting
163
    else:
164
        entries = inv.iter_entries(subdir_object)
3613.2.2 by Robert Collins
Refactor exporters to remove obvious duplication to a helper function.
165
    if subdir is None:
166
        entries.next() # skip root
167
    for entry in entries:
168
        # The .bzr* namespace is reserved for "magic" files like
169
        # .bzrignore and .bzrrules - do not export these
170
        if entry[0].startswith(".bzr"):
171
            continue
4010.2.1 by James Westby
Handle files that are not present in the tree when exporting (#174539)
172
        if subdir is None:
173
            if not tree.has_filename(entry[0]):
174
                continue
175
        else:
176
            if not tree.has_filename(os.path.join(subdir, entry[0])):
177
                continue
3613.2.2 by Robert Collins
Refactor exporters to remove obvious duplication to a helper function.
178
        yield entry
179
180
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
181
register_lazy_exporter(None, [], 'bzrlib.export.dir_exporter', 'dir_exporter')
182
register_lazy_exporter('dir', [], 'bzrlib.export.dir_exporter', 'dir_exporter')
183
register_lazy_exporter('tar', ['.tar'], 'bzrlib.export.tar_exporter', 'tar_exporter')
184
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.
185
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.
186
register_lazy_exporter('zip', ['.zip'], 'bzrlib.export.zip_exporter', 'zip_exporter')
187