~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to weavefile.py

  • Committer: Martin Pool
  • Date: 2005-07-01 02:36:27 UTC
  • mto: This revision was merged to the branch mainline in revision 852.
  • Revision ID: mbp@sourcefrog.net-20050701023627-d8422b67a4c1d6d1
Show profile when converting inventory too.

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
There is one format marker followed by a blank line, followed by a
27
27
series of version headers, followed by the weave itself.
28
28
 
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.
 
29
Each version marker has 'v' and the version, then 'i' and the included
 
30
previous versions.  The inclusions do not need to list versions
 
31
included by a parent.
36
32
 
37
33
The weave is bracketed by 'w' and 'W' lines, and includes the '{}[]'
38
34
processing instructions.  Lines of text are prefixed by '.' if the
40
36
"""
41
37
 
42
38
# TODO: When extracting a single version it'd be enough to just pass
43
 
# an iterator returning the weave lines...  We don't really need to
44
 
# deserialize it into memory.
45
 
 
46
 
FORMAT_1 = '# bzr weave file v5\n'
 
39
# an iterator returning the weave lines...
 
40
 
 
41
FORMAT_1 = '# bzr weave file v1\n'
 
42
 
47
43
 
48
44
 
49
45
def write_weave(weave, f, format=None):
50
46
    if format == None or format == 1:
51
 
        return write_weave_v5(weave, f)
 
47
        return write_weave_v1(weave, f)
52
48
    else:
53
49
        raise ValueError("unknown weave format %r" % format)
54
50
 
55
51
 
56
 
def write_weave_v5(weave, f):
 
52
def write_weave_v1(weave, f):
57
53
    """Write weave to file f."""
58
54
    print >>f, FORMAT_1,
59
55
 
60
 
    for version, included in enumerate(weave._parents):
 
56
    for version, included in enumerate(weave._v):
 
57
        print >>f, 'v', version
61
58
        if included:
62
 
            # mininc = weave.minimal_parents(version)
63
 
            mininc = included
 
59
            # find a minimal expression of it; bias towards using
 
60
            # later revisions
 
61
            li = list(included)
 
62
            li.sort()
 
63
            li.reverse()
 
64
 
 
65
            mininc = []
 
66
            gotit = set()
 
67
 
 
68
            for pv in li:
 
69
                if pv not in gotit:
 
70
                    mininc.append(pv)
 
71
                    gotit.update(weave._v[pv])
 
72
 
 
73
            assert mininc[0] >= 0
 
74
            assert mininc[-1] < version
64
75
            print >>f, 'i',
65
76
            for i in mininc:
66
77
                print >>f, i,
67
78
            print >>f
68
79
        else:
69
80
            print >>f, 'i'
70
 
        print >>f, '1', weave._sha1s[version]
71
 
        print >>f, 'n', weave._names[version]
72
81
        print >>f
73
82
 
74
83
    print >>f, 'w'
75
84
 
76
 
    for l in weave._weave:
 
85
    for l in weave._l:
77
86
        if isinstance(l, tuple):
78
87
            assert l[0] in '{}[]'
79
 
            if l[0] == '}':
80
 
                print >>f, '}'
81
 
            else:
82
 
                print >>f, '%s %d' % l
 
88
            print >>f, '%s %d' % l
83
89
        else: # text line
84
90
            if not l:
85
91
                print >>f, ', '
95
101
 
96
102
 
97
103
def read_weave(f):
98
 
    return read_weave_v5(f)
99
 
 
100
 
 
101
 
def read_weave_v5(f):
 
104
    return read_weave_v1(f)
 
105
 
 
106
 
 
107
def read_weave_v1(f):
102
108
    from weave import Weave, WeaveFormatError
103
 
    w = Weave(getattr(f, 'name', None))
 
109
    w = Weave()
104
110
 
 
111
    wfe = WeaveFormatError
105
112
    l = f.readline()
106
113
    if l != FORMAT_1:
107
114
        raise WeaveFormatError('invalid weave file header: %r' % l)
108
115
 
109
 
    ver = 0
 
116
    v_cnt = 0
110
117
    while True:
111
118
        l = f.readline()
112
 
        if l[0] == 'i':
 
119
        if l.startswith('v '):
 
120
            ver = int(l[2:])
 
121
            if ver != v_cnt:
 
122
                raise WeaveFormatError('version %d!=%d out of order'
 
123
                                       % (ver, v_cnt))
 
124
            v_cnt += 1
 
125
            
 
126
            l = f.readline()[:-1]
 
127
            if l[0] != 'i':
 
128
                raise WeaveFormatError('unexpected line %r' % l)
113
129
            if len(l) > 2:
114
 
                w._parents.append(map(int, l[2:].split(' ')))
 
130
                included = map(int, l[2:].split(' '))
 
131
                full = set()
 
132
                for pv in included:
 
133
                    full.add(pv)
 
134
                    full.update(w._v[pv])
 
135
                w._addversion(full)
115
136
            else:
116
 
                w._parents.append([])
117
 
 
118
 
            l = f.readline()[:-1]
119
 
            assert l.startswith('1 ')
120
 
            w._sha1s.append(l[2:])
121
 
                
122
 
            l = f.readline()
123
 
            assert l.startswith('n ')
124
 
            name = l[2:-1]
125
 
            assert name not in w._name_map
126
 
            w._names.append(name)
127
 
            w._name_map[name] = ver
128
 
                
129
 
            l = f.readline()
130
 
            assert l == '\n'
131
 
 
132
 
            ver += 1
 
137
                w._addversion(None)
 
138
            assert f.readline() == '\n'
133
139
        elif l == 'w\n':
134
140
            break
135
141
        else:
140
146
        if l == 'W\n':
141
147
            break
142
148
        elif l.startswith('. '):
143
 
            w._weave.append(l[2:])  # include newline
 
149
            w._l.append(l[2:])           # include newline
144
150
        elif l.startswith(', '):
145
 
            w._weave.append(l[2:-1])        # exclude newline
146
 
        elif l == '}\n':
147
 
            w._weave.append(('}', None))
 
151
            w._l.append(l[2:-1])        # exclude newline
148
152
        else:
149
 
            assert l[0] in '{[]', l
 
153
            assert l[0] in '{}[]', l
150
154
            assert l[1] == ' ', l
151
 
            w._weave.append((intern(l[0]), int(l[2:])))
 
155
            w._l.append((l[0], int(l[2:])))
152
156
 
153
157
    return w
154
158