/brz/remove-bazaar

To get this branch, use:
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.
25
"""
26
27
# TODO: When extracting a single version it'd be enough to just pass
28
# an iterator returning the weave lines...
29
30
FORMAT_1 = '# bzr weave file v1'
31
32
33
34
35
def write_weave_v1(weave, f):
36
    """Write weave to file f."""
37
    print >>f, FORMAT_1
38
    print >>f
39
40
    for version, verinfo in enumerate(weave._v):
41
        print >>f, 'v', version
42
        included = list(verinfo.included)
43
        included.sort()
44
        print >>f, 'i',
45
        for i in included:
46
            print >>f, i,
47
        print >>f
48
        print >>f
49
50
    print >>f, 'w'
51
52
    for l in weave._l:
53
        if isinstance(l, tuple):
54
            assert len(l) == 2
55
            assert l[0] in '{}[]'
56
            print >>f, '%s %d' % l
57
        else:
58
            assert '\n' not in l
59
            print >>f, '.', l
60
61
    print >>f, 'W'
62
63
64
def read_weave_v1(f):
65
    from weave import Weave, VerInfo
66
    w = Weave()
67
68
    assert f.readline() == FORMAT_1+'\n'
69
    assert f.readline() == '\n'
70
71
    while True:
72
        l = f.readline()
73
        if l[0] == 'v':
74
            l = f.readline()[:-1]
75
            if l[0] != 'i':
76
                raise Exception(`l`)
77
            if len(l) > 2:
78
                included = map(int, l[2:].split(' '))
79
                w._v.append(VerInfo(included))
80
            else:
81
                w._v.append(VerInfo())
82
            assert f.readline() == '\n'
83
        elif l[0] == 'w':
84
            break
85
        else:
86
            assert 0, l
87
88
    while True:
89
        l = f.readline()
90
        if l == 'W\n':
91
            break
92
        elif l[:2] == '. ':
93
            w._l.append(l[2:-1])
94
        else:
95
            assert l[0] in '{}[]', l
96
            assert l[1] == ' ', l
97
            w._l.append((l[0], int(l[2:])))
98
99
    return w
100