~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/weavefile.py

- improved handling of non-ascii branch names and test
  patch from Joel Rosdahl

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
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
5
5
# the Free Software Foundation; either version 2 of the License, or
6
6
# (at your option) any later version.
7
 
#
 
7
 
8
8
# This program is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
11
# GNU General Public License for more details.
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
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
19
 
45
45
 
46
46
 
47
47
def write_weave(weave, f, format=None):
48
 
    if format is None or format == 1:
 
48
    if format == None or format == 1:
49
49
        return write_weave_v5(weave, f)
50
50
    else:
51
51
        raise ValueError("unknown weave format %r" % format)
93
93
 
94
94
 
95
95
def read_weave(f):
96
 
    # FIXME: detect the weave type and dispatch
97
 
    from bzrlib.trace import mutter
98
 
    from weave import Weave
 
96
    return read_weave_v5(f)
 
97
 
 
98
 
 
99
def read_weave_v5(f):
 
100
    from weave import Weave, WeaveFormatError
99
101
    w = Weave(getattr(f, 'name', None))
100
 
    _read_weave_v5(f, w)
101
 
    return w
102
 
 
103
 
 
104
 
def _read_weave_v5(f, w):
105
 
    """Private helper routine to read a weave format 5 file into memory.
106
 
    
107
 
    This is only to be used by read_weave and WeaveFile.__init__.
108
 
    """
109
 
    #  200   0   2075.5080   1084.0360   bzrlib.weavefile:104(_read_weave_v5)
110
 
    # +60412 0    366.5900    366.5900   +<method 'readline' of 'file' objects>
111
 
    # +59982 0    320.5280    320.5280   +<method 'startswith' of 'str' objects>
112
 
    # +59363 0    297.8080    297.8080   +<method 'append' of 'list' objects>
113
 
    # replace readline call with iter over all lines ->
114
 
    # safe because we already suck on memory.
115
 
    #  200   0   1492.7170    802.6220   bzrlib.weavefile:104(_read_weave_v5)
116
 
    # +59982 0    329.9100    329.9100   +<method 'startswith' of 'str' objects>
117
 
    # +59363 0    320.2980    320.2980   +<method 'append' of 'list' objects>
118
 
    # replaced startswith with slice lookups:
119
 
    #  200   0    851.7250    501.1120   bzrlib.weavefile:104(_read_weave_v5)
120
 
    # +59363 0    311.8780    311.8780   +<method 'append' of 'list' objects>
121
 
    # +200   0     30.2500     30.2500   +<method 'readlines' of 'file' objects>
122
 
                  
123
 
    from weave import WeaveFormatError
124
 
 
125
 
    lines = iter(f.readlines())
126
 
    
127
 
    try:
128
 
        l = lines.next()
129
 
    except StopIteration:
130
 
        raise WeaveFormatError('invalid weave file: no header')
131
 
 
 
102
 
 
103
    l = f.readline()
132
104
    if l != FORMAT_1:
133
105
        raise WeaveFormatError('invalid weave file header: %r' % l)
134
106
 
135
107
    ver = 0
136
 
    # read weave header.
137
108
    while True:
138
 
        l = lines.next()
 
109
        l = f.readline()
139
110
        if l[0] == 'i':
140
111
            if len(l) > 2:
141
112
                w._parents.append(map(int, l[2:].split(' ')))
142
113
            else:
143
114
                w._parents.append([])
144
115
 
145
 
            l = lines.next()[:-1]
146
 
            assert '1 ' == l[0:2]
 
116
            l = f.readline()[:-1]
 
117
            assert l.startswith('1 ')
147
118
            w._sha1s.append(l[2:])
148
119
                
149
 
            l = lines.next()
150
 
            assert 'n ' == l[0:2]
 
120
            l = f.readline()
 
121
            assert l.startswith('n ')
151
122
            name = l[2:-1]
152
123
            assert name not in w._name_map
153
124
            w._names.append(name)
154
125
            w._name_map[name] = ver
155
126
                
156
 
            l = lines.next()
 
127
            l = f.readline()
157
128
            assert l == '\n'
158
129
 
159
130
            ver += 1
162
133
        else:
163
134
            raise WeaveFormatError('unexpected line %r' % l)
164
135
 
165
 
    # read weave body
166
136
    while True:
167
 
        l = lines.next()
 
137
        l = f.readline()
168
138
        if l == 'W\n':
169
139
            break
170
 
        elif '. ' == l[0:2]:
 
140
        elif l.startswith('. '):
171
141
            w._weave.append(l[2:])  # include newline
172
 
        elif ', ' == l[0:2]:
 
142
        elif l.startswith(', '):
173
143
            w._weave.append(l[2:-1])        # exclude newline
174
144
        elif l == '}\n':
175
145
            w._weave.append(('}', None))