303
303
new_tree.id2path(ie.file_id)])
304
304
action.add_property('last-changed', ie.revision)
305
305
action.write(self.to_file)
308
class BundleReader(object):
309
"""This class reads in a bundle from a file, and returns
310
a Bundle object, which can then be applied against a tree.
312
def __init__(self, from_file):
313
"""Read in the bundle from the file.
315
:param from_file: A file-like object (must have iterator support).
317
object.__init__(self)
318
self.from_file = iter(from_file)
319
self._next_line = None
321
self.info = BundleInfo()
322
# We put the actual inventory ids in the footer, so that the patch
323
# is easier to read for humans.
324
# Unfortunately, that means we need to read everything before we
325
# can create a proper bundle.
331
while self._next_line is not None:
332
if not self._read_revision_header():
334
if self._next_line is None:
340
"""Make sure that the information read in makes sense
341
and passes appropriate checksums.
343
# Fill in all the missing blanks for the revisions
344
# and generate the real_revisions list.
345
self.info.complete_info()
348
"""yield the next line, but secretly
349
keep 1 extra line for peeking.
351
for line in self.from_file:
352
last = self._next_line
353
self._next_line = line
355
#mutter('yielding line: %r' % last)
357
last = self._next_line
358
self._next_line = None
359
#mutter('yielding line: %r' % last)
362
def _read_revision_header(self):
363
found_something = False
364
self.info.revisions.append(RevisionInfo(None))
365
for line in self._next():
366
# The bzr header is terminated with a blank line
367
# which does not start with '#'
368
if line is None or line == '\n':
370
found_something = True
371
self._handle_next(line)
372
if not found_something:
373
# Nothing was there, so remove the added revision
374
self.info.revisions.pop()
375
return found_something
377
def _read_next_entry(self, line, indent=1):
378
"""Read in a key-value pair
380
if not line.startswith('#'):
381
raise MalformedHeader('Bzr header did not start with #')
382
line = line[1:-1].decode('utf-8') # Remove the '#' and '\n'
383
if line[:indent] == ' '*indent:
386
return None, None# Ignore blank lines
388
loc = line.find(': ')
393
value = self._read_many(indent=indent+2)
394
elif line[-1:] == ':':
396
value = self._read_many(indent=indent+2)
398
raise MalformedHeader('While looking for key: value pairs,'
399
' did not find the colon %r' % (line))
401
key = key.replace(' ', '_')
402
#mutter('found %s: %s' % (key, value))
405
def _handle_next(self, line):
408
key, value = self._read_next_entry(line, indent=1)
409
mutter('_handle_next %r => %r' % (key, value))
413
revision_info = self.info.revisions[-1]
414
if hasattr(revision_info, key):
415
if getattr(revision_info, key) is None:
416
setattr(revision_info, key, value)
418
raise MalformedHeader('Duplicated Key: %s' % key)
420
# What do we do with a key we don't recognize
421
raise MalformedHeader('Unknown Key: "%s"' % key)
423
def _read_many(self, indent):
424
"""If a line ends with no entry, that means that it should be
425
followed with multiple lines of values.
427
This detects the end of the list, because it will be a line that
428
does not start properly indented.
431
start = '#' + (' '*indent)
433
if self._next_line is None or self._next_line[:len(start)] != start:
436
for line in self._next():
437
values.append(line[len(start):-1].decode('utf-8'))
438
if self._next_line is None or self._next_line[:len(start)] != start:
442
def _read_one_patch(self):
443
"""Read in one patch, return the complete patch, along with
446
:return: action, lines, do_continue
448
#mutter('_read_one_patch: %r' % self._next_line)
449
# Peek and see if there are no patches
450
if self._next_line is None or self._next_line.startswith('#'):
451
return None, [], False
455
for line in self._next():
457
if not line.startswith('==='):
458
raise MalformedPatches('The first line of all patches'
459
' should be a bzr meta line "==="'
461
action = line[4:-1].decode('utf-8')
462
elif line.startswith('... '):
463
action += line[len('... '):-1].decode('utf-8')
465
if (self._next_line is not None and
466
self._next_line.startswith('===')):
467
return action, lines, True
468
elif self._next_line is None or self._next_line.startswith('#'):
469
return action, lines, False
473
elif not line.startswith('... '):
476
return action, lines, False
478
def _read_patches(self):
480
revision_actions = []
482
action, lines, do_continue = self._read_one_patch()
483
if action is not None:
484
revision_actions.append((action, lines))
485
assert self.info.revisions[-1].tree_actions is None
486
self.info.revisions[-1].tree_actions = revision_actions
488
def _read_footer(self):
489
"""Read the rest of the meta information.
491
:param first_line: The previous step iterates past what it
492
can handle. That extra line is given here.
494
for line in self._next():
495
self._handle_next(line)
496
if not self._next_line.startswith('#'):
499
if self._next_line is None: