~bzr-pqm/bzr/bzr.dev

1185.47.1 by Martin Pool
[broken] start converting basic_io to more rfc822-like format
1
# \subsection{Example usage}
2
# 
3
# \textbf{XXX:} Move these to object serialization code. 
4
5
def write_revision(writer, revision):
6
    s = Stanza(revision=revision.revision_id,
7
               committer=revision.committer, 
8
               timezone=long(revision.timezone),
9
               timestamp=long(revision.timestamp),
10
               inventory_sha1=revision.inventory_sha1,
11
               message=revision.message)
12
    for parent_id in revision.parent_ids:
13
        s.add('parent', parent_id)
14
    for prop_name, prop_value in revision.properties.items():
15
        s.add(prop_name, prop_value)
16
    writer.write_stanza(s)
17
18
def write_inventory(writer, inventory):
19
    s = Stanza(inventory_version=7)
20
    writer.write_stanza(s)
21
22
    for path, ie in inventory.iter_entries():
23
        s = Stanza()
24
        s.add(ie.kind, ie.file_id)
25
        for attr in ['name', 'parent_id', 'revision', \
26
                     'text_sha1', 'text_size', 'executable', \
27
                     'symlink_target', \
28
                     ]:
29
            attr_val = getattr(ie, attr, None)
30
            if attr == 'executable' and attr_val == 0:
31
                continue
32
            if attr == 'parent_id' and attr_val == 'TREE_ROOT':
33
                continue
34
            if attr_val is not None:
35
                s.add(attr, attr_val)
36
        writer.write_stanza(s)
37
38
39
def read_inventory(inv_file):
40
    """Read inventory object from rio formatted inventory file"""
41
    from bzrlib.inventory import Inventory, InventoryFile
42
    s = read_stanza(inv_file)
43
    assert s['inventory_version'] == 7
44
    inv = Inventory()
45
    for s in read_stanzas(inv_file):
46
        kind, file_id = s.items[0]
47
        parent_id = None
48
        if 'parent_id' in s:
49
            parent_id = s['parent_id']
50
        if kind == 'file':
51
            ie = InventoryFile(file_id, s['name'], parent_id)
52
            ie.text_sha1 = s['text_sha1']
53
            ie.text_size = s['text_size']
54
        else:
55
            raise NotImplementedError()
56
        inv.add(ie)
57
    return inv
58
59
60