3
1
# Copyright (C) 2005 Canonical Ltd
5
3
# This program is free software; you can redistribute it and/or modify
6
4
# it under the terms of the GNU General Public License as published by
7
5
# the Free Software Foundation; either version 2 of the License, or
8
6
# (at your option) any later version.
10
8
# This program is distributed in the hope that it will be useful,
11
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
11
# GNU General Public License for more details.
15
13
# You should have received a copy of the GNU General Public License
16
14
# along with this program; if not, write to the Free Software
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
17
# Author: Martin Pool <mbp@canonical.com>
56
54
def write_weave_v5(weave, f):
57
55
"""Write weave to file f."""
60
58
for version, included in enumerate(weave._parents):
62
60
# mininc = weave.minimal_parents(version)
63
f.write(' '.join(str(i) for i in mininc))
70
print >>f, '1', weave._sha1s[version]
71
print >>f, 'n', weave._names[version]
67
f.write('1 ' + weave._sha1s[version] + '\n')
68
f.write('n ' + weave._names[version] + '\n')
76
73
for l in weave._weave:
77
74
if isinstance(l, tuple):
82
print >>f, '%s %d' % l
78
f.write('%s %d\n' % l)
86
82
elif l[-1] == '\n':
87
assert l.find('\n', 0, -1) == -1
90
assert l.find('\n') == -1
85
f.write(', ' + l + '\n')
98
return read_weave_v5(f)
101
def read_weave_v5(f):
102
from weave import Weave, WeaveFormatError
92
# FIXME: detect the weave type and dispatch
93
from bzrlib.trace import mutter
94
from weave import Weave
103
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
121
lines = iter(f.readlines())
125
except StopIteration:
126
raise WeaveFormatError('invalid weave file: no header')
106
128
if l != FORMAT_1:
107
129
raise WeaveFormatError('invalid weave file header: %r' % l)
114
137
w._parents.append(map(int, l[2:].split(' ')))
116
139
w._parents.append([])
118
l = f.readline()[:-1]
119
assert l.startswith('1 ')
140
l = lines.next()[:-1]
120
141
w._sha1s.append(l[2:])
123
assert l.startswith('n ')
125
assert name not in w._name_map
126
144
w._names.append(name)
127
145
w._name_map[name] = ver
136
151
raise WeaveFormatError('unexpected line %r' % l)
142
elif l.startswith('. '):
143
159
w._weave.append(l[2:]) # include newline
144
elif l.startswith(', '):
145
161
w._weave.append(l[2:-1]) # exclude newline
147
163
w._weave.append(('}', None))
149
assert l[0] in '{[]', l
150
assert l[1] == ' ', l
151
165
w._weave.append((intern(l[0]), int(l[2:])))