1
# Copyright (C) 2005-2010 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# 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
# Author: Martin Pool <mbp@canonical.com>
19
from __future__ import absolute_import
23
"""Store and retrieve weaves in files.
25
There is one format marker followed by a blank line, followed by a
26
series of version headers, followed by the weave itself.
28
Each version marker has
30
'i' parent version indexes
34
The inclusions do not need to list versions included by a parent.
36
The weave is bracketed by 'w' and 'W' lines, and includes the '{}[]'
37
processing instructions. Lines of text are prefixed by '.' if the
38
line contains a newline, or ',' if not.
41
# TODO: When extracting a single version it'd be enough to just pass
42
# an iterator returning the weave lines... We don't really need to
43
# deserialize it into memory.
45
FORMAT_1 = '# bzr weave file v5\n'
48
def write_weave(weave, f, format=None):
49
if format is None or format == 1:
50
return write_weave_v5(weave, f)
52
raise ValueError("unknown weave format %r" % format)
55
def write_weave_v5(weave, f):
56
"""Write weave to file f."""
59
for version, included in enumerate(weave._parents):
61
# mininc = weave.minimal_parents(version)
64
f.write(' '.join(str(i) for i in mininc))
68
f.write('1 ' + weave._sha1s[version] + '\n')
69
f.write('n ' + weave._names[version] + '\n')
74
for l in weave._weave:
75
if isinstance(l, tuple):
79
f.write('%s %d\n' % l)
86
f.write(', ' + l + '\n')
93
# FIXME: detect the weave type and dispatch
94
from bzrlib.trace import mutter
95
from weave import Weave
96
w = Weave(getattr(f, 'name', None))
101
def _read_weave_v5(f, w):
102
"""Private helper routine to read a weave format 5 file into memory.
104
This is only to be used by read_weave and WeaveFile.__init__.
106
# 200 0 2075.5080 1084.0360 bzrlib.weavefile:104(_read_weave_v5)
107
# +60412 0 366.5900 366.5900 +<method 'readline' of 'file' objects>
108
# +59982 0 320.5280 320.5280 +<method 'startswith' of 'str' objects>
109
# +59363 0 297.8080 297.8080 +<method 'append' of 'list' objects>
110
# replace readline call with iter over all lines ->
111
# safe because we already suck on memory.
112
# 200 0 1492.7170 802.6220 bzrlib.weavefile:104(_read_weave_v5)
113
# +59982 0 329.9100 329.9100 +<method 'startswith' of 'str' objects>
114
# +59363 0 320.2980 320.2980 +<method 'append' of 'list' objects>
115
# replaced startswith with slice lookups:
116
# 200 0 851.7250 501.1120 bzrlib.weavefile:104(_read_weave_v5)
117
# +59363 0 311.8780 311.8780 +<method 'append' of 'list' objects>
118
# +200 0 30.2500 30.2500 +<method 'readlines' of 'file' objects>
120
from weave import WeaveFormatError
123
lines = iter(f.readlines())
129
except StopIteration:
130
raise WeaveFormatError('invalid weave file: no header')
133
raise WeaveFormatError('invalid weave file header: %r' % l)
141
w._parents.append(map(int, l[2:].split(' ')))
143
w._parents.append([])
144
l = lines.next()[:-1]
145
w._sha1s.append(l[2:])
148
w._names.append(name)
149
w._name_map[name] = ver
155
raise WeaveFormatError('unexpected line %r' % l)
163
w._weave.append(l[2:]) # include newline
165
w._weave.append(l[2:-1]) # exclude newline
167
w._weave.append(('}', None))
169
w._weave.append((intern(l[0]), int(l[2:])))