/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/weavefile.py

  • Committer: Martin Pool
  • Date: 2005-07-11 04:08:33 UTC
  • Revision ID: mbp@sourcefrog.net-20050711040832-b516f622d7e5d1f3
- fix up refactoring of weave

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
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 'i' and the included previous versions, then
 
30
'1' and the SHA-1 of the text, if known.  The inclusions do not need
 
31
to list versions included by a parent.
 
32
 
 
33
The weave is bracketed by 'w' and 'W' lines, and includes the '{}[]'
 
34
processing instructions.  Lines of text are prefixed by '.' if the
 
35
line contains a newline, or ',' if not.
 
36
"""
 
37
 
 
38
# TODO: When extracting a single version it'd be enough to just pass
 
39
# an iterator returning the weave lines...
 
40
 
 
41
FORMAT_1 = '# bzr weave file v3\n'
 
42
 
 
43
 
 
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
 
 
50
 
 
51
def write_weave_v1(weave, f):
 
52
    """Write weave to file f."""
 
53
    print >>f, FORMAT_1,
 
54
 
 
55
    for version, included in enumerate(weave._v):
 
56
        if included:
 
57
            mininc = weave.minimal_parents(version)
 
58
            print >>f, 'i',
 
59
            for i in mininc:
 
60
                print >>f, i,
 
61
            print >>f
 
62
        else:
 
63
            print >>f, 'i'
 
64
        print >>f, '1', weave._sha1s[version]
 
65
        print >>f
 
66
 
 
67
    print >>f, 'w'
 
68
 
 
69
    for l in weave._l:
 
70
        if isinstance(l, tuple):
 
71
            assert l[0] in '{}[]'
 
72
            print >>f, '%s %d' % l
 
73
        else: # text line
 
74
            if not l:
 
75
                print >>f, ', '
 
76
            elif l[-1] == '\n':
 
77
                assert l.find('\n', 0, -1) == -1
 
78
                print >>f, '.', l,
 
79
            else:
 
80
                assert l.find('\n') == -1
 
81
                print >>f, ',', l
 
82
 
 
83
    print >>f, 'W'
 
84
 
 
85
 
 
86
 
 
87
def read_weave(f):
 
88
    return read_weave_v1(f)
 
89
 
 
90
 
 
91
def read_weave_v1(f):
 
92
    from weave import Weave, WeaveFormatError
 
93
    w = Weave()
 
94
 
 
95
    wfe = WeaveFormatError
 
96
    l = f.readline()
 
97
    if l != FORMAT_1:
 
98
        raise WeaveFormatError('invalid weave file header: %r' % l)
 
99
 
 
100
    ver = 0
 
101
    while True:
 
102
        l = f.readline()
 
103
        if l[0] == 'i':
 
104
            ver += 1
 
105
 
 
106
            if len(l) > 2:
 
107
                included = map(int, l[2:].split(' '))
 
108
                full = set()
 
109
                for pv in included:
 
110
                    full.add(pv)
 
111
                    full.update(w._v[pv])
 
112
                w._addversion(full)
 
113
            else:
 
114
                w._addversion(None)
 
115
 
 
116
            l = f.readline()[:-1]
 
117
            assert l.startswith('1 ')
 
118
            w._sha1s.append(l[2:])
 
119
                
 
120
            l = f.readline()
 
121
            assert l == '\n'
 
122
        elif l == 'w\n':
 
123
            break
 
124
        else:
 
125
            raise WeaveFormatError('unexpected line %r' % l)
 
126
 
 
127
    while True:
 
128
        l = f.readline()
 
129
        if l == 'W\n':
 
130
            break
 
131
        elif l.startswith('. '):
 
132
            w._l.append(intern(l[2:]))  # include newline
 
133
        elif l.startswith(', '):
 
134
            w._l.append(l[2:-1])        # exclude newline
 
135
        else:
 
136
            assert l[0] in '{}[]', l
 
137
            assert l[1] == ' ', l
 
138
            w._l.append((intern(l[0]), int(l[2:])))
 
139
 
 
140
    return w
 
141