~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/weavefile.py

  • Committer: Martin Pool
  • Date: 2005-07-11 03:12:11 UTC
  • Revision ID: mbp@sourcefrog.net-20050711031211-9e04b4d2f79fb7b9
- fix typo

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#! /usr/bin/python
 
2
 
1
3
# Copyright (C) 2005 Canonical Ltd
2
 
#
 
4
 
3
5
# This program is free software; you can redistribute it and/or modify
4
6
# it under the terms of the GNU General Public License as published by
5
7
# the Free Software Foundation; either version 2 of the License, or
6
8
# (at your option) any later version.
7
 
#
 
9
 
8
10
# This program is distributed in the hope that it will be useful,
9
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
13
# GNU General Public License for more details.
12
 
#
 
14
 
13
15
# You should have received a copy of the GNU General Public License
14
16
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
 
#
 
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
18
 
17
19
# Author: Martin Pool <mbp@canonical.com>
18
20
 
19
21
 
24
26
There is one format marker followed by a blank line, followed by a
25
27
series of version headers, followed by the weave itself.
26
28
 
27
 
Each version marker has
28
 
 
29
 
 'i'   parent version indexes
30
 
 '1'   SHA-1 of text
31
 
 'n'   name
32
 
 
33
 
The inclusions do not need to list versions included by a parent.
 
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.
34
32
 
35
33
The weave is bracketed by 'w' and 'W' lines, and includes the '{}[]'
36
34
processing instructions.  Lines of text are prefixed by '.' if the
38
36
"""
39
37
 
40
38
# TODO: When extracting a single version it'd be enough to just pass
41
 
# an iterator returning the weave lines...  We don't really need to
42
 
# deserialize it into memory.
 
39
# an iterator returning the weave lines...
43
40
 
44
 
FORMAT_1 = '# bzr weave file v5\n'
 
41
FORMAT_1 = '# bzr weave file v3\n'
45
42
 
46
43
 
47
44
def write_weave(weave, f, format=None):
48
 
    if format is None or format == 1:
49
 
        return write_weave_v5(weave, f)
 
45
    if format == None or format == 1:
 
46
        return write_weave_v1(weave, f)
50
47
    else:
51
48
        raise ValueError("unknown weave format %r" % format)
52
49
 
53
50
 
54
 
def write_weave_v5(weave, f):
 
51
def write_weave_v1(weave, f):
55
52
    """Write weave to file f."""
56
 
    f.write(FORMAT_1)
 
53
    print >>f, FORMAT_1,
57
54
 
58
 
    for version, included in enumerate(weave._parents):
 
55
    for version, included in enumerate(weave._v):
59
56
        if included:
60
 
            # mininc = weave.minimal_parents(version)
61
 
            mininc = included
62
 
            f.write('i ')
63
 
            f.write(' '.join(str(i) for i in mininc))
64
 
            f.write('\n')
 
57
            # find a minimal expression of it; bias towards using
 
58
            # later revisions
 
59
            li = list(included)
 
60
            li.sort()
 
61
            li.reverse()
 
62
 
 
63
            mininc = []
 
64
            gotit = set()
 
65
 
 
66
            for pv in li:
 
67
                if pv not in gotit:
 
68
                    mininc.append(pv)
 
69
                    gotit.update(weave._v[pv])
 
70
 
 
71
            assert mininc[0] >= 0
 
72
            assert mininc[-1] < version
 
73
            print >>f, 'i',
 
74
            for i in mininc:
 
75
                print >>f, i,
 
76
            print >>f
65
77
        else:
66
 
            f.write('i\n')
67
 
        f.write('1 ' + weave._sha1s[version] + '\n')
68
 
        f.write('n ' + weave._names[version] + '\n')
69
 
        f.write('\n')
70
 
 
71
 
    f.write('w\n')
72
 
 
73
 
    for l in weave._weave:
 
78
            print >>f, 'i'
 
79
        print >>f, '1', weave._sha1s[version]
 
80
        print >>f
 
81
 
 
82
    print >>f, 'w'
 
83
 
 
84
    for l in weave._l:
74
85
        if isinstance(l, tuple):
75
 
            if l[0] == '}':
76
 
                f.write('}\n')
77
 
            else:
78
 
                f.write('%s %d\n' % l)
 
86
            assert l[0] in '{}[]'
 
87
            print >>f, '%s %d' % l
79
88
        else: # text line
80
89
            if not l:
81
 
                f.write(', \n')
 
90
                print >>f, ', '
82
91
            elif l[-1] == '\n':
83
 
                f.write('. ' + l)
 
92
                assert l.find('\n', 0, -1) == -1
 
93
                print >>f, '.', l,
84
94
            else:
85
 
                f.write(', ' + l + '\n')
 
95
                assert l.find('\n') == -1
 
96
                print >>f, ',', l
86
97
 
87
 
    f.write('W\n')
 
98
    print >>f, 'W'
88
99
 
89
100
 
90
101
 
91
102
def read_weave(f):
92
 
    # FIXME: detect the weave type and dispatch
93
 
    from bzrlib.trace import mutter
94
 
    from weave import Weave
95
 
    w = Weave(getattr(f, 'name', None))
96
 
    _read_weave_v5(f, w)
97
 
    return w
98
 
 
99
 
 
100
 
def _read_weave_v5(f, w):
101
 
    """Private helper routine to read a weave format 5 file into memory.
102
 
 
103
 
    This is only to be used by read_weave and WeaveFile.__init__.
104
 
    """
105
 
    #  200   0   2075.5080   1084.0360   bzrlib.weavefile:104(_read_weave_v5)
106
 
    # +60412 0    366.5900    366.5900   +<method 'readline' of 'file' objects>
107
 
    # +59982 0    320.5280    320.5280   +<method 'startswith' of 'str' objects>
108
 
    # +59363 0    297.8080    297.8080   +<method 'append' of 'list' objects>
109
 
    # replace readline call with iter over all lines ->
110
 
    # safe because we already suck on memory.
111
 
    #  200   0   1492.7170    802.6220   bzrlib.weavefile:104(_read_weave_v5)
112
 
    # +59982 0    329.9100    329.9100   +<method 'startswith' of 'str' objects>
113
 
    # +59363 0    320.2980    320.2980   +<method 'append' of 'list' objects>
114
 
    # replaced startswith with slice lookups:
115
 
    #  200   0    851.7250    501.1120   bzrlib.weavefile:104(_read_weave_v5)
116
 
    # +59363 0    311.8780    311.8780   +<method 'append' of 'list' objects>
117
 
    # +200   0     30.2500     30.2500   +<method 'readlines' of 'file' objects>
118
 
 
119
 
    from weave import WeaveFormatError
120
 
 
121
 
    lines = iter(f.readlines())
122
 
 
123
 
    try:
124
 
        l = lines.next()
125
 
    except StopIteration:
126
 
        raise WeaveFormatError('invalid weave file: no header')
127
 
 
 
103
    return read_weave_v1(f)
 
104
 
 
105
 
 
106
def read_weave_v1(f):
 
107
    from weave import Weave, WeaveFormatError
 
108
    w = Weave()
 
109
 
 
110
    wfe = WeaveFormatError
 
111
    l = f.readline()
128
112
    if l != FORMAT_1:
129
113
        raise WeaveFormatError('invalid weave file header: %r' % l)
130
114
 
131
115
    ver = 0
132
 
    # read weave header.
133
116
    while True:
134
 
        l = lines.next()
 
117
        l = f.readline()
135
118
        if l[0] == 'i':
 
119
            ver += 1
 
120
 
136
121
            if len(l) > 2:
137
 
                w._parents.append(map(int, l[2:].split(' ')))
 
122
                included = map(int, l[2:].split(' '))
 
123
                full = set()
 
124
                for pv in included:
 
125
                    full.add(pv)
 
126
                    full.update(w._v[pv])
 
127
                w._addversion(full)
138
128
            else:
139
 
                w._parents.append([])
140
 
            l = lines.next()[:-1]
 
129
                w._addversion(None)
 
130
 
 
131
            l = f.readline()[:-1]
 
132
            assert l.startswith('1 ')
141
133
            w._sha1s.append(l[2:])
142
 
            l = lines.next()
143
 
            name = l[2:-1]
144
 
            w._names.append(name)
145
 
            w._name_map[name] = ver
146
 
            l = lines.next()
147
 
            ver += 1
 
134
                
 
135
            l = f.readline()
 
136
            assert l == '\n'
148
137
        elif l == 'w\n':
149
138
            break
150
139
        else:
151
140
            raise WeaveFormatError('unexpected line %r' % l)
152
141
 
153
 
    # read weave body
154
142
    while True:
155
 
        l = lines.next()
 
143
        l = f.readline()
156
144
        if l == 'W\n':
157
145
            break
158
 
        elif '. ' == l[0:2]:
159
 
            w._weave.append(l[2:])  # include newline
160
 
        elif ', ' == l[0:2]:
161
 
            w._weave.append(l[2:-1])        # exclude newline
162
 
        elif l == '}\n':
163
 
            w._weave.append(('}', None))
 
146
        elif l.startswith('. '):
 
147
            w._l.append(intern(l[2:]))  # include newline
 
148
        elif l.startswith(', '):
 
149
            w._l.append(l[2:-1])        # exclude newline
164
150
        else:
165
 
            w._weave.append((intern(l[0]), int(l[2:])))
 
151
            assert l[0] in '{}[]', l
 
152
            assert l[1] == ' ', l
 
153
            w._l.append((intern(l[0]), int(l[2:])))
 
154
 
166
155
    return w
 
156