~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/weavefile.py

  • Committer: Jelmer Vernooij
  • Date: 2011-12-19 13:23:58 UTC
  • mto: This revision was merged to the branch mainline in revision 6386.
  • Revision ID: jelmer@canonical.com-20111219132358-uvs5a6y92gomzacd
Move importing from future until after doc string, otherwise the doc string will disappear.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005 Canonical Ltd
 
1
# Copyright (C) 2005-2010 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
#
17
17
# Author: Martin Pool <mbp@canonical.com>
18
18
 
19
 
 
20
 
 
21
 
 
22
19
"""Store and retrieve weaves in files.
23
20
 
24
21
There is one format marker followed by a blank line, followed by a
37
34
line contains a newline, or ',' if not.
38
35
"""
39
36
 
 
37
from __future__ import absolute_import
 
38
 
40
39
# TODO: When extracting a single version it'd be enough to just pass
41
40
# an iterator returning the weave lines...  We don't really need to
42
41
# deserialize it into memory.
53
52
 
54
53
def write_weave_v5(weave, f):
55
54
    """Write weave to file f."""
56
 
    print >>f, FORMAT_1,
 
55
    f.write(FORMAT_1)
57
56
 
58
57
    for version, included in enumerate(weave._parents):
59
58
        if included:
60
59
            # mininc = weave.minimal_parents(version)
61
60
            mininc = included
62
 
            print >>f, 'i',
63
 
            for i in mininc:
64
 
                print >>f, i,
65
 
            print >>f
 
61
            f.write('i ')
 
62
            f.write(' '.join(str(i) for i in mininc))
 
63
            f.write('\n')
66
64
        else:
67
 
            print >>f, 'i'
68
 
        print >>f, '1', weave._sha1s[version]
69
 
        print >>f, 'n', weave._names[version]
70
 
        print >>f
 
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')
71
69
 
72
 
    print >>f, 'w'
 
70
    f.write('w\n')
73
71
 
74
72
    for l in weave._weave:
75
73
        if isinstance(l, tuple):
76
 
            assert l[0] in '{}[]'
77
74
            if l[0] == '}':
78
 
                print >>f, '}'
 
75
                f.write('}\n')
79
76
            else:
80
 
                print >>f, '%s %d' % l
 
77
                f.write('%s %d\n' % l)
81
78
        else: # text line
82
79
            if not l:
83
 
                print >>f, ', '
 
80
                f.write(', \n')
84
81
            elif l[-1] == '\n':
85
 
                assert l.find('\n', 0, -1) == -1
86
 
                print >>f, '.', l,
 
82
                f.write('. ' + l)
87
83
            else:
88
 
                assert l.find('\n') == -1
89
 
                print >>f, ',', l
 
84
                f.write(', ' + l + '\n')
90
85
 
91
 
    print >>f, 'W'
 
86
    f.write('W\n')
92
87
 
93
88
 
94
89
 
103
98
 
104
99
def _read_weave_v5(f, w):
105
100
    """Private helper routine to read a weave format 5 file into memory.
106
 
    
 
101
 
107
102
    This is only to be used by read_weave and WeaveFile.__init__.
108
103
    """
109
104
    #  200   0   2075.5080   1084.0360   bzrlib.weavefile:104(_read_weave_v5)
119
114
    #  200   0    851.7250    501.1120   bzrlib.weavefile:104(_read_weave_v5)
120
115
    # +59363 0    311.8780    311.8780   +<method 'append' of 'list' objects>
121
116
    # +200   0     30.2500     30.2500   +<method 'readlines' of 'file' objects>
122
 
                  
 
117
 
123
118
    from weave import WeaveFormatError
124
119
 
125
 
    lines = iter(f.readlines())
126
 
    
 
120
    try:
 
121
        lines = iter(f.readlines())
 
122
    finally:
 
123
        f.close()
 
124
 
127
125
    try:
128
126
        l = lines.next()
129
127
    except StopIteration:
141
139
                w._parents.append(map(int, l[2:].split(' ')))
142
140
            else:
143
141
                w._parents.append([])
144
 
 
145
142
            l = lines.next()[:-1]
146
 
            assert '1 ' == l[0:2]
147
143
            w._sha1s.append(l[2:])
148
 
                
149
144
            l = lines.next()
150
 
            assert 'n ' == l[0:2]
151
145
            name = l[2:-1]
152
 
            assert name not in w._name_map
153
146
            w._names.append(name)
154
147
            w._name_map[name] = ver
155
 
                
156
148
            l = lines.next()
157
 
            assert l == '\n'
158
 
 
159
149
            ver += 1
160
150
        elif l == 'w\n':
161
151
            break
174
164
        elif l == '}\n':
175
165
            w._weave.append(('}', None))
176
166
        else:
177
 
            assert l[0] in '{[]', l
178
 
            assert l[1] == ' ', l
179
167
            w._weave.append((intern(l[0]), int(l[2:])))
180
 
 
181
168
    return w
182