1
# Copyright (C) 2005-2010 Canonical Ltd
3
# Copyright (C) 2005 Canonical Ltd
3
5
# This program is free software; you can redistribute it and/or modify
4
6
# it under the terms of the GNU General Public License as published by
5
7
# the Free Software Foundation; either version 2 of the License, or
6
8
# (at your option) any later version.
8
10
# This program is distributed in the hope that it will be useful,
9
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
13
# GNU General Public License for more details.
13
15
# You should have received a copy of the GNU General Public License
14
16
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
19
# Author: Martin Pool <mbp@canonical.com>
40
37
# TODO: When extracting a single version it'd be enough to just pass
41
# an iterator returning the weave lines... We don't really need to
42
# deserialize it into memory.
44
FORMAT_1 = '# bzr weave file v5\n'
38
# an iterator returning the weave lines...
40
FORMAT_1 = '# bzr weave file v1\n'
47
44
def write_weave(weave, f, format=None):
48
if format is None or format == 1:
49
return write_weave_v5(weave, f)
45
if format == None or format == 1:
46
return write_weave_v1(weave, f)
51
48
raise ValueError("unknown weave format %r" % format)
54
def write_weave_v5(weave, f):
51
def write_weave_v1(weave, f):
55
52
"""Write weave to file f."""
58
for version, included in enumerate(weave._parents):
60
# mininc = weave.minimal_parents(version)
63
f.write(' '.join(str(i) for i in mininc))
55
for version, verinfo in enumerate(weave._v):
56
print >>f, 'v', version
58
included = list(verinfo.included)
60
assert included[0] >= 0
61
assert included[-1] < version
67
f.write('1 ' + weave._sha1s[version] + '\n')
68
f.write('n ' + weave._names[version] + '\n')
73
for l in weave._weave:
74
73
if isinstance(l, tuple):
78
f.write('%s %d\n' % l)
75
print >>f, '%s %d' % l
82
79
elif l[-1] == '\n':
80
assert l.find('\n', 0, -1) == -1
85
f.write(', ' + l + '\n')
83
assert l.find('\n') == -1
92
# FIXME: detect the weave type and dispatch
93
from bzrlib.trace import mutter
94
from weave import Weave
95
w = Weave(getattr(f, 'name', None))
100
def _read_weave_v5(f, w):
101
"""Private helper routine to read a weave format 5 file into memory.
103
This is only to be used by read_weave and WeaveFile.__init__.
105
# 200 0 2075.5080 1084.0360 bzrlib.weavefile:104(_read_weave_v5)
106
# +60412 0 366.5900 366.5900 +<method 'readline' of 'file' objects>
107
# +59982 0 320.5280 320.5280 +<method 'startswith' of 'str' objects>
108
# +59363 0 297.8080 297.8080 +<method 'append' of 'list' objects>
109
# replace readline call with iter over all lines ->
110
# safe because we already suck on memory.
111
# 200 0 1492.7170 802.6220 bzrlib.weavefile:104(_read_weave_v5)
112
# +59982 0 329.9100 329.9100 +<method 'startswith' of 'str' objects>
113
# +59363 0 320.2980 320.2980 +<method 'append' of 'list' objects>
114
# replaced startswith with slice lookups:
115
# 200 0 851.7250 501.1120 bzrlib.weavefile:104(_read_weave_v5)
116
# +59363 0 311.8780 311.8780 +<method 'append' of 'list' objects>
117
# +200 0 30.2500 30.2500 +<method 'readlines' of 'file' objects>
119
from weave import WeaveFormatError
122
lines = iter(f.readlines())
128
except StopIteration:
129
raise WeaveFormatError('invalid weave file: no header')
91
return read_weave_v1(f)
95
from weave import Weave, VerInfo, WeaveFormatError
98
wfe = WeaveFormatError
131
100
if l != FORMAT_1:
132
101
raise WeaveFormatError('invalid weave file header: %r' % l)
106
if l.startswith('v '):
109
raise WeaveFormatError('version %d!=%d out of order'
113
l = f.readline()[:-1]
115
raise WeaveFormatError('unexpected line %r' % l)
140
w._parents.append(map(int, l[2:].split(' ')))
117
included = map(int, l[2:].split(' '))
118
w._v.append(VerInfo(included))
142
w._parents.append([])
143
l = lines.next()[:-1]
144
w._sha1s.append(l[2:])
147
w._names.append(name)
148
w._name_map[name] = ver
120
w._v.append(VerInfo())
121
assert f.readline() == '\n'
154
125
raise WeaveFormatError('unexpected line %r' % l)
162
w._weave.append(l[2:]) # include newline
164
w._weave.append(l[2:-1]) # exclude newline
166
w._weave.append(('}', None))
131
elif l.startswith('. '):
132
w._l.append(l[2:]) # include newline
133
elif l.startswith(', '):
134
w._l.append(l[2:-1]) # exclude newline
168
w._weave.append((intern(l[0]), int(l[2:])))
136
assert l[0] in '{}[]', l
137
assert l[1] == ' ', l
138
w._l.append((l[0], int(l[2:])))