~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/pack.py

(jameinel) Allow 'bzr serve' to interpret SIGHUP as a graceful shutdown.
 (bug #795025) (John A Meinel)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007 Canonical Ltd
 
1
# Copyright (C) 2007, 2009, 2010 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
"""Container format for Bazaar data.
18
18
 
34
34
 
35
35
def _check_name(name):
36
36
    """Do some basic checking of 'name'.
37
 
    
 
37
 
38
38
    At the moment, this just checks that there are no whitespace characters in a
39
39
    name.
40
40
 
47
47
 
48
48
def _check_name_encoding(name):
49
49
    """Check that 'name' is valid UTF-8.
50
 
    
 
50
 
51
51
    This is separate from _check_name because UTF-8 decoding is relatively
52
52
    expensive, and we usually want to avoid it.
53
53
 
61
61
 
62
62
class ContainerSerialiser(object):
63
63
    """A helper class for serialising containers.
64
 
    
 
64
 
65
65
    It simply returns bytes from method calls to 'begin', 'end' and
66
66
    'bytes_record'.  You may find ContainerWriter to be a more convenient
67
67
    interface.
138
138
 
139
139
    def add_bytes_record(self, bytes, names):
140
140
        """Add a Bytes record with the given names.
141
 
        
 
141
 
142
142
        :param bytes: The bytes to insert.
143
143
        :param names: The names to give the inserted bytes. Each name is
144
144
            a tuple of bytestrings. The bytestrings may not contain
159
159
 
160
160
 
161
161
class ReadVFile(object):
162
 
    """Adapt a readv result iterator to a file like protocol."""
 
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.
163
170
 
164
171
    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)
165
181
        self.readv_result = readv_result
166
 
        # the most recent readv result block
167
182
        self._string = None
168
183
 
169
184
    def _next(self):
170
185
        if (self._string is None or
171
186
            self._string.tell() == self._string_length):
172
 
            length, data = self.readv_result.next()
 
187
            offset, data = self.readv_result.next()
173
188
            self._string_length = len(data)
174
189
            self._string = StringIO(data)
175
190
 
177
192
        self._next()
178
193
        result = self._string.read(length)
179
194
        if len(result) < length:
180
 
            raise errors.BzrError('request for too much data from a readv hunk.')
 
195
            raise errors.BzrError('wanted %d bytes but next '
 
196
                'hunk only contains %d: %r...' %
 
197
                (length, len(result), result[:20]))
181
198
        return result
182
199
 
183
200
    def readline(self):
185
202
        self._next()
186
203
        result = self._string.readline()
187
204
        if self._string.tell() == self._string_length and result[-1] != '\n':
188
 
            raise errors.BzrError('short readline in the readvfile hunk.')
 
205
            raise errors.BzrError('short readline in the readvfile hunk: %r'
 
206
                % (result, ))
189
207
        return result
190
208
 
191
209
 
234
252
        is a ``list`` and bytes is a function that takes one argument,
235
253
        ``max_length``.
236
254
 
237
 
        You **must not** call the callable after advancing the interator to the
 
255
        You **must not** call the callable after advancing the iterator to the
238
256
        next record.  That is, this code is invalid::
239
257
 
240
258
            record_iter = container.iter_records()
241
259
            names1, callable1 = record_iter.next()
242
260
            names2, callable2 = record_iter.next()
243
261
            bytes1 = callable1(None)
244
 
        
 
262
 
245
263
        As it will give incorrect results and invalidate the state of the
246
264
        ContainerReader.
247
265
 
248
 
        :raises ContainerError: if any sort of containter corruption is
 
266
        :raises ContainerError: if any sort of container corruption is
249
267
            detected, e.g. UnknownContainerFormatError is the format of the
250
268
            container is unrecognised.
251
269
        :seealso: ContainerReader.read
252
270
        """
253
271
        self._read_format()
254
272
        return self._iter_records()
255
 
    
 
273
 
256
274
    def iter_record_objects(self):
257
275
        """Iterate over the container, yielding each record as it is read.
258
276
 
260
278
        methods.  Like with iter_records, it is not safe to use a record object
261
279
        after advancing the iterator to yield next record.
262
280
 
263
 
        :raises ContainerError: if any sort of containter corruption is
 
281
        :raises ContainerError: if any sort of container corruption is
264
282
            detected, e.g. UnknownContainerFormatError is the format of the
265
283
            container is unrecognised.
266
284
        :seealso: iter_records
267
285
        """
268
286
        self._read_format()
269
287
        return self._iter_record_objects()
270
 
    
 
288
 
271
289
    def _iter_records(self):
272
290
        for record in self._iter_record_objects():
273
291
            yield record.read()
315
333
                # risk that the same unicode string has been encoded two
316
334
                # different ways.
317
335
                if name_tuple in all_names:
318
 
                    raise errors.DuplicateRecordNameError(name_tuple)
 
336
                    raise errors.DuplicateRecordNameError(name_tuple[0])
319
337
                all_names.add(name_tuple)
320
338
        excess_bytes = self.reader_func(1)
321
339
        if excess_bytes != '':
342
360
        except ValueError:
343
361
            raise errors.InvalidRecordError(
344
362
                "%r is not a valid length." % (length_line,))
345
 
        
 
363
 
346
364
        # Read the list of names.
347
365
        names = []
348
366
        while True:
406
424
        # the buffer.
407
425
        last_buffer_length = None
408
426
        cur_buffer_length = len(self._buffer)
409
 
        while cur_buffer_length != last_buffer_length:
 
427
        last_state_handler = None
 
428
        while (cur_buffer_length != last_buffer_length
 
429
               or last_state_handler != self._state_handler):
410
430
            last_buffer_length = cur_buffer_length
 
431
            last_state_handler = self._state_handler
411
432
            self._state_handler()
412
433
            cur_buffer_length = len(self._buffer)
413
434
 
414
 
    def read_pending_records(self):
415
 
        records = self._parsed_records
416
 
        self._parsed_records = []
417
 
        return records
418
 
    
 
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
 
419
445
    def _consume_line(self):
420
446
        """Take a line out of the buffer, and return the line.
421
447
 
468
494
            for name_part in name_parts:
469
495
                _check_name(name_part)
470
496
            self._current_record_names.append(name_parts)
471
 
            
 
497
 
472
498
    def _state_expecting_body(self):
473
499
        if len(self._buffer) >= self._current_record_length:
474
500
            body_bytes = self._buffer[:self._current_record_length]