/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/bzr/xml5.py

  • Committer: Jelmer Vernooij
  • Date: 2020-06-23 01:02:30 UTC
  • mfrom: (7490.40.27 work)
  • mto: This revision was merged to the branch mainline in revision 7517.
  • Revision ID: jelmer@jelmer.uk-20200623010230-62nnywznmb76h6ut
Merge lp:brz/3.1.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2008, 2009, 2010 Canonical Ltd
 
2
#
 
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.
 
7
#
 
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.
 
12
#
 
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
 
16
 
 
17
from .. import (
 
18
    cache_utf8,
 
19
    errors,
 
20
    osutils,
 
21
    )
 
22
from . import (
 
23
    inventory,
 
24
    xml6,
 
25
    )
 
26
from .xml_serializer import (
 
27
    encode_and_escape,
 
28
    get_utf8_or_ascii,
 
29
    unpack_inventory_entry,
 
30
    )
 
31
 
 
32
 
 
33
class Serializer_v5(xml6.Serializer_v6):
 
34
    """Version 5 serializer
 
35
 
 
36
    Packs objects into XML and vice versa.
 
37
    """
 
38
    format_num = b'5'
 
39
    root_id = inventory.ROOT_ID
 
40
 
 
41
    def _unpack_inventory(self, elt, revision_id, entry_cache=None,
 
42
                          return_from_cache=False):
 
43
        """Construct from XML Element
 
44
        """
 
45
        root_id = elt.get('file_id') or inventory.ROOT_ID
 
46
        root_id = get_utf8_or_ascii(root_id)
 
47
 
 
48
        format = elt.get('format')
 
49
        if format is not None:
 
50
            if format != '5':
 
51
                raise errors.BzrError("invalid format version %r on inventory"
 
52
                                      % format)
 
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)
 
63
        byid = inv._byid
 
64
        for e in elt:
 
65
            ie = unpack_inventory_entry(e, entry_cache=entry_cache,
 
66
                                        return_from_cache=return_from_cache)
 
67
            parent_id = ie.parent_id
 
68
            if parent_id is None:
 
69
                ie.parent_id = parent_id = root_id
 
70
            try:
 
71
                parent = byid[parent_id]
 
72
            except KeyError:
 
73
                raise errors.BzrError("parent_id {%s} not in inventory"
 
74
                                      % (parent_id,))
 
75
            if ie.file_id in byid:
 
76
                raise errors.DuplicateFileId(ie.file_id,
 
77
                                             byid[ie.file_id])
 
78
            if ie.name in parent.children:
 
79
                raise errors.BzrError(
 
80
                    "%s is already versioned" % (
 
81
                        osutils.pathjoin(
 
82
                            inv.id2path(parent_id), ie.name).encode('utf-8'),))
 
83
            parent.children[ie.name] = ie
 
84
            byid[ie.file_id] = ie
 
85
        if revision_id is not None:
 
86
            inv.root.revision = revision_id
 
87
        self._check_cache_size(len(inv), entry_cache)
 
88
        return inv
 
89
 
 
90
    def _check_revisions(self, inv):
 
91
        """Extension point for subclasses to check during serialisation.
 
92
 
 
93
        In this version, no checking is done.
 
94
 
 
95
        :param inv: An inventory about to be serialised, to be checked.
 
96
        :raises: AssertionError if an error has occurred.
 
97
        """
 
98
 
 
99
    def _append_inventory_root(self, append, inv):
 
100
        """Append the inventory root to output."""
 
101
        if inv.root.file_id not in (None, inventory.ROOT_ID):
 
102
            fileid = b''.join([b' file_id="', encode_and_escape(inv.root.file_id), b'"'])
 
103
        else:
 
104
            fileid = b""
 
105
        if inv.revision_id is not None:
 
106
            revid = b''.join([b' revision_id="', encode_and_escape(inv.revision_id), b'"'])
 
107
        else:
 
108
            revid = b""
 
109
        append(b'<inventory%s format="5"%s>\n' % (fileid, revid))
 
110
 
 
111
 
 
112
serializer_v5 = Serializer_v5()