/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/newinventory.py

  • Committer: mbp at sourcefrog
  • Date: 2005-04-07 05:56:55 UTC
  • Revision ID: mbp@sourcefrog.net-20050407055655-fa3e3775a2fc8b326a72f1c6
improved new-inventory

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# (C) 2005 Canonical Ltd
 
2
 
 
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.
 
7
 
 
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.
 
12
 
 
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
 
16
 
 
17
from cElementTree import Element, ElementTree, SubElement
 
18
 
 
19
 
 
20
def write_inventory(inv, f):
 
21
    el = Element('inventory', {'version': '2'})
 
22
    el.text = '\n'
 
23
    
 
24
    root = Element('root_directory', {'id': inv.root.file_id})
 
25
    root.tail = root.text = '\n'
 
26
    el.append(root)
 
27
 
 
28
    def descend(parent_el, ie):
 
29
        kind = ie.kind
 
30
        el = Element(kind, {'name': ie.name,
 
31
                            'id': ie.file_id,})
 
32
        
 
33
        if kind == 'file':
 
34
            if ie.text_id:
 
35
                el.set('text_id', ie.text_id)
 
36
            if ie.text_sha1:
 
37
                el.set('text_sha1', ie.text_sha1)
 
38
            if ie.text_size != None:
 
39
                el.set('text_size', ('%d' % ie.text_size))
 
40
        elif kind != 'directory':
 
41
            bailout('unknown InventoryEntry kind %r' % kind)
 
42
 
 
43
        el.tail = '\n'
 
44
        parent_el.append(el)
 
45
 
 
46
        if kind == 'directory':
 
47
            el.text = '\n' # break before having children
 
48
            l = ie.children.items()
 
49
            l.sort()
 
50
            for child_name, child_ie in l:
 
51
                descend(el, child_ie)
 
52
                
 
53
        
 
54
    # walk down through inventory, adding all directories
 
55
 
 
56
    l = inv.root.children.items()
 
57
    l.sort()
 
58
    for entry_name, ie in l:
 
59
        descend(root, ie)
 
60
    
 
61
    ElementTree(el).write(f, 'utf-8')
 
62
    f.write('\n')
 
63
 
 
64
 
 
65
# This writes out an inventory without building an XML tree first,
 
66
# just to see if it's faster.  Not currently used.
 
67
def write_slacker_inventory(inv, f):
 
68
    def descend(ie):
 
69
        kind = ie.kind
 
70
        f.write('<%s name="%s" id="%s" ' % (kind, ie.name, ie.file_id))
 
71
 
 
72
        if kind == 'file':
 
73
            if ie.text_id:
 
74
                f.write('text_id="%s" ' % ie.text_id)
 
75
            if ie.text_sha1:
 
76
                f.write('text_sha1="%s" ' % ie.text_sha1)
 
77
            if ie.text_size != None:
 
78
                f.write('text_size="%d" ' % ie.text_size)
 
79
            f.write('/>\n')
 
80
        elif kind == 'directory':
 
81
            f.write('>\n')
 
82
            
 
83
            l = ie.children.items()
 
84
            l.sort()
 
85
            for child_name, child_ie in l:
 
86
                descend(child_ie)
 
87
 
 
88
            f.write('</directory>\n')
 
89
        else:
 
90
            bailout('unknown InventoryEntry kind %r' % kind)
 
91
 
 
92
    f.write('<inventory>\n')
 
93
    f.write('<root_directory id="bogus-root-id">\n')
 
94
 
 
95
    l = inv.root.children.items()
 
96
    l.sort()
 
97
    for entry_name, ie in l:
 
98
        descend(ie)
 
99
 
 
100
    f.write('</root_directory>\n')
 
101
    f.write('</inventory>\n')
 
102
    
 
103
 
 
104
 
 
105
def read_new_inventory(f):
 
106
    from inventory import Inventory, InventoryEntry
 
107
    
 
108
    def descend(parent_ie, el):
 
109
        kind = el.tag
 
110
        name = el.get('name')
 
111
        file_id = el.get('id')
 
112
        ie = InventoryEntry(file_id, name, el.tag)
 
113
        parent_ie.children[name] = ie
 
114
        inv._byid[file_id] = ie
 
115
        if kind == 'directory':
 
116
            for child_el in el:
 
117
                descend(ie, child_el)
 
118
        elif kind == 'file':
 
119
            assert len(el) == 0
 
120
            ie.text_id = el.get('text_id')
 
121
            v = el.get('text_size')
 
122
            ie.text_size = v and int(v)
 
123
            ie.text_sha1 = el.get('text_sha1')
 
124
        else:
 
125
            bailout("unknown inventory entry %r" % kind)
 
126
 
 
127
    inv_el = ElementTree().parse(f)
 
128
    assert inv_el.tag == 'inventory'
 
129
    root_el = inv_el[0]
 
130
    assert root_el.tag == 'root_directory'
 
131
 
 
132
    inv = Inventory()
 
133
    for el in root_el:
 
134
        descend(inv.root, el)