~bzr-pqm/bzr/bzr.dev

1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
1
# Copyright (C) 2005, 2006 Canonical Ltd
2
#
1189 by Martin Pool
- BROKEN: partial support for commit into weave
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.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
7
#
1189 by Martin Pool
- BROKEN: partial support for commit into weave
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.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
12
#
1189 by Martin Pool
- BROKEN: partial support for commit into weave
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17
1911.2.6 by John Arbash Meinel
Cache revision ids and file ids as part of xml processing. A custom xml parser could just call decode/encode directly.
18
from bzrlib import (
19
    cache_utf8,
20
    )
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
21
from bzrlib.xml_serializer import SubElement, Element, Serializer
1189 by Martin Pool
- BROKEN: partial support for commit into weave
22
from bzrlib.inventory import ROOT_ID, Inventory, InventoryEntry
1399.1.8 by Robert Collins
factor out inventory directory logic into 'InventoryDirectory' class
23
import bzrlib.inventory as inventory
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
24
from bzrlib.revision import Revision
1189 by Martin Pool
- BROKEN: partial support for commit into weave
25
from bzrlib.errors import BzrError
26
27
28
class Serializer_v5(Serializer):
29
    """Version 5 serializer
30
31
    Packs objects into XML and vice versa.
32
    """
33
    
34
    __slots__ = []
35
    
36
    def _pack_inventory(self, inv):
37
        """Convert to XML Element"""
1852.6.3 by Robert Collins
Make iter(Tree) consistent for all tree types.
38
        entries = inv.iter_entries()
1393.1.59 by Martin Pool
- put 'format=5' on inventory and revision xml
39
        e = Element('inventory',
40
                    format='5')
1189 by Martin Pool
- BROKEN: partial support for commit into weave
41
        e.text = '\n'
1852.6.3 by Robert Collins
Make iter(Tree) consistent for all tree types.
42
        path, root = entries.next()
43
        if root.file_id not in (None, ROOT_ID):
44
            e.set('file_id', root.file_id)
1638.1.2 by Robert Collins
Change the basis-inventory file to not have the revision-id in the file name.
45
        if inv.revision_id is not None:
46
            e.set('revision_id', inv.revision_id)
1852.6.3 by Robert Collins
Make iter(Tree) consistent for all tree types.
47
        for path, ie in entries:
1189 by Martin Pool
- BROKEN: partial support for commit into weave
48
            e.append(self._pack_entry(ie))
49
        return e
50
51
    def _pack_entry(self, ie):
52
        """Convert InventoryEntry to XML element"""
1704.2.24 by Martin Pool
todo
53
        # TODO: should just be a plain assertion
1399.1.6 by Robert Collins
move exporting functionality into inventory.py - uncovers bug in symlink support
54
        if not InventoryEntry.versionable_kind(ie.kind):
55
            raise AssertionError('unsupported entry kind %s' % ie.kind)
1189 by Martin Pool
- BROKEN: partial support for commit into weave
56
        e = Element(ie.kind)
57
        e.set('name', ie.name)
58
        e.set('file_id', ie.file_id)
59
60
        if ie.text_size != None:
61
            e.set('text_size', '%d' % ie.text_size)
62
1092.2.22 by Robert Collins
text_version and name_version unification looking reasonable
63
        for f in ['text_sha1', 'revision', 'symlink_target']:
1189 by Martin Pool
- BROKEN: partial support for commit into weave
64
            v = getattr(ie, f)
65
            if v != None:
66
                e.set(f, v)
67
1398 by Robert Collins
integrate in Gustavos x-bit patch
68
        if ie.executable:
69
            e.set('executable', 'yes')
70
1189 by Martin Pool
- BROKEN: partial support for commit into weave
71
        # to be conservative, we don't externalize the root pointers
72
        # for now, leaving them as null in the xml form.  in a future
73
        # version it will be implied by nested elements.
74
        if ie.parent_id != ROOT_ID:
75
            assert isinstance(ie.parent_id, basestring)
76
            e.set('parent_id', ie.parent_id)
77
        e.tail = '\n'
78
        return e
79
80
    def _pack_revision(self, rev):
81
        """Revision object -> xml tree"""
82
        root = Element('revision',
83
                       committer = rev.committer,
84
                       timestamp = '%.9f' % rev.timestamp,
85
                       revision_id = rev.revision_id,
86
                       inventory_sha1 = rev.inventory_sha1,
1393.1.59 by Martin Pool
- put 'format=5' on inventory and revision xml
87
                       format='5',
1189 by Martin Pool
- BROKEN: partial support for commit into weave
88
                       )
1913.1.1 by John Arbash Meinel
Fix bug #55783
89
        if rev.timezone is not None:
1189 by Martin Pool
- BROKEN: partial support for commit into weave
90
            root.set('timezone', str(rev.timezone))
91
        root.text = '\n'
92
        msg = SubElement(root, 'message')
93
        msg.text = rev.message
94
        msg.tail = '\n'
1313 by Martin Pool
- rename to Revision.parent_ids to avoid confusion with old usage
95
        if rev.parent_ids:
1189 by Martin Pool
- BROKEN: partial support for commit into weave
96
            pelts = SubElement(root, 'parents')
97
            pelts.tail = pelts.text = '\n'
1313 by Martin Pool
- rename to Revision.parent_ids to avoid confusion with old usage
98
            for parent_id in rev.parent_ids:
1311 by Martin Pool
- remove RevisionReference; just hold parent ids directly
99
                assert isinstance(parent_id, basestring)
1189 by Martin Pool
- BROKEN: partial support for commit into weave
100
                p = SubElement(pelts, 'revision_ref')
101
                p.tail = '\n'
1311 by Martin Pool
- remove RevisionReference; just hold parent ids directly
102
                p.set('revision_id', parent_id)
1185.16.36 by Martin Pool
- store revision properties in revision xml
103
        if rev.properties:
104
            self._pack_revision_properties(rev, root)
1189 by Martin Pool
- BROKEN: partial support for commit into weave
105
        return root
1185.16.36 by Martin Pool
- store revision properties in revision xml
106
107
108
    def _pack_revision_properties(self, rev, under_element):
109
        top_elt = SubElement(under_element, 'properties')
110
        for prop_name, prop_value in sorted(rev.properties.items()):
111
            assert isinstance(prop_name, basestring) 
112
            assert isinstance(prop_value, basestring) 
113
            prop_elt = SubElement(top_elt, 'property')
114
            prop_elt.set('name', prop_name)
115
            prop_elt.text = prop_value
116
            prop_elt.tail = '\n'
117
        top_elt.tail = '\n'
118
1189 by Martin Pool
- BROKEN: partial support for commit into weave
119
120
    def _unpack_inventory(self, elt):
121
        """Construct from XML Element
122
        """
123
        assert elt.tag == 'inventory'
124
        root_id = elt.get('file_id') or ROOT_ID
1393.1.59 by Martin Pool
- put 'format=5' on inventory and revision xml
125
        format = elt.get('format')
126
        if format is not None:
127
            if format != '5':
128
                raise BzrError("invalid format version %r on inventory"
129
                                % format)
1638.1.2 by Robert Collins
Change the basis-inventory file to not have the revision-id in the file name.
130
        revision_id = elt.get('revision_id')
1911.2.6 by John Arbash Meinel
Cache revision ids and file ids as part of xml processing. A custom xml parser could just call decode/encode directly.
131
        if revision_id is not None:
132
            revision_id = cache_utf8.get_cached_unicode(revision_id)
1638.1.2 by Robert Collins
Change the basis-inventory file to not have the revision-id in the file name.
133
        inv = Inventory(root_id, revision_id=revision_id)
1189 by Martin Pool
- BROKEN: partial support for commit into weave
134
        for e in elt:
135
            ie = self._unpack_entry(e)
136
            if ie.parent_id == ROOT_ID:
137
                ie.parent_id = root_id
138
            inv.add(ie)
139
        return inv
140
141
142
    def _unpack_entry(self, elt):
143
        kind = elt.tag
1399.1.6 by Robert Collins
move exporting functionality into inventory.py - uncovers bug in symlink support
144
        if not InventoryEntry.versionable_kind(kind):
1092.2.20 by Robert Collins
symlink and weaves, whaddya know
145
            raise AssertionError('unsupported entry kind %s' % kind)
1189 by Martin Pool
- BROKEN: partial support for commit into weave
146
1911.2.6 by John Arbash Meinel
Cache revision ids and file ids as part of xml processing. A custom xml parser could just call decode/encode directly.
147
        get_cached = cache_utf8.get_cached_unicode
148
1189 by Martin Pool
- BROKEN: partial support for commit into weave
149
        parent_id = elt.get('parent_id')
150
        if parent_id == None:
151
            parent_id = ROOT_ID
1911.2.6 by John Arbash Meinel
Cache revision ids and file ids as part of xml processing. A custom xml parser could just call decode/encode directly.
152
        parent_id = get_cached(parent_id)
153
        file_id = get_cached(elt.get('file_id'))
1189 by Martin Pool
- BROKEN: partial support for commit into weave
154
1399.1.8 by Robert Collins
factor out inventory directory logic into 'InventoryDirectory' class
155
        if kind == 'directory':
1911.2.6 by John Arbash Meinel
Cache revision ids and file ids as part of xml processing. A custom xml parser could just call decode/encode directly.
156
            ie = inventory.InventoryDirectory(file_id,
1399.1.8 by Robert Collins
factor out inventory directory logic into 'InventoryDirectory' class
157
                                              elt.get('name'),
158
                                              parent_id)
1399.1.9 by Robert Collins
factor out file related logic from InventoryEntry to InventoryFile
159
        elif kind == 'file':
1911.2.6 by John Arbash Meinel
Cache revision ids and file ids as part of xml processing. A custom xml parser could just call decode/encode directly.
160
            ie = inventory.InventoryFile(file_id,
1399.1.9 by Robert Collins
factor out file related logic from InventoryEntry to InventoryFile
161
                                         elt.get('name'),
162
                                         parent_id)
163
            ie.text_sha1 = elt.get('text_sha1')
164
            if elt.get('executable') == 'yes':
165
                ie.executable = True
166
            v = elt.get('text_size')
167
            ie.text_size = v and int(v)
1399.1.10 by Robert Collins
remove kind from the InventoryEntry constructor - only child classes should be created now
168
        elif kind == 'symlink':
1911.2.6 by John Arbash Meinel
Cache revision ids and file ids as part of xml processing. A custom xml parser could just call decode/encode directly.
169
            ie = inventory.InventoryLink(file_id,
1399.1.10 by Robert Collins
remove kind from the InventoryEntry constructor - only child classes should be created now
170
                                         elt.get('name'),
171
                                         parent_id)
172
            ie.symlink_target = elt.get('symlink_target')
1399.1.8 by Robert Collins
factor out inventory directory logic into 'InventoryDirectory' class
173
        else:
1399.1.10 by Robert Collins
remove kind from the InventoryEntry constructor - only child classes should be created now
174
            raise BzrError("unknown kind %r" % kind)
1911.2.6 by John Arbash Meinel
Cache revision ids and file ids as part of xml processing. A custom xml parser could just call decode/encode directly.
175
        revision = elt.get('revision')
176
        if revision is not None:
177
            revision = get_cached(revision)
178
        ie.revision = revision
1189 by Martin Pool
- BROKEN: partial support for commit into weave
179
180
        return ie
181
182
183
    def _unpack_revision(self, elt):
184
        """XML Element -> Revision object"""
185
        assert elt.tag == 'revision'
1393.1.59 by Martin Pool
- put 'format=5' on inventory and revision xml
186
        format = elt.get('format')
187
        if format is not None:
188
            if format != '5':
189
                raise BzrError("invalid format version %r on inventory"
190
                                % format)
1911.2.6 by John Arbash Meinel
Cache revision ids and file ids as part of xml processing. A custom xml parser could just call decode/encode directly.
191
        get_cached = cache_utf8.get_cached_unicode
1189 by Martin Pool
- BROKEN: partial support for commit into weave
192
        rev = Revision(committer = elt.get('committer'),
193
                       timestamp = float(elt.get('timestamp')),
1911.2.6 by John Arbash Meinel
Cache revision ids and file ids as part of xml processing. A custom xml parser could just call decode/encode directly.
194
                       revision_id = get_cached(elt.get('revision_id')),
1189 by Martin Pool
- BROKEN: partial support for commit into weave
195
                       inventory_sha1 = elt.get('inventory_sha1')
196
                       )
197
        parents = elt.find('parents') or []
198
        for p in parents:
199
            assert p.tag == 'revision_ref', \
200
                   "bad parent node tag %r" % p.tag
1911.2.6 by John Arbash Meinel
Cache revision ids and file ids as part of xml processing. A custom xml parser could just call decode/encode directly.
201
            rev.parent_ids.append(get_cached(p.get('revision_id')))
1185.16.37 by Martin Pool
- properties are retrieved when revisions are loaded
202
        self._unpack_revision_properties(elt, rev)
1189 by Martin Pool
- BROKEN: partial support for commit into weave
203
        v = elt.get('timezone')
1913.1.1 by John Arbash Meinel
Fix bug #55783
204
        if v is None:
205
            rev.timezone = 0
206
        else:
207
            rev.timezone = int(v)
1189 by Martin Pool
- BROKEN: partial support for commit into weave
208
        rev.message = elt.findtext('message') # text of <message>
209
        return rev
210
211
1185.16.37 by Martin Pool
- properties are retrieved when revisions are loaded
212
    def _unpack_revision_properties(self, elt, rev):
213
        """Unpack properties onto a revision."""
214
        props_elt = elt.find('properties')
215
        assert len(rev.properties) == 0
216
        if not props_elt:
217
            return
218
        for prop_elt in props_elt:
219
            assert prop_elt.tag == 'property', \
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
220
                "bad tag under properties list: %r" % prop_elt.tag
1185.16.37 by Martin Pool
- properties are retrieved when revisions are loaded
221
            name = prop_elt.get('name')
222
            value = prop_elt.text
1886.1.1 by John Arbash Meinel
Fix bug #47782,
223
            # If a property had an empty value ('') cElementTree reads
224
            # that back as None, convert it back to '', so that all
225
            # properties have string values
226
            if value is None:
227
                value = ''
1185.16.37 by Martin Pool
- properties are retrieved when revisions are loaded
228
            assert name not in rev.properties, \
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
229
                "repeated property %r" % name
1185.16.37 by Martin Pool
- properties are retrieved when revisions are loaded
230
            rev.properties[name] = value
231
232
1189 by Martin Pool
- BROKEN: partial support for commit into weave
233
serializer_v5 = Serializer_v5()