~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
1083 by Martin Pool
- add space to store revision-id in weave files
29
Each version marker has
30
31
 'i'   parent version indexes
32
 '1'   SHA-1 of text
33
 'n'   name
34
35
The inclusions do not need to list versions included by a parent.
0.1.72 by Martin Pool
Go back to weave lines normally having newlines at the end.
36
37
The weave is bracketed by 'w' and 'W' lines, and includes the '{}[]'
38
processing instructions.  Lines of text are prefixed by '.' if the
39
line contains a newline, or ',' if not.
0.1.69 by Martin Pool
Simple text-based format for storing weaves, cleaner than
40
"""
41
42
# TODO: When extracting a single version it'd be enough to just pass
1075 by Martin Pool
- don't store redundant version number at end of insert blocks
43
# an iterator returning the weave lines...  We don't really need to
44
# deserialize it into memory.
0.1.69 by Martin Pool
Simple text-based format for storing weaves, cleaner than
45
1083 by Martin Pool
- add space to store revision-id in weave files
46
FORMAT_1 = '# bzr weave file v5\n'
0.1.69 by Martin Pool
Simple text-based format for storing weaves, cleaner than
47
48
0.1.74 by Martin Pool
Add format-hidden readwrite methods
49
def write_weave(weave, f, format=None):
50
    if format == None or format == 1:
1083 by Martin Pool
- add space to store revision-id in weave files
51
        return write_weave_v5(weave, f)
0.1.74 by Martin Pool
Add format-hidden readwrite methods
52
    else:
53
        raise ValueError("unknown weave format %r" % format)
54
0.1.69 by Martin Pool
Simple text-based format for storing weaves, cleaner than
55
1083 by Martin Pool
- add space to store revision-id in weave files
56
def write_weave_v5(weave, f):
0.1.69 by Martin Pool
Simple text-based format for storing weaves, cleaner than
57
    """Write weave to file f."""
0.1.73 by Martin Pool
Clean up assertions for weavefile
58
    print >>f, FORMAT_1,
0.1.69 by Martin Pool
Simple text-based format for storing weaves, cleaner than
59
944 by Martin Pool
- refactor member names in Weave code
60
    for version, included in enumerate(weave._parents):
0.1.75 by Martin Pool
Remove VerInfo class; just store sets directly in the list of
61
        if included:
892 by Martin Pool
- weave stores only direct parents, and calculates and memoizes expansion as needed
62
            # mininc = weave.minimal_parents(version)
63
            mininc = included
0.1.72 by Martin Pool
Go back to weave lines normally having newlines at the end.
64
            print >>f, 'i',
0.1.79 by Martin Pool
In the weavefile, store only the minimum revisions added, not the full
65
            for i in mininc:
0.1.72 by Martin Pool
Go back to weave lines normally having newlines at the end.
66
                print >>f, i,
67
            print >>f
68
        else:
69
            print >>f, 'i'
0.1.89 by Martin Pool
Store SHA1 in weave file for later verification
70
        print >>f, '1', weave._sha1s[version]
1083 by Martin Pool
- add space to store revision-id in weave files
71
        print >>f, 'n', weave._names[version]
0.1.69 by Martin Pool
Simple text-based format for storing weaves, cleaner than
72
        print >>f
73
74
    print >>f, 'w'
75
944 by Martin Pool
- refactor member names in Weave code
76
    for l in weave._weave:
0.1.69 by Martin Pool
Simple text-based format for storing weaves, cleaner than
77
        if isinstance(l, tuple):
78
            assert l[0] in '{}[]'
1075 by Martin Pool
- don't store redundant version number at end of insert blocks
79
            if l[0] == '}':
80
                print >>f, '}'
81
            else:
82
                print >>f, '%s %d' % l
0.1.72 by Martin Pool
Go back to weave lines normally having newlines at the end.
83
        else: # text line
84
            if not l:
85
                print >>f, ', '
86
            elif l[-1] == '\n':
0.1.73 by Martin Pool
Clean up assertions for weavefile
87
                assert l.find('\n', 0, -1) == -1
0.1.72 by Martin Pool
Go back to weave lines normally having newlines at the end.
88
                print >>f, '.', l,
89
            else:
0.1.73 by Martin Pool
Clean up assertions for weavefile
90
                assert l.find('\n') == -1
0.1.72 by Martin Pool
Go back to weave lines normally having newlines at the end.
91
                print >>f, ',', l
0.1.69 by Martin Pool
Simple text-based format for storing weaves, cleaner than
92
93
    print >>f, 'W'
94
95
0.1.74 by Martin Pool
Add format-hidden readwrite methods
96
97
def read_weave(f):
1083 by Martin Pool
- add space to store revision-id in weave files
98
    return read_weave_v5(f)
99
100
101
def read_weave_v5(f):
0.1.75 by Martin Pool
Remove VerInfo class; just store sets directly in the list of
102
    from weave import Weave, WeaveFormatError
0.1.69 by Martin Pool
Simple text-based format for storing weaves, cleaner than
103
    w = Weave()
104
0.1.73 by Martin Pool
Clean up assertions for weavefile
105
    wfe = WeaveFormatError
106
    l = f.readline()
107
    if l != FORMAT_1:
108
        raise WeaveFormatError('invalid weave file header: %r' % l)
0.1.69 by Martin Pool
Simple text-based format for storing weaves, cleaner than
109
0.1.90 by Martin Pool
Remove redundant 'v' lines from weave file
110
    ver = 0
0.1.69 by Martin Pool
Simple text-based format for storing weaves, cleaner than
111
    while True:
112
        l = f.readline()
0.1.90 by Martin Pool
Remove redundant 'v' lines from weave file
113
        if l[0] == 'i':
0.1.69 by Martin Pool
Simple text-based format for storing weaves, cleaner than
114
            if len(l) > 2:
1079 by Martin Pool
- weavefile can just use lists for read-in ancestry, not frozensets
115
                w._parents.append(map(int, l[2:].split(' ')))
0.1.69 by Martin Pool
Simple text-based format for storing weaves, cleaner than
116
            else:
1079 by Martin Pool
- weavefile can just use lists for read-in ancestry, not frozensets
117
                w._parents.append([])
0.1.89 by Martin Pool
Store SHA1 in weave file for later verification
118
119
            l = f.readline()[:-1]
120
            assert l.startswith('1 ')
121
            w._sha1s.append(l[2:])
122
                
0.1.93 by Martin Pool
Fix assertion with side effects
123
            l = f.readline()
1083 by Martin Pool
- add space to store revision-id in weave files
124
            assert l.startswith('n ')
125
            name = l[2:-1]
126
            assert name not in w._name_map
127
            w._names.append(name)
128
            w._name_map[name] = ver
129
                
130
            l = f.readline()
0.1.93 by Martin Pool
Fix assertion with side effects
131
            assert l == '\n'
1083 by Martin Pool
- add space to store revision-id in weave files
132
133
            ver += 1
0.1.73 by Martin Pool
Clean up assertions for weavefile
134
        elif l == 'w\n':
0.1.69 by Martin Pool
Simple text-based format for storing weaves, cleaner than
135
            break
136
        else:
0.1.73 by Martin Pool
Clean up assertions for weavefile
137
            raise WeaveFormatError('unexpected line %r' % l)
0.1.69 by Martin Pool
Simple text-based format for storing weaves, cleaner than
138
139
    while True:
140
        l = f.readline()
141
        if l == 'W\n':
142
            break
0.1.73 by Martin Pool
Clean up assertions for weavefile
143
        elif l.startswith('. '):
944 by Martin Pool
- refactor member names in Weave code
144
            w._weave.append(l[2:])  # include newline
0.1.73 by Martin Pool
Clean up assertions for weavefile
145
        elif l.startswith(', '):
944 by Martin Pool
- refactor member names in Weave code
146
            w._weave.append(l[2:-1])        # exclude newline
1075 by Martin Pool
- don't store redundant version number at end of insert blocks
147
        elif l == '}\n':
148
            w._weave.append(('}', None))
0.1.69 by Martin Pool
Simple text-based format for storing weaves, cleaner than
149
        else:
1075 by Martin Pool
- don't store redundant version number at end of insert blocks
150
            assert l[0] in '{[]', l
0.1.69 by Martin Pool
Simple text-based format for storing weaves, cleaner than
151
            assert l[1] == ' ', l
944 by Martin Pool
- refactor member names in Weave code
152
            w._weave.append((intern(l[0]), int(l[2:])))
0.1.69 by Martin Pool
Simple text-based format for storing weaves, cleaner than
153
154
    return w
155