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
31
'i' parent version indexes
35
The inclusions do not need to list versions included by a parent.
37
The weave is bracketed by 'w' and 'W' lines, and includes the '{}[]'
38
processing instructions. Lines of text are prefixed by '.' if the
39
line contains a newline, or ',' if not.
42
# TODO: When extracting a single version it'd be enough to just pass
43
# an iterator returning the weave lines... We don't really need to
44
# deserialize it into memory.
46
FORMAT_1 = '# bzr weave file v5\n'
49
def write_weave(weave, f, format=None):
50
if format == None or format == 1:
51
return write_weave_v5(weave, f)
53
raise ValueError("unknown weave format %r" % format)
56
def write_weave_v5(weave, f):
57
"""Write weave to file f."""
60
for version, included in enumerate(weave._parents):
62
# mininc = weave.minimal_parents(version)
70
print >>f, '1', weave._sha1s[version]
71
print >>f, 'n', weave._names[version]
76
for l in weave._weave:
77
if isinstance(l, tuple):
82
print >>f, '%s %d' % l
87
assert l.find('\n', 0, -1) == -1
90
assert l.find('\n') == -1
98
return read_weave_v5(f)
101
def read_weave_v5(f):
102
from weave import Weave, WeaveFormatError
107
raise WeaveFormatError('invalid weave file header: %r' % l)
114
w._parents.append(map(int, l[2:].split(' ')))
116
w._parents.append([])
118
l = f.readline()[:-1]
119
assert l.startswith('1 ')
120
w._sha1s.append(l[2:])
123
assert l.startswith('n ')
125
assert name not in w._name_map
126
w._names.append(name)
127
w._name_map[name] = ver
136
raise WeaveFormatError('unexpected line %r' % l)
142
elif l.startswith('. '):
143
w._weave.append(l[2:]) # include newline
144
elif l.startswith(', '):
145
w._weave.append(l[2:-1]) # exclude newline
147
w._weave.append(('}', None))
149
assert l[0] in '{[]', l
150
assert l[1] == ' ', l
151
w._weave.append((intern(l[0]), int(l[2:])))