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
21
from bzrlib.xml_serializer import SubElement, Element, Serializer
22
from bzrlib.inventory import ROOT_ID, Inventory, InventoryEntry
23
import bzrlib.inventory as inventory
24
from bzrlib.revision import Revision
25
from bzrlib.errors import BzrError
28
class Serializer_v5(Serializer):
29
"""Version 5 serializer
31
Packs objects into XML and vice versa.
36
def _pack_inventory(self, inv):
37
"""Convert to XML Element"""
38
entries = inv.iter_entries()
39
e = Element('inventory',
42
path, root = entries.next()
43
if root.file_id not in (None, ROOT_ID):
44
e.set('file_id', root.file_id)
45
if inv.revision_id is not None:
46
e.set('revision_id', inv.revision_id)
47
for path, ie in entries:
48
e.append(self._pack_entry(ie))
51
def _pack_entry(self, ie):
52
"""Convert InventoryEntry to XML element"""
53
# TODO: should just be a plain assertion
54
if not InventoryEntry.versionable_kind(ie.kind):
55
raise AssertionError('unsupported entry kind %s' % ie.kind)
57
e.set('name', ie.name)
58
e.set('file_id', ie.file_id)
60
if ie.text_size != None:
61
e.set('text_size', '%d' % ie.text_size)
63
for f in ['text_sha1', 'revision', 'symlink_target']:
69
e.set('executable', 'yes')
71
# to be conservative, we don't externalize the root pointers
72
# for now, leaving them as null in the xml form. in a future
73
# version it will be implied by nested elements.
74
if ie.parent_id != ROOT_ID:
75
assert isinstance(ie.parent_id, basestring)
76
e.set('parent_id', ie.parent_id)
80
def _pack_revision(self, rev):
81
"""Revision object -> xml tree"""
82
root = Element('revision',
83
committer = rev.committer,
84
timestamp = '%.9f' % rev.timestamp,
85
revision_id = rev.revision_id,
86
inventory_sha1 = rev.inventory_sha1,
90
root.set('timezone', str(rev.timezone))
92
msg = SubElement(root, 'message')
93
msg.text = rev.message
96
pelts = SubElement(root, 'parents')
97
pelts.tail = pelts.text = '\n'
98
for parent_id in rev.parent_ids:
99
assert isinstance(parent_id, basestring)
100
p = SubElement(pelts, 'revision_ref')
102
p.set('revision_id', parent_id)
104
self._pack_revision_properties(rev, root)
108
def _pack_revision_properties(self, rev, under_element):
109
top_elt = SubElement(under_element, 'properties')
110
for prop_name, prop_value in sorted(rev.properties.items()):
111
assert isinstance(prop_name, basestring)
112
assert isinstance(prop_value, basestring)
113
prop_elt = SubElement(top_elt, 'property')
114
prop_elt.set('name', prop_name)
115
prop_elt.text = prop_value
120
def _unpack_inventory(self, elt):
121
"""Construct from XML Element
123
assert elt.tag == 'inventory'
124
root_id = elt.get('file_id') or ROOT_ID
125
format = elt.get('format')
126
if format is not None:
128
raise BzrError("invalid format version %r on inventory"
130
revision_id = elt.get('revision_id')
131
if revision_id is not None:
132
revision_id = cache_utf8.get_cached_unicode(revision_id)
133
inv = Inventory(root_id, revision_id=revision_id)
135
ie = self._unpack_entry(e)
136
if ie.parent_id == ROOT_ID:
137
ie.parent_id = root_id
142
def _unpack_entry(self, elt):
144
if not InventoryEntry.versionable_kind(kind):
145
raise AssertionError('unsupported entry kind %s' % kind)
147
get_cached = cache_utf8.get_cached_unicode
149
parent_id = elt.get('parent_id')
150
if parent_id == None:
152
parent_id = get_cached(parent_id)
153
file_id = get_cached(elt.get('file_id'))
155
if kind == 'directory':
156
ie = inventory.InventoryDirectory(file_id,
160
ie = inventory.InventoryFile(file_id,
163
ie.text_sha1 = elt.get('text_sha1')
164
if elt.get('executable') == 'yes':
166
v = elt.get('text_size')
167
ie.text_size = v and int(v)
168
elif kind == 'symlink':
169
ie = inventory.InventoryLink(file_id,
172
ie.symlink_target = elt.get('symlink_target')
174
raise BzrError("unknown kind %r" % kind)
175
revision = elt.get('revision')
176
if revision is not None:
177
revision = get_cached(revision)
178
ie.revision = revision
183
def _unpack_revision(self, elt):
184
"""XML Element -> Revision object"""
185
assert elt.tag == 'revision'
186
format = elt.get('format')
187
if format is not None:
189
raise BzrError("invalid format version %r on inventory"
191
get_cached = cache_utf8.get_cached_unicode
192
rev = Revision(committer = elt.get('committer'),
193
timestamp = float(elt.get('timestamp')),
194
revision_id = get_cached(elt.get('revision_id')),
195
inventory_sha1 = elt.get('inventory_sha1')
197
parents = elt.find('parents') or []
199
assert p.tag == 'revision_ref', \
200
"bad parent node tag %r" % p.tag
201
rev.parent_ids.append(get_cached(p.get('revision_id')))
202
self._unpack_revision_properties(elt, rev)
203
v = elt.get('timezone')
204
rev.timezone = v and int(v)
205
rev.message = elt.findtext('message') # text of <message>
209
def _unpack_revision_properties(self, elt, rev):
210
"""Unpack properties onto a revision."""
211
props_elt = elt.find('properties')
212
assert len(rev.properties) == 0
215
for prop_elt in props_elt:
216
assert prop_elt.tag == 'property', \
217
"bad tag under properties list: %r" % prop_elt.tag
218
name = prop_elt.get('name')
219
value = prop_elt.text
220
# If a property had an empty value ('') cElementTree reads
221
# that back as None, convert it back to '', so that all
222
# properties have string values
225
assert name not in rev.properties, \
226
"repeated property %r" % name
227
rev.properties[name] = value
230
serializer_v5 = Serializer_v5()