~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/pack.py

  • Committer: Robert Collins
  • Date: 2007-07-04 08:08:13 UTC
  • mfrom: (2572 +trunk)
  • mto: This revision was merged to the branch mainline in revision 2587.
  • Revision ID: robertc@robertcollins.net-20070704080813-wzebx0r88fvwj5rq
Merge bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2007 Canonical Ltd
 
2
#
 
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.
 
7
#
 
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.
 
12
#
 
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
 
 
17
"""Container format for Bazaar data.
 
18
 
 
19
"Containers" and "records" are described in doc/developers/container-format.txt.
 
20
"""
 
21
 
 
22
import re
 
23
 
 
24
from bzrlib import errors
 
25
 
 
26
 
 
27
FORMAT_ONE = "Bazaar pack format 1 (introduced in 0.18)"
 
28
 
 
29
 
 
30
_whitespace_re = re.compile('[\t\n\x0b\x0c\r ]')
 
31
 
 
32
 
 
33
def _check_name(name):
 
34
    """Do some basic checking of 'name'.
 
35
    
 
36
    At the moment, this just checks that there are no whitespace characters in a
 
37
    name.
 
38
 
 
39
    :raises InvalidRecordError: if name is not valid.
 
40
    :seealso: _check_name_encoding
 
41
    """
 
42
    if _whitespace_re.search(name) is not None:
 
43
        raise errors.InvalidRecordError("%r is not a valid name." % (name,))
 
44
 
 
45
 
 
46
def _check_name_encoding(name):
 
47
    """Check that 'name' is valid UTF-8.
 
48
    
 
49
    This is separate from _check_name because UTF-8 decoding is relatively
 
50
    expensive, and we usually want to avoid it.
 
51
 
 
52
    :raises InvalidRecordError: if name is not valid UTF-8.
 
53
    """
 
54
    try:
 
55
        name.decode('utf-8')
 
56
    except UnicodeDecodeError, e:
 
57
        raise errors.InvalidRecordError(str(e))
 
58
 
 
59
 
 
60
class ContainerWriter(object):
 
61
    """A class for writing containers."""
 
62
 
 
63
    def __init__(self, write_func):
 
64
        """Constructor.
 
65
 
 
66
        :param write_func: a callable that will be called when this
 
67
            ContainerWriter needs to write some bytes.
 
68
        """
 
69
        self.write_func = write_func
 
70
 
 
71
    def begin(self):
 
72
        """Begin writing a container."""
 
73
        self.write_func(FORMAT_ONE + "\n")
 
74
 
 
75
    def end(self):
 
76
        """Finish writing a container."""
 
77
        self.write_func("E")
 
78
 
 
79
    def add_bytes_record(self, bytes, names):
 
80
        """Add a Bytes record with the given names."""
 
81
        # Kind marker
 
82
        self.write_func("B")
 
83
        # Length
 
84
        self.write_func(str(len(bytes)) + "\n")
 
85
        # Names
 
86
        for name in names:
 
87
            # Make sure we're writing valid names.  Note that we will leave a
 
88
            # half-written record if a name is bad!
 
89
            _check_name(name)
 
90
            self.write_func(name + "\n")
 
91
        # End of headers
 
92
        self.write_func("\n")
 
93
        # Finally, the contents.
 
94
        self.write_func(bytes)
 
95
 
 
96
 
 
97
class BaseReader(object):
 
98
 
 
99
    def __init__(self, source_file):
 
100
        """Constructor.
 
101
 
 
102
        :param source_file: a file-like object with `read` and `readline`
 
103
            methods.
 
104
        """
 
105
        self._source = source_file
 
106
 
 
107
    def reader_func(self, length=None):
 
108
        return self._source.read(length)
 
109
 
 
110
    def _read_line(self):
 
111
        line = self._source.readline()
 
112
        if not line.endswith('\n'):
 
113
            raise errors.UnexpectedEndOfContainerError()
 
114
        return line.rstrip('\n')
 
115
 
 
116
 
 
117
class ContainerReader(BaseReader):
 
118
    """A class for reading Bazaar's container format."""
 
119
 
 
120
    def iter_records(self):
 
121
        """Iterate over the container, yielding each record as it is read.
 
122
 
 
123
        Each yielded record will be a 2-tuple of (names, callable), where names
 
124
        is a ``list`` and bytes is a function that takes one argument,
 
125
        ``max_length``.
 
126
 
 
127
        You **must not** call the callable after advancing the interator to the
 
128
        next record.  That is, this code is invalid::
 
129
 
 
130
            record_iter = container.iter_records()
 
131
            names1, callable1 = record_iter.next()
 
132
            names2, callable2 = record_iter.next()
 
133
            bytes1 = callable1(None)
 
134
        
 
135
        As it will give incorrect results and invalidate the state of the
 
136
        ContainerReader.
 
137
 
 
138
        :raises ContainerError: if any sort of containter corruption is
 
139
            detected, e.g. UnknownContainerFormatError is the format of the
 
140
            container is unrecognised.
 
141
        :seealso: ContainerReader.read
 
142
        """
 
143
        self._read_format()
 
144
        return self._iter_records()
 
145
    
 
146
    def iter_record_objects(self):
 
147
        """Iterate over the container, yielding each record as it is read.
 
148
 
 
149
        Each yielded record will be an object with ``read`` and ``validate``
 
150
        methods.  Like with iter_records, it is not safe to use a record object
 
151
        after advancing the iterator to yield next record.
 
152
 
 
153
        :raises ContainerError: if any sort of containter corruption is
 
154
            detected, e.g. UnknownContainerFormatError is the format of the
 
155
            container is unrecognised.
 
156
        :seealso: iter_records
 
157
        """
 
158
        self._read_format()
 
159
        return self._iter_record_objects()
 
160
    
 
161
    def _iter_records(self):
 
162
        for record in self._iter_record_objects():
 
163
            yield record.read()
 
164
 
 
165
    def _iter_record_objects(self):
 
166
        while True:
 
167
            record_kind = self.reader_func(1)
 
168
            if record_kind == 'B':
 
169
                # Bytes record.
 
170
                reader = BytesRecordReader(self._source)
 
171
                yield reader
 
172
            elif record_kind == 'E':
 
173
                # End marker.  There are no more records.
 
174
                return
 
175
            elif record_kind == '':
 
176
                # End of stream encountered, but no End Marker record seen, so
 
177
                # this container is incomplete.
 
178
                raise errors.UnexpectedEndOfContainerError()
 
179
            else:
 
180
                # Unknown record type.
 
181
                raise errors.UnknownRecordTypeError(record_kind)
 
182
 
 
183
    def _read_format(self):
 
184
        format = self._read_line()
 
185
        if format != FORMAT_ONE:
 
186
            raise errors.UnknownContainerFormatError(format)
 
187
 
 
188
    def validate(self):
 
189
        """Validate this container and its records.
 
190
 
 
191
        Validating consumes the data stream just like iter_records and
 
192
        iter_record_objects, so you cannot call it after
 
193
        iter_records/iter_record_objects.
 
194
 
 
195
        :raises ContainerError: if something is invalid.
 
196
        """
 
197
        all_names = set()
 
198
        for record_names, read_bytes in self.iter_records():
 
199
            read_bytes(None)
 
200
            for name in record_names:
 
201
                _check_name_encoding(name)
 
202
                # Check that the name is unique.  Note that Python will refuse
 
203
                # to decode non-shortest forms of UTF-8 encoding, so there is no
 
204
                # risk that the same unicode string has been encoded two
 
205
                # different ways.
 
206
                if name in all_names:
 
207
                    raise errors.DuplicateRecordNameError(name)
 
208
                all_names.add(name)
 
209
        excess_bytes = self.reader_func(1)
 
210
        if excess_bytes != '':
 
211
            raise errors.ContainerHasExcessDataError(excess_bytes)
 
212
 
 
213
 
 
214
class BytesRecordReader(BaseReader):
 
215
 
 
216
    def read(self):
 
217
        """Read this record.
 
218
 
 
219
        You can either validate or read a record, you can't do both.
 
220
 
 
221
        :returns: A tuple of (names, callable).  The callable can be called
 
222
            repeatedly to obtain the bytes for the record, with a max_length
 
223
            argument.  If max_length is None, returns all the bytes.  Because
 
224
            records can be arbitrarily large, using None is not recommended
 
225
            unless you have reason to believe the content will fit in memory.
 
226
        """
 
227
        # Read the content length.
 
228
        length_line = self._read_line()
 
229
        try:
 
230
            length = int(length_line)
 
231
        except ValueError:
 
232
            raise errors.InvalidRecordError(
 
233
                "%r is not a valid length." % (length_line,))
 
234
        
 
235
        # Read the list of names.
 
236
        names = []
 
237
        while True:
 
238
            name = self._read_line()
 
239
            if name == '':
 
240
                break
 
241
            _check_name(name)
 
242
            names.append(name)
 
243
 
 
244
        self._remaining_length = length
 
245
        return names, self._content_reader
 
246
 
 
247
    def _content_reader(self, max_length):
 
248
        if max_length is None:
 
249
            length_to_read = self._remaining_length
 
250
        else:
 
251
            length_to_read = min(max_length, self._remaining_length)
 
252
        self._remaining_length -= length_to_read
 
253
        bytes = self.reader_func(length_to_read)
 
254
        if len(bytes) != length_to_read:
 
255
            raise errors.UnexpectedEndOfContainerError()
 
256
        return bytes
 
257
 
 
258
    def validate(self):
 
259
        """Validate this record.
 
260
 
 
261
        You can either validate or read, you can't do both.
 
262
 
 
263
        :raises ContainerError: if this record is invalid.
 
264
        """
 
265
        names, read_bytes = self.read()
 
266
        for name in names:
 
267
            _check_name_encoding(name)
 
268
        read_bytes(None)
 
269