/brz/remove-bazaar

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