/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/pack.py

  • Committer: John Arbash Meinel
  • Date: 2007-08-02 21:00:51 UTC
  • mto: This revision was merged to the branch mainline in revision 2670.
  • Revision ID: john@arbash-meinel.com-20070802210051-lqpg4mgbeyl3h2ld
(Lukáš Lalinský) Add a special header for intptr_t for MSVC which doesn't have it in the standard place

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007, 2009, 2010 Canonical Ltd
 
1
# Copyright (C) 2007 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
"""Container format for Bazaar data.
18
18
 
19
 
"Containers" and "records" are described in
20
 
doc/developers/container-format.txt.
 
19
"Containers" and "records" are described in doc/developers/container-format.txt.
21
20
"""
22
21
 
23
22
from cStringIO import StringIO
34
33
 
35
34
def _check_name(name):
36
35
    """Do some basic checking of 'name'.
37
 
 
 
36
    
38
37
    At the moment, this just checks that there are no whitespace characters in a
39
38
    name.
40
39
 
47
46
 
48
47
def _check_name_encoding(name):
49
48
    """Check that 'name' is valid UTF-8.
50
 
 
 
49
    
51
50
    This is separate from _check_name because UTF-8 decoding is relatively
52
51
    expensive, and we usually want to avoid it.
53
52
 
59
58
        raise errors.InvalidRecordError(str(e))
60
59
 
61
60
 
62
 
class ContainerSerialiser(object):
63
 
    """A helper class for serialising containers.
64
 
 
65
 
    It simply returns bytes from method calls to 'begin', 'end' and
66
 
    'bytes_record'.  You may find ContainerWriter to be a more convenient
67
 
    interface.
68
 
    """
69
 
 
70
 
    def begin(self):
71
 
        """Return the bytes to begin a container."""
72
 
        return FORMAT_ONE + "\n"
73
 
 
74
 
    def end(self):
75
 
        """Return the bytes to finish a container."""
76
 
        return "E"
77
 
 
78
 
    def bytes_record(self, bytes, names):
79
 
        """Return the bytes for a Bytes record with the given name and
80
 
        contents.
81
 
        """
82
 
        # Kind marker
83
 
        byte_sections = ["B"]
84
 
        # Length
85
 
        byte_sections.append(str(len(bytes)) + "\n")
86
 
        # Names
87
 
        for name_tuple in names:
88
 
            # Make sure we're writing valid names.  Note that we will leave a
89
 
            # half-written record if a name is bad!
90
 
            for name in name_tuple:
91
 
                _check_name(name)
92
 
            byte_sections.append('\x00'.join(name_tuple) + "\n")
93
 
        # End of headers
94
 
        byte_sections.append("\n")
95
 
        # Finally, the contents.
96
 
        byte_sections.append(bytes)
97
 
        # XXX: This causes a memory copy of bytes in size, but is usually
98
 
        # faster than two write calls (12 vs 13 seconds to output a gig of
99
 
        # 1k records.) - results may differ on significantly larger records
100
 
        # like .iso's but as they should be rare in any case and thus not
101
 
        # likely to be the common case. The biggest issue is causing extreme
102
 
        # memory pressure in that case. One possibly improvement here is to
103
 
        # check the size of the content before deciding to join here vs call
104
 
        # write twice.
105
 
        return ''.join(byte_sections)
106
 
 
107
 
 
108
61
class ContainerWriter(object):
109
 
    """A class for writing containers to a file.
110
 
 
111
 
    :attribute records_written: The number of user records added to the
112
 
        container. This does not count the prelude or suffix of the container
113
 
        introduced by the begin() and end() methods.
114
 
    """
 
62
    """A class for writing containers."""
115
63
 
116
64
    def __init__(self, write_func):
117
65
        """Constructor.
121
69
        """
122
70
        self._write_func = write_func
123
71
        self.current_offset = 0
124
 
        self.records_written = 0
125
 
        self._serialiser = ContainerSerialiser()
126
72
 
127
73
    def begin(self):
128
74
        """Begin writing a container."""
129
 
        self.write_func(self._serialiser.begin())
 
75
        self.write_func(FORMAT_ONE + "\n")
130
76
 
131
77
    def write_func(self, bytes):
132
78
        self._write_func(bytes)
134
80
 
135
81
    def end(self):
136
82
        """Finish writing a container."""
137
 
        self.write_func(self._serialiser.end())
 
83
        self.write_func("E")
138
84
 
139
85
    def add_bytes_record(self, bytes, names):
140
86
        """Add a Bytes record with the given names.
141
 
 
 
87
        
142
88
        :param bytes: The bytes to insert.
143
 
        :param names: The names to give the inserted bytes. Each name is
144
 
            a tuple of bytestrings. The bytestrings may not contain
145
 
            whitespace.
 
89
        :param names: The names to give the inserted bytes.
146
90
        :return: An offset, length tuple. The offset is the offset
147
91
            of the record within the container, and the length is the
148
92
            length of data that will need to be read to reconstitute the
151
95
            and thus are only suitable for use by a ContainerReader.
152
96
        """
153
97
        current_offset = self.current_offset
154
 
        serialised_record = self._serialiser.bytes_record(bytes, names)
155
 
        self.write_func(serialised_record)
156
 
        self.records_written += 1
 
98
        # Kind marker
 
99
        self.write_func("B")
 
100
        # Length
 
101
        self.write_func(str(len(bytes)) + "\n")
 
102
        # Names
 
103
        for name in names:
 
104
            # Make sure we're writing valid names.  Note that we will leave a
 
105
            # half-written record if a name is bad!
 
106
            _check_name(name)
 
107
            self.write_func(name + "\n")
 
108
        # End of headers
 
109
        self.write_func("\n")
 
110
        # Finally, the contents.
 
111
        self.write_func(bytes)
157
112
        # return a memo of where we wrote data to allow random access.
158
113
        return current_offset, self.current_offset - current_offset
159
114
 
160
115
 
161
116
class ReadVFile(object):
162
 
    """Adapt a readv result iterator to a file like protocol.
163
 
    
164
 
    The readv result must support the iterator protocol returning (offset,
165
 
    data_bytes) pairs.
166
 
    """
167
 
 
168
 
    # XXX: This could be a generic transport class, as other code may want to
169
 
    # gradually consume the readv result.
 
117
    """Adapt a readv result iterator to a file like protocol."""
170
118
 
171
119
    def __init__(self, readv_result):
172
 
        """Construct a new ReadVFile wrapper.
173
 
 
174
 
        :seealso: make_readv_reader
175
 
 
176
 
        :param readv_result: the most recent readv result - list or generator
177
 
        """
178
 
        # readv can return a sequence or an iterator, but we require an
179
 
        # iterator to know how much has been consumed.
180
 
        readv_result = iter(readv_result)
181
120
        self.readv_result = readv_result
 
121
        # the most recent readv result block
182
122
        self._string = None
183
123
 
184
124
    def _next(self):
185
125
        if (self._string is None or
186
126
            self._string.tell() == self._string_length):
187
 
            offset, data = self.readv_result.next()
 
127
            length, data = self.readv_result.next()
188
128
            self._string_length = len(data)
189
129
            self._string = StringIO(data)
190
130
 
192
132
        self._next()
193
133
        result = self._string.read(length)
194
134
        if len(result) < length:
195
 
            raise errors.BzrError('wanted %d bytes but next '
196
 
                'hunk only contains %d: %r...' %
197
 
                (length, len(result), result[:20]))
 
135
            raise errors.BzrError('request for too much data from a readv hunk.')
198
136
        return result
199
137
 
200
138
    def readline(self):
202
140
        self._next()
203
141
        result = self._string.readline()
204
142
        if self._string.tell() == self._string_length and result[-1] != '\n':
205
 
            raise errors.BzrError('short readline in the readvfile hunk: %r'
206
 
                % (result, ))
 
143
            raise errors.BzrError('short readline in the readvfile hunk.')
207
144
        return result
208
145
 
209
146
 
252
189
        is a ``list`` and bytes is a function that takes one argument,
253
190
        ``max_length``.
254
191
 
255
 
        You **must not** call the callable after advancing the iterator to the
 
192
        You **must not** call the callable after advancing the interator to the
256
193
        next record.  That is, this code is invalid::
257
194
 
258
195
            record_iter = container.iter_records()
259
196
            names1, callable1 = record_iter.next()
260
197
            names2, callable2 = record_iter.next()
261
198
            bytes1 = callable1(None)
262
 
 
 
199
        
263
200
        As it will give incorrect results and invalidate the state of the
264
201
        ContainerReader.
265
202
 
266
 
        :raises ContainerError: if any sort of container corruption is
 
203
        :raises ContainerError: if any sort of containter corruption is
267
204
            detected, e.g. UnknownContainerFormatError is the format of the
268
205
            container is unrecognised.
269
206
        :seealso: ContainerReader.read
270
207
        """
271
208
        self._read_format()
272
209
        return self._iter_records()
273
 
 
 
210
    
274
211
    def iter_record_objects(self):
275
212
        """Iterate over the container, yielding each record as it is read.
276
213
 
278
215
        methods.  Like with iter_records, it is not safe to use a record object
279
216
        after advancing the iterator to yield next record.
280
217
 
281
 
        :raises ContainerError: if any sort of container corruption is
 
218
        :raises ContainerError: if any sort of containter corruption is
282
219
            detected, e.g. UnknownContainerFormatError is the format of the
283
220
            container is unrecognised.
284
221
        :seealso: iter_records
285
222
        """
286
223
        self._read_format()
287
224
        return self._iter_record_objects()
288
 
 
 
225
    
289
226
    def _iter_records(self):
290
227
        for record in self._iter_record_objects():
291
228
            yield record.read()
325
262
        all_names = set()
326
263
        for record_names, read_bytes in self.iter_records():
327
264
            read_bytes(None)
328
 
            for name_tuple in record_names:
329
 
                for name in name_tuple:
330
 
                    _check_name_encoding(name)
 
265
            for name in record_names:
 
266
                _check_name_encoding(name)
331
267
                # Check that the name is unique.  Note that Python will refuse
332
268
                # to decode non-shortest forms of UTF-8 encoding, so there is no
333
269
                # risk that the same unicode string has been encoded two
334
270
                # different ways.
335
 
                if name_tuple in all_names:
336
 
                    raise errors.DuplicateRecordNameError(name_tuple)
337
 
                all_names.add(name_tuple)
 
271
                if name in all_names:
 
272
                    raise errors.DuplicateRecordNameError(name)
 
273
                all_names.add(name)
338
274
        excess_bytes = self.reader_func(1)
339
275
        if excess_bytes != '':
340
276
            raise errors.ContainerHasExcessDataError(excess_bytes)
360
296
        except ValueError:
361
297
            raise errors.InvalidRecordError(
362
298
                "%r is not a valid length." % (length_line,))
363
 
 
 
299
        
364
300
        # Read the list of names.
365
301
        names = []
366
302
        while True:
367
 
            name_line = self._read_line()
368
 
            if name_line == '':
 
303
            name = self._read_line()
 
304
            if name == '':
369
305
                break
370
 
            name_tuple = tuple(name_line.split('\x00'))
371
 
            for name in name_tuple:
372
 
                _check_name(name)
373
 
            names.append(name_tuple)
 
306
            _check_name(name)
 
307
            names.append(name)
374
308
 
375
309
        self._remaining_length = length
376
310
        return names, self._content_reader
394
328
        :raises ContainerError: if this record is invalid.
395
329
        """
396
330
        names, read_bytes = self.read()
397
 
        for name_tuple in names:
398
 
            for name in name_tuple:
399
 
                _check_name_encoding(name)
 
331
        for name in names:
 
332
            _check_name_encoding(name)
400
333
        read_bytes(None)
401
334
 
402
 
 
403
 
class ContainerPushParser(object):
404
 
    """A "push" parser for container format 1.
405
 
 
406
 
    It accepts bytes via the ``accept_bytes`` method, and parses them into
407
 
    records which can be retrieved via the ``read_pending_records`` method.
408
 
    """
409
 
 
410
 
    def __init__(self):
411
 
        self._buffer = ''
412
 
        self._state_handler = self._state_expecting_format_line
413
 
        self._parsed_records = []
414
 
        self._reset_current_record()
415
 
        self.finished = False
416
 
 
417
 
    def _reset_current_record(self):
418
 
        self._current_record_length = None
419
 
        self._current_record_names = []
420
 
 
421
 
    def accept_bytes(self, bytes):
422
 
        self._buffer += bytes
423
 
        # Keep iterating the state machine until it stops consuming bytes from
424
 
        # the buffer.
425
 
        last_buffer_length = None
426
 
        cur_buffer_length = len(self._buffer)
427
 
        last_state_handler = None
428
 
        while (cur_buffer_length != last_buffer_length
429
 
               or last_state_handler != self._state_handler):
430
 
            last_buffer_length = cur_buffer_length
431
 
            last_state_handler = self._state_handler
432
 
            self._state_handler()
433
 
            cur_buffer_length = len(self._buffer)
434
 
 
435
 
    def read_pending_records(self, max=None):
436
 
        if max:
437
 
            records = self._parsed_records[:max]
438
 
            del self._parsed_records[:max]
439
 
            return records
440
 
        else:
441
 
            records = self._parsed_records
442
 
            self._parsed_records = []
443
 
            return records
444
 
 
445
 
    def _consume_line(self):
446
 
        """Take a line out of the buffer, and return the line.
447
 
 
448
 
        If a newline byte is not found in the buffer, the buffer is
449
 
        unchanged and this returns None instead.
450
 
        """
451
 
        newline_pos = self._buffer.find('\n')
452
 
        if newline_pos != -1:
453
 
            line = self._buffer[:newline_pos]
454
 
            self._buffer = self._buffer[newline_pos+1:]
455
 
            return line
456
 
        else:
457
 
            return None
458
 
 
459
 
    def _state_expecting_format_line(self):
460
 
        line = self._consume_line()
461
 
        if line is not None:
462
 
            if line != FORMAT_ONE:
463
 
                raise errors.UnknownContainerFormatError(line)
464
 
            self._state_handler = self._state_expecting_record_type
465
 
 
466
 
    def _state_expecting_record_type(self):
467
 
        if len(self._buffer) >= 1:
468
 
            record_type = self._buffer[0]
469
 
            self._buffer = self._buffer[1:]
470
 
            if record_type == 'B':
471
 
                self._state_handler = self._state_expecting_length
472
 
            elif record_type == 'E':
473
 
                self.finished = True
474
 
                self._state_handler = self._state_expecting_nothing
475
 
            else:
476
 
                raise errors.UnknownRecordTypeError(record_type)
477
 
 
478
 
    def _state_expecting_length(self):
479
 
        line = self._consume_line()
480
 
        if line is not None:
481
 
            try:
482
 
                self._current_record_length = int(line)
483
 
            except ValueError:
484
 
                raise errors.InvalidRecordError(
485
 
                    "%r is not a valid length." % (line,))
486
 
            self._state_handler = self._state_expecting_name
487
 
 
488
 
    def _state_expecting_name(self):
489
 
        encoded_name_parts = self._consume_line()
490
 
        if encoded_name_parts == '':
491
 
            self._state_handler = self._state_expecting_body
492
 
        elif encoded_name_parts:
493
 
            name_parts = tuple(encoded_name_parts.split('\x00'))
494
 
            for name_part in name_parts:
495
 
                _check_name(name_part)
496
 
            self._current_record_names.append(name_parts)
497
 
 
498
 
    def _state_expecting_body(self):
499
 
        if len(self._buffer) >= self._current_record_length:
500
 
            body_bytes = self._buffer[:self._current_record_length]
501
 
            self._buffer = self._buffer[self._current_record_length:]
502
 
            record = (self._current_record_names, body_bytes)
503
 
            self._parsed_records.append(record)
504
 
            self._reset_current_record()
505
 
            self._state_handler = self._state_expecting_record_type
506
 
 
507
 
    def _state_expecting_nothing(self):
508
 
        pass
509
 
 
510
 
    def read_size_hint(self):
511
 
        hint = 16384
512
 
        if self._state_handler == self._state_expecting_body:
513
 
            remaining = self._current_record_length - len(self._buffer)
514
 
            if remaining < 0:
515
 
                remaining = 0
516
 
            return max(hint, remaining)
517
 
        return hint
518
 
 
519
 
 
520
 
def iter_records_from_file(source_file):
521
 
    parser = ContainerPushParser()
522
 
    while True:
523
 
        bytes = source_file.read(parser.read_size_hint())
524
 
        parser.accept_bytes(bytes)
525
 
        for record in parser.read_pending_records():
526
 
            yield record
527
 
        if parser.finished:
528
 
            break
529