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 'v' and the version, then 'i' and the included
30
previous versions. The inclusions do not need to list versions
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 v1\n'
45
def write_weave(weave, f, format=None):
46
if format == None or format == 1:
47
return write_weave_v1(weave, f)
49
raise ValueError("unknown weave format %r" % format)
52
def write_weave_v1(weave, f):
53
"""Write weave to file f."""
56
for version, included in enumerate(weave._v):
57
print >>f, 'v', version
59
# find a minimal expression of it; bias towards using
71
gotit.update(weave._v[pv])
74
assert mininc[-1] < version
86
if isinstance(l, tuple):
88
print >>f, '%s %d' % l
93
assert l.find('\n', 0, -1) == -1
96
assert l.find('\n') == -1
104
return read_weave_v1(f)
107
def read_weave_v1(f):
108
from weave import Weave, WeaveFormatError
111
wfe = WeaveFormatError
114
raise WeaveFormatError('invalid weave file header: %r' % l)
119
if l.startswith('v '):
122
raise WeaveFormatError('version %d!=%d out of order'
126
l = f.readline()[:-1]
128
raise WeaveFormatError('unexpected line %r' % l)
130
included = map(int, l[2:].split(' '))
134
full.update(w._v[pv])
138
assert f.readline() == '\n'
142
raise WeaveFormatError('unexpected line %r' % l)
148
elif l.startswith('. '):
149
w._l.append(l[2:]) # include newline
150
elif l.startswith(', '):
151
w._l.append(l[2:-1]) # exclude newline
153
assert l[0] in '{}[]', l
154
assert l[1] == ' ', l
155
w._l.append((l[0], int(l[2:])))