/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
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
    """
5718.5.1 by Jelmer Vernooij
per_file_timestamp -> force_mtime.
62
    def _loader(tree, dest, root, subdir, filtered, force_mtime):
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)
5076.2.1 by Jelmer Vernooij
Add use_tree_timestamp argument to exporters.
64
        return func(tree, dest, root, subdir, filtered=filtered,
5718.5.1 by Jelmer Vernooij
per_file_timestamp -> force_mtime.
65
                    force_mtime=force_mtime)
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)
67
68
5076.2.1 by Jelmer Vernooij
Add use_tree_timestamp argument to exporters.
69
def export(tree, dest, format=None, root=None, subdir=None, filtered=False,
5076.2.3 by Jelmer Vernooij
Review comments from Rob.
70
           per_file_timestamps=False):
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
71
    """Export the given Tree to the specific destination.
72
73
    :param tree: A Tree (such as RevisionTree) to export
74
    :param dest: The destination where the files,etc should be put
75
    :param format: The format (dir, zip, etc), if None, it will check the
76
                   extension on dest, looking for a match
77
    :param root: The root location inside the format.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
78
                 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.
79
                 extract into a subdirectory, rather than into the
80
                 current working directory.
81
                 If root is None, the default root will be
82
                 selected as the destination without its
83
                 extension.
3613.2.1 by Robert Collins
Teach export how to export a subdirectory. (Robert Collins)
84
    :param subdir: A starting directory within the tree. None means to export
85
        the entire tree, and anything else should specify the relative path to
86
        a directory to start exporting from.
3368.2.32 by Ian Clatworthy
add --filters to export command
87
    :param filtered: If True, content filtering is applied to the
88
                     files exported.
5076.2.3 by Jelmer Vernooij
Review comments from Rob.
89
    :param per_file_timestamps: Whether to use the timestamp stored in the 
90
        tree rather than now(). This will do a revision lookup 
91
        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.
92
    """
93
    global _exporters, _exporter_extensions
94
95
    if format is None:
96
        for ext in _exporter_extensions:
97
            if dest.endswith(ext):
98
                format = _exporter_extensions[ext]
99
                break
100
101
    # Most of the exporters will just have to call
102
    # this function anyway, so why not do it for them
103
    if root is None:
104
        root = get_root_name(dest)
105
1963.2.1 by Robey Pointer
remove usage of has_key()
106
    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.
107
        raise errors.NoSuchExportFormat(format)
5718.5.1 by Jelmer Vernooij
per_file_timestamp -> force_mtime.
108
109
    if not per_file_timestamps:
110
        force_mtime = time.time()
111
    else:
112
        force_mtime = None
113
5718.5.4 by Jelmer Vernooij
fix timestamp in tgz files.
114
    trace.mutter('export version %r', tree)
115
2947.2.1 by Robert Collins
(robertc) Fix export to lock the repository. (Robert Collins)
116
    tree.lock_read()
117
    try:
5076.2.1 by Jelmer Vernooij
Add use_tree_timestamp argument to exporters.
118
        return _exporters[format](tree, dest, root, subdir, filtered=filtered,
5718.5.1 by Jelmer Vernooij
per_file_timestamp -> force_mtime.
119
                                  force_mtime=force_mtime)
2947.2.1 by Robert Collins
(robertc) Fix export to lock the repository. (Robert Collins)
120
    finally:
121
        tree.unlock()
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
122
123
124
def get_root_name(dest):
125
    """Get just the root name for an export.
126
127
    """
128
    global _exporter_extensions
5718.5.18 by Jelmer Vernooij
Don't export to '-', but rather to ''.
129
    if dest == '-':
130
        # Exporting to -/foo doesn't make sense so use relative paths.
131
        return ''
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
132
    dest = os.path.basename(dest)
133
    for ext in _exporter_extensions:
134
        if dest.endswith(ext):
135
            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.
136
    return dest
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
137
138
5718.5.8 by Jelmer Vernooij
add skip_special option.
139
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.
140
    """Iter the entries for tree suitable for exporting.
141
142
    :param tree: A tree object.
4988.10.2 by michal
bzr export won't fail on symlink exporting
143
    :param subdir: None or the path of an entry to start exporting from.
5718.5.8 by Jelmer Vernooij
add skip_special option.
144
    :param skip_special: Whether to skip .bzr files.
3613.2.2 by Robert Collins
Refactor exporters to remove obvious duplication to a helper function.
145
    """
146
    inv = tree.inventory
147
    if subdir is None:
4988.10.1 by michal
- bug #511987 fixed, export of single file
148
        subdir_object = None
4988.10.2 by michal
bzr export won't fail on symlink exporting
149
    else:
150
        subdir_id = inv.path2id(subdir)
151
        if subdir_id is not None:
152
            subdir_object = inv[subdir_id]
153
        # XXX: subdir is path not an id, so NoSuchId isn't proper error
154
        else:
155
            raise errors.NoSuchId(tree, subdir)
156
    if subdir_object is not None and subdir_object.kind != 'directory':
4988.10.1 by michal
- bug #511987 fixed, export of single file
157
        yield subdir_object.name, subdir_object
158
        return
4988.10.2 by michal
bzr export won't fail on symlink exporting
159
    else:
160
        entries = inv.iter_entries(subdir_object)
3613.2.2 by Robert Collins
Refactor exporters to remove obvious duplication to a helper function.
161
    if subdir is None:
162
        entries.next() # skip root
163
    for entry in entries:
164
        # The .bzr* namespace is reserved for "magic" files like
165
        # .bzrignore and .bzrrules - do not export these
5718.5.8 by Jelmer Vernooij
add skip_special option.
166
        if skip_special and entry[0].startswith(".bzr"):
3613.2.2 by Robert Collins
Refactor exporters to remove obvious duplication to a helper function.
167
            continue
4010.2.1 by James Westby
Handle files that are not present in the tree when exporting (#174539)
168
        if subdir is None:
169
            if not tree.has_filename(entry[0]):
170
                continue
171
        else:
172
            if not tree.has_filename(os.path.join(subdir, entry[0])):
173
                continue
3613.2.2 by Robert Collins
Refactor exporters to remove obvious duplication to a helper function.
174
        yield entry
175
176
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
177
register_lazy_exporter(None, [], 'bzrlib.export.dir_exporter', 'dir_exporter')
178
register_lazy_exporter('dir', [], 'bzrlib.export.dir_exporter', 'dir_exporter')
5718.5.2 by Jelmer Vernooij
Factor out export_tarball.
179
register_lazy_exporter('tar', ['.tar'], 'bzrlib.export.tar_exporter', 'plain_tar_exporter')
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
180
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.
181
register_lazy_exporter('tbz2', ['.tar.bz2', '.tbz2'], 'bzrlib.export.tar_exporter', 'tbz_exporter')
5718.5.17 by Jelmer Vernooij
Support tar.lzma.
182
register_lazy_exporter('tlzma', ['.tar.lzma'], 'bzrlib.export.tar_exporter', 'tar_lzma_exporter')
183
register_lazy_exporter('txz', ['.tar.xz'], 'bzrlib.export.tar_exporter', 'tar_xz_exporter')
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
184
register_lazy_exporter('zip', ['.zip'], 'bzrlib.export.zip_exporter', 'zip_exporter')
185