1
# Copyright (C) 2005, 2006 Canonical Ltd
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
5
5
# the Free Software Foundation; either version 2 of the License, or
6
6
# (at your option) any later version.
8
8
# This program is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
11
# GNU General Public License for more details.
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
from bzrlib.xml import ElementTree, SubElement, Element, Serializer
18
from bzrlib.xml_serializer import SubElement, Element, Serializer
19
19
from bzrlib.inventory import ROOT_ID, Inventory, InventoryEntry
20
from bzrlib.revision import Revision, RevisionReference
20
import bzrlib.inventory as inventory
21
from bzrlib.revision import Revision
21
22
from bzrlib.errors import BzrError
27
25
class Serializer_v5(Serializer):
28
26
"""Version 5 serializer
35
33
def _pack_inventory(self, inv):
36
34
"""Convert to XML Element"""
37
e = Element('inventory')
35
entries = inv.iter_entries()
36
e = Element('inventory',
39
if inv.root.file_id not in (None, ROOT_ID):
40
e.set('file_id', inv.root.file_id)
41
for path, ie in inv.iter_entries():
39
path, root = entries.next()
40
if root.file_id not in (None, ROOT_ID):
41
e.set('file_id', root.file_id)
42
if inv.revision_id is not None:
43
e.set('revision_id', inv.revision_id)
44
for path, ie in entries:
42
45
e.append(self._pack_entry(ie))
46
48
def _pack_entry(self, ie):
47
49
"""Convert InventoryEntry to XML element"""
48
assert ie.kind == 'directory' or ie.kind == 'file'
50
# TODO: should just be a plain assertion
51
if not InventoryEntry.versionable_kind(ie.kind):
52
raise AssertionError('unsupported entry kind %s' % ie.kind)
49
53
e = Element(ie.kind)
50
54
e.set('name', ie.name)
51
55
e.set('file_id', ie.file_id)
53
57
if ie.text_size != None:
54
58
e.set('text_size', '%d' % ie.text_size)
56
for f in ['text_version', 'text_sha1', 'entry_version']:
60
for f in ['text_sha1', 'revision', 'symlink_target']:
66
e.set('executable', 'yes')
61
68
# to be conservative, we don't externalize the root pointers
62
69
# for now, leaving them as null in the xml form. in a future
63
70
# version it will be implied by nested elements.
64
71
if ie.parent_id != ROOT_ID:
65
72
assert isinstance(ie.parent_id, basestring)
66
73
e.set('parent_id', ie.parent_id)
73
77
def _pack_revision(self, rev):
74
78
"""Revision object -> xml tree"""
75
79
root = Element('revision',
77
81
timestamp = '%.9f' % rev.timestamp,
78
82
revision_id = rev.revision_id,
79
83
inventory_sha1 = rev.inventory_sha1,
82
87
root.set('timezone', str(rev.timezone))
85
89
msg = SubElement(root, 'message')
86
90
msg.text = rev.message
90
93
pelts = SubElement(root, 'parents')
91
94
pelts.tail = pelts.text = '\n'
92
for rr in rev.parents:
93
assert isinstance(rr, RevisionReference)
95
for parent_id in rev.parent_ids:
96
assert isinstance(parent_id, basestring)
94
97
p = SubElement(pelts, 'revision_ref')
97
p.set('revision_id', rr.revision_id)
99
p.set('revision_id', parent_id)
101
self._pack_revision_properties(rev, root)
105
def _pack_revision_properties(self, rev, under_element):
106
top_elt = SubElement(under_element, 'properties')
107
for prop_name, prop_value in sorted(rev.properties.items()):
108
assert isinstance(prop_name, basestring)
109
assert isinstance(prop_value, basestring)
110
prop_elt = SubElement(top_elt, 'property')
111
prop_elt.set('name', prop_name)
112
prop_elt.text = prop_value
103
117
def _unpack_inventory(self, elt):
104
118
"""Construct from XML Element
106
120
assert elt.tag == 'inventory'
107
121
root_id = elt.get('file_id') or ROOT_ID
108
inv = Inventory(root_id)
122
format = elt.get('format')
123
if format is not None:
125
raise BzrError("invalid format version %r on inventory"
127
revision_id = elt.get('revision_id')
128
inv = Inventory(root_id, revision_id=revision_id)
110
130
ie = self._unpack_entry(e)
111
131
if ie.parent_id == ROOT_ID:
117
137
def _unpack_entry(self, elt):
119
assert kind == 'directory' or kind == 'file'
139
if not InventoryEntry.versionable_kind(kind):
140
raise AssertionError('unsupported entry kind %s' % kind)
121
142
parent_id = elt.get('parent_id')
122
143
if parent_id == None:
123
144
parent_id = ROOT_ID
125
ie = InventoryEntry(elt.get('file_id'),
129
ie.text_version = elt.get('text_version')
130
ie.entry_version = elt.get('entry_version')
131
ie.text_sha1 = elt.get('text_sha1')
132
v = elt.get('text_size')
133
ie.text_size = v and int(v)
146
if kind == 'directory':
147
ie = inventory.InventoryDirectory(elt.get('file_id'),
151
ie = inventory.InventoryFile(elt.get('file_id'),
154
ie.text_sha1 = elt.get('text_sha1')
155
if elt.get('executable') == 'yes':
157
v = elt.get('text_size')
158
ie.text_size = v and int(v)
159
elif kind == 'symlink':
160
ie = inventory.InventoryLink(elt.get('file_id'),
163
ie.symlink_target = elt.get('symlink_target')
165
raise BzrError("unknown kind %r" % kind)
166
ie.revision = elt.get('revision')
138
171
def _unpack_revision(self, elt):
139
172
"""XML Element -> Revision object"""
140
173
assert elt.tag == 'revision'
174
format = elt.get('format')
175
if format is not None:
177
raise BzrError("invalid format version %r on inventory"
142
179
rev = Revision(committer = elt.get('committer'),
143
180
timestamp = float(elt.get('timestamp')),
144
181
revision_id = elt.get('revision_id'),
145
182
inventory_sha1 = elt.get('inventory_sha1')
148
184
parents = elt.find('parents') or []
149
185
for p in parents:
150
186
assert p.tag == 'revision_ref', \
151
187
"bad parent node tag %r" % p.tag
152
rev_ref = RevisionReference(p.get('revision_id'))
153
rev.parents.append(rev_ref)
188
rev.parent_ids.append(p.get('revision_id'))
189
self._unpack_revision_properties(elt, rev)
155
190
v = elt.get('timezone')
156
191
rev.timezone = v and int(v)
158
192
rev.message = elt.findtext('message') # text of <message>
196
def _unpack_revision_properties(self, elt, rev):
197
"""Unpack properties onto a revision."""
198
props_elt = elt.find('properties')
199
assert len(rev.properties) == 0
202
for prop_elt in props_elt:
203
assert prop_elt.tag == 'property', \
204
"bad tag under properties list: %r" % prop_elt.tag
205
name = prop_elt.get('name')
206
value = prop_elt.text
207
# If a property had an empty value ('') cElementTree reads
208
# that back as None, convert it back to '', so that all
209
# properties have string values
212
assert name not in rev.properties, \
213
"repeated property %r" % name
214
rev.properties[name] = value
163
217
serializer_v5 = Serializer_v5()