1
# Copyright (C) 2005, 2006 Canonical Ltd
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.
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.
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
23
from bzrlib.xml_serializer import SubElement, Element, Serializer
24
from bzrlib.inventory import ROOT_ID, Inventory, InventoryEntry
25
from bzrlib.revision import Revision
26
from bzrlib.errors import BzrError
29
class Serializer_v5(Serializer):
30
"""Version 5 serializer
32
Packs objects into XML and vice versa.
35
__slots__ = ['_utf8_re']
40
def write_inventory_to_string(self, inv):
41
sio = cStringIO.StringIO()
42
self.write_inventory(inv, sio)
45
def write_inventory(self, inv, f):
46
"""Write inventory to a file.
48
:param inv: the inventory to write.
49
:param f: the file to write.
52
self._append_inventory_root(output, inv)
53
entries = inv.iter_entries()
54
root_path, root_ie = entries.next()
55
for path, ie in entries:
56
self._append_entry(output, ie)
57
f.write(''.join(output))
58
# elt = self._pack_inventory(inv)
59
# for child in elt.getchildren():
60
# if isinstance(child, inventory.InventoryDirectory):
63
# ElementTree(child).write(f, 'utf-8')
64
f.write('</inventory>\n')
66
def _append_inventory_root(self, output, inv):
67
"""Append the inventory root to output."""
68
output.append('<inventory')
69
if inv.root.file_id not in (None, ROOT_ID):
70
output.append(' file_id="')
71
self._append_utf8_escaped(output, inv.root.file_id)
72
output.append(' format="5"')
73
if inv.revision_id is not None:
74
output.append(' revision_id="')
75
self._append_utf8_escaped(output, inv.revision_id)
78
def _append_entry(self, output, ie):
79
"""Convert InventoryEntry to XML element and append to output."""
80
# TODO: should just be a plain assertion
81
assert InventoryEntry.versionable_kind(ie.kind), \
82
'unsupported entry kind %s' % ie.kind
85
output.append(ie.kind)
87
output.append(' executable="yes"')
88
output.append(' file_id="')
89
self._append_utf8_escaped(output, ie.file_id)
90
output.append(' name="')
91
self._append_utf8_escaped(output, ie.name)
92
if ie.parent_id != ROOT_ID:
93
assert isinstance(ie.parent_id, basestring)
94
output.append(' parent_id="')
95
self._append_utf8_escaped(output, ie.parent_id)
96
if ie.revision is not None:
97
output.append(' revision="')
98
self._append_utf8_escaped(output, ie.revision)
99
if ie.symlink_target is not None:
100
output.append(' symlink_target="')
101
self._append_utf8_escaped(output, ie.symlink_target)
102
if ie.text_sha1 is not None:
103
output.append(' text_size="')
104
output.append(ie.text_sha1)
106
if ie.text_size is not None:
107
output.append(' text_size="%d"' % ie.text_size)
108
output.append(" />\n")
111
def _append_utf8_escaped(self, output, a_string):
112
"""Append a_string to output as utf8."""
113
if self._utf8_re is None:
115
self._utf8_re = re.compile("[&'\"<>]")
116
# escape attribute value
117
text = a_string.encode('utf8')
118
output.append(self._utf8_re.sub(self._utf8_escape_replace, text))
123
"'":"'", # FIXME: overkill
128
def _utf8_escape_replace(self, match, map=_utf8_escape_map):
129
return map[match.group()]
131
def _pack_inventory(self, inv):
132
"""Convert to XML Element"""
133
entries = inv.iter_entries()
134
e = Element('inventory',
137
path, root = entries.next()
138
if root.file_id not in (None, ROOT_ID):
139
e.set('file_id', root.file_id)
140
if inv.revision_id is not None:
141
e.set('revision_id', inv.revision_id)
142
for path, ie in entries:
143
e.append(self._pack_entry(ie))
146
def _pack_entry(self, ie):
147
"""Convert InventoryEntry to XML element"""
148
# TODO: should just be a plain assertion
149
if not InventoryEntry.versionable_kind(ie.kind):
150
raise AssertionError('unsupported entry kind %s' % ie.kind)
152
e.set('name', ie.name)
153
e.set('file_id', ie.file_id)
155
if ie.text_size != None:
156
e.set('text_size', '%d' % ie.text_size)
158
for f in ['text_sha1', 'revision', 'symlink_target']:
164
e.set('executable', 'yes')
166
# to be conservative, we don't externalize the root pointers
167
# for now, leaving them as null in the xml form. in a future
168
# version it will be implied by nested elements.
169
if ie.parent_id != ROOT_ID:
170
assert isinstance(ie.parent_id, basestring)
171
e.set('parent_id', ie.parent_id)
175
def _pack_revision(self, rev):
176
"""Revision object -> xml tree"""
177
root = Element('revision',
178
committer = rev.committer,
179
timestamp = '%.9f' % rev.timestamp,
180
revision_id = rev.revision_id,
181
inventory_sha1 = rev.inventory_sha1,
184
if rev.timezone is not None:
185
root.set('timezone', str(rev.timezone))
187
msg = SubElement(root, 'message')
188
msg.text = rev.message
191
pelts = SubElement(root, 'parents')
192
pelts.tail = pelts.text = '\n'
193
for parent_id in rev.parent_ids:
194
assert isinstance(parent_id, basestring)
195
p = SubElement(pelts, 'revision_ref')
197
p.set('revision_id', parent_id)
199
self._pack_revision_properties(rev, root)
203
def _pack_revision_properties(self, rev, under_element):
204
top_elt = SubElement(under_element, 'properties')
205
for prop_name, prop_value in sorted(rev.properties.items()):
206
assert isinstance(prop_name, basestring)
207
assert isinstance(prop_value, basestring)
208
prop_elt = SubElement(top_elt, 'property')
209
prop_elt.set('name', prop_name)
210
prop_elt.text = prop_value
215
def _unpack_inventory(self, elt):
216
"""Construct from XML Element
218
assert elt.tag == 'inventory'
219
root_id = elt.get('file_id') or ROOT_ID
220
format = elt.get('format')
221
if format is not None:
223
raise BzrError("invalid format version %r on inventory"
225
revision_id = elt.get('revision_id')
226
if revision_id is not None:
227
revision_id = cache_utf8.get_cached_unicode(revision_id)
228
inv = Inventory(root_id, revision_id=revision_id)
230
ie = self._unpack_entry(e)
231
if ie.parent_id == ROOT_ID:
232
ie.parent_id = root_id
237
def _unpack_entry(self, elt):
239
if not InventoryEntry.versionable_kind(kind):
240
raise AssertionError('unsupported entry kind %s' % kind)
242
get_cached = cache_utf8.get_cached_unicode
244
parent_id = elt.get('parent_id')
245
if parent_id == None:
247
parent_id = get_cached(parent_id)
248
file_id = get_cached(elt.get('file_id'))
250
if kind == 'directory':
251
ie = inventory.InventoryDirectory(file_id,
255
ie = inventory.InventoryFile(file_id,
258
ie.text_sha1 = elt.get('text_sha1')
259
if elt.get('executable') == 'yes':
261
v = elt.get('text_size')
262
ie.text_size = v and int(v)
263
elif kind == 'symlink':
264
ie = inventory.InventoryLink(file_id,
267
ie.symlink_target = elt.get('symlink_target')
269
raise BzrError("unknown kind %r" % kind)
270
revision = elt.get('revision')
271
if revision is not None:
272
revision = get_cached(revision)
273
ie.revision = revision
278
def _unpack_revision(self, elt):
279
"""XML Element -> Revision object"""
280
assert elt.tag == 'revision'
281
format = elt.get('format')
282
if format is not None:
284
raise BzrError("invalid format version %r on inventory"
286
get_cached = cache_utf8.get_cached_unicode
287
rev = Revision(committer = elt.get('committer'),
288
timestamp = float(elt.get('timestamp')),
289
revision_id = get_cached(elt.get('revision_id')),
290
inventory_sha1 = elt.get('inventory_sha1')
292
parents = elt.find('parents') or []
294
assert p.tag == 'revision_ref', \
295
"bad parent node tag %r" % p.tag
296
rev.parent_ids.append(get_cached(p.get('revision_id')))
297
self._unpack_revision_properties(elt, rev)
298
v = elt.get('timezone')
302
rev.timezone = int(v)
303
rev.message = elt.findtext('message') # text of <message>
307
def _unpack_revision_properties(self, elt, rev):
308
"""Unpack properties onto a revision."""
309
props_elt = elt.find('properties')
310
assert len(rev.properties) == 0
313
for prop_elt in props_elt:
314
assert prop_elt.tag == 'property', \
315
"bad tag under properties list: %r" % prop_elt.tag
316
name = prop_elt.get('name')
317
value = prop_elt.text
318
# If a property had an empty value ('') cElementTree reads
319
# that back as None, convert it back to '', so that all
320
# properties have string values
323
assert name not in rev.properties, \
324
"repeated property %r" % name
325
rev.properties[name] = value
328
serializer_v5 = Serializer_v5()