1
# Copyright (C) 2005, 2006 Canonical Ltd
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.
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.
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
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
"""Serializer factory for reading and writing bundles.
21
from StringIO import StringIO
24
import bzrlib.errors as errors
25
from bzrlib.diff import internal_diff
26
from bzrlib.revision import NULL_REVISION
27
# For backwards-compatibility
28
from bzrlib.timestamp import unpack_highres_date, format_highres_date
31
# New bundles should try to use this header format
32
BUNDLE_HEADER = '# Bazaar revision bundle v'
33
BUNDLE_HEADER_RE = re.compile(
34
r'^# Bazaar revision bundle v(?P<version>\d+[\w.]*)(?P<lineending>\r?)\n$')
35
CHANGESET_OLD_HEADER_RE = re.compile(
36
r'^# Bazaar-NG changeset v(?P<version>\d+[\w.]*)(?P<lineending>\r?)\n$')
43
def _get_bundle_header(version):
44
return '%s%s\n' % (BUNDLE_HEADER, version)
47
return getattr(f, 'name', '<unknown>')
51
"""Read in a bundle from a filelike object.
53
:param f: A file-like object
54
:return: A list of Bundle objects
58
m = BUNDLE_HEADER_RE.match(line)
60
if m.group('lineending') != '':
61
raise errors.UnsupportedEOLMarker()
62
version = m.group('version')
64
elif line.startswith(BUNDLE_HEADER):
65
raise errors.MalformedHeader(
66
'Extra characters after version number')
67
m = CHANGESET_OLD_HEADER_RE.match(line)
69
version = m.group('version')
70
raise errors.BundleNotSupported(version,
71
'old format bundles not supported')
74
raise errors.NotABundle('Did not find an opening header')
76
# Now we have a version, to figure out how to read the bundle
77
if version not in _serializers:
78
raise errors.BundleNotSupported(version,
79
'version not listed in known versions')
81
serializer = _serializers[version](version)
83
return serializer.read(f)
86
def get_serializer(version):
88
return _serializers[version](version)
90
raise errors.BundleNotSupported(version, 'unknown bundle format')
93
def write(source, revision_ids, f, version=None, forced_bases={}):
94
"""Serialize a list of bundles to a filelike object.
96
:param source: A source for revision information
97
:param revision_ids: The list of revision ids to serialize
98
:param f: The file to output to
99
:param version: [optional] target serialization version
104
return get_serializer(version).write(source, revision_ids,
110
def write_bundle(repository, revision_id, base_revision_id, out, format=None):
111
"""Write a bundle of revisions.
113
:param repository: Repository containing revisions to serialize.
114
:param revision_id: Head revision_id of the bundle.
115
:param base_revision_id: Revision assumed to be present in repositories
117
:param out: Output file.
119
repository.lock_read()
121
return get_serializer(format).write_bundle(repository, revision_id,
122
base_revision_id, out)
127
class BundleSerializer(object):
128
"""The base class for Serializers.
130
Common functionality should be included here.
132
def __init__(self, version):
133
self.version = version
136
"""Read the rest of the bundles from the supplied file.
138
:param f: The file to read from
139
:return: A list of bundle trees
141
raise NotImplementedError
143
def write_bundle(self, repository, target, base, fileobj):
144
"""Write the bundle to the supplied file.
146
:param repository: The repository to retrieve revision data from
147
:param target: The revision to provide data for
148
:param base: The most recent of ancestor of the revision that does not
149
need to be included in the bundle
150
:param fileobj: The file to output to
152
raise NotImplementedError
154
def write(self, source, revision_ids, forced_bases, f):
155
"""Write the bundle to the supplied file.
157
DEPRECATED: see write_bundle
158
:param source: A source for revision information
159
:param revision_ids: The list of revision ids to serialize
160
:param forced_bases: A dict of revision -> base that overrides default
161
:param f: The file to output to
163
raise NotImplementedError
165
def _write_bundle(self, repository, revision_id, base_revision_id, out):
166
"""Helper function for translating write_bundle to write"""
167
forced_bases = {revision_id:base_revision_id}
168
if base_revision_id is NULL_REVISION:
169
base_revision_id = None
170
revision_ids = set(repository.get_ancestry(revision_id,
172
revision_ids.difference_update(repository.get_ancestry(
173
base_revision_id, topo_sorted=False))
174
revision_ids = list(repository.get_graph().iter_topo_order(
176
revision_ids.reverse()
177
self.write(repository, revision_ids, forced_bases, out)
181
def register(version, klass, overwrite=False):
182
"""Register a BundleSerializer version.
184
:param version: The version associated with this format
185
:param klass: The class to instantiate, which must take a version argument
189
_serializers[version] = klass
192
if version not in _serializers:
193
_serializers[version] = klass
196
def register_lazy(version, module, classname, overwrite=False):
197
"""Register lazy-loaded bundle serializer.
199
:param version: The version associated with this reader
200
:param module: String indicating what module should be loaded
201
:param classname: Name of the class that will be instantiated
202
:param overwrite: Should this version override a default
204
def _loader(version):
205
mod = __import__(module, globals(), locals(), [classname])
206
klass = getattr(mod, classname)
207
return klass(version)
208
register(version, _loader, overwrite=overwrite)
211
def binary_diff(old_filename, old_lines, new_filename, new_lines, to_file):
213
internal_diff(old_filename, old_lines, new_filename, new_lines, temp,
216
base64.encode(temp, to_file)
219
register_lazy('0.8', 'bzrlib.bundle.serializer.v08', 'BundleSerializerV08')
220
register_lazy('0.9', 'bzrlib.bundle.serializer.v09', 'BundleSerializerV09')
221
register_lazy(v4_string, 'bzrlib.bundle.serializer.v4',
222
'BundleSerializerV4')
223
register_lazy(None, 'bzrlib.bundle.serializer.v4', 'BundleSerializerV4')