424
423
valid_actions[action](kind, extra, lines)
427
class BundleReader(object):
428
"""This class reads in a bundle from a file, and returns
429
a Bundle object, which can then be applied against a tree.
431
def __init__(self, from_file):
432
"""Read in the bundle from the file.
434
:param from_file: A file-like object (must have iterator support).
436
object.__init__(self)
437
self.from_file = iter(from_file)
438
self._next_line = None
440
self.info = BundleInfo()
441
# We put the actual inventory ids in the footer, so that the patch
442
# is easier to read for humans.
443
# Unfortunately, that means we need to read everything before we
444
# can create a proper bundle.
450
while self._next_line is not None:
451
self._read_revision_header()
452
if self._next_line is None:
458
"""Make sure that the information read in makes sense
459
and passes appropriate checksums.
461
# Fill in all the missing blanks for the revisions
462
# and generate the real_revisions list.
463
self.info.complete_info()
465
def get_bundle(self, repository):
466
"""Return the meta information, and a Bundle tree which can
467
be used to populate the local stores and working tree, respectively.
469
return self.info, self.revision_tree(repository, self.info.target)
471
def revision_tree(self, repository, revision_id, base=None):
472
return self.info.revision_tree(repository, revision_id, base)
475
"""yield the next line, but secretly
476
keep 1 extra line for peeking.
478
for line in self.from_file:
479
last = self._next_line
480
self._next_line = line
482
#mutter('yielding line: %r' % last)
484
last = self._next_line
485
self._next_line = None
486
#mutter('yielding line: %r' % last)
489
def _read_header(self):
490
"""Read the bzr header"""
491
header = get_header()
493
for line in self._next():
495
# not all mailers will keep trailing whitespace
498
if (not line.startswith('# ') or not line.endswith('\n')
499
or line[2:-1].decode('utf-8') != header[0]):
500
raise MalformedHeader('Found a header, but it'
501
' was improperly formatted')
502
header.pop(0) # We read this line.
504
break # We found everything.
505
elif (line.startswith('#') and line.endswith('\n')):
506
line = line[1:-1].strip().decode('utf-8')
507
if line[:len(header_str)] == header_str:
508
if line == header[0]:
511
raise MalformedHeader('Found what looks like'
512
' a header, but did not match')
515
raise NotABundle('Did not find an opening header')
517
def _read_revision_header(self):
518
self.info.revisions.append(RevisionInfo(None))
519
for line in self._next():
520
# The bzr header is terminated with a blank line
521
# which does not start with '#'
522
if line is None or line == '\n':
524
self._handle_next(line)
526
def _read_next_entry(self, line, indent=1):
527
"""Read in a key-value pair
529
if not line.startswith('#'):
530
raise MalformedHeader('Bzr header did not start with #')
531
line = line[1:-1].decode('utf-8') # Remove the '#' and '\n'
532
if line[:indent] == ' '*indent:
535
return None, None# Ignore blank lines
537
loc = line.find(': ')
542
value = self._read_many(indent=indent+2)
543
elif line[-1:] == ':':
545
value = self._read_many(indent=indent+2)
547
raise MalformedHeader('While looking for key: value pairs,'
548
' did not find the colon %r' % (line))
550
key = key.replace(' ', '_')
551
#mutter('found %s: %s' % (key, value))
554
def _handle_next(self, line):
557
key, value = self._read_next_entry(line, indent=1)
558
mutter('_handle_next %r => %r' % (key, value))
562
revision_info = self.info.revisions[-1]
563
if hasattr(revision_info, key):
564
if getattr(revision_info, key) is None:
565
setattr(revision_info, key, value)
567
raise MalformedHeader('Duplicated Key: %s' % key)
569
# What do we do with a key we don't recognize
570
raise MalformedHeader('Unknown Key: "%s"' % key)
572
def _read_many(self, indent):
573
"""If a line ends with no entry, that means that it should be
574
followed with multiple lines of values.
576
This detects the end of the list, because it will be a line that
577
does not start properly indented.
580
start = '#' + (' '*indent)
582
if self._next_line is None or self._next_line[:len(start)] != start:
585
for line in self._next():
586
values.append(line[len(start):-1].decode('utf-8'))
587
if self._next_line is None or self._next_line[:len(start)] != start:
591
def _read_one_patch(self):
592
"""Read in one patch, return the complete patch, along with
595
:return: action, lines, do_continue
597
#mutter('_read_one_patch: %r' % self._next_line)
598
# Peek and see if there are no patches
599
if self._next_line is None or self._next_line.startswith('#'):
600
return None, [], False
604
for line in self._next():
606
if not line.startswith('==='):
607
raise MalformedPatches('The first line of all patches'
608
' should be a bzr meta line "==="'
610
action = line[4:-1].decode('utf-8')
611
elif line.startswith('... '):
612
action += line[len('... '):-1].decode('utf-8')
614
if (self._next_line is not None and
615
self._next_line.startswith('===')):
616
return action, lines, True
617
elif self._next_line is None or self._next_line.startswith('#'):
618
return action, lines, False
622
elif not line.startswith('... '):
625
return action, lines, False
627
def _read_patches(self):
629
revision_actions = []
631
action, lines, do_continue = self._read_one_patch()
632
if action is not None:
633
revision_actions.append((action, lines))
634
assert self.info.revisions[-1].tree_actions is None
635
self.info.revisions[-1].tree_actions = revision_actions
637
def _read_footer(self):
638
"""Read the rest of the meta information.
640
:param first_line: The previous step iterates past what it
641
can handle. That extra line is given here.
643
for line in self._next():
644
self._handle_next(line)
645
if not self._next_line.startswith('#'):
648
if self._next_line is None:
652
426
class BundleTree(Tree):
653
427
def __init__(self, base_tree, revision_id):
654
428
self.base_tree = base_tree