303
304
new_tree.id2path(ie.file_id)])
304
305
action.add_property('last-changed', ie.revision)
305
306
action.write(self.to_file)
309
class BundleReader(object):
310
"""This class reads in a bundle from a file, and returns
311
a Bundle object, which can then be applied against a tree.
313
def __init__(self, from_file):
314
"""Read in the bundle from the file.
316
:param from_file: A file-like object (must have iterator support).
318
object.__init__(self)
319
self.from_file = iter(from_file)
320
self._next_line = None
322
self.info = BundleInfo()
323
# We put the actual inventory ids in the footer, so that the patch
324
# is easier to read for humans.
325
# Unfortunately, that means we need to read everything before we
326
# can create a proper bundle.
332
while self._next_line is not None:
333
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()
347
def get_bundle(self, repository):
348
"""Return the meta information, and a Bundle tree which can
349
be used to populate the local stores and working tree, respectively.
351
return self.info, self.revision_tree(repository, self.info.target)
353
def revision_tree(self, repository, revision_id, base=None):
354
return self.info.revision_tree(repository, revision_id, base)
357
"""yield the next line, but secretly
358
keep 1 extra line for peeking.
360
for line in self.from_file:
361
last = self._next_line
362
self._next_line = line
364
#mutter('yielding line: %r' % last)
366
last = self._next_line
367
self._next_line = None
368
#mutter('yielding line: %r' % last)
371
def _read_header(self):
372
"""Read the bzr header"""
373
header = get_header()
375
for line in self._next():
377
# not all mailers will keep trailing whitespace
380
if (not line.startswith('# ') or not line.endswith('\n')
381
or line[2:-1].decode('utf-8') != header[0]):
382
raise MalformedHeader('Found a header, but it'
383
' was improperly formatted')
384
header.pop(0) # We read this line.
386
break # We found everything.
387
elif (line.startswith('#') and line.endswith('\n')):
388
line = line[1:-1].strip().decode('utf-8')
389
if line[:len(header_str)] == header_str:
390
if line == header[0]:
393
raise MalformedHeader('Found what looks like'
394
' a header, but did not match')
397
raise NotABundle('Did not find an opening header')
399
def _read_revision_header(self):
400
self.info.revisions.append(RevisionInfo(None))
401
for line in self._next():
402
# The bzr header is terminated with a blank line
403
# which does not start with '#'
404
if line is None or line == '\n':
406
self._handle_next(line)
408
def _read_next_entry(self, line, indent=1):
409
"""Read in a key-value pair
411
if not line.startswith('#'):
412
raise MalformedHeader('Bzr header did not start with #')
413
line = line[1:-1].decode('utf-8') # Remove the '#' and '\n'
414
if line[:indent] == ' '*indent:
417
return None, None# Ignore blank lines
419
loc = line.find(': ')
424
value = self._read_many(indent=indent+2)
425
elif line[-1:] == ':':
427
value = self._read_many(indent=indent+2)
429
raise MalformedHeader('While looking for key: value pairs,'
430
' did not find the colon %r' % (line))
432
key = key.replace(' ', '_')
433
#mutter('found %s: %s' % (key, value))
436
def _handle_next(self, line):
439
key, value = self._read_next_entry(line, indent=1)
440
mutter('_handle_next %r => %r' % (key, value))
444
revision_info = self.info.revisions[-1]
445
if hasattr(revision_info, key):
446
if getattr(revision_info, key) is None:
447
setattr(revision_info, key, value)
449
raise MalformedHeader('Duplicated Key: %s' % key)
451
# What do we do with a key we don't recognize
452
raise MalformedHeader('Unknown Key: "%s"' % key)
454
def _read_many(self, indent):
455
"""If a line ends with no entry, that means that it should be
456
followed with multiple lines of values.
458
This detects the end of the list, because it will be a line that
459
does not start properly indented.
462
start = '#' + (' '*indent)
464
if self._next_line is None or self._next_line[:len(start)] != start:
467
for line in self._next():
468
values.append(line[len(start):-1].decode('utf-8'))
469
if self._next_line is None or self._next_line[:len(start)] != start:
473
def _read_one_patch(self):
474
"""Read in one patch, return the complete patch, along with
477
:return: action, lines, do_continue
479
#mutter('_read_one_patch: %r' % self._next_line)
480
# Peek and see if there are no patches
481
if self._next_line is None or self._next_line.startswith('#'):
482
return None, [], False
486
for line in self._next():
488
if not line.startswith('==='):
489
raise MalformedPatches('The first line of all patches'
490
' should be a bzr meta line "==="'
492
action = line[4:-1].decode('utf-8')
493
elif line.startswith('... '):
494
action += line[len('... '):-1].decode('utf-8')
496
if (self._next_line is not None and
497
self._next_line.startswith('===')):
498
return action, lines, True
499
elif self._next_line is None or self._next_line.startswith('#'):
500
return action, lines, False
504
elif not line.startswith('... '):
507
return action, lines, False
509
def _read_patches(self):
511
revision_actions = []
513
action, lines, do_continue = self._read_one_patch()
514
if action is not None:
515
revision_actions.append((action, lines))
516
assert self.info.revisions[-1].tree_actions is None
517
self.info.revisions[-1].tree_actions = revision_actions
519
def _read_footer(self):
520
"""Read the rest of the meta information.
522
:param first_line: The previous step iterates past what it
523
can handle. That extra line is given here.
525
for line in self._next():
526
self._handle_next(line)
527
if not self._next_line.startswith('#'):
530
if self._next_line is None: