~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/weavefile.py

  • Committer: Martin Pool
  • Date: 2005-07-23 13:52:38 UTC
  • Revision ID: mbp@sourcefrog.net-20050723135238-96b1580de8dff136
doc

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2010 Canonical Ltd
2
 
#
 
1
#! /usr/bin/python
 
2
 
 
3
# Copyright (C) 2005 Canonical Ltd
 
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
 
 
21
 
 
22
 
 
23
 
19
24
"""Store and retrieve weaves in files.
20
25
 
21
26
There is one format marker followed by a blank line, followed by a
22
27
series of version headers, followed by the weave itself.
23
28
 
24
 
Each version marker has
25
 
 
26
 
 'i'   parent version indexes
27
 
 '1'   SHA-1 of text
28
 
 'n'   name
29
 
 
30
 
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.
31
32
 
32
33
The weave is bracketed by 'w' and 'W' lines, and includes the '{}[]'
33
34
processing instructions.  Lines of text are prefixed by '.' if the
34
35
line contains a newline, or ',' if not.
35
36
"""
36
37
 
37
 
from __future__ import absolute_import
38
 
 
39
38
# TODO: When extracting a single version it'd be enough to just pass
40
 
# an iterator returning the weave lines...  We don't really need to
41
 
# deserialize it into memory.
 
39
# an iterator returning the weave lines...
42
40
 
43
 
FORMAT_1 = '# bzr weave file v5\n'
 
41
FORMAT_1 = '# bzr weave file v3\n'
44
42
 
45
43
 
46
44
def write_weave(weave, f, format=None):
47
 
    if format is None or format == 1:
48
 
        return write_weave_v5(weave, f)
 
45
    if format == None or format == 1:
 
46
        return write_weave_v1(weave, f)
49
47
    else:
50
48
        raise ValueError("unknown weave format %r" % format)
51
49
 
52
50
 
53
 
def write_weave_v5(weave, f):
 
51
def write_weave_v1(weave, f):
54
52
    """Write weave to file f."""
55
 
    f.write(FORMAT_1)
 
53
    print >>f, FORMAT_1,
56
54
 
57
55
    for version, included in enumerate(weave._parents):
58
56
        if included:
59
57
            # mininc = weave.minimal_parents(version)
60
58
            mininc = included
61
 
            f.write('i ')
62
 
            f.write(' '.join(str(i) for i in mininc))
63
 
            f.write('\n')
 
59
            print >>f, 'i',
 
60
            for i in mininc:
 
61
                print >>f, i,
 
62
            print >>f
64
63
        else:
65
 
            f.write('i\n')
66
 
        f.write('1 ' + weave._sha1s[version] + '\n')
67
 
        f.write('n ' + weave._names[version] + '\n')
68
 
        f.write('\n')
 
64
            print >>f, 'i'
 
65
        print >>f, '1', weave._sha1s[version]
 
66
        print >>f
69
67
 
70
 
    f.write('w\n')
 
68
    print >>f, 'w'
71
69
 
72
70
    for l in weave._weave:
73
71
        if isinstance(l, tuple):
74
 
            if l[0] == '}':
75
 
                f.write('}\n')
76
 
            else:
77
 
                f.write('%s %d\n' % l)
 
72
            assert l[0] in '{}[]'
 
73
            print >>f, '%s %d' % l
78
74
        else: # text line
79
75
            if not l:
80
 
                f.write(', \n')
 
76
                print >>f, ', '
81
77
            elif l[-1] == '\n':
82
 
                f.write('. ' + l)
 
78
                assert l.find('\n', 0, -1) == -1
 
79
                print >>f, '.', l,
83
80
            else:
84
 
                f.write(', ' + l + '\n')
 
81
                assert l.find('\n') == -1
 
82
                print >>f, ',', l
85
83
 
86
 
    f.write('W\n')
 
84
    print >>f, 'W'
87
85
 
88
86
 
89
87
 
90
88
def read_weave(f):
91
 
    # FIXME: detect the weave type and dispatch
92
 
    from bzrlib.weave import Weave
93
 
    w = Weave(getattr(f, 'name', None))
94
 
    _read_weave_v5(f, w)
95
 
    return w
96
 
 
97
 
 
98
 
def _read_weave_v5(f, w):
99
 
    """Private helper routine to read a weave format 5 file into memory.
100
 
 
101
 
    This is only to be used by read_weave and WeaveFile.__init__.
102
 
    """
103
 
    #  200   0   2075.5080   1084.0360   bzrlib.weavefile:104(_read_weave_v5)
104
 
    # +60412 0    366.5900    366.5900   +<method 'readline' of 'file' objects>
105
 
    # +59982 0    320.5280    320.5280   +<method 'startswith' of 'str' objects>
106
 
    # +59363 0    297.8080    297.8080   +<method 'append' of 'list' objects>
107
 
    # replace readline call with iter over all lines ->
108
 
    # safe because we already suck on memory.
109
 
    #  200   0   1492.7170    802.6220   bzrlib.weavefile:104(_read_weave_v5)
110
 
    # +59982 0    329.9100    329.9100   +<method 'startswith' of 'str' objects>
111
 
    # +59363 0    320.2980    320.2980   +<method 'append' of 'list' objects>
112
 
    # replaced startswith with slice lookups:
113
 
    #  200   0    851.7250    501.1120   bzrlib.weavefile:104(_read_weave_v5)
114
 
    # +59363 0    311.8780    311.8780   +<method 'append' of 'list' objects>
115
 
    # +200   0     30.2500     30.2500   +<method 'readlines' of 'file' objects>
116
 
 
117
 
    from bzrlib.weave import WeaveFormatError
118
 
 
119
 
    try:
120
 
        lines = iter(f.readlines())
121
 
    finally:
122
 
        f.close()
123
 
 
124
 
    try:
125
 
        l = lines.next()
126
 
    except StopIteration:
127
 
        raise WeaveFormatError('invalid weave file: no header')
128
 
 
 
89
    return read_weave_v1(f)
 
90
 
 
91
 
 
92
def read_weave_v1(f):
 
93
    from weave import Weave, WeaveFormatError
 
94
    w = Weave()
 
95
 
 
96
    wfe = WeaveFormatError
 
97
    l = f.readline()
129
98
    if l != FORMAT_1:
130
99
        raise WeaveFormatError('invalid weave file header: %r' % l)
131
100
 
132
101
    ver = 0
133
 
    # read weave header.
134
102
    while True:
135
 
        l = lines.next()
 
103
        l = f.readline()
136
104
        if l[0] == 'i':
 
105
            ver += 1
 
106
 
137
107
            if len(l) > 2:
138
 
                w._parents.append(map(int, l[2:].split(' ')))
 
108
                w._parents.append(frozenset(map(int, l[2:].split(' '))))
139
109
            else:
140
 
                w._parents.append([])
141
 
            l = lines.next()[:-1]
 
110
                w._parents.append(frozenset())
 
111
 
 
112
            l = f.readline()[:-1]
 
113
            assert l.startswith('1 ')
142
114
            w._sha1s.append(l[2:])
143
 
            l = lines.next()
144
 
            name = l[2:-1]
145
 
            w._names.append(name)
146
 
            w._name_map[name] = ver
147
 
            l = lines.next()
148
 
            ver += 1
 
115
                
 
116
            l = f.readline()
 
117
            assert l == '\n'
149
118
        elif l == 'w\n':
150
119
            break
151
120
        else:
152
121
            raise WeaveFormatError('unexpected line %r' % l)
153
122
 
154
 
    # read weave body
155
123
    while True:
156
 
        l = lines.next()
 
124
        l = f.readline()
157
125
        if l == 'W\n':
158
126
            break
159
 
        elif '. ' == l[0:2]:
 
127
        elif l.startswith('. '):
160
128
            w._weave.append(l[2:])  # include newline
161
 
        elif ', ' == l[0:2]:
 
129
        elif l.startswith(', '):
162
130
            w._weave.append(l[2:-1])        # exclude newline
163
 
        elif l == '}\n':
164
 
            w._weave.append(('}', None))
165
131
        else:
 
132
            assert l[0] in '{}[]', l
 
133
            assert l[1] == ' ', l
166
134
            w._weave.append((intern(l[0]), int(l[2:])))
 
135
 
167
136
    return w
 
137