~bzr-pqm/bzr/bzr.dev

2052.3.1 by John Arbash Meinel
Add tests to cleanup the copyright of all source files
1
# Copyright (C) 2005, 2006 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
24
import bzrlib.errors as errors
1185.82.96 by Aaron Bentley
Got first binary test passing
25
from bzrlib.diff import internal_diff
1185.82.58 by Aaron Bentley
Handle empty branches properly
26
from bzrlib.revision import NULL_REVISION
1551.12.46 by Aaron Bentley
Import highres date functions to old location
27
# For backwards-compatibility
28
from bzrlib.timestamp import unpack_highres_date, format_highres_date
29
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
30
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
31
# New bundles should try to use this header format
32
BUNDLE_HEADER = '# Bazaar revision bundle v'
1907.2.2 by Hermann Kraus
Detect wrong eol markers.
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$')
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
37
38
1793.3.16 by John Arbash Meinel
Add tests to ensure that we gracefully handle opening and trailing non-bundle text.
39
_serializers = {}
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
40
2520.4.136 by Aaron Bentley
Fix format strings
41
v4_string = '4'
2520.4.123 by Aaron Bentley
Cleanup of bundle code
42
2520.4.14 by Aaron Bentley
Get most tests passing, use format header
43
def _get_bundle_header(version):
44
    return '%s%s\n' % (BUNDLE_HEADER, version)
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
45
46
def _get_filename(f):
1963.2.4 by Robey Pointer
remove usage of hasattr
47
    return getattr(f, 'name', '<unknown>')
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
48
49
1793.2.2 by Aaron Bentley
Move BundleReader into v07 serializer
50
def read_bundle(f):
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
51
    """Read in a bundle from a filelike object.
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
52
53
    :param f: A file-like object
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
54
    :return: A list of Bundle objects
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
55
    """
56
    version = None
57
    for line in f:
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
58
        m = BUNDLE_HEADER_RE.match(line)
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
59
        if m:
1907.2.2 by Hermann Kraus
Detect wrong eol markers.
60
            if m.group('lineending') != '':
61
                raise errors.UnsupportedEOLMarker()
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
62
            version = m.group('version')
63
            break
1793.2.7 by Aaron Bentley
Fix reporting of malformed, (especially, crlf) bundles
64
        elif line.startswith(BUNDLE_HEADER):
1907.2.1 by Hermann Kraus
Convert bundle errors from Exception to BzrNewError.
65
            raise errors.MalformedHeader(
66
                'Extra characters after version number')
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
67
        m = CHANGESET_OLD_HEADER_RE.match(line)
68
        if m:
69
            version = m.group('version')
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
70
            raise errors.BundleNotSupported(version,
1907.2.1 by Hermann Kraus
Convert bundle errors from Exception to BzrNewError.
71
                'old format bundles not supported')
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
72
73
    if version is None:
1793.2.2 by Aaron Bentley
Move BundleReader into v07 serializer
74
        raise errors.NotABundle('Did not find an opening header')
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
75
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
76
    # Now we have a version, to figure out how to read the bundle
1963.2.1 by Robey Pointer
remove usage of has_key()
77
    if version not in _serializers:
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
78
        raise errors.BundleNotSupported(version,
1907.2.1 by Hermann Kraus
Convert bundle errors from Exception to BzrNewError.
79
            'version not listed in known versions')
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
80
81
    serializer = _serializers[version](version)
82
83
    return serializer.read(f)
84
85
2520.4.53 by Aaron Bentley
refactor bundle serialization to make write_bundle primary
86
def get_serializer(version):
87
    try:
88
        return _serializers[version](version)
89
    except KeyError:
90
        raise errors.BundleNotSupported(version, 'unknown bundle format')
91
92
1185.82.74 by Aaron Bentley
Allow custom base for any revision
93
def write(source, revision_ids, f, version=None, forced_bases={}):
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
94
    """Serialize a list of bundles to a filelike object.
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
95
1185.82.4 by John Arbash Meinel
Created output format, slightly simplified code
96
    :param source: A source for revision information
97
    :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.
98
    :param f: The file to output to
99
    :param version: [optional] target serialization version
100
    """
101
1927.1.1 by John Arbash Meinel
Lock the repository more often
102
    source.lock_read()
103
    try:
2520.4.53 by Aaron Bentley
refactor bundle serialization to make write_bundle primary
104
        return get_serializer(version).write(source, revision_ids,
105
                                             forced_bases, f)
1927.1.1 by John Arbash Meinel
Lock the repository more often
106
    finally:
107
        source.unlock()
1185.82.4 by John Arbash Meinel
Created output format, slightly simplified code
108
109
1910.2.50 by Aaron Bentley
start work on format 0.9 serializer
110
def write_bundle(repository, revision_id, base_revision_id, out, format=None):
1910.7.17 by Andrew Bennetts
Various cosmetic changes.
111
    """Write a bundle of revisions.
112
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
116
         applying the bundle.
117
    :param out: Output file.
118
    """
2520.4.54 by Aaron Bentley
Hang a create_bundle method off repository
119
    repository.lock_read()
120
    try:
121
        return get_serializer(format).write_bundle(repository, revision_id,
122
                                                   base_revision_id, out)
123
    finally:
124
        repository.unlock()
1185.82.53 by Aaron Bentley
Factored out write_changeset to select revisions
125
126
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
127
class BundleSerializer(object):
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
128
    """The base class for Serializers.
129
130
    Common functionality should be included here.
131
    """
132
    def __init__(self, version):
133
        self.version = version
134
135
    def read(self, f):
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
136
        """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.
137
138
        :param f: The file to read from
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
139
        :return: A list of bundle trees
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
140
        """
141
        raise NotImplementedError
142
2520.4.53 by Aaron Bentley
refactor bundle serialization to make write_bundle primary
143
    def write_bundle(self, repository, target, base, fileobj):
144
        """Write the bundle to the supplied file.
145
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
151
        """
152
        raise NotImplementedError
153
1185.82.74 by Aaron Bentley
Allow custom base for any revision
154
    def write(self, source, revision_ids, forced_bases, f):
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
155
        """Write the bundle to the supplied file.
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
156
2520.4.53 by Aaron Bentley
refactor bundle serialization to make write_bundle primary
157
        DEPRECATED: see write_bundle
1185.82.4 by John Arbash Meinel
Created output format, slightly simplified code
158
        :param source: A source for revision information
159
        :param revision_ids: The list of revision ids to serialize
1185.82.74 by Aaron Bentley
Allow custom base for any revision
160
        :param forced_bases: A dict of revision -> base that overrides default
1185.82.4 by John Arbash Meinel
Created output format, slightly simplified code
161
        :param f: The file to output to
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
162
        """
1185.82.4 by John Arbash Meinel
Created output format, slightly simplified code
163
        raise NotImplementedError
164
2520.4.53 by Aaron Bentley
refactor bundle serialization to make write_bundle primary
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
2520.4.63 by Aaron Bentley
Merge bzr.dev
170
        revision_ids = set(repository.get_ancestry(revision_id,
171
                           topo_sorted=False))
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(
175
            revision_ids))
176
        revision_ids.reverse()
2520.4.53 by Aaron Bentley
refactor bundle serialization to make write_bundle primary
177
        self.write(repository, revision_ids, forced_bases, out)
178
        return revision_ids
179
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
180
181
def register(version, klass, overwrite=False):
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
182
    """Register a BundleSerializer version.
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
183
184
    :param version: The version associated with this format
185
    :param klass: The class to instantiate, which must take a version argument
186
    """
187
    global _serializers
188
    if overwrite:
189
        _serializers[version] = klass
190
        return
191
1963.2.1 by Robey Pointer
remove usage of has_key()
192
    if version not in _serializers:
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
193
        _serializers[version] = klass
194
195
196
def register_lazy(version, module, classname, overwrite=False):
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
197
    """Register lazy-loaded bundle serializer.
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
198
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
203
    """
204
    def _loader(version):
205
        mod = __import__(module, globals(), locals(), [classname])
206
        klass = getattr(mod, classname)
207
        return klass(version)
1185.82.4 by John Arbash Meinel
Created output format, slightly simplified code
208
    register(version, _loader, overwrite=overwrite)
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
209
210
1185.82.96 by Aaron Bentley
Got first binary test passing
211
def binary_diff(old_filename, old_lines, new_filename, new_lines, to_file):
212
    temp = StringIO()
213
    internal_diff(old_filename, old_lines, new_filename, new_lines, temp,
214
                  allow_binary=True)
215
    temp.seek(0)
216
    base64.encode(temp, to_file)
217
    to_file.write('\n')
218
1551.7.3 by Aaron Bentley
Fix strict testaments, as_sha1
219
register_lazy('0.8', 'bzrlib.bundle.serializer.v08', 'BundleSerializerV08')
1910.2.50 by Aaron Bentley
start work on format 0.9 serializer
220
register_lazy('0.9', 'bzrlib.bundle.serializer.v09', 'BundleSerializerV09')
2520.4.123 by Aaron Bentley
Cleanup of bundle code
221
register_lazy(v4_string, 'bzrlib.bundle.serializer.v4',
2520.4.72 by Aaron Bentley
Rename format to 4alpha
222
              'BundleSerializerV4')
223
register_lazy(None, 'bzrlib.bundle.serializer.v4', 'BundleSerializerV4')
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
224