/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: Martin Pool
  • Date: 2005-06-28 03:02:31 UTC
  • Revision ID: mbp@sourcefrog.net-20050628030231-d311e4ebcd467ef4
Merge John's import-speedup branch:

                                                                                         
  777 John Arbash Meinel <john@arbash-meinel.com>       Sun 2005-06-26 22:20:32 -0500
      revision-id: john@arbash-meinel.com-20050627032031-e82a50db3863b18e
      bzr selftest was not using the correct bzr

  776 John Arbash Meinel <john@arbash-meinel.com>       Sun 2005-06-26 22:20:22 -0500
      revision-id: john@arbash-meinel.com-20050627032021-c9f21fde989ddaee
      Add was using an old mutter

  775 John Arbash Meinel <john@arbash-meinel.com>       Sun 2005-06-26 22:02:33 -0500
      revision-id: john@arbash-meinel.com-20050627030233-9165cfe98fc63298
      Cleaned up to be less different

  774 John Arbash Meinel <john@arbash-meinel.com>       Sun 2005-06-26 21:54:53 -0500
      revision-id: john@arbash-meinel.com-20050627025452-4260d0e744edef43
      Allow BZR_PLUGIN_PATH='' to negate plugin loading.

  773 John Arbash Meinel <john@arbash-meinel.com>       Sun 2005-06-26 21:49:34 -0500
      revision-id: john@arbash-meinel.com-20050627024933-b7158f67b7b9eae5
      Finished the previous cleanup (allowing load_plugins to be called twice)

  772 John Arbash Meinel <john@arbash-meinel.com>       Sun 2005-06-26 21:45:08 -0500
      revision-id: john@arbash-meinel.com-20050627024508-723b1df510d196fc
      Work on making the tests pass. versioning.py is calling run_cmd directly, but plugins have been loaded.

  771 John Arbash Meinel <john@arbash-meinel.com>       Sun 2005-06-26 21:32:29 -0500
      revision-id: john@arbash-meinel.com-20050627023228-79972744d7c53e15
      Got it down a little bit more by removing import of tree and inventory.

  770 John Arbash Meinel <john@arbash-meinel.com>       Sun 2005-06-26 21:26:05 -0500
      revision-id: john@arbash-meinel.com-20050627022604-350b9773ef622f95
      Reducing the number of import from bzrlib/__init__.py and bzrlib/branch.py

  769 John Arbash Meinel <john@arbash-meinel.com>       Sun 2005-06-26 20:32:25 -0500
      revision-id: john@arbash-meinel.com-20050627013225-32dd044f10d23948
      Updated revision.py and xml.py to include SubElement.

  768 John Arbash Meinel <john@arbash-meinel.com>       Sun 2005-06-26 20:03:56 -0500
      revision-id: john@arbash-meinel.com-20050627010356-ee66919e1c377faf
      Minor typo

  767 John Arbash Meinel <john@arbash-meinel.com>       Sun 2005-06-26 20:03:13 -0500
      revision-id: john@arbash-meinel.com-20050627010312-40d024007eb85051
      Caching the import

  766 John Arbash Meinel <john@arbash-meinel.com>       Sun 2005-06-26 19:51:47 -0500
      revision-id: john@arbash-meinel.com-20050627005147-5281c99e48ed1834
      Created wrapper functions for lazy import of ElementTree

  765 John Arbash Meinel <john@arbash-meinel.com>       Sun 2005-06-26 19:46:37 -0500
      revision-id: john@arbash-meinel.com-20050627004636-bf432902004a94c5
      Removed all of the test imports of cElementTree

  764 John Arbash Meinel <john@arbash-meinel.com>       Sun 2005-06-26 19:43:59 -0500
      revision-id: john@arbash-meinel.com-20050627004358-d137fbe9570dd71b
      Trying to make bzr startup faster.

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 xml import ElementTree, Element
 
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
            raise BzrError('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
 
 
66
def escape_attr(text):
 
67
    return text.replace("&", "&amp;") \
 
68
           .replace("'", "&apos;") \
 
69
           .replace('"', "&quot;") \
 
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.
 
76
def write_slacker_inventory(inv, f):
 
77
    def descend(ie):
 
78
        kind = ie.kind
 
79
        f.write('<%s name="%s" id="%s" ' % (kind, escape_attr(ie.name),
 
80
                                            escape_attr(ie.file_id)))
 
81
 
 
82
        if kind == 'file':
 
83
            if ie.text_id:
 
84
                f.write('text_id="%s" ' % ie.text_id)
 
85
            if ie.text_sha1:
 
86
                f.write('text_sha1="%s" ' % ie.text_sha1)
 
87
            if ie.text_size != None:
 
88
                f.write('text_size="%d" ' % ie.text_size)
 
89
            f.write('/>\n')
 
90
        elif kind == 'directory':
 
91
            f.write('>\n')
 
92
            
 
93
            l = ie.children.items()
 
94
            l.sort()
 
95
            for child_name, child_ie in l:
 
96
                descend(child_ie)
 
97
 
 
98
            f.write('</directory>\n')
 
99
        else:
 
100
            raise BzrError('unknown InventoryEntry kind %r' % kind)
 
101
 
 
102
    f.write('<inventory>\n')
 
103
    f.write('<root_directory id="%s">\n' % escape_attr(inv.root.file_id))
 
104
 
 
105
    l = inv.root.children.items()
 
106
    l.sort()
 
107
    for entry_name, ie in l:
 
108
        descend(ie)
 
109
 
 
110
    f.write('</root_directory>\n')
 
111
    f.write('</inventory>\n')
 
112
    
 
113
 
 
114
 
 
115
def read_new_inventory(f):
 
116
    from inventory import Inventory, InventoryEntry
 
117
    
 
118
    def descend(parent_ie, el):
 
119
        kind = el.tag
 
120
        name = el.get('name')
 
121
        file_id = el.get('id')
 
122
        ie = InventoryEntry(file_id, name, el.tag)
 
123
        parent_ie.children[name] = ie
 
124
        inv._byid[file_id] = ie
 
125
        if kind == 'directory':
 
126
            for child_el in el:
 
127
                descend(ie, child_el)
 
128
        elif kind == 'file':
 
129
            assert len(el) == 0
 
130
            ie.text_id = el.get('text_id')
 
131
            v = el.get('text_size')
 
132
            ie.text_size = v and int(v)
 
133
            ie.text_sha1 = el.get('text_sha1')
 
134
        else:
 
135
            raise BzrError("unknown inventory entry %r" % kind)
 
136
 
 
137
    inv_el = ElementTree().parse(f)
 
138
    assert inv_el.tag == 'inventory'
 
139
    root_el = inv_el[0]
 
140
    assert root_el.tag == 'root_directory'
 
141
 
 
142
    inv = Inventory()
 
143
    for el in root_el:
 
144
        descend(inv.root, el)