~bzr-pqm/bzr/bzr.dev

5452.4.3 by John Arbash Meinel
Merge bzr.dev to resolve bzr-2.3.txt (aka NEWS)
1
# Copyright (C) 2005, 2006, 2007, 2009, 2010 Canonical Ltd
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
2
#
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
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.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
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.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
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.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
16
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
17
"""Serializer factory for reading and writing bundles.
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
18
"""
19
1185.82.96 by Aaron Bentley
Got first binary test passing
20
import base64
21
from StringIO import StringIO
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
22
import re
23
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,
27
    )
1185.82.96 by Aaron Bentley
Got first binary test passing
28
from bzrlib.diff import internal_diff
1185.82.58 by Aaron Bentley
Handle empty branches properly
29
from bzrlib.revision import NULL_REVISION
1551.12.46 by Aaron Bentley
Import highres date functions to old location
30
# For backwards-compatibility
31
from bzrlib.timestamp import unpack_highres_date, format_highres_date
32
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
33
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
34
# New bundles should try to use this header format
35
BUNDLE_HEADER = '# Bazaar revision bundle v'
1907.2.2 by Hermann Kraus
Detect wrong eol markers.
36
BUNDLE_HEADER_RE = re.compile(
37
    r'^# Bazaar revision bundle v(?P<version>\d+[\w.]*)(?P<lineending>\r?)\n$')
38
CHANGESET_OLD_HEADER_RE = re.compile(
39
    r'^# Bazaar-NG changeset v(?P<version>\d+[\w.]*)(?P<lineending>\r?)\n$')
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
40
41
1793.3.16 by John Arbash Meinel
Add tests to ensure that we gracefully handle opening and trailing non-bundle text.
42
_serializers = {}
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
43
2520.4.136 by Aaron Bentley
Fix format strings
44
v4_string = '4'
2520.4.123 by Aaron Bentley
Cleanup of bundle code
45
2520.4.14 by Aaron Bentley
Get most tests passing, use format header
46
def _get_bundle_header(version):
47
    return '%s%s\n' % (BUNDLE_HEADER, version)
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
48
49
def _get_filename(f):
1963.2.4 by Robey Pointer
remove usage of hasattr
50
    return getattr(f, 'name', '<unknown>')
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
51
52
1793.2.2 by Aaron Bentley
Move BundleReader into v07 serializer
53
def read_bundle(f):
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
54
    """Read in a bundle from a filelike object.
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
55
56
    :param f: A file-like object
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
57
    :return: A list of Bundle objects
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
58
    """
59
    version = None
60
    for line in f:
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
61
        m = BUNDLE_HEADER_RE.match(line)
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
62
        if m:
1907.2.2 by Hermann Kraus
Detect wrong eol markers.
63
            if m.group('lineending') != '':
64
                raise errors.UnsupportedEOLMarker()
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
65
            version = m.group('version')
66
            break
1793.2.7 by Aaron Bentley
Fix reporting of malformed, (especially, crlf) bundles
67
        elif line.startswith(BUNDLE_HEADER):
1907.2.1 by Hermann Kraus
Convert bundle errors from Exception to BzrNewError.
68
            raise errors.MalformedHeader(
69
                'Extra characters after version number')
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
70
        m = CHANGESET_OLD_HEADER_RE.match(line)
71
        if m:
72
            version = m.group('version')
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
73
            raise errors.BundleNotSupported(version,
1907.2.1 by Hermann Kraus
Convert bundle errors from Exception to BzrNewError.
74
                'old format bundles not supported')
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
75
76
    if version is None:
1793.2.2 by Aaron Bentley
Move BundleReader into v07 serializer
77
        raise errors.NotABundle('Did not find an opening header')
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
78
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
79
    # Now we have a version, to figure out how to read the bundle
1963.2.1 by Robey Pointer
remove usage of has_key()
80
    if version not in _serializers:
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
81
        raise errors.BundleNotSupported(version,
1907.2.1 by Hermann Kraus
Convert bundle errors from Exception to BzrNewError.
82
            'version not listed in known versions')
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
83
84
    serializer = _serializers[version](version)
85
86
    return serializer.read(f)
87
88
2520.4.53 by Aaron Bentley
refactor bundle serialization to make write_bundle primary
89
def get_serializer(version):
90
    try:
91
        return _serializers[version](version)
92
    except KeyError:
93
        raise errors.BundleNotSupported(version, 'unknown bundle format')
94
95
1185.82.74 by Aaron Bentley
Allow custom base for any revision
96
def write(source, revision_ids, f, version=None, forced_bases={}):
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
97
    """Serialize a list of bundles to a filelike object.
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
98
1185.82.4 by John Arbash Meinel
Created output format, slightly simplified code
99
    :param source: A source for revision information
100
    :param revision_ids: The list of revision ids to serialize
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
101
    :param f: The file to output to
102
    :param version: [optional] target serialization version
103
    """
104
1927.1.1 by John Arbash Meinel
Lock the repository more often
105
    source.lock_read()
106
    try:
2520.4.53 by Aaron Bentley
refactor bundle serialization to make write_bundle primary
107
        return get_serializer(version).write(source, revision_ids,
108
                                             forced_bases, f)
1927.1.1 by John Arbash Meinel
Lock the repository more often
109
    finally:
110
        source.unlock()
1185.82.4 by John Arbash Meinel
Created output format, slightly simplified code
111
112
1910.2.50 by Aaron Bentley
start work on format 0.9 serializer
113
def write_bundle(repository, revision_id, base_revision_id, out, format=None):
1910.7.17 by Andrew Bennetts
Various cosmetic changes.
114
    """Write a bundle of revisions.
115
116
    :param repository: Repository containing revisions to serialize.
117
    :param revision_id: Head revision_id of the bundle.
118
    :param base_revision_id: Revision assumed to be present in repositories
119
         applying the bundle.
120
    :param out: Output file.
121
    """
2520.4.54 by Aaron Bentley
Hang a create_bundle method off repository
122
    repository.lock_read()
123
    try:
124
        return get_serializer(format).write_bundle(repository, revision_id,
125
                                                   base_revision_id, out)
126
    finally:
127
        repository.unlock()
1185.82.53 by Aaron Bentley
Factored out write_changeset to select revisions
128
129
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
130
class BundleSerializer(object):
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
131
    """The base class for Serializers.
132
133
    Common functionality should be included here.
134
    """
135
    def __init__(self, version):
136
        self.version = version
137
138
    def read(self, f):
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
139
        """Read the rest of the bundles from the supplied file.
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
140
141
        :param f: The file to read from
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
142
        :return: A list of bundle trees
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
143
        """
144
        raise NotImplementedError
145
2520.4.53 by Aaron Bentley
refactor bundle serialization to make write_bundle primary
146
    def write_bundle(self, repository, target, base, fileobj):
147
        """Write the bundle to the supplied file.
148
149
        :param repository: The repository to retrieve revision data from
150
        :param target: The revision to provide data for
151
        :param base: The most recent of ancestor of the revision that does not
152
            need to be included in the bundle
153
        :param fileobj: The file to output to
154
        """
155
        raise NotImplementedError
156
157
    def _write_bundle(self, repository, revision_id, base_revision_id, out):
158
        """Helper function for translating write_bundle to write"""
159
        forced_bases = {revision_id:base_revision_id}
160
        if base_revision_id is NULL_REVISION:
161
            base_revision_id = None
2520.4.63 by Aaron Bentley
Merge bzr.dev
162
        revision_ids = set(repository.get_ancestry(revision_id,
163
                           topo_sorted=False))
164
        revision_ids.difference_update(repository.get_ancestry(
165
            base_revision_id, topo_sorted=False))
166
        revision_ids = list(repository.get_graph().iter_topo_order(
167
            revision_ids))
168
        revision_ids.reverse()
2520.4.53 by Aaron Bentley
refactor bundle serialization to make write_bundle primary
169
        self.write(repository, revision_ids, forced_bases, out)
170
        return revision_ids
171
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
172
173
def register(version, klass, overwrite=False):
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
174
    """Register a BundleSerializer version.
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
175
176
    :param version: The version associated with this format
177
    :param klass: The class to instantiate, which must take a version argument
178
    """
179
    global _serializers
180
    if overwrite:
181
        _serializers[version] = klass
182
        return
183
1963.2.1 by Robey Pointer
remove usage of has_key()
184
    if version not in _serializers:
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
185
        _serializers[version] = klass
186
187
188
def register_lazy(version, module, classname, overwrite=False):
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
189
    """Register lazy-loaded bundle serializer.
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
190
191
    :param version: The version associated with this reader
192
    :param module: String indicating what module should be loaded
193
    :param classname: Name of the class that will be instantiated
194
    :param overwrite: Should this version override a default
195
    """
196
    def _loader(version):
5436.2.1 by Andrew Bennetts
Add bzrlib.pyutils, which has get_named_object, a wrapper around __import__.
197
        klass = pyutils.get_named_object(module, classname)
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
198
        return klass(version)
1185.82.4 by John Arbash Meinel
Created output format, slightly simplified code
199
    register(version, _loader, overwrite=overwrite)
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
200
201
1185.82.96 by Aaron Bentley
Got first binary test passing
202
def binary_diff(old_filename, old_lines, new_filename, new_lines, to_file):
203
    temp = StringIO()
204
    internal_diff(old_filename, old_lines, new_filename, new_lines, temp,
205
                  allow_binary=True)
206
    temp.seek(0)
207
    base64.encode(temp, to_file)
208
    to_file.write('\n')
209
1551.7.3 by Aaron Bentley
Fix strict testaments, as_sha1
210
register_lazy('0.8', 'bzrlib.bundle.serializer.v08', 'BundleSerializerV08')
1910.2.50 by Aaron Bentley
start work on format 0.9 serializer
211
register_lazy('0.9', 'bzrlib.bundle.serializer.v09', 'BundleSerializerV09')
2520.4.123 by Aaron Bentley
Cleanup of bundle code
212
register_lazy(v4_string, 'bzrlib.bundle.serializer.v4',
2520.4.72 by Aaron Bentley
Rename format to 4alpha
213
              'BundleSerializerV4')
214
register_lazy(None, 'bzrlib.bundle.serializer.v4', 'BundleSerializerV4')
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
215