/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/weavefile.py

  • Committer: v.ladeuil+lp at free
  • Date: 2006-10-12 14:29:32 UTC
  • mto: (2145.1.1 keepalive)
  • mto: This revision was merged to the branch mainline in revision 2146.
  • Revision ID: v.ladeuil+lp@free.fr-20061012142932-7221fe16d2b48fa3
Shuffle http related test code. Hopefully it ends up at the right place :)

* bzrlib/tests/HttpServer.py: 
New file. bzrlib.tests.ChrootedTestCase use HttpServer. So the
class can't be defined in bzrlib.tests.HTTPUtils because it
creates a circular dependency (bzrlib.tests.HTTPUtils needs to
import bzrlib.tests).

* bzrlib/transport/http/_urllib.py: 
Transfer test server definition to bzrlib.tests.HttpServer. Clean
up imports.

* bzrlib/transport/http/_pycurl.py: 
Transfer test server definition to bzrlib.tests.HttpServer. Clean
up imports.

* bzrlib/transport/http/__init__.py: 
Transfer all test related code to either bzrlib.tests.HttpServer
and bzrlib.tests.HTTPUtils.
Fix all use of TransportNotPossible and InvalidURL by prefixing it
by 'errors.' (this seems to be the preferred way in the rest of
bzr).
Get rid of unused imports.

* bzrlib/tests/test_transport.py:
(ReadonlyDecoratorTransportTest.test_local_parameters,
FakeNFSDecoratorTests.test_http_parameters): Use HttpServer from
bzrlib.tests.HttpServer instead of bzrlib.transport.http.

* bzrlib/tests/test_sftp_transport.py:
(set_test_transport_to_sftp): Use HttpServer from
bzrlib.tests.HttpServer instead of bzrlib.transport.http.

* bzrlib/tests/test_selftest.py:
(TestTestCaseWithTransport.test_get_readonly_url_http): Use
HttpServer from bzrlib.tests.HttpServer instead of
bzrlib.transport.http.

* bzrlib/tests/test_repository.py: 
Does *not* use HttpServer.

* bzrlib/tests/test_http.py: 
Build on top of bzrlib.tests.HttpServer and bzrlib.tests.HTTPUtils
instead of bzrlib.transport.http.

* bzrlib/tests/test_bzrdir.py:
(ChrootedTests.setUp): Use HttpServer from bzrlib.tests.HttpServer
instead of bzrlib.transport.http.

* bzrlib/tests/branch_implementations/test_http.py:
(HTTPBranchTests.setUp): Use HttpServer from bzrlib.tests.HttpServer
instead of bzrlib.transport.http.

* bzrlib/tests/branch_implementations/test_branch.py:
(ChrootedTests.setUp): Use HttpServer from bzrlib.tests.HttpServer
instead of bzrlib.transport.http.

* bzrlib/tests/__init__.py:
(ChrootedTestCase.setUp): Use HttpServer from
bzrlib.tests.HttpServer instead of bzrlib.transport.http.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#! /usr/bin/python
2
 
 
3
1
# Copyright (C) 2005 Canonical Ltd
4
 
 
 
2
#
5
3
# This program is free software; you can redistribute it and/or modify
6
4
# it under the terms of the GNU General Public License as published by
7
5
# the Free Software Foundation; either version 2 of the License, or
8
6
# (at your option) any later version.
9
 
 
 
7
#
10
8
# This program is distributed in the hope that it will be useful,
11
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
11
# GNU General Public License for more details.
14
 
 
 
12
#
15
13
# You should have received a copy of the GNU General Public License
16
14
# along with this program; if not, write to the Free Software
17
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
 
 
 
16
#
19
17
# Author: Martin Pool <mbp@canonical.com>
20
18
 
21
19
 
26
24
There is one format marker followed by a blank line, followed by a
27
25
series of version headers, followed by the weave itself.
28
26
 
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.
 
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.
32
34
 
33
35
The weave is bracketed by 'w' and 'W' lines, and includes the '{}[]'
34
36
processing instructions.  Lines of text are prefixed by '.' if the
36
38
"""
37
39
 
38
40
# TODO: When extracting a single version it'd be enough to just pass
39
 
# an iterator returning the weave lines...
 
41
# an iterator returning the weave lines...  We don't really need to
 
42
# deserialize it into memory.
40
43
 
41
 
FORMAT_1 = '# bzr weave file v3\n'
 
44
FORMAT_1 = '# bzr weave file v5\n'
42
45
 
43
46
 
44
47
def write_weave(weave, f, format=None):
45
 
    if format == None or format == 1:
46
 
        return write_weave_v1(weave, f)
 
48
    if format is None or format == 1:
 
49
        return write_weave_v5(weave, f)
47
50
    else:
48
51
        raise ValueError("unknown weave format %r" % format)
49
52
 
50
53
 
51
 
def write_weave_v1(weave, f):
 
54
def write_weave_v5(weave, f):
52
55
    """Write weave to file f."""
53
56
    print >>f, FORMAT_1,
54
57
 
63
66
        else:
64
67
            print >>f, 'i'
65
68
        print >>f, '1', weave._sha1s[version]
 
69
        print >>f, 'n', weave._names[version]
66
70
        print >>f
67
71
 
68
72
    print >>f, 'w'
70
74
    for l in weave._weave:
71
75
        if isinstance(l, tuple):
72
76
            assert l[0] in '{}[]'
73
 
            print >>f, '%s %d' % l
 
77
            if l[0] == '}':
 
78
                print >>f, '}'
 
79
            else:
 
80
                print >>f, '%s %d' % l
74
81
        else: # text line
75
82
            if not l:
76
83
                print >>f, ', '
86
93
 
87
94
 
88
95
def read_weave(f):
89
 
    return read_weave_v1(f)
90
 
 
91
 
 
92
 
def read_weave_v1(f):
93
 
    from weave import Weave, WeaveFormatError
94
 
    w = Weave()
95
 
 
96
 
    wfe = WeaveFormatError
97
 
    l = f.readline()
 
96
    # FIXME: detect the weave type and dispatch
 
97
    from bzrlib.trace import mutter
 
98
    from weave import Weave
 
99
    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
 
98
132
    if l != FORMAT_1:
99
133
        raise WeaveFormatError('invalid weave file header: %r' % l)
100
134
 
101
135
    ver = 0
 
136
    # read weave header.
102
137
    while True:
103
 
        l = f.readline()
 
138
        l = lines.next()
104
139
        if l[0] == 'i':
105
 
            ver += 1
106
 
 
107
140
            if len(l) > 2:
108
 
                w._parents.append(frozenset(map(int, l[2:].split(' '))))
 
141
                w._parents.append(map(int, l[2:].split(' ')))
109
142
            else:
110
 
                w._parents.append(frozenset())
 
143
                w._parents.append([])
111
144
 
112
 
            l = f.readline()[:-1]
113
 
            assert l.startswith('1 ')
 
145
            l = lines.next()[:-1]
 
146
            assert '1 ' == l[0:2]
114
147
            w._sha1s.append(l[2:])
115
148
                
116
 
            l = f.readline()
 
149
            l = lines.next()
 
150
            assert 'n ' == l[0:2]
 
151
            name = l[2:-1]
 
152
            assert name not in w._name_map
 
153
            w._names.append(name)
 
154
            w._name_map[name] = ver
 
155
                
 
156
            l = lines.next()
117
157
            assert l == '\n'
 
158
 
 
159
            ver += 1
118
160
        elif l == 'w\n':
119
161
            break
120
162
        else:
121
163
            raise WeaveFormatError('unexpected line %r' % l)
122
164
 
 
165
    # read weave body
123
166
    while True:
124
 
        l = f.readline()
 
167
        l = lines.next()
125
168
        if l == 'W\n':
126
169
            break
127
 
        elif l.startswith('. '):
 
170
        elif '. ' == l[0:2]:
128
171
            w._weave.append(l[2:])  # include newline
129
 
        elif l.startswith(', '):
 
172
        elif ', ' == l[0:2]:
130
173
            w._weave.append(l[2:-1])        # exclude newline
 
174
        elif l == '}\n':
 
175
            w._weave.append(('}', None))
131
176
        else:
132
 
            assert l[0] in '{}[]', l
 
177
            assert l[0] in '{[]', l
133
178
            assert l[1] == ' ', l
134
179
            w._weave.append((intern(l[0]), int(l[2:])))
135
180