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