54
65
# TODO: add a 'validate_utf8' for things like revision_id and file_id
55
66
# and a validator for parent-ids
56
_schema = {'format': (None, int, _is_format_10),
57
'committer': ('committer', str, cache_utf8.decode),
58
'timezone': ('timezone', int, None),
59
'timestamp': ('timestamp', str, float),
60
'revision-id': ('revision_id', str, None),
61
'parent-ids': ('parent_ids', list, None),
62
'inventory-sha1': ('inventory_sha1', str, None),
63
'message': ('message', str, cache_utf8.decode),
64
'properties': ('properties', dict, _validate_properties),
67
_schema = {b'format': (None, int, _is_format_10),
68
b'committer': ('committer', bytes, cache_utf8.decode),
69
b'timezone': ('timezone', int, None),
70
b'timestamp': ('timestamp', bytes, float),
71
b'revision-id': ('revision_id', bytes, None),
72
b'parent-ids': ('parent_ids', list, None),
73
b'inventory-sha1': ('inventory_sha1', bytes, None),
74
b'message': ('message', bytes, cache_utf8.decode),
75
b'properties': ('properties', dict, _validate_properties),
67
78
def write_revision_to_string(self, rev):
68
79
encode_utf8 = cache_utf8._utf8_encode
70
81
# This lets us control the ordering, so that we are able to create
74
("committer", encode_utf8(rev.committer)[0]),
85
(b"committer", encode_utf8(rev.committer)[0]),
76
87
if rev.timezone is not None:
77
ret.append(("timezone", rev.timezone))
88
ret.append((b"timezone", rev.timezone))
78
89
# For bzr revisions, the most common property is just 'branch-nick'
79
90
# which changes infrequently.
81
for key, value in rev.properties.iteritems():
82
revprops[key] = encode_utf8(value)[0]
83
ret.append(('properties', revprops))
92
for key, value in rev.properties.items():
93
revprops[encode_utf8(key)[0]] = encode_utf8(value)[0]
94
ret.append((b'properties', revprops))
85
("timestamp", "%.3f" % rev.timestamp),
86
("revision-id", rev.revision_id),
87
("parent-ids", rev.parent_ids),
88
("inventory-sha1", rev.inventory_sha1),
89
("message", encode_utf8(rev.message)[0]),
96
(b"timestamp", b"%.3f" % rev.timestamp),
97
(b"revision-id", rev.revision_id),
98
(b"parent-ids", rev.parent_ids),
99
(b"inventory-sha1", rev.inventory_sha1),
100
(b"message", encode_utf8(rev.message)[0]),
91
102
return bencode.bencode(ret)
131
142
return self.read_revision_from_string(f.read())
134
class CHKSerializerSubtree(BEncodeRevisionSerializer1, xml7.Serializer_v7):
135
"""A CHKInventory based serializer that supports tree references"""
145
class CHKSerializer(serializer.Serializer):
146
"""A CHKInventory based serializer with 'plain' behaviour."""
137
supported_kinds = set(['file', 'directory', 'symlink', 'tree-reference'])
139
149
revision_format_num = None
140
150
support_altered_by_hack = False
142
def _unpack_entry(self, elt, entry_cache=None, return_from_cache=False):
144
if not kind in self.supported_kinds:
145
raise AssertionError('unsupported entry kind %s' % kind)
146
if kind == 'tree-reference':
147
file_id = elt.attrib['file_id']
148
name = elt.attrib['name']
149
parent_id = elt.attrib['parent_id']
150
revision = elt.get('revision')
151
reference_revision = elt.get('reference_revision')
152
return inventory.TreeReference(file_id, name, parent_id, revision,
151
supported_kinds = {'file', 'directory', 'symlink', 'tree-reference'}
153
def __init__(self, node_size, search_key_name):
154
self.maximum_size = node_size
155
self.search_key_name = search_key_name
157
def _unpack_inventory(self, elt, revision_id=None, entry_cache=None,
158
return_from_cache=False):
159
"""Construct from XML Element"""
160
inv = xml_serializer.unpack_inventory_flat(elt, self.format_num,
161
xml_serializer.unpack_inventory_entry, entry_cache,
165
def read_inventory_from_string(self, xml_string, revision_id=None,
166
entry_cache=None, return_from_cache=False):
167
"""Read xml_string into an inventory object.
169
:param xml_string: The xml to read.
170
:param revision_id: If not-None, the expected revision id of the
172
:param entry_cache: An optional cache of InventoryEntry objects. If
173
supplied we will look up entries via (file_id, revision_id) which
174
should map to a valid InventoryEntry (File/Directory/etc) object.
175
:param return_from_cache: Return entries directly from the cache,
176
rather than copying them first. This is only safe if the caller
177
promises not to mutate the returned inventory entries, but it can
178
make some operations significantly faster.
181
return self._unpack_inventory(
182
xml_serializer.fromstring(xml_string), revision_id,
183
entry_cache=entry_cache,
184
return_from_cache=return_from_cache)
185
except xml_serializer.ParseError as e:
186
raise errors.UnexpectedInventoryFormat(e)
188
def read_inventory(self, f, revision_id=None):
189
"""Read an inventory from a file-like object."""
192
return self._unpack_inventory(self._read_element(f),
196
except xml_serializer.ParseError as e:
197
raise errors.UnexpectedInventoryFormat(e)
199
def write_inventory_to_lines(self, inv):
200
"""Return a list of lines with the encoded inventory."""
201
return self.write_inventory(inv, None)
203
def write_inventory_to_string(self, inv, working=False):
204
"""Just call write_inventory with a BytesIO and return the value.
206
:param working: If True skip history data - text_sha1, text_size,
207
reference_revision, symlink_target.
210
self.write_inventory(inv, sio, working)
211
return sio.getvalue()
213
def write_inventory(self, inv, f, working=False):
214
"""Write inventory to a file.
216
:param inv: the inventory to write.
217
:param f: the file to write. (May be None if the lines are the desired
219
:param working: If True skip history data - text_sha1, text_size,
220
reference_revision, symlink_target.
221
:return: The inventory as a list of lines.
224
append = output.append
225
if inv.revision_id is not None:
226
revid1 = b' revision_id="'
227
revid2 = xml_serializer.encode_and_escape(inv.revision_id)
155
return xml7.Serializer_v7._unpack_entry(self, elt,
156
entry_cache=entry_cache, return_from_cache=return_from_cache)
158
def __init__(self, node_size, search_key_name):
159
self.maximum_size = node_size
160
self.search_key_name = search_key_name
163
class CHKSerializer(xml6.Serializer_v6):
164
"""A CHKInventory based serializer with 'plain' behaviour."""
167
revision_format_num = None
168
support_altered_by_hack = False
170
def __init__(self, node_size, search_key_name):
171
self.maximum_size = node_size
172
self.search_key_name = search_key_name
175
chk_serializer_255_bigpage = CHKSerializer(65536, 'hash-255-way')
231
append(b'<inventory format="%s"%s%s>\n' % (
232
self.format_num, revid1, revid2))
233
append(b'<directory file_id="%s name="%s revision="%s />\n' % (
234
xml_serializer.encode_and_escape(inv.root.file_id),
235
xml_serializer.encode_and_escape(inv.root.name),
236
xml_serializer.encode_and_escape(inv.root.revision)))
237
xml_serializer.serialize_inventory_flat(inv,
239
root_id=None, supported_kinds=self.supported_kinds,
246
chk_serializer_255_bigpage = CHKSerializer(65536, b'hash-255-way')
178
249
class CHKBEncodeSerializer(BEncodeRevisionSerializer1, CHKSerializer):
179
250
"""A CHKInventory and BEncode based serializer with 'plain' behaviour."""
184
chk_bencode_serializer = CHKBEncodeSerializer(65536, 'hash-255-way')
255
chk_bencode_serializer = CHKBEncodeSerializer(65536, b'hash-255-way')