~bzr-pqm/bzr/bzr.dev

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