/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
1185.82.53 by Aaron Bentley
Factored out write_changeset to select revisions
123
    base_ancestry = set(repository.get_ancestry(base_revision_id))
124
    revision_ids = [r for r in repository.get_ancestry(revision_id) if r
125
                    not in base_ancestry]
126
    revision_ids = list(reversed(revision_ids))
1910.2.50 by Aaron Bentley
start work on format 0.9 serializer
127
    write(repository, revision_ids, out, format,
1185.82.74 by Aaron Bentley
Allow custom base for any revision
128
          forced_bases = {revision_id:base_revision_id})
1185.82.53 by Aaron Bentley
Factored out write_changeset to select revisions
129
    return revision_ids
130
131
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
132
class BundleSerializer(object):
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
133
    """The base class for Serializers.
134
135
    Common functionality should be included here.
136
    """
137
    def __init__(self, version):
138
        self.version = version
139
140
    def read(self, f):
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
141
        """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.
142
143
        :param f: The file to read from
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
144
        :return: A list of bundle trees
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
145
        """
146
        raise NotImplementedError
147
1185.82.74 by Aaron Bentley
Allow custom base for any revision
148
    def write(self, source, revision_ids, forced_bases, f):
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
149
        """Write the bundle to the supplied file.
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
150
1185.82.4 by John Arbash Meinel
Created output format, slightly simplified code
151
        :param source: A source for revision information
152
        :param revision_ids: The list of revision ids to serialize
1185.82.74 by Aaron Bentley
Allow custom base for any revision
153
        :param forced_bases: A dict of revision -> base that overrides default
1185.82.4 by John Arbash Meinel
Created output format, slightly simplified code
154
        :param f: The file to output to
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
155
        """
1185.82.4 by John Arbash Meinel
Created output format, slightly simplified code
156
        raise NotImplementedError
157
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
158
159
def register(version, klass, overwrite=False):
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
160
    """Register a BundleSerializer version.
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
161
162
    :param version: The version associated with this format
163
    :param klass: The class to instantiate, which must take a version argument
164
    """
165
    global _serializers
166
    if overwrite:
167
        _serializers[version] = klass
168
        return
169
1963.2.1 by Robey Pointer
remove usage of has_key()
170
    if version not in _serializers:
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
171
        _serializers[version] = klass
172
173
174
def register_lazy(version, module, classname, overwrite=False):
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
175
    """Register lazy-loaded bundle serializer.
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
176
177
    :param version: The version associated with this reader
178
    :param module: String indicating what module should be loaded
179
    :param classname: Name of the class that will be instantiated
180
    :param overwrite: Should this version override a default
181
    """
182
    def _loader(version):
183
        mod = __import__(module, globals(), locals(), [classname])
184
        klass = getattr(mod, classname)
185
        return klass(version)
1185.82.4 by John Arbash Meinel
Created output format, slightly simplified code
186
    register(version, _loader, overwrite=overwrite)
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
187
188
1185.82.96 by Aaron Bentley
Got first binary test passing
189
def binary_diff(old_filename, old_lines, new_filename, new_lines, to_file):
190
    temp = StringIO()
191
    internal_diff(old_filename, old_lines, new_filename, new_lines, temp,
192
                  allow_binary=True)
193
    temp.seek(0)
194
    base64.encode(temp, to_file)
195
    to_file.write('\n')
196
1551.7.3 by Aaron Bentley
Fix strict testaments, as_sha1
197
register_lazy('0.8', 'bzrlib.bundle.serializer.v08', 'BundleSerializerV08')
1910.2.50 by Aaron Bentley
start work on format 0.9 serializer
198
register_lazy('0.9', 'bzrlib.bundle.serializer.v09', 'BundleSerializerV09')
2230.3.17 by Aaron Bentley
Set default serializer to 0.9
199
register_lazy(None, 'bzrlib.bundle.serializer.v09', 'BundleSerializerV09')
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
200