~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/weavefile.py

  • Committer: Martin Pool
  • Date: 2005-07-22 22:37:53 UTC
  • Revision ID: mbp@sourcefrog.net-20050722223753-7dced4e32d3ce21d
- add the start of a test for inventory file-id matching

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
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
45
    if format == None or format == 1:
49
 
        return write_weave_v5(weave, f)
 
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
53
    print >>f, FORMAT_1,
57
54
 
66
63
        else:
67
64
            print >>f, 'i'
68
65
        print >>f, '1', weave._sha1s[version]
69
 
        print >>f, 'n', weave._names[version]
70
66
        print >>f
71
67
 
72
68
    print >>f, 'w'
74
70
    for l in weave._weave:
75
71
        if isinstance(l, tuple):
76
72
            assert l[0] in '{}[]'
77
 
            if l[0] == '}':
78
 
                print >>f, '}'
79
 
            else:
80
 
                print >>f, '%s %d' % l
 
73
            print >>f, '%s %d' % l
81
74
        else: # text line
82
75
            if not l:
83
76
                print >>f, ', '
93
86
 
94
87
 
95
88
def read_weave(f):
96
 
    return read_weave_v5(f)
97
 
 
98
 
 
99
 
def read_weave_v5(f):
 
89
    return read_weave_v1(f)
 
90
 
 
91
 
 
92
def read_weave_v1(f):
100
93
    from weave import Weave, WeaveFormatError
101
 
    w = Weave(getattr(f, 'name', None))
 
94
    w = Weave()
102
95
 
 
96
    wfe = WeaveFormatError
103
97
    l = f.readline()
104
98
    if l != FORMAT_1:
105
99
        raise WeaveFormatError('invalid weave file header: %r' % l)
108
102
    while True:
109
103
        l = f.readline()
110
104
        if l[0] == 'i':
 
105
            ver += 1
 
106
 
111
107
            if len(l) > 2:
112
 
                w._parents.append(map(int, l[2:].split(' ')))
 
108
                w._parents.append(frozenset(map(int, l[2:].split(' '))))
113
109
            else:
114
 
                w._parents.append([])
 
110
                w._parents.append(frozenset())
115
111
 
116
112
            l = f.readline()[:-1]
117
113
            assert l.startswith('1 ')
118
114
            w._sha1s.append(l[2:])
119
115
                
120
116
            l = f.readline()
121
 
            assert l.startswith('n ')
122
 
            name = l[2:-1]
123
 
            assert name not in w._name_map
124
 
            w._names.append(name)
125
 
            w._name_map[name] = ver
126
 
                
127
 
            l = f.readline()
128
117
            assert l == '\n'
129
 
 
130
 
            ver += 1
131
118
        elif l == 'w\n':
132
119
            break
133
120
        else:
141
128
            w._weave.append(l[2:])  # include newline
142
129
        elif l.startswith(', '):
143
130
            w._weave.append(l[2:-1])        # exclude newline
144
 
        elif l == '}\n':
145
 
            w._weave.append(('}', None))
146
131
        else:
147
 
            assert l[0] in '{[]', l
 
132
            assert l[0] in '{}[]', l
148
133
            assert l[1] == ' ', l
149
134
            w._weave.append((intern(l[0]), int(l[2:])))
150
135