1
# Copyright (C) 2005-2010 Canonical Ltd
1
# Copyright (C) 2005 Canonical Ltd
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
5
5
# the Free Software Foundation; either version 2 of the License, or
6
6
# (at your option) any later version.
8
8
# This program is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
11
# GNU General Public License for more details.
13
13
# You should have received a copy of the GNU General Public License
14
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
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
17
# Author: Martin Pool <mbp@canonical.com>
19
22
"""Store and retrieve weaves in files.
21
24
There is one format marker followed by a blank line, followed by a
34
37
line contains a newline, or ',' if not.
37
from __future__ import absolute_import
39
from ..sixish import bytesintern
41
40
# TODO: When extracting a single version it'd be enough to just pass
42
41
# an iterator returning the weave lines... We don't really need to
43
42
# deserialize it into memory.
45
FORMAT_1 = b'# bzr weave file v5\n'
44
FORMAT_1 = '# bzr weave file v5\n'
48
47
def write_weave(weave, f, format=None):
49
if format is None or format == 1:
48
if format == None or format == 1:
50
49
return write_weave_v5(weave, f)
52
51
raise ValueError("unknown weave format %r" % format)
55
54
def write_weave_v5(weave, f):
56
55
"""Write weave to file f."""
59
58
for version, included in enumerate(weave._parents):
61
60
# mininc = weave.minimal_parents(version)
64
f.write(b' '.join(b'%d' % i for i in mininc))
68
f.write(b'1 ' + weave._sha1s[version] + b'\n')
69
f.write(b'n ' + weave._names[version] + b'\n')
68
print >>f, '1', weave._sha1s[version]
69
print >>f, 'n', weave._names[version]
74
74
for l in weave._weave:
75
75
if isinstance(l, tuple):
79
f.write(l[0] + b' %d\n' % l[1])
80
print >>f, '%s %d' % l
83
elif l.endswith(b'\n'):
85
assert l.find('\n', 0, -1) == -1
86
f.write(b', ' + l + b'\n')
88
assert l.find('\n') == -1
92
# FIXME: detect the weave type and dispatch
93
from .weave import Weave
96
return read_weave_v5(f)
100
from weave import Weave, WeaveFormatError
94
101
w = Weave(getattr(f, 'name', None))
99
def _read_weave_v5(f, w):
100
"""Private helper routine to read a weave format 5 file into memory.
102
This is only to be used by read_weave and WeaveFile.__init__.
104
# 200 0 2075.5080 1084.0360 breezy.weavefile:104(_read_weave_v5)
105
# +60412 0 366.5900 366.5900 +<method 'readline' of 'file' objects>
106
# +59982 0 320.5280 320.5280 +<method 'startswith' of 'str' objects>
107
# +59363 0 297.8080 297.8080 +<method 'append' of 'list' objects>
108
# replace readline call with iter over all lines ->
109
# safe because we already suck on memory.
110
# 200 0 1492.7170 802.6220 breezy.weavefile:104(_read_weave_v5)
111
# +59982 0 329.9100 329.9100 +<method 'startswith' of 'str' objects>
112
# +59363 0 320.2980 320.2980 +<method 'append' of 'list' objects>
113
# replaced startswith with slice lookups:
114
# 200 0 851.7250 501.1120 breezy.weavefile:104(_read_weave_v5)
115
# +59363 0 311.8780 311.8780 +<method 'append' of 'list' objects>
116
# +200 0 30.2500 30.2500 +<method 'readlines' of 'file' objects>
118
from .weave import WeaveFormatError
121
lines = iter(f.readlines())
127
except StopIteration:
128
raise WeaveFormatError('invalid weave file: no header')
130
104
if l != FORMAT_1:
131
105
raise WeaveFormatError('invalid weave file header: %r' % l)
139
w._parents.append(list(map(int, l[2:].split(b' '))))
112
w._parents.append(map(int, l[2:].split(' ')))
141
114
w._parents.append([])
116
l = f.readline()[:-1]
117
assert l.startswith('1 ')
143
118
w._sha1s.append(l[2:])
121
assert l.startswith('n ')
123
assert name not in w._name_map
146
124
w._names.append(name)
147
125
w._name_map[name] = ver
153
134
raise WeaveFormatError('unexpected line %r' % l)
160
elif b'. ' == l[0:2]:
140
elif l.startswith('. '):
161
141
w._weave.append(l[2:]) # include newline
162
elif b', ' == l[0:2]:
142
elif l.startswith(', '):
163
143
w._weave.append(l[2:-1]) # exclude newline
165
w._weave.append((b'}', None))
145
w._weave.append(('}', None))
167
w._weave.append((bytesintern(l[0:1]), int(l[2:].decode('ascii'))))
147
assert l[0] in '{[]', l
148
assert l[1] == ' ', l
149
w._weave.append((intern(l[0]), int(l[2:])))