1
# (C) 2005 Canonical Ltd
 
 
3
# This program is free software; you can redistribute it and/or modify
 
 
4
# it under the terms of the GNU General Public License as published by
 
 
5
# the Free Software Foundation; either version 2 of the License, or
 
 
6
# (at your option) any later version.
 
 
8
# This program is distributed in the hope that it will be useful,
 
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
 
11
# GNU General Public License for more details.
 
 
13
# You should have received a copy of the GNU General Public License
 
 
14
# along with this program; if not, write to the Free Software
 
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
 
17
from cElementTree import Element, ElementTree, SubElement
 
 
20
def write_inventory(inv, f):
 
 
21
    el = Element('inventory', {'version': '2'})
 
 
23
    root = Element('root_directory', {'id': 'bogus-root-id'})
 
 
26
    def descend(parent_el, ie):
 
 
28
        el = Element(kind, {'name': ie.name,
 
 
33
                el.set('text_id', ie.text_id)
 
 
35
                el.set('text_sha1', ie.text_sha1)
 
 
36
            if ie.text_size != None:
 
 
37
                el.set('text_size', ('%d' % ie.text_size))
 
 
38
        elif kind != 'directory':
 
 
39
            bailout('unknown InventoryEntry kind %r' % kind)
 
 
44
        if kind == 'directory':
 
 
45
            l = ie.children.items()
 
 
47
            for child_name, child_ie in l:
 
 
51
    # walk down through inventory, adding all directories
 
 
53
    l = inv._root.children.items()
 
 
55
    for entry_name, ie in l:
 
 
58
    ElementTree(el).write(f, 'utf-8')
 
 
63
def write_slacker_inventory(inv, f):
 
 
66
        f.write('<%s name="%s" id="%s" ' % (kind, ie.name, ie.file_id))
 
 
70
                f.write('text_id="%s" ' % ie.text_id)
 
 
72
                f.write('text_sha1="%s" ' % ie.text_sha1)
 
 
73
            if ie.text_size != None:
 
 
74
                f.write('text_size="%d" ' % ie.text_size)
 
 
76
        elif kind == 'directory':
 
 
79
            l = ie.children.items()
 
 
81
            for child_name, child_ie in l:
 
 
84
            f.write('</directory>\n')
 
 
86
            bailout('unknown InventoryEntry kind %r' % kind)
 
 
88
    f.write('<inventory>\n')
 
 
89
    f.write('<root_directory id="bogus-root-id">\n')
 
 
91
    l = inv._root.children.items()
 
 
93
    for entry_name, ie in l:
 
 
96
    f.write('</root_directory>\n')
 
 
97
    f.write('</inventory>\n')
 
 
101
def read_new_inventory(f):
 
 
102
    from inventory import Inventory, InventoryEntry
 
 
104
    def descend(parent_ie, el):
 
 
106
        name = el.get('name')
 
 
107
        file_id = el.get('id')
 
 
108
        ie = InventoryEntry(file_id, name, el.tag)
 
 
109
        parent_ie.children[name] = ie
 
 
110
        inv._byid[file_id] = ie
 
 
111
        if kind == 'directory':
 
 
113
                descend(ie, child_el)
 
 
116
            ie.text_id = el.get('text_id')
 
 
117
            v = el.get('text_size')
 
 
118
            ie.text_size = v and int(v)
 
 
119
            ie.text_sha1 = el.get('text_sha1')
 
 
121
            bailout("unknown inventory entry %r" % kind)
 
 
123
    inv_el = ElementTree().parse(f)
 
 
124
    assert inv_el.tag == 'inventory'
 
 
126
    assert root_el.tag == 'root_directory'
 
 
130
        descend(inv._root, el)