3
# Copyright (C) 2005 Canonical Ltd
5
# This program is free software; you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 2 of the License, or
8
# (at your option) any later version.
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
15
# You should have received a copy of the GNU General Public License
16
# along with this program; if not, write to the Free Software
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
# Author: Martin Pool <mbp@canonical.com>
24
"""Store and retrieve weaves in files.
26
There is one format marker followed by a blank line, followed by a
27
series of version headers, followed by the weave itself.
29
Each version marker has 'i' and the included previous versions, then
30
'1' and the SHA-1 of the text, if known. The inclusions do not need
31
to list versions included by a parent.
33
The weave is bracketed by 'w' and 'W' lines, and includes the '{}[]'
34
processing instructions. Lines of text are prefixed by '.' if the
35
line contains a newline, or ',' if not.
38
# TODO: When extracting a single version it'd be enough to just pass
39
# an iterator returning the weave lines...
41
FORMAT_1 = '# bzr weave file v3\n'
44
def write_weave(weave, f, format=None):
45
if format == None or format == 1:
46
return write_weave_v1(weave, f)
48
raise ValueError("unknown weave format %r" % format)
51
def write_weave_v1(weave, f):
52
"""Write weave to file f."""
55
for version, included in enumerate(weave._v):
57
# find a minimal expression of it; bias towards using
69
gotit.update(weave._v[pv])
72
assert mininc[-1] < version
79
print >>f, '1', weave._sha1s[version]
85
if isinstance(l, tuple):
87
print >>f, '%s %d' % l
92
assert l.find('\n', 0, -1) == -1
95
assert l.find('\n') == -1
103
return read_weave_v1(f)
106
def read_weave_v1(f):
107
from weave import Weave, WeaveFormatError
110
wfe = WeaveFormatError
113
raise WeaveFormatError('invalid weave file header: %r' % l)
122
included = map(int, l[2:].split(' '))
126
full.update(w._v[pv])
131
l = f.readline()[:-1]
132
assert l.startswith('1 ')
133
w._sha1s.append(l[2:])
140
raise WeaveFormatError('unexpected line %r' % l)
146
elif l.startswith('. '):
147
w._l.append(intern(l[2:])) # include newline
148
elif l.startswith(', '):
149
w._l.append(l[2:-1]) # exclude newline
151
assert l[0] in '{}[]', l
152
assert l[1] == ' ', l
153
w._l.append((intern(l[0]), int(l[2:])))