/brz/remove-bazaar

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