/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,
6989.1.1 by Jelmer Vernooij
Use format registry for bundles.
27
    registry,
5436.2.1 by Andrew Bennetts
Add bzrlib.pyutils, which has get_named_object, a wrapper around __import__.
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
6855.4.5 by Jelmer Vernooij
Fix more bees, use with rather than try/finally for some files.
39
BUNDLE_HEADER = b'# Bazaar revision bundle v'
1907.2.2 by Hermann Kraus
Detect wrong eol markers.
40
BUNDLE_HEADER_RE = re.compile(
6855.3.1 by Jelmer Vernooij
Several more fixes.
41
    br'^# Bazaar revision bundle v(?P<version>\d+[\w.]*)(?P<lineending>\r?)\n$')
1907.2.2 by Hermann Kraus
Detect wrong eol markers.
42
CHANGESET_OLD_HEADER_RE = re.compile(
6855.3.1 by Jelmer Vernooij
Several more fixes.
43
    br'^# 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
6989.1.1 by Jelmer Vernooij
Use format registry for bundles.
46
serializer_registry = registry.Registry()
2520.4.123 by Aaron Bentley
Cleanup of bundle code
47
48
2520.4.14 by Aaron Bentley
Get most tests passing, use format header
49
def _get_bundle_header(version):
6973.7.8 by Jelmer Vernooij
Fix more tests.
50
    return b''.join([BUNDLE_HEADER, version.encode('ascii'), b'\n'])
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
51
52
def _get_filename(f):
1963.2.4 by Robey Pointer
remove usage of hasattr
53
    return getattr(f, 'name', '<unknown>')
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
54
55
1793.2.2 by Aaron Bentley
Move BundleReader into v07 serializer
56
def read_bundle(f):
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
57
    """Read in a bundle from a filelike object.
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
58
59
    :param f: A file-like object
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
60
    :return: A list of Bundle objects
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
61
    """
62
    version = None
63
    for line in f:
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
64
        m = BUNDLE_HEADER_RE.match(line)
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
65
        if m:
6973.7.10 by Jelmer Vernooij
More fixes.
66
            if m.group('lineending') != b'':
1907.2.2 by Hermann Kraus
Detect wrong eol markers.
67
                raise errors.UnsupportedEOLMarker()
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
68
            version = m.group('version')
69
            break
1793.2.7 by Aaron Bentley
Fix reporting of malformed, (especially, crlf) bundles
70
        elif line.startswith(BUNDLE_HEADER):
1907.2.1 by Hermann Kraus
Convert bundle errors from Exception to BzrNewError.
71
            raise errors.MalformedHeader(
72
                'Extra characters after version number')
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
73
        m = CHANGESET_OLD_HEADER_RE.match(line)
74
        if m:
75
            version = m.group('version')
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
76
            raise errors.BundleNotSupported(version,
1907.2.1 by Hermann Kraus
Convert bundle errors from Exception to BzrNewError.
77
                'old format bundles not supported')
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
78
79
    if version is None:
1793.2.2 by Aaron Bentley
Move BundleReader into v07 serializer
80
        raise errors.NotABundle('Did not find an opening header')
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
81
6973.7.11 by Jelmer Vernooij
Merge trunk.
82
    return get_serializer(version.decode('ascii')).read(f)
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
83
84
2520.4.53 by Aaron Bentley
refactor bundle serialization to make write_bundle primary
85
def get_serializer(version):
86
    try:
6989.1.1 by Jelmer Vernooij
Use format registry for bundles.
87
        serializer = serializer_registry.get(version)
2520.4.53 by Aaron Bentley
refactor bundle serialization to make write_bundle primary
88
    except KeyError:
6989.1.1 by Jelmer Vernooij
Use format registry for bundles.
89
        raise errors.BundleNotSupported(version,
90
            'unknown bundle format')
91
92
    return serializer(version)
2520.4.53 by Aaron Bentley
refactor bundle serialization to make write_bundle primary
93
94
1185.82.74 by Aaron Bentley
Allow custom base for any revision
95
def write(source, revision_ids, f, version=None, forced_bases={}):
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
96
    """Serialize a list of bundles to a filelike object.
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
97
1185.82.4 by John Arbash Meinel
Created output format, slightly simplified code
98
    :param source: A source for revision information
99
    :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.
100
    :param f: The file to output to
101
    :param version: [optional] target serialization version
102
    """
103
6754.8.4 by Jelmer Vernooij
Use new context stuff.
104
    with source.lock_read():
2520.4.53 by Aaron Bentley
refactor bundle serialization to make write_bundle primary
105
        return get_serializer(version).write(source, revision_ids,
106
                                             forced_bases, f)
1185.82.4 by John Arbash Meinel
Created output format, slightly simplified code
107
108
1910.2.50 by Aaron Bentley
start work on format 0.9 serializer
109
def write_bundle(repository, revision_id, base_revision_id, out, format=None):
1910.7.17 by Andrew Bennetts
Various cosmetic changes.
110
    """Write a bundle of revisions.
111
112
    :param repository: Repository containing revisions to serialize.
113
    :param revision_id: Head revision_id of the bundle.
114
    :param base_revision_id: Revision assumed to be present in repositories
115
         applying the bundle.
116
    :param out: Output file.
7029.2.1 by Jelmer Vernooij
don't make assumptions about the order in which revision ids are returned by write_bundle.
117
    :return: List of revision ids written
1910.7.17 by Andrew Bennetts
Various cosmetic changes.
118
    """
6754.8.4 by Jelmer Vernooij
Use new context stuff.
119
    with repository.lock_read():
2520.4.54 by Aaron Bentley
Hang a create_bundle method off repository
120
        return get_serializer(format).write_bundle(repository, revision_id,
121
                                                   base_revision_id, out)
1185.82.53 by Aaron Bentley
Factored out write_changeset to select revisions
122
123
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
124
class BundleSerializer(object):
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
125
    """The base class for Serializers.
126
127
    Common functionality should be included here.
128
    """
129
    def __init__(self, version):
130
        self.version = version
131
132
    def read(self, f):
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
133
        """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.
134
135
        :param f: The file to read from
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
136
        :return: A list of bundle trees
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
137
        """
138
        raise NotImplementedError
139
2520.4.53 by Aaron Bentley
refactor bundle serialization to make write_bundle primary
140
    def write_bundle(self, repository, target, base, fileobj):
141
        """Write the bundle to the supplied file.
142
143
        :param repository: The repository to retrieve revision data from
144
        :param target: The revision to provide data for
145
        :param base: The most recent of ancestor of the revision that does not
146
            need to be included in the bundle
147
        :param fileobj: The file to output to
7029.2.1 by Jelmer Vernooij
don't make assumptions about the order in which revision ids are returned by write_bundle.
148
        :return: List of revision ids written
2520.4.53 by Aaron Bentley
refactor bundle serialization to make write_bundle primary
149
        """
150
        raise NotImplementedError
151
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
152
1185.82.96 by Aaron Bentley
Got first binary test passing
153
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
154
    temp = BytesIO()
1185.82.96 by Aaron Bentley
Got first binary test passing
155
    internal_diff(old_filename, old_lines, new_filename, new_lines, temp,
156
                  allow_binary=True)
157
    temp.seek(0)
158
    base64.encode(temp, to_file)
159
    to_file.write('\n')
160
6989.1.1 by Jelmer Vernooij
Use format registry for bundles.
161
serializer_registry.register_lazy('0.8', 'breezy.bundle.serializer.v08', 'BundleSerializerV08')
162
serializer_registry.register_lazy('0.9', 'breezy.bundle.serializer.v09', 'BundleSerializerV09')
163
serializer_registry.register_lazy('4', 'breezy.bundle.serializer.v4',
2520.4.72 by Aaron Bentley
Rename format to 4alpha
164
              'BundleSerializerV4')
6989.1.1 by Jelmer Vernooij
Use format registry for bundles.
165
serializer_registry.default_key = '4'