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 |
||
|
0.1.74
by Martin Pool
Add format-hidden readwrite methods |
44 |
def write_weave(weave, f, format=None): |
45 |
if format == None or format == 1: |
|
46 |
return write_weave_v1(weave, f) |
|
47 |
else: |
|
48 |
raise ValueError("unknown weave format %r" % format) |
|
49 |
||
|
0.1.69
by Martin Pool
Simple text-based format for storing weaves, cleaner than |
50 |
|
51 |
def write_weave_v1(weave, f): |
|
52 |
"""Write weave to file f.""" |
|
|
0.1.73
by Martin Pool
Clean up assertions for weavefile |
53 |
print >>f, FORMAT_1, |
|
0.1.69
by Martin Pool
Simple text-based format for storing weaves, cleaner than |
54 |
|
|
0.1.75
by Martin Pool
Remove VerInfo class; just store sets directly in the list of |
55 |
for version, included in enumerate(weave._v): |
|
0.1.69
by Martin Pool
Simple text-based format for storing weaves, cleaner than |
56 |
print >>f, 'v', version |
|
0.1.75
by Martin Pool
Remove VerInfo class; just store sets directly in the list of |
57 |
if included: |
58 |
included = list(included) |
|
|
0.1.72
by Martin Pool
Go back to weave lines normally having newlines at the end. |
59 |
included.sort() |
60 |
assert included[0] >= 0 |
|
61 |
assert included[-1] < version |
|
62 |
print >>f, 'i', |
|
63 |
for i in included: |
|
64 |
print >>f, i, |
|
65 |
print >>f |
|
66 |
else: |
|
67 |
print >>f, 'i' |
|
|
0.1.69
by Martin Pool
Simple text-based format for storing weaves, cleaner than |
68 |
print >>f |
69 |
||
70 |
print >>f, 'w' |
|
71 |
||
72 |
for l in weave._l: |
|
73 |
if isinstance(l, tuple): |
|
74 |
assert l[0] in '{}[]' |
|
75 |
print >>f, '%s %d' % l |
|
|
0.1.72
by Martin Pool
Go back to weave lines normally having newlines at the end. |
76 |
else: # text line |
77 |
if not l: |
|
78 |
print >>f, ', ' |
|
79 |
elif l[-1] == '\n': |
|
|
0.1.73
by Martin Pool
Clean up assertions for weavefile |
80 |
assert l.find('\n', 0, -1) == -1 |
|
0.1.72
by Martin Pool
Go back to weave lines normally having newlines at the end. |
81 |
print >>f, '.', l, |
82 |
else: |
|
|
0.1.73
by Martin Pool
Clean up assertions for weavefile |
83 |
assert l.find('\n') == -1 |
|
0.1.72
by Martin Pool
Go back to weave lines normally having newlines at the end. |
84 |
print >>f, ',', l |
|
0.1.69
by Martin Pool
Simple text-based format for storing weaves, cleaner than |
85 |
|
86 |
print >>f, 'W' |
|
87 |
||
88 |
||
|
0.1.74
by Martin Pool
Add format-hidden readwrite methods |
89 |
|
90 |
def read_weave(f): |
|
91 |
return read_weave_v1(f) |
|
92 |
||
93 |
||
|
0.1.69
by Martin Pool
Simple text-based format for storing weaves, cleaner than |
94 |
def read_weave_v1(f): |
|
0.1.75
by Martin Pool
Remove VerInfo class; just store sets directly in the list of |
95 |
from weave import Weave, WeaveFormatError |
|
0.1.69
by Martin Pool
Simple text-based format for storing weaves, cleaner than |
96 |
w = Weave() |
97 |
||
|
0.1.73
by Martin Pool
Clean up assertions for weavefile |
98 |
wfe = WeaveFormatError |
99 |
l = f.readline() |
|
100 |
if l != FORMAT_1: |
|
101 |
raise WeaveFormatError('invalid weave file header: %r' % l) |
|
|
0.1.69
by Martin Pool
Simple text-based format for storing weaves, cleaner than |
102 |
|
|
0.1.73
by Martin Pool
Clean up assertions for weavefile |
103 |
v_cnt = 0 |
|
0.1.69
by Martin Pool
Simple text-based format for storing weaves, cleaner than |
104 |
while True: |
105 |
l = f.readline() |
|
|
0.1.73
by Martin Pool
Clean up assertions for weavefile |
106 |
if l.startswith('v '): |
107 |
ver = int(l[2:]) |
|
108 |
if ver != v_cnt: |
|
109 |
raise WeaveFormatError('version %d!=%d out of order' |
|
110 |
% (ver, v_cnt)) |
|
111 |
v_cnt += 1 |
|
112 |
||
|
0.1.69
by Martin Pool
Simple text-based format for storing weaves, cleaner than |
113 |
l = f.readline()[:-1] |
114 |
if l[0] != 'i': |
|
|
0.1.73
by Martin Pool
Clean up assertions for weavefile |
115 |
raise WeaveFormatError('unexpected line %r' % l) |
|
0.1.69
by Martin Pool
Simple text-based format for storing weaves, cleaner than |
116 |
if len(l) > 2: |
117 |
included = map(int, l[2:].split(' ')) |
|
|
0.1.75
by Martin Pool
Remove VerInfo class; just store sets directly in the list of |
118 |
w._addversion(included) |
|
0.1.69
by Martin Pool
Simple text-based format for storing weaves, cleaner than |
119 |
else: |
|
0.1.75
by Martin Pool
Remove VerInfo class; just store sets directly in the list of |
120 |
w._addversion(None) |
|
0.1.69
by Martin Pool
Simple text-based format for storing weaves, cleaner than |
121 |
assert f.readline() == '\n' |
|
0.1.73
by Martin Pool
Clean up assertions for weavefile |
122 |
elif l == 'w\n': |
|
0.1.69
by Martin Pool
Simple text-based format for storing weaves, cleaner than |
123 |
break
|
124 |
else: |
|
|
0.1.73
by Martin Pool
Clean up assertions for weavefile |
125 |
raise WeaveFormatError('unexpected line %r' % l) |
|
0.1.69
by Martin Pool
Simple text-based format for storing weaves, cleaner than |
126 |
|
127 |
while True: |
|
128 |
l = f.readline() |
|
129 |
if l == 'W\n': |
|
130 |
break
|
|
|
0.1.73
by Martin Pool
Clean up assertions for weavefile |
131 |
elif l.startswith('. '): |
|
0.1.72
by Martin Pool
Go back to weave lines normally having newlines at the end. |
132 |
w._l.append(l[2:]) # include newline |
|
0.1.73
by Martin Pool
Clean up assertions for weavefile |
133 |
elif l.startswith(', '): |
|
0.1.72
by Martin Pool
Go back to weave lines normally having newlines at the end. |
134 |
w._l.append(l[2:-1]) # exclude newline |
|
0.1.69
by Martin Pool
Simple text-based format for storing weaves, cleaner than |
135 |
else: |
136 |
assert l[0] in '{}[]', l |
|
137 |
assert l[1] == ' ', l |
|
138 |
w._l.append((l[0], int(l[2:]))) |
|
139 |
||
140 |
return w |
|
141 |