300
303
new_tree.id2path(ie.file_id)])
301
304
action.add_property('last-changed', ie.revision)
302
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
if not line.startswith('#'):
372
found_something = True
373
self._handle_next(line)
374
if not found_something:
375
# Nothing was there, so remove the added revision
376
self.info.revisions.pop()
377
return found_something
379
def _read_next_entry(self, line, indent=1):
380
"""Read in a key-value pair
382
if not line.startswith('#'):
383
raise errors.MalformedHeader('Bzr header did not start with #')
384
line = line[1:-1].decode('utf-8') # Remove the '#' and '\n'
385
if line[:indent] == ' '*indent:
388
return None, None# Ignore blank lines
390
loc = line.find(': ')
395
value = self._read_many(indent=indent+2)
396
elif line[-1:] == ':':
398
value = self._read_many(indent=indent+2)
400
raise errors.MalformedHeader('While looking for key: value pairs,'
401
' did not find the colon %r' % (line))
403
key = key.replace(' ', '_')
404
#mutter('found %s: %s' % (key, value))
407
def _handle_next(self, line):
410
key, value = self._read_next_entry(line, indent=1)
411
mutter('_handle_next %r => %r' % (key, value))
415
revision_info = self.info.revisions[-1]
416
if hasattr(revision_info, key):
417
if getattr(revision_info, key) is None:
418
setattr(revision_info, key, value)
420
raise errors.MalformedHeader('Duplicated Key: %s' % key)
422
# What do we do with a key we don't recognize
423
raise errors.MalformedHeader('Unknown Key: "%s"' % key)
425
def _read_many(self, indent):
426
"""If a line ends with no entry, that means that it should be
427
followed with multiple lines of values.
429
This detects the end of the list, because it will be a line that
430
does not start properly indented.
433
start = '#' + (' '*indent)
435
if self._next_line is None or self._next_line[:len(start)] != start:
438
for line in self._next():
439
values.append(line[len(start):-1].decode('utf-8'))
440
if self._next_line is None or self._next_line[:len(start)] != start:
444
def _read_one_patch(self):
445
"""Read in one patch, return the complete patch, along with
448
:return: action, lines, do_continue
450
#mutter('_read_one_patch: %r' % self._next_line)
451
# Peek and see if there are no patches
452
if self._next_line is None or self._next_line.startswith('#'):
453
return None, [], False
457
for line in self._next():
459
if not line.startswith('==='):
460
raise errors.MalformedPatches('The first line of all patches'
461
' should be a bzr meta line "==="'
463
action = line[4:-1].decode('utf-8')
464
elif line.startswith('... '):
465
action += line[len('... '):-1].decode('utf-8')
467
if (self._next_line is not None and
468
self._next_line.startswith('===')):
469
return action, lines, True
470
elif self._next_line is None or self._next_line.startswith('#'):
471
return action, lines, False
475
elif not line.startswith('... '):
478
return action, lines, False
480
def _read_patches(self):
482
revision_actions = []
484
action, lines, do_continue = self._read_one_patch()
485
if action is not None:
486
revision_actions.append((action, lines))
487
assert self.info.revisions[-1].tree_actions is None
488
self.info.revisions[-1].tree_actions = revision_actions
490
def _read_footer(self):
491
"""Read the rest of the meta information.
493
:param first_line: The previous step iterates past what it
494
can handle. That extra line is given here.
496
for line in self._next():
497
self._handle_next(line)
498
if self._next_line is None:
500
if not self._next_line.startswith('#'):
501
# Consume the trailing \n and stop processing