~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to contrib/newinventory.py

  • Committer: John Arbash Meinel
  • Date: 2005-09-29 21:13:03 UTC
  • mto: (1393.1.12)
  • mto: This revision was merged to the branch mainline in revision 1396.
  • Revision ID: john@arbash-meinel.com-20050929211303-7f1f9bf969d65dc3
All tests pass.

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
 
from cElementTree import Element, ElementTree, SubElement
 
17
from bzrlib.xml import ElementTree, Element
18
18
 
19
19
 
20
20
def write_inventory(inv, f):
21
21
    el = Element('inventory', {'version': '2'})
22
 
    
23
 
    root = Element('root_directory', {'id': 'bogus-root-id'})
 
22
    el.text = '\n'
 
23
 
 
24
    root = Element('root_directory', {'id': inv.root.file_id})
 
25
    root.tail = root.text = '\n'
24
26
    el.append(root)
25
27
 
26
28
    def descend(parent_el, ie):
36
38
            if ie.text_size != None:
37
39
                el.set('text_size', ('%d' % ie.text_size))
38
40
        elif kind != 'directory':
39
 
            bailout('unknown InventoryEntry kind %r' % kind)
 
41
            raise BzrError('unknown InventoryEntry kind %r' % kind)
40
42
 
41
43
        el.tail = '\n'
42
44
        parent_el.append(el)
43
45
 
44
46
        if kind == 'directory':
 
47
            el.text = '\n' # break before having children
45
48
            l = ie.children.items()
46
49
            l.sort()
47
50
            for child_name, child_ie in l:
50
53
        
51
54
    # walk down through inventory, adding all directories
52
55
 
53
 
    l = inv._root.children.items()
 
56
    l = inv.root.children.items()
54
57
    l.sort()
55
58
    for entry_name, ie in l:
56
59
        descend(root, ie)
60
63
 
61
64
 
62
65
 
 
66
def escape_attr(text):
 
67
    return text.replace("&", "&") \
 
68
           .replace("'", "'") \
 
69
           .replace('"', """) \
 
70
           .replace("<", "&lt;") \
 
71
           .replace(">", "&gt;")
 
72
 
 
73
 
 
74
# This writes out an inventory without building an XML tree first,
 
75
# just to see if it's faster.  Not currently used.
63
76
def write_slacker_inventory(inv, f):
64
77
    def descend(ie):
65
78
        kind = ie.kind
66
 
        f.write('<%s name="%s" id="%s" ' % (kind, ie.name, ie.file_id))
 
79
        f.write('<%s name="%s" id="%s" ' % (kind, escape_attr(ie.name),
 
80
                                            escape_attr(ie.file_id)))
67
81
 
68
82
        if kind == 'file':
69
83
            if ie.text_id:
83
97
 
84
98
            f.write('</directory>\n')
85
99
        else:
86
 
            bailout('unknown InventoryEntry kind %r' % kind)
 
100
            raise BzrError('unknown InventoryEntry kind %r' % kind)
87
101
 
88
102
    f.write('<inventory>\n')
89
 
    f.write('<root_directory id="bogus-root-id">\n')
 
103
    f.write('<root_directory id="%s">\n' % escape_attr(inv.root.file_id))
90
104
 
91
 
    l = inv._root.children.items()
 
105
    l = inv.root.children.items()
92
106
    l.sort()
93
107
    for entry_name, ie in l:
94
108
        descend(ie)
118
132
            ie.text_size = v and int(v)
119
133
            ie.text_sha1 = el.get('text_sha1')
120
134
        else:
121
 
            bailout("unknown inventory entry %r" % kind)
 
135
            raise BzrError("unknown inventory entry %r" % kind)
122
136
 
123
137
    inv_el = ElementTree().parse(f)
124
138
    assert inv_el.tag == 'inventory'
125
139
    root_el = inv_el[0]
126
140
    assert root_el.tag == 'root_directory'
127
141
 
128
 
    inv = Inventory()
 
142
    inv = Inventory(inv_el.get('file_id'))
129
143
    for el in root_el:
130
 
        descend(inv._root, el)
 
144
        descend(inv.root, el)