~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/weavefile.py

  • Committer: John Arbash Meinel
  • Date: 2007-05-03 16:42:30 UTC
  • mto: This revision was merged to the branch mainline in revision 2481.
  • Revision ID: john@arbash-meinel.com-20070503164230-y0411liq6w3bphj0
Vastly improve bundle install performance by only validating the bundle one time

Show diffs side-by-side

added added

removed removed

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