1297
by Martin Pool
- split old xml v4 serialization into separate file |
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 |
1297
by Martin Pool
- split old xml v4 serialization into separate file |
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 |
1297
by Martin Pool
- split old xml v4 serialization into separate file |
20 |
from bzrlib.errors import BzrError |
21 |
||
22 |
||
23 |
||
24 |
||
25 |
||
26 |
||
27 |
class _Serializer_v4(Serializer): |
|
28 |
"""Version 0.0.4 serializer
|
|
29 |
||
30 |
You should use the serialzer_v4 singleton."""
|
|
31 |
||
32 |
__slots__ = [] |
|
33 |
||
34 |
def _pack_inventory(self, inv): |
|
35 |
"""Convert to XML Element"""
|
|
36 |
e = Element('inventory') |
|
37 |
e.text = '\n' |
|
38 |
if inv.root.file_id not in (None, ROOT_ID): |
|
39 |
e.set('file_id', inv.root.file_id) |
|
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"""
|
|
47 |
e = Element('entry') |
|
48 |
e.set('name', ie.name) |
|
49 |
e.set('file_id', ie.file_id) |
|
50 |
e.set('kind', ie.kind) |
|
51 |
||
52 |
if ie.text_size != None: |
|
53 |
e.set('text_size', '%d' % ie.text_size) |
|
54 |
||
1092.2.20
by Robert Collins
symlink and weaves, whaddya know |
55 |
for f in ['text_id', 'text_sha1', 'symlink_target']: |
1297
by Martin Pool
- split old xml v4 serialization into separate file |
56 |
v = getattr(ie, f) |
57 |
if v != None: |
|
58 |
e.set(f, v) |
|
59 |
||
60 |
# to be conservative, we don't externalize the root pointers
|
|
61 |
# for now, leaving them as null in the xml form. in a future
|
|
62 |
# version it will be implied by nested elements.
|
|
63 |
if ie.parent_id != ROOT_ID: |
|
64 |
assert isinstance(ie.parent_id, basestring) |
|
65 |
e.set('parent_id', ie.parent_id) |
|
66 |
||
67 |
e.tail = '\n' |
|
68 |
||
69 |
return e |
|
70 |
||
71 |
||
72 |
def _unpack_inventory(self, elt): |
|
73 |
"""Construct from XML Element
|
|
74 |
"""
|
|
75 |
assert elt.tag == 'inventory' |
|
76 |
root_id = elt.get('file_id') or ROOT_ID |
|
77 |
inv = Inventory(root_id) |
|
78 |
for e in elt: |
|
79 |
ie = self._unpack_entry(e) |
|
80 |
if ie.parent_id == ROOT_ID: |
|
81 |
ie.parent_id = root_id |
|
82 |
inv.add(ie) |
|
83 |
return inv |
|
84 |
||
85 |
||
86 |
def _unpack_entry(self, elt): |
|
87 |
assert elt.tag == 'entry' |
|
88 |
||
89 |
## original format inventories don't have a parent_id for
|
|
90 |
## nodes in the root directory, but it's cleaner to use one
|
|
91 |
## internally.
|
|
92 |
parent_id = elt.get('parent_id') |
|
93 |
if parent_id == None: |
|
94 |
parent_id = ROOT_ID |
|
95 |
||
1399.1.8
by Robert Collins
factor out inventory directory logic into 'InventoryDirectory' class |
96 |
kind = elt.get('kind') |
97 |
if kind == 'directory': |
|
98 |
ie = inventory.InventoryDirectory(elt.get('file_id'), |
|
99 |
elt.get('name'), |
|
100 |
parent_id) |
|
1399.1.9
by Robert Collins
factor out file related logic from InventoryEntry to InventoryFile |
101 |
elif kind == 'file': |
102 |
ie = inventory.InventoryFile(elt.get('file_id'), |
|
103 |
elt.get('name'), |
|
104 |
parent_id) |
|
105 |
ie.text_id = elt.get('text_id') |
|
106 |
ie.text_sha1 = elt.get('text_sha1') |
|
107 |
v = elt.get('text_size') |
|
108 |
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 |
109 |
elif kind == 'symlink': |
110 |
ie = inventory.InventoryLink(elt.get('file_id'), |
|
111 |
elt.get('name'), |
|
112 |
parent_id) |
|
113 |
ie.symlink_target = elt.get('symlink_target') |
|
1399.1.8
by Robert Collins
factor out inventory directory logic into 'InventoryDirectory' class |
114 |
else: |
1399.1.10
by Robert Collins
remove kind from the InventoryEntry constructor - only child classes should be created now |
115 |
raise BzrError("unknown kind %r" % kind) |
1297
by Martin Pool
- split old xml v4 serialization into separate file |
116 |
|
1185.31.4
by John Arbash Meinel
Fixing mutter() calls to not have to do string processing. |
117 |
## mutter("read inventoryentry: %r", elt.attrib)
|
1297
by Martin Pool
- split old xml v4 serialization into separate file |
118 |
|
119 |
return ie |
|
120 |
||
121 |
||
122 |
def _pack_revision(self, rev): |
|
123 |
"""Revision object -> xml tree"""
|
|
124 |
root = Element('revision', |
|
125 |
committer = rev.committer, |
|
126 |
timestamp = '%.9f' % rev.timestamp, |
|
127 |
revision_id = rev.revision_id, |
|
128 |
inventory_id = rev.inventory_id, |
|
129 |
inventory_sha1 = rev.inventory_sha1, |
|
130 |
)
|
|
131 |
if rev.timezone: |
|
132 |
root.set('timezone', str(rev.timezone)) |
|
133 |
root.text = '\n' |
|
134 |
||
135 |
msg = SubElement(root, 'message') |
|
136 |
msg.text = rev.message |
|
137 |
msg.tail = '\n' |
|
138 |
||
139 |
if rev.parents: |
|
140 |
pelts = SubElement(root, 'parents') |
|
141 |
pelts.tail = pelts.text = '\n' |
|
1311
by Martin Pool
- remove RevisionReference; just hold parent ids directly |
142 |
for i, parent_id in enumerate(rev.parents): |
1297
by Martin Pool
- split old xml v4 serialization into separate file |
143 |
p = SubElement(pelts, 'revision_ref') |
144 |
p.tail = '\n' |
|
1311
by Martin Pool
- remove RevisionReference; just hold parent ids directly |
145 |
assert parent_id |
146 |
p.set('revision_id', parent_id) |
|
147 |
if i < len(rev.parent_sha1s): |
|
148 |
p.set('revision_sha1', rev.parent_sha1s[i]) |
|
1297
by Martin Pool
- split old xml v4 serialization into separate file |
149 |
return root |
150 |
||
151 |
||
152 |
def _unpack_revision(self, elt): |
|
153 |
"""XML Element -> Revision object"""
|
|
154 |
||
155 |
# <changeset> is deprecated...
|
|
156 |
if elt.tag not in ('revision', 'changeset'): |
|
157 |
raise BzrError("unexpected tag in revision file: %r" % elt) |
|
158 |
||
159 |
rev = Revision(committer = elt.get('committer'), |
|
160 |
timestamp = float(elt.get('timestamp')), |
|
161 |
revision_id = elt.get('revision_id'), |
|
162 |
inventory_id = elt.get('inventory_id'), |
|
163 |
inventory_sha1 = elt.get('inventory_sha1') |
|
164 |
)
|
|
165 |
||
166 |
precursor = elt.get('precursor') |
|
167 |
precursor_sha1 = elt.get('precursor_sha1') |
|
168 |
||
169 |
pelts = elt.find('parents') |
|
170 |
||
171 |
if pelts: |
|
172 |
for p in pelts: |
|
173 |
assert p.tag == 'revision_ref', \ |
|
174 |
"bad parent node tag %r" % p.tag |
|
1313
by Martin Pool
- rename to Revision.parent_ids to avoid confusion with old usage |
175 |
rev.parent_ids.append(p.get('revision_id')) |
1311
by Martin Pool
- remove RevisionReference; just hold parent ids directly |
176 |
rev.parent_sha1s.append(p.get('revision_sha1')) |
1297
by Martin Pool
- split old xml v4 serialization into separate file |
177 |
if precursor: |
178 |
# must be consistent
|
|
1314
by Martin Pool
- Fix up one more RevisionReference bit |
179 |
prec_parent = rev.parent_ids[0] |
1297
by Martin Pool
- split old xml v4 serialization into separate file |
180 |
assert prec_parent == precursor |
181 |
elif precursor: |
|
182 |
# revisions written prior to 0.0.5 have a single precursor
|
|
183 |
# give as an attribute
|
|
1313
by Martin Pool
- rename to Revision.parent_ids to avoid confusion with old usage |
184 |
rev.parent_ids.append(precursor) |
1311
by Martin Pool
- remove RevisionReference; just hold parent ids directly |
185 |
rev.parent_sha1s.append(precursor_sha1) |
1297
by Martin Pool
- split old xml v4 serialization into separate file |
186 |
|
187 |
v = elt.get('timezone') |
|
188 |
rev.timezone = v and int(v) |
|
189 |
||
190 |
rev.message = elt.findtext('message') # text of <message> |
|
191 |
return rev |
|
192 |
||
193 |
||
194 |
||
195 |
||
196 |
"""singleton instance"""
|
|
197 |
serializer_v4 = _Serializer_v4() |
|
198 |