/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
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
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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.14 by Aaron Bentley
Get most tests passing, use format header
41
def _get_bundle_header(version):
42
    return '%s%s\n' % (BUNDLE_HEADER, version)
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
43
44
def _get_filename(f):
1963.2.4 by Robey Pointer
remove usage of hasattr
45
    return getattr(f, 'name', '<unknown>')
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
46
47
1793.2.2 by Aaron Bentley
Move BundleReader into v07 serializer
48
def read_bundle(f):
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
49
    """Read in a bundle from a filelike object.
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
50
51
    :param f: A file-like object
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
52
    :return: A list of Bundle objects
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
53
    """
54
    version = None
55
    for line in f:
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
56
        m = BUNDLE_HEADER_RE.match(line)
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
57
        if m:
1907.2.2 by Hermann Kraus
Detect wrong eol markers.
58
            if m.group('lineending') != '':
59
                raise errors.UnsupportedEOLMarker()
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
60
            version = m.group('version')
61
            break
1793.2.7 by Aaron Bentley
Fix reporting of malformed, (especially, crlf) bundles
62
        elif line.startswith(BUNDLE_HEADER):
1907.2.1 by Hermann Kraus
Convert bundle errors from Exception to BzrNewError.
63
            raise errors.MalformedHeader(
64
                'Extra characters after version number')
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
65
        m = CHANGESET_OLD_HEADER_RE.match(line)
66
        if m:
67
            version = m.group('version')
1907.2.1 by Hermann Kraus
Convert bundle errors from Exception to BzrNewError.
68
            raise errors.BundleNotSupported(version, 
69
                'old format bundles not supported')
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
70
71
    if version is None:
1793.2.2 by Aaron Bentley
Move BundleReader into v07 serializer
72
        raise errors.NotABundle('Did not find an opening header')
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
73
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
74
    # Now we have a version, to figure out how to read the bundle 
1963.2.1 by Robey Pointer
remove usage of has_key()
75
    if version not in _serializers:
1907.2.1 by Hermann Kraus
Convert bundle errors from Exception to BzrNewError.
76
        raise errors.BundleNotSupported(version, 
77
            'version not listed in known versions')
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
78
79
    serializer = _serializers[version](version)
80
81
    return serializer.read(f)
82
83
2520.4.53 by Aaron Bentley
refactor bundle serialization to make write_bundle primary
84
def get_serializer(version):
85
    try:
86
        return _serializers[version](version)
87
    except KeyError:
88
        raise errors.BundleNotSupported(version, 'unknown bundle format')
89
90
1185.82.74 by Aaron Bentley
Allow custom base for any revision
91
def write(source, revision_ids, f, version=None, forced_bases={}):
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
92
    """Serialize a list of bundles to a filelike object.
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
93
1185.82.4 by John Arbash Meinel
Created output format, slightly simplified code
94
    :param source: A source for revision information
95
    :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.
96
    :param f: The file to output to
97
    :param version: [optional] target serialization version
98
    """
99
1927.1.1 by John Arbash Meinel
Lock the repository more often
100
    source.lock_read()
101
    try:
2520.4.53 by Aaron Bentley
refactor bundle serialization to make write_bundle primary
102
        return get_serializer(version).write(source, revision_ids,
103
                                             forced_bases, f)
1927.1.1 by John Arbash Meinel
Lock the repository more often
104
    finally:
105
        source.unlock()
1185.82.4 by John Arbash Meinel
Created output format, slightly simplified code
106
107
1910.2.50 by Aaron Bentley
start work on format 0.9 serializer
108
def write_bundle(repository, revision_id, base_revision_id, out, format=None):
1910.7.17 by Andrew Bennetts
Various cosmetic changes.
109
    """Write a bundle of revisions.
110
111
    :param repository: Repository containing revisions to serialize.
112
    :param revision_id: Head revision_id of the bundle.
113
    :param base_revision_id: Revision assumed to be present in repositories
114
         applying the bundle.
115
    :param out: Output file.
116
    """
2520.4.54 by Aaron Bentley
Hang a create_bundle method off repository
117
    repository.lock_read()
118
    try:
119
        return get_serializer(format).write_bundle(repository, revision_id,
120
                                                   base_revision_id, out)
121
    finally:
122
        repository.unlock()
1185.82.53 by Aaron Bentley
Factored out write_changeset to select revisions
123
124
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
125
class BundleSerializer(object):
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
126
    """The base class for Serializers.
127
128
    Common functionality should be included here.
129
    """
130
    def __init__(self, version):
131
        self.version = version
132
133
    def read(self, f):
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
134
        """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.
135
136
        :param f: The file to read from
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
137
        :return: A list of bundle trees
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
138
        """
139
        raise NotImplementedError
140
2520.4.53 by Aaron Bentley
refactor bundle serialization to make write_bundle primary
141
    def write_bundle(self, repository, target, base, fileobj):
142
        """Write the bundle to the supplied file.
143
144
        :param repository: The repository to retrieve revision data from
145
        :param target: The revision to provide data for
146
        :param base: The most recent of ancestor of the revision that does not
147
            need to be included in the bundle
148
        :param fileobj: The file to output to
149
        """
150
        raise NotImplementedError
151
1185.82.74 by Aaron Bentley
Allow custom base for any revision
152
    def write(self, source, revision_ids, forced_bases, f):
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
153
        """Write the bundle to the supplied file.
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
154
2520.4.53 by Aaron Bentley
refactor bundle serialization to make write_bundle primary
155
        DEPRECATED: see write_bundle
1185.82.4 by John Arbash Meinel
Created output format, slightly simplified code
156
        :param source: A source for revision information
157
        :param revision_ids: The list of revision ids to serialize
1185.82.74 by Aaron Bentley
Allow custom base for any revision
158
        :param forced_bases: A dict of revision -> base that overrides default
1185.82.4 by John Arbash Meinel
Created output format, slightly simplified code
159
        :param f: The file to output to
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
160
        """
1185.82.4 by John Arbash Meinel
Created output format, slightly simplified code
161
        raise NotImplementedError
162
2520.4.53 by Aaron Bentley
refactor bundle serialization to make write_bundle primary
163
    def _write_bundle(self, repository, revision_id, base_revision_id, out):
164
        """Helper function for translating write_bundle to write"""
165
        forced_bases = {revision_id:base_revision_id}
166
        if base_revision_id is NULL_REVISION:
167
            base_revision_id = None
2520.4.63 by Aaron Bentley
Merge bzr.dev
168
        revision_ids = set(repository.get_ancestry(revision_id,
169
                           topo_sorted=False))
170
        revision_ids.difference_update(repository.get_ancestry(
171
            base_revision_id, topo_sorted=False))
172
        revision_ids = list(repository.get_graph().iter_topo_order(
173
            revision_ids))
174
        revision_ids.reverse()
2520.4.53 by Aaron Bentley
refactor bundle serialization to make write_bundle primary
175
        self.write(repository, revision_ids, forced_bases, out)
176
        return revision_ids
177
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
178
179
def register(version, klass, overwrite=False):
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
180
    """Register a BundleSerializer version.
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
181
182
    :param version: The version associated with this format
183
    :param klass: The class to instantiate, which must take a version argument
184
    """
185
    global _serializers
186
    if overwrite:
187
        _serializers[version] = klass
188
        return
189
1963.2.1 by Robey Pointer
remove usage of has_key()
190
    if version not in _serializers:
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
191
        _serializers[version] = klass
192
193
194
def register_lazy(version, module, classname, overwrite=False):
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
195
    """Register lazy-loaded bundle serializer.
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
196
197
    :param version: The version associated with this reader
198
    :param module: String indicating what module should be loaded
199
    :param classname: Name of the class that will be instantiated
200
    :param overwrite: Should this version override a default
201
    """
202
    def _loader(version):
203
        mod = __import__(module, globals(), locals(), [classname])
204
        klass = getattr(mod, classname)
205
        return klass(version)
1185.82.4 by John Arbash Meinel
Created output format, slightly simplified code
206
    register(version, _loader, overwrite=overwrite)
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
207
208
1185.82.96 by Aaron Bentley
Got first binary test passing
209
def binary_diff(old_filename, old_lines, new_filename, new_lines, to_file):
210
    temp = StringIO()
211
    internal_diff(old_filename, old_lines, new_filename, new_lines, temp,
212
                  allow_binary=True)
213
    temp.seek(0)
214
    base64.encode(temp, to_file)
215
    to_file.write('\n')
216
1551.7.3 by Aaron Bentley
Fix strict testaments, as_sha1
217
register_lazy('0.8', 'bzrlib.bundle.serializer.v08', 'BundleSerializerV08')
1910.2.50 by Aaron Bentley
start work on format 0.9 serializer
218
register_lazy('0.9', 'bzrlib.bundle.serializer.v09', 'BundleSerializerV09')
2520.4.72 by Aaron Bentley
Rename format to 4alpha
219
register_lazy('4alpha', 'bzrlib.bundle.serializer.v4',
220
              'BundleSerializerV4')
221
register_lazy(None, 'bzrlib.bundle.serializer.v4', 'BundleSerializerV4')
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
222