327
327
entry_cache[key] = ie.copy()
332
def unpack_inventory_flat(elt, format_num, unpack_entry):
333
"""Unpack a flat XML inventory.
335
:param elt: XML element for the inventory
336
:param format_num: Expected format number
337
:param unpack_entry: Function for unpacking inventory entries
338
:return: An inventory
339
:raise UnexpectedInventoryFormat: When unexpected elements or data is
342
if elt.tag != 'inventory':
343
raise errors.UnexpectedInventoryFormat('Root tag is %r' % elt.tag)
344
format = elt.get('format')
345
if format != format_num:
346
raise errors.UnexpectedInventoryFormat('Invalid format version %r'
348
revision_id = elt.get('revision_id')
349
if revision_id is not None:
350
revision_id = cache_utf8.encode(revision_id)
351
inv = inventory.Inventory(root_id=None, revision_id=revision_id)
358
def serialize_inventory_flat(inv, append_inventory_root, root_id,
359
supported_kinds, working):
360
"""Serialize an inventory to a flat XML file.
362
:param inv: Inventory to serialize
363
:param working: If True skip history data - text_sha1, text_size,
364
reference_revision, symlink_target. self._check_revisions(inv)
367
append = output.append
368
append_inventory_root(append, inv)
369
entries = inv.iter_entries()
371
root_path, root_ie = entries.next()
372
for path, ie in entries:
373
if ie.parent_id != root_id:
374
parent_str = ' parent_id="'
375
parent_id = encode_and_escape(ie.parent_id)
379
if ie.kind == 'file':
381
executable = ' executable="yes"'
385
append('<file%s file_id="%s name="%s%s%s revision="%s '
386
'text_sha1="%s" text_size="%d" />\n' % (
387
executable, encode_and_escape(ie.file_id),
388
encode_and_escape(ie.name), parent_str, parent_id,
389
encode_and_escape(ie.revision), ie.text_sha1,
392
append('<file%s file_id="%s name="%s%s%s />\n' % (
393
executable, encode_and_escape(ie.file_id),
394
encode_and_escape(ie.name), parent_str, parent_id))
395
elif ie.kind == 'directory':
397
append('<directory file_id="%s name="%s%s%s revision="%s '
399
encode_and_escape(ie.file_id),
400
encode_and_escape(ie.name),
401
parent_str, parent_id,
402
encode_and_escape(ie.revision)))
404
append('<directory file_id="%s name="%s%s%s />\n' % (
405
encode_and_escape(ie.file_id),
406
encode_and_escape(ie.name),
407
parent_str, parent_id))
408
elif ie.kind == 'symlink':
410
append('<symlink file_id="%s name="%s%s%s revision="%s '
411
'symlink_target="%s />\n' % (
412
encode_and_escape(ie.file_id),
413
encode_and_escape(ie.name),
414
parent_str, parent_id,
415
encode_and_escape(ie.revision),
416
encode_and_escape(ie.symlink_target)))
418
append('<symlink file_id="%s name="%s%s%s />\n' % (
419
encode_and_escape(ie.file_id),
420
encode_and_escape(ie.name),
421
parent_str, parent_id))
422
elif ie.kind == 'tree-reference':
423
if ie.kind not in supported_kinds:
424
raise errors.UnsupportedInventoryKind(ie.kind)
426
append('<tree-reference file_id="%s name="%s%s%s '
427
'revision="%s reference_revision="%s />\n' % (
428
encode_and_escape(ie.file_id),
429
encode_and_escape(ie.name),
430
parent_str, parent_id,
431
encode_and_escape(ie.revision),
432
encode_and_escape(ie.reference_revision)))
434
append('<tree-reference file_id="%s name="%s%s%s />\n' % (
435
encode_and_escape(ie.file_id),
436
encode_and_escape(ie.name),
437
parent_str, parent_id))
439
raise errors.UnsupportedInventoryKind(ie.kind)
440
append('</inventory>\n')