~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/xml5.py

  • Committer: Robert Collins
  • Date: 2005-10-03 05:54:35 UTC
  • mto: (1393.1.30)
  • mto: This revision was merged to the branch mainline in revision 1400.
  • Revision ID: robertc@robertcollins.net-20051003055434-c8ebd30d1de10247
move exporting functionality into inventory.py - uncovers bug in symlink support

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#! /usr/bin/env python
 
2
 
1
3
# This program is free software; you can redistribute it and/or modify
2
4
# it under the terms of the GNU General Public License as published by
3
5
# the Free Software Foundation; either version 2 of the License, or
13
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
14
16
 
15
17
 
16
 
from bzrlib.xml_serializer import ElementTree, SubElement, Element, Serializer
 
18
from bzrlib.xml import ElementTree, SubElement, Element, Serializer
17
19
from bzrlib.inventory import ROOT_ID, Inventory, InventoryEntry
18
 
import bzrlib.inventory as inventory
19
20
from bzrlib.revision import Revision        
20
21
from bzrlib.errors import BzrError
21
22
 
22
23
 
 
24
 
 
25
 
 
26
 
23
27
class Serializer_v5(Serializer):
24
28
    """Version 5 serializer
25
29
 
30
34
    
31
35
    def _pack_inventory(self, inv):
32
36
        """Convert to XML Element"""
33
 
        e = Element('inventory',
34
 
                    format='5')
 
37
        e = Element('inventory')
35
38
        e.text = '\n'
36
39
        if inv.root.file_id not in (None, ROOT_ID):
37
40
            e.set('file_id', inv.root.file_id)
38
 
        if inv.revision_id is not None:
39
 
            e.set('revision_id', inv.revision_id)
40
41
        for path, ie in inv.iter_entries():
41
42
            e.append(self._pack_entry(ie))
42
43
        return e
80
81
                       timestamp = '%.9f' % rev.timestamp,
81
82
                       revision_id = rev.revision_id,
82
83
                       inventory_sha1 = rev.inventory_sha1,
83
 
                       format='5',
84
84
                       )
85
85
        if rev.timezone:
86
86
            root.set('timezone', str(rev.timezone))
87
87
        root.text = '\n'
 
88
 
88
89
        msg = SubElement(root, 'message')
89
90
        msg.text = rev.message
90
91
        msg.tail = '\n'
 
92
 
91
93
        if rev.parent_ids:
92
94
            pelts = SubElement(root, 'parents')
93
95
            pelts.tail = pelts.text = '\n'
96
98
                p = SubElement(pelts, 'revision_ref')
97
99
                p.tail = '\n'
98
100
                p.set('revision_id', parent_id)
99
 
        if rev.properties:
100
 
            self._pack_revision_properties(rev, root)
101
101
        return root
102
102
 
103
 
 
104
 
    def _pack_revision_properties(self, rev, under_element):
105
 
        top_elt = SubElement(under_element, 'properties')
106
 
        for prop_name, prop_value in sorted(rev.properties.items()):
107
 
            assert isinstance(prop_name, basestring) 
108
 
            assert isinstance(prop_value, basestring) 
109
 
            prop_elt = SubElement(top_elt, 'property')
110
 
            prop_elt.set('name', prop_name)
111
 
            prop_elt.text = prop_value
112
 
            prop_elt.tail = '\n'
113
 
        top_elt.tail = '\n'
114
 
 
 
103
    
115
104
 
116
105
    def _unpack_inventory(self, elt):
117
106
        """Construct from XML Element
118
107
        """
119
108
        assert elt.tag == 'inventory'
120
109
        root_id = elt.get('file_id') or ROOT_ID
121
 
        format = elt.get('format')
122
 
        if format is not None:
123
 
            if format != '5':
124
 
                raise BzrError("invalid format version %r on inventory"
125
 
                                % format)
126
 
        revision_id = elt.get('revision_id')
127
 
        inv = Inventory(root_id, revision_id=revision_id)
 
110
        inv = Inventory(root_id)
128
111
        for e in elt:
129
112
            ie = self._unpack_entry(e)
130
113
            if ie.parent_id == ROOT_ID:
142
125
        if parent_id == None:
143
126
            parent_id = ROOT_ID
144
127
 
145
 
        if kind == 'directory':
146
 
            ie = inventory.InventoryDirectory(elt.get('file_id'),
147
 
                                              elt.get('name'),
148
 
                                              parent_id)
149
 
        elif kind == 'file':
150
 
            ie = inventory.InventoryFile(elt.get('file_id'),
151
 
                                         elt.get('name'),
152
 
                                         parent_id)
153
 
            ie.text_sha1 = elt.get('text_sha1')
154
 
            if elt.get('executable') == 'yes':
155
 
                ie.executable = True
156
 
            v = elt.get('text_size')
157
 
            ie.text_size = v and int(v)
158
 
        elif kind == 'symlink':
159
 
            ie = inventory.InventoryLink(elt.get('file_id'),
160
 
                                         elt.get('name'),
161
 
                                         parent_id)
162
 
            ie.symlink_target = elt.get('symlink_target')
163
 
        else:
164
 
            raise BzrError("unknown kind %r" % kind)
 
128
        ie = InventoryEntry(elt.get('file_id'),
 
129
                            elt.get('name'),
 
130
                            kind,
 
131
                            parent_id)
165
132
        ie.revision = elt.get('revision')
 
133
        ie.text_sha1 = elt.get('text_sha1')
 
134
        ie.symlink_target = elt.get('symlink_target')
 
135
        if elt.get('executable') == 'yes':
 
136
            ie.executable = True
 
137
        v = elt.get('text_size')
 
138
        ie.text_size = v and int(v)
166
139
 
167
140
        return ie
168
141
 
170
143
    def _unpack_revision(self, elt):
171
144
        """XML Element -> Revision object"""
172
145
        assert elt.tag == 'revision'
173
 
        format = elt.get('format')
174
 
        if format is not None:
175
 
            if format != '5':
176
 
                raise BzrError("invalid format version %r on inventory"
177
 
                                % format)
 
146
        
178
147
        rev = Revision(committer = elt.get('committer'),
179
148
                       timestamp = float(elt.get('timestamp')),
180
149
                       revision_id = elt.get('revision_id'),
181
150
                       inventory_sha1 = elt.get('inventory_sha1')
182
151
                       )
 
152
 
183
153
        parents = elt.find('parents') or []
184
154
        for p in parents:
185
155
            assert p.tag == 'revision_ref', \
186
156
                   "bad parent node tag %r" % p.tag
187
157
            rev.parent_ids.append(p.get('revision_id'))
188
 
        self._unpack_revision_properties(elt, rev)
 
158
 
189
159
        v = elt.get('timezone')
190
160
        rev.timezone = v and int(v)
 
161
 
191
162
        rev.message = elt.findtext('message') # text of <message>
192
163
        return rev
193
164
 
194
165
 
195
 
    def _unpack_revision_properties(self, elt, rev):
196
 
        """Unpack properties onto a revision."""
197
 
        props_elt = elt.find('properties')
198
 
        assert len(rev.properties) == 0
199
 
        if not props_elt:
200
 
            return
201
 
        for prop_elt in props_elt:
202
 
            assert prop_elt.tag == 'property', \
203
 
                "bad tag under properties list: %r" % p.tag
204
 
            name = prop_elt.get('name')
205
 
            value = prop_elt.text
206
 
            assert name not in rev.properties, \
207
 
                "repeated property %r" % p.name
208
 
            rev.properties[name] = value
209
 
 
210
166
 
211
167
serializer_v5 = Serializer_v5()