/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
    """
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
    """
5809.3.1 by Aaron Bentley
Switch to iter_entries_by_dir.
146
    if subdir == '':
147
        subdir = None
148
    if subdir is not None:
149
        subdir = subdir.rstrip('/')
150
    entries = tree.iter_entries_by_dir()
151
    entries.next() # skip root
152
    for path, entry in entries:
3613.2.2 by Robert Collins
Refactor exporters to remove obvious duplication to a helper function.
153
        # The .bzr* namespace is reserved for "magic" files like
154
        # .bzrignore and .bzrrules - do not export these
5809.3.1 by Aaron Bentley
Switch to iter_entries_by_dir.
155
        if skip_special and path.startswith(".bzr"):
3613.2.2 by Robert Collins
Refactor exporters to remove obvious duplication to a helper function.
156
            continue
5809.3.1 by Aaron Bentley
Switch to iter_entries_by_dir.
157
        if path == subdir:
158
            if entry.kind == 'directory':
159
                continue
160
            final_path = entry.name
161
        elif subdir is not None:
162
            if path.startswith(subdir + '/'):
163
                final_path = path[len(subdir) + 1:]
164
            else:
4010.2.1 by James Westby
Handle files that are not present in the tree when exporting (#174539)
165
                continue
166
        else:
5809.3.1 by Aaron Bentley
Switch to iter_entries_by_dir.
167
            final_path = path
168
        if not tree.has_filename(path):
169
            continue
170
        yield final_path, entry
3613.2.2 by Robert Collins
Refactor exporters to remove obvious duplication to a helper function.
171
172
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
173
register_lazy_exporter(None, [], 'bzrlib.export.dir_exporter', 'dir_exporter')
174
register_lazy_exporter('dir', [], 'bzrlib.export.dir_exporter', 'dir_exporter')
5718.5.2 by Jelmer Vernooij
Factor out export_tarball.
175
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.
176
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.
177
register_lazy_exporter('tbz2', ['.tar.bz2', '.tbz2'], 'bzrlib.export.tar_exporter', 'tbz_exporter')
5718.5.17 by Jelmer Vernooij
Support tar.lzma.
178
register_lazy_exporter('tlzma', ['.tar.lzma'], 'bzrlib.export.tar_exporter', 'tar_lzma_exporter')
179
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.
180
register_lazy_exporter('zip', ['.zip'], 'bzrlib.export.zip_exporter', 'zip_exporter')
181