bzr branch
http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
|
0.1.69
by Martin Pool
Simple text-based format for storing weaves, cleaner than |
1 |
#! /usr/bin/python
|
2 |
||
3 |
# Copyright (C) 2005 Canonical Ltd
|
|
4 |
||
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.
|
|
9 |
||
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.
|
|
14 |
||
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
|
|
18 |
||
19 |
# Author: Martin Pool <mbp@canonical.com>
|
|
20 |
||
21 |
||
22 |
||
23 |
||
24 |
"""Store and retrieve weaves in files.
|
|
|
0.1.72
by Martin Pool
Go back to weave lines normally having newlines at the end. |
25 |
|
26 |
There is one format marker followed by a blank line, followed by a
|
|
27 |
series of version headers, followed by the weave itself.
|
|
28 |
||
29 |
Each version marker has 'v' and the version, then 'i' and the included
|
|
30 |
previous versions.
|
|
31 |
||
32 |
The weave is bracketed by 'w' and 'W' lines, and includes the '{}[]'
|
|
33 |
processing instructions. Lines of text are prefixed by '.' if the
|
|
34 |
line contains a newline, or ',' if not.
|
|
|
0.1.69
by Martin Pool
Simple text-based format for storing weaves, cleaner than |
35 |
"""
|
36 |
||
37 |
# TODO: When extracting a single version it'd be enough to just pass
|
|
38 |
# an iterator returning the weave lines...
|
|
39 |
||
|
0.1.73
by Martin Pool
Clean up assertions for weavefile |
40 |
FORMAT_1 = '# bzr weave file v1\n' |
|
0.1.69
by Martin Pool
Simple text-based format for storing weaves, cleaner than |
41 |
|
42 |
||
43 |
||
44 |
||
45 |
def write_weave_v1(weave, f): |
|
46 |
"""Write weave to file f.""" |
|
|
0.1.73
by Martin Pool
Clean up assertions for weavefile |
47 |
print >>f, FORMAT_1, |
|
0.1.69
by Martin Pool
Simple text-based format for storing weaves, cleaner than |
48 |
|
49 |
for version, verinfo in enumerate(weave._v): |
|
50 |
print >>f, 'v', version |
|
|
0.1.72
by Martin Pool
Go back to weave lines normally having newlines at the end. |
51 |
if verinfo.included: |
52 |
included = list(verinfo.included) |
|
53 |
included.sort() |
|
54 |
assert included[0] >= 0 |
|
55 |
assert included[-1] < version |
|
56 |
print >>f, 'i', |
|
57 |
for i in included: |
|
58 |
print >>f, i, |
|
59 |
print >>f |
|
60 |
else: |
|
61 |
print >>f, 'i' |
|
|
0.1.69
by Martin Pool
Simple text-based format for storing weaves, cleaner than |
62 |
print >>f |
63 |
||
64 |
print >>f, 'w' |
|
65 |
||
66 |
for l in weave._l: |
|
67 |
if isinstance(l, tuple): |
|
68 |
assert l[0] in '{}[]' |
|
69 |
print >>f, '%s %d' % l |
|
|
0.1.72
by Martin Pool
Go back to weave lines normally having newlines at the end. |
70 |
else: # text line |
71 |
if not l: |
|
72 |
print >>f, ', ' |
|
73 |
elif l[-1] == '\n': |
|
|
0.1.73
by Martin Pool
Clean up assertions for weavefile |
74 |
assert l.find('\n', 0, -1) == -1 |
|
0.1.72
by Martin Pool
Go back to weave lines normally having newlines at the end. |
75 |
print >>f, '.', l, |
76 |
else: |
|
|
0.1.73
by Martin Pool
Clean up assertions for weavefile |
77 |
assert l.find('\n') == -1 |
|
0.1.72
by Martin Pool
Go back to weave lines normally having newlines at the end. |
78 |
print >>f, ',', l |
|
0.1.69
by Martin Pool
Simple text-based format for storing weaves, cleaner than |
79 |
|
80 |
print >>f, 'W' |
|
81 |
||
82 |
||
83 |
def read_weave_v1(f): |
|
|
0.1.73
by Martin Pool
Clean up assertions for weavefile |
84 |
from weave import Weave, VerInfo, WeaveFormatError |
|
0.1.69
by Martin Pool
Simple text-based format for storing weaves, cleaner than |
85 |
w = Weave() |
86 |
||
|
0.1.73
by Martin Pool
Clean up assertions for weavefile |
87 |
wfe = WeaveFormatError |
88 |
l = f.readline() |
|
89 |
if l != FORMAT_1: |
|
90 |
raise WeaveFormatError('invalid weave file header: %r' % l) |
|
|
0.1.69
by Martin Pool
Simple text-based format for storing weaves, cleaner than |
91 |
|
|
0.1.73
by Martin Pool
Clean up assertions for weavefile |
92 |
v_cnt = 0 |
|
0.1.69
by Martin Pool
Simple text-based format for storing weaves, cleaner than |
93 |
while True: |
94 |
l = f.readline() |
|
|
0.1.73
by Martin Pool
Clean up assertions for weavefile |
95 |
if l.startswith('v '): |
96 |
ver = int(l[2:]) |
|
97 |
if ver != v_cnt: |
|
98 |
raise WeaveFormatError('version %d!=%d out of order' |
|
99 |
% (ver, v_cnt)) |
|
100 |
v_cnt += 1 |
|
101 |
||
|
0.1.69
by Martin Pool
Simple text-based format for storing weaves, cleaner than |
102 |
l = f.readline()[:-1] |
103 |
if l[0] != 'i': |
|
|
0.1.73
by Martin Pool
Clean up assertions for weavefile |
104 |
raise WeaveFormatError('unexpected line %r' % l) |
|
0.1.69
by Martin Pool
Simple text-based format for storing weaves, cleaner than |
105 |
if len(l) > 2: |
106 |
included = map(int, l[2:].split(' ')) |
|
107 |
w._v.append(VerInfo(included)) |
|
108 |
else: |
|
109 |
w._v.append(VerInfo()) |
|
110 |
assert f.readline() == '\n' |
|
|
0.1.73
by Martin Pool
Clean up assertions for weavefile |
111 |
elif l == 'w\n': |
|
0.1.69
by Martin Pool
Simple text-based format for storing weaves, cleaner than |
112 |
break
|
113 |
else: |
|
|
0.1.73
by Martin Pool
Clean up assertions for weavefile |
114 |
raise WeaveFormatError('unexpected line %r' % l) |
|
0.1.69
by Martin Pool
Simple text-based format for storing weaves, cleaner than |
115 |
|
116 |
while True: |
|
117 |
l = f.readline() |
|
118 |
if l == 'W\n': |
|
119 |
break
|
|
|
0.1.73
by Martin Pool
Clean up assertions for weavefile |
120 |
elif l.startswith('. '): |
|
0.1.72
by Martin Pool
Go back to weave lines normally having newlines at the end. |
121 |
w._l.append(l[2:]) # include newline |
|
0.1.73
by Martin Pool
Clean up assertions for weavefile |
122 |
elif l.startswith(', '): |
|
0.1.72
by Martin Pool
Go back to weave lines normally having newlines at the end. |
123 |
w._l.append(l[2:-1]) # exclude newline |
|
0.1.69
by Martin Pool
Simple text-based format for storing weaves, cleaner than |
124 |
else: |
125 |
assert l[0] in '{}[]', l |
|
126 |
assert l[1] == ' ', l |
|
127 |
w._l.append((l[0], int(l[2:]))) |
|
128 |
||
129 |
return w |
|
130 |