1
# Copyright (C) 2008, 2009, 2010 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26
from .xml_serializer import (
29
unpack_inventory_entry,
33
class Serializer_v5(xml6.Serializer_v6):
34
"""Version 5 serializer
36
Packs objects into XML and vice versa.
39
root_id = inventory.ROOT_ID
41
def _unpack_inventory(self, elt, revision_id, entry_cache=None,
42
return_from_cache=False):
43
"""Construct from XML Element
45
root_id = elt.get('file_id') or inventory.ROOT_ID
46
root_id = get_utf8_or_ascii(root_id)
48
format = elt.get('format')
49
if format is not None:
51
raise errors.BzrError("invalid format version %r on inventory"
53
data_revision_id = elt.get('revision_id')
54
if data_revision_id is not None:
55
revision_id = cache_utf8.encode(data_revision_id)
56
inv = inventory.Inventory(root_id, revision_id=revision_id)
57
# Optimizations tested
58
# baseline w/entry cache 2.85s
59
# using inv._byid 2.55s
60
# avoiding attributes 2.46s
61
# adding assertions 2.50s
62
# last_parent cache 2.52s (worse, removed)
65
ie = unpack_inventory_entry(e, entry_cache=entry_cache,
66
return_from_cache=return_from_cache)
67
parent_id = ie.parent_id
69
ie.parent_id = parent_id = root_id
71
parent = byid[parent_id]
73
raise errors.BzrError("parent_id {%s} not in inventory"
75
if ie.file_id in byid:
76
raise inventory.DuplicateFileId(ie.file_id, byid[ie.file_id])
77
if ie.name in parent.children:
78
raise errors.BzrError(
79
"%s is already versioned" % (
81
inv.id2path(parent_id), ie.name).encode('utf-8'),))
82
parent.children[ie.name] = ie
84
if revision_id is not None:
85
inv.root.revision = revision_id
86
self._check_cache_size(len(inv), entry_cache)
89
def _check_revisions(self, inv):
90
"""Extension point for subclasses to check during serialisation.
92
In this version, no checking is done.
94
:param inv: An inventory about to be serialised, to be checked.
95
:raises: AssertionError if an error has occurred.
98
def _append_inventory_root(self, append, inv):
99
"""Append the inventory root to output."""
100
if inv.root.file_id not in (None, inventory.ROOT_ID):
101
fileid = b''.join([b' file_id="', encode_and_escape(inv.root.file_id), b'"'])
104
if inv.revision_id is not None:
105
revid = b''.join([b' revision_id="', encode_and_escape(inv.revision_id), b'"'])
108
append(b'<inventory%s format="5"%s>\n' % (fileid, revid))
111
serializer_v5 = Serializer_v5()