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>
19
24
"""Store and retrieve weaves in files.
21
26
There is one format marker followed by a blank line, followed by a
53
56
def write_weave_v5(weave, f):
54
57
"""Write weave to file f."""
57
60
for version, included in enumerate(weave._parents):
59
62
# mininc = weave.minimal_parents(version)
62
f.write(' '.join(str(i) for i in mininc))
66
f.write('1 ' + weave._sha1s[version] + '\n')
67
f.write('n ' + weave._names[version] + '\n')
70
print >>f, '1', weave._sha1s[version]
71
print >>f, 'n', weave._names[version]
72
76
for l in weave._weave:
73
77
if isinstance(l, tuple):
77
f.write('%s %d\n' % l)
82
print >>f, '%s %d' % l
81
86
elif l[-1] == '\n':
87
assert l.find('\n', 0, -1) == -1
84
f.write(', ' + l + '\n')
90
assert l.find('\n') == -1
91
# FIXME: detect the weave type and dispatch
92
from bzrlib.weave import Weave
98
return read_weave_v5(f)
101
def read_weave_v5(f):
102
from weave import Weave, WeaveFormatError
93
103
w = Weave(getattr(f, 'name', None))
98
def _read_weave_v5(f, w):
99
"""Private helper routine to read a weave format 5 file into memory.
101
This is only to be used by read_weave and WeaveFile.__init__.
103
# 200 0 2075.5080 1084.0360 bzrlib.weavefile:104(_read_weave_v5)
104
# +60412 0 366.5900 366.5900 +<method 'readline' of 'file' objects>
105
# +59982 0 320.5280 320.5280 +<method 'startswith' of 'str' objects>
106
# +59363 0 297.8080 297.8080 +<method 'append' of 'list' objects>
107
# replace readline call with iter over all lines ->
108
# safe because we already suck on memory.
109
# 200 0 1492.7170 802.6220 bzrlib.weavefile:104(_read_weave_v5)
110
# +59982 0 329.9100 329.9100 +<method 'startswith' of 'str' objects>
111
# +59363 0 320.2980 320.2980 +<method 'append' of 'list' objects>
112
# replaced startswith with slice lookups:
113
# 200 0 851.7250 501.1120 bzrlib.weavefile:104(_read_weave_v5)
114
# +59363 0 311.8780 311.8780 +<method 'append' of 'list' objects>
115
# +200 0 30.2500 30.2500 +<method 'readlines' of 'file' objects>
117
from bzrlib.weave import WeaveFormatError
120
lines = iter(f.readlines())
126
except StopIteration:
127
raise WeaveFormatError('invalid weave file: no header')
129
106
if l != FORMAT_1:
130
107
raise WeaveFormatError('invalid weave file header: %r' % l)
138
114
w._parents.append(map(int, l[2:].split(' ')))
140
116
w._parents.append([])
141
l = lines.next()[:-1]
118
l = f.readline()[:-1]
119
assert l.startswith('1 ')
142
120
w._sha1s.append(l[2:])
123
assert l.startswith('n ')
125
assert name not in w._name_map
145
126
w._names.append(name)
146
127
w._name_map[name] = ver
152
136
raise WeaveFormatError('unexpected line %r' % l)
142
elif l.startswith('. '):
160
143
w._weave.append(l[2:]) # include newline
144
elif l.startswith(', '):
162
145
w._weave.append(l[2:-1]) # exclude newline
164
147
w._weave.append(('}', None))
149
assert l[0] in '{[]', l
150
assert l[1] == ' ', l
166
151
w._weave.append((intern(l[0]), int(l[2:])))