13
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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
23
27
class Serializer_v5(Serializer):
24
28
"""Version 5 serializer
31
35
def _pack_inventory(self, inv):
32
36
"""Convert to XML Element"""
33
e = Element('inventory',
37
e = Element('inventory')
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))
80
81
timestamp = '%.9f' % rev.timestamp,
81
82
revision_id = rev.revision_id,
82
83
inventory_sha1 = rev.inventory_sha1,
86
86
root.set('timezone', str(rev.timezone))
88
89
msg = SubElement(root, 'message')
89
90
msg.text = rev.message
92
94
pelts = SubElement(root, 'parents')
93
95
pelts.tail = pelts.text = '\n'
96
98
p = SubElement(pelts, 'revision_ref')
98
100
p.set('revision_id', parent_id)
100
self._pack_revision_properties(rev, root)
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
116
105
def _unpack_inventory(self, elt):
117
106
"""Construct from XML Element
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:
124
raise BzrError("invalid format version %r on inventory"
126
revision_id = elt.get('revision_id')
127
inv = Inventory(root_id, revision_id=revision_id)
110
inv = Inventory(root_id)
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
145
if kind == 'directory':
146
ie = inventory.InventoryDirectory(elt.get('file_id'),
150
ie = inventory.InventoryFile(elt.get('file_id'),
153
ie.text_sha1 = elt.get('text_sha1')
154
if elt.get('executable') == 'yes':
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'),
162
ie.symlink_target = elt.get('symlink_target')
164
raise BzrError("unknown kind %r" % kind)
128
ie = InventoryEntry(elt.get('file_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':
137
v = elt.get('text_size')
138
ie.text_size = v and int(v)
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:
176
raise BzrError("invalid format version %r on inventory"
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')
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)
189
159
v = elt.get('timezone')
190
160
rev.timezone = v and int(v)
191
162
rev.message = elt.findtext('message') # text of <message>
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
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
211
167
serializer_v5 = Serializer_v5()