~bzr-pqm/bzr/bzr.dev

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
0.1.90 by Martin Pool
Remove redundant 'v' lines from weave file
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.
0.1.72 by Martin Pool
Go back to weave lines normally having newlines at the end.
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.
0.1.69 by Martin Pool
Simple text-based format for storing weaves, cleaner than
36
"""
37
38
# TODO: When extracting a single version it'd be enough to just pass
39
# an iterator returning the weave lines...
40
0.1.90 by Martin Pool
Remove redundant 'v' lines from weave file
41
FORMAT_1 = '# bzr weave file v3\n'
0.1.69 by Martin Pool
Simple text-based format for storing weaves, cleaner than
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):
56
        if included:
892 by Martin Pool
- weave stores only direct parents, and calculates and memoizes expansion as needed
57
            # mininc = weave.minimal_parents(version)
58
            mininc = included
0.1.72 by Martin Pool
Go back to weave lines normally having newlines at the end.
59
            print >>f, 'i',
0.1.79 by Martin Pool
In the weavefile, store only the minimum revisions added, not the full
60
            for i in mininc:
0.1.72 by Martin Pool
Go back to weave lines normally having newlines at the end.
61
                print >>f, i,
62
            print >>f
63
        else:
64
            print >>f, 'i'
0.1.89 by Martin Pool
Store SHA1 in weave file for later verification
65
        print >>f, '1', weave._sha1s[version]
0.1.69 by Martin Pool
Simple text-based format for storing weaves, cleaner than
66
        print >>f
67
68
    print >>f, 'w'
69
70
    for l in weave._l:
71
        if isinstance(l, tuple):
72
            assert l[0] in '{}[]'
73
            print >>f, '%s %d' % l
0.1.72 by Martin Pool
Go back to weave lines normally having newlines at the end.
74
        else: # text line
75
            if not l:
76
                print >>f, ', '
77
            elif l[-1] == '\n':
0.1.73 by Martin Pool
Clean up assertions for weavefile
78
                assert l.find('\n', 0, -1) == -1
0.1.72 by Martin Pool
Go back to weave lines normally having newlines at the end.
79
                print >>f, '.', l,
80
            else:
0.1.73 by Martin Pool
Clean up assertions for weavefile
81
                assert l.find('\n') == -1
0.1.72 by Martin Pool
Go back to weave lines normally having newlines at the end.
82
                print >>f, ',', l
0.1.69 by Martin Pool
Simple text-based format for storing weaves, cleaner than
83
84
    print >>f, 'W'
85
86
0.1.74 by Martin Pool
Add format-hidden readwrite methods
87
88
def read_weave(f):
89
    return read_weave_v1(f)
90
91
0.1.69 by Martin Pool
Simple text-based format for storing weaves, cleaner than
92
def read_weave_v1(f):
0.1.75 by Martin Pool
Remove VerInfo class; just store sets directly in the list of
93
    from weave import Weave, WeaveFormatError
0.1.69 by Martin Pool
Simple text-based format for storing weaves, cleaner than
94
    w = Weave()
95
0.1.73 by Martin Pool
Clean up assertions for weavefile
96
    wfe = WeaveFormatError
97
    l = f.readline()
98
    if l != FORMAT_1:
99
        raise WeaveFormatError('invalid weave file header: %r' % l)
0.1.69 by Martin Pool
Simple text-based format for storing weaves, cleaner than
100
0.1.90 by Martin Pool
Remove redundant 'v' lines from weave file
101
    ver = 0
0.1.69 by Martin Pool
Simple text-based format for storing weaves, cleaner than
102
    while True:
103
        l = f.readline()
0.1.90 by Martin Pool
Remove redundant 'v' lines from weave file
104
        if l[0] == 'i':
105
            ver += 1
106
0.1.69 by Martin Pool
Simple text-based format for storing weaves, cleaner than
107
            if len(l) > 2:
108
                included = map(int, l[2:].split(' '))
892 by Martin Pool
- weave stores only direct parents, and calculates and memoizes expansion as needed
109
                w._addversion(included)
0.1.69 by Martin Pool
Simple text-based format for storing weaves, cleaner than
110
            else:
0.1.75 by Martin Pool
Remove VerInfo class; just store sets directly in the list of
111
                w._addversion(None)
0.1.89 by Martin Pool
Store SHA1 in weave file for later verification
112
113
            l = f.readline()[:-1]
114
            assert l.startswith('1 ')
115
            w._sha1s.append(l[2:])
116
                
0.1.93 by Martin Pool
Fix assertion with side effects
117
            l = f.readline()
118
            assert l == '\n'
0.1.73 by Martin Pool
Clean up assertions for weavefile
119
        elif l == 'w\n':
0.1.69 by Martin Pool
Simple text-based format for storing weaves, cleaner than
120
            break
121
        else:
0.1.73 by Martin Pool
Clean up assertions for weavefile
122
            raise WeaveFormatError('unexpected line %r' % l)
0.1.69 by Martin Pool
Simple text-based format for storing weaves, cleaner than
123
124
    while True:
125
        l = f.readline()
126
        if l == 'W\n':
127
            break
0.1.73 by Martin Pool
Clean up assertions for weavefile
128
        elif l.startswith('. '):
0.1.95 by Martin Pool
- preliminary merge conflict detection
129
            w._l.append(intern(l[2:]))  # include newline
0.1.73 by Martin Pool
Clean up assertions for weavefile
130
        elif l.startswith(', '):
0.1.72 by Martin Pool
Go back to weave lines normally having newlines at the end.
131
            w._l.append(l[2:-1])        # exclude newline
0.1.69 by Martin Pool
Simple text-based format for storing weaves, cleaner than
132
        else:
133
            assert l[0] in '{}[]', l
134
            assert l[1] == ' ', l
0.1.95 by Martin Pool
- preliminary merge conflict detection
135
            w._l.append((intern(l[0]), int(l[2:])))
0.1.69 by Martin Pool
Simple text-based format for storing weaves, cleaner than
136
137
    return w
138