~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/export/__init__.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2011-06-28 15:15:59 UTC
  • mfrom: (5967.6.5 cleanup-export)
  • Revision ID: pqm@pqm.ubuntu.com-20110628151559-dvc0trfnmfux4roq
(mbp) cleanup export code (Martin Pool)

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
 
"""Export functionality, which can take a Tree and create a different representation.
18
 
 
19
 
Such as non-controlled directories, tarfiles, zipfiles, etc.
 
17
"""Export trees to tarballs, non-controlled directories, zipfiles, etc.
20
18
"""
21
19
 
22
20
import os
32
30
# Maps filename extensions => export format name
33
31
_exporter_extensions = {}
34
32
 
 
33
 
35
34
def register_exporter(format, extensions, func, override=False):
36
35
    """Register an exporter.
37
36
 
61
60
    """
62
61
    def _loader(tree, dest, root, subdir, filtered, force_mtime, fileobj):
63
62
        func = pyutils.get_named_object(module, funcname)
64
 
        return func(tree, dest, root, subdir, filtered=filtered, 
 
63
        return func(tree, dest, root, subdir, filtered=filtered,
65
64
                    force_mtime=force_mtime, fileobj=fileobj)
 
65
 
66
66
    register_exporter(scheme, extensions, _loader)
67
 
    
 
67
 
 
68
 
68
69
def get_export_generator(tree, dest=None, format=None, root=None, subdir=None,
69
70
                         filtered=False, per_file_timestamps=False,
70
71
                         fileobj=None):
71
72
    """Returns a generator that exports the given tree.
72
 
    
 
73
 
73
74
    The generator is expected to yield None while exporting the tree while the
74
75
    actual export is written to ``fileobj``.
75
76
 
123
124
 
124
125
    try:
125
126
        tree.lock_read()
126
 
    
 
127
 
127
128
        for _ in _exporters[format](tree, dest, root, subdir,
128
 
                                    filtered=filtered, 
 
129
                                    filtered=filtered,
129
130
                                    force_mtime=force_mtime, fileobj=fileobj):
130
 
            
 
131
 
131
132
            yield
132
 
    finally:    
 
133
    finally:
133
134
        tree.unlock()
134
135
 
135
136
 
136
 
def export(tree, dest, format=None, root=None, subdir=None, filtered=False, 
 
137
def export(tree, dest, format=None, root=None, subdir=None, filtered=False,
137
138
           per_file_timestamps=False, fileobj=None):
138
139
    """Export the given Tree to the specific destination.
139
140
 
153
154
        a directory to start exporting from.
154
155
    :param filtered: If True, content filtering is applied to the
155
156
                     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 
 
157
    :param per_file_timestamps: Whether to use the timestamp stored in the
 
158
        tree rather than now(). This will do a revision lookup
158
159
        for every file so will be significantly slower.
159
160
    :param fileobj: Optional file object to use
160
161
    """
161
 
    for _ in get_export_generator(tree, dest, format, root, subdir, filtered, 
 
162
    for _ in get_export_generator(tree, dest, format, root, subdir, filtered,
162
163
                                  per_file_timestamps, fileobj):
163
 
        
164
164
        pass
165
165
 
 
166
 
166
167
def get_root_name(dest):
167
168
    """Get just the root name for an export.
168
169
 
190
191
    if subdir is not None:
191
192
        subdir = subdir.rstrip('/')
192
193
    entries = tree.iter_entries_by_dir()
193
 
    entries.next() # skip root
 
194
    entries.next()  # skip root
194
195
    for path, entry in entries:
195
196
        # The .bzr* namespace is reserved for "magic" files like
196
197
        # .bzrignore and .bzrrules - do not export these
209
210
            final_path = path
210
211
        if not tree.has_filename(path):
211
212
            continue
212
 
        
 
213
 
213
214
        yield final_path, entry
214
215
 
215
216
 
219
220
                       'dir_exporter_generator')
220
221
register_lazy_exporter('tar', ['.tar'], 'bzrlib.export.tar_exporter',
221
222
                       'plain_tar_exporter_generator')
222
 
register_lazy_exporter('tgz', ['.tar.gz', '.tgz'], 'bzrlib.export.tar_exporter',
 
223
register_lazy_exporter('tgz', ['.tar.gz', '.tgz'],
 
224
                       'bzrlib.export.tar_exporter',
223
225
                       'tgz_exporter_generator')
224
226
register_lazy_exporter('tbz2', ['.tar.bz2', '.tbz2'],
225
227
                       'bzrlib.export.tar_exporter', 'tbz_exporter_generator')
229
231
                       'tar_xz_exporter_generator')
230
232
register_lazy_exporter('zip', ['.zip'], 'bzrlib.export.zip_exporter',
231
233
                       'zip_exporter_generator')
232