1
# This program is free software; you can redistribute it and/or modify
2
# it under the terms of the GNU General Public License as published by
3
# the Free Software Foundation; either version 2 of the License, or
4
# (at your option) any later version.
6
# This program is distributed in the hope that it will be useful,
7
# but WITHOUT ANY WARRANTY; without even the implied warranty of
8
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9
# GNU General Public License for more details.
11
# You should have received a copy of the GNU General Public License
12
# along with this program; if not, write to the Free Software
13
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16
from bzrlib.xml_serializer import ElementTree, SubElement, Element, Serializer
17
from bzrlib.inventory import ROOT_ID, Inventory, InventoryEntry
18
import bzrlib.inventory as inventory
19
from bzrlib.revision import Revision
20
from bzrlib.errors import BzrError
23
class Serializer_v5(Serializer):
24
"""Version 5 serializer
26
Packs objects into XML and vice versa.
31
def _pack_inventory(self, inv):
32
"""Convert to XML Element"""
33
e = Element('inventory',
36
if inv.root.file_id not in (None, ROOT_ID):
37
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
for path, ie in inv.iter_entries():
41
e.append(self._pack_entry(ie))
45
def _pack_entry(self, ie):
46
"""Convert InventoryEntry to XML element"""
47
if not InventoryEntry.versionable_kind(ie.kind):
48
raise AssertionError('unsupported entry kind %s' % ie.kind)
50
e.set('name', ie.name)
51
e.set('file_id', ie.file_id)
53
if ie.text_size != None:
54
e.set('text_size', '%d' % ie.text_size)
56
for f in ['text_sha1', 'revision', 'symlink_target']:
62
e.set('executable', 'yes')
64
# to be conservative, we don't externalize the root pointers
65
# for now, leaving them as null in the xml form. in a future
66
# version it will be implied by nested elements.
67
if ie.parent_id != ROOT_ID:
68
assert isinstance(ie.parent_id, basestring)
69
e.set('parent_id', ie.parent_id)
76
def _pack_revision(self, rev):
77
"""Revision object -> xml tree"""
78
root = Element('revision',
79
committer = rev.committer,
80
timestamp = '%.9f' % rev.timestamp,
81
revision_id = rev.revision_id,
82
inventory_sha1 = rev.inventory_sha1,
86
root.set('timezone', str(rev.timezone))
88
msg = SubElement(root, 'message')
89
msg.text = rev.message
92
pelts = SubElement(root, 'parents')
93
pelts.tail = pelts.text = '\n'
94
for parent_id in rev.parent_ids:
95
assert isinstance(parent_id, basestring)
96
p = SubElement(pelts, 'revision_ref')
98
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
def _unpack_inventory(self, elt):
117
"""Construct from XML Element
119
assert elt.tag == 'inventory'
120
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)
129
ie = self._unpack_entry(e)
130
if ie.parent_id == ROOT_ID:
131
ie.parent_id = root_id
136
def _unpack_entry(self, elt):
138
if not InventoryEntry.versionable_kind(kind):
139
raise AssertionError('unsupported entry kind %s' % kind)
141
parent_id = elt.get('parent_id')
142
if parent_id == None:
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)
165
ie.revision = elt.get('revision')
170
def _unpack_revision(self, elt):
171
"""XML Element -> Revision object"""
172
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
rev = Revision(committer = elt.get('committer'),
179
timestamp = float(elt.get('timestamp')),
180
revision_id = elt.get('revision_id'),
181
inventory_sha1 = elt.get('inventory_sha1')
183
parents = elt.find('parents') or []
185
assert p.tag == 'revision_ref', \
186
"bad parent node tag %r" % p.tag
187
rev.parent_ids.append(p.get('revision_id'))
188
self._unpack_revision_properties(elt, rev)
189
v = elt.get('timezone')
190
rev.timezone = v and int(v)
191
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
serializer_v5 = Serializer_v5()