~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/transport/memory.py

  • Committer: John Arbash Meinel
  • Date: 2007-05-04 18:59:36 UTC
  • mto: This revision was merged to the branch mainline in revision 2643.
  • Revision ID: john@arbash-meinel.com-20070504185936-1mjdoqmtz74xe5mg
A C implementation of _fields_to_entry_0_parents drops the time from 400ms to 330ms for a 21k-entry tree

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
from cStringIO import StringIO
28
28
import warnings
29
29
 
30
 
from bzrlib.errors import (
31
 
    FileExists,
32
 
    LockError,
33
 
    InProcessTransport,
34
 
    NoSuchFile,
35
 
    TransportError,
36
 
    )
 
30
from bzrlib.errors import TransportError, NoSuchFile, FileExists, LockError
37
31
from bzrlib.trace import mutter
38
 
from bzrlib.transport import (
39
 
    AppendBasedFileStream,
40
 
    _file_streams,
41
 
    LateReadError,
42
 
    register_transport,
43
 
    Server,
44
 
    Transport,
45
 
    )
 
32
from bzrlib.transport import (Transport, register_transport, Server)
46
33
import bzrlib.urlutils as urlutils
47
34
 
48
35
 
130
117
            raise NoSuchFile(relpath)
131
118
        del self._files[_abspath]
132
119
 
133
 
    def external_url(self):
134
 
        """See bzrlib.transport.Transport.external_url."""
135
 
        # MemoryTransport's are only accessible in-process
136
 
        # so we raise here
137
 
        raise InProcessTransport(self)
138
 
 
139
120
    def get(self, relpath):
140
121
        """See Transport.get()."""
141
122
        _abspath = self._abspath(relpath)
142
123
        if not _abspath in self._files:
143
 
            if _abspath in self._dirs:
144
 
                return LateReadError(relpath)
145
 
            else:
146
 
                raise NoSuchFile(relpath)
 
124
            raise NoSuchFile(relpath)
147
125
        return StringIO(self._files[_abspath][0])
148
126
 
149
127
    def put_file(self, relpath, f, mode=None):
158
136
                'undefined', bytes, 0, 1,
159
137
                'put_file must be given a file of bytes, not unicode.')
160
138
        self._files[_abspath] = (bytes, mode)
161
 
        return len(bytes)
162
139
 
163
140
    def mkdir(self, relpath, mode=None):
164
141
        """See Transport.mkdir()."""
168
145
            raise FileExists(relpath)
169
146
        self._dirs[_abspath]=mode
170
147
 
171
 
    def open_write_stream(self, relpath, mode=None):
172
 
        """See Transport.open_write_stream."""
173
 
        self.put_bytes(relpath, "", mode)
174
 
        result = AppendBasedFileStream(self, relpath)
175
 
        _file_streams[self.abspath(relpath)] = result
176
 
        return result
177
 
 
178
148
    def listable(self):
179
149
        """See Transport.listable."""
180
150
        return True
261
231
    def _abspath(self, relpath):
262
232
        """Generate an internal absolute path."""
263
233
        relpath = urlutils.unescape(relpath)
264
 
        if relpath[:1] == '/':
 
234
        if relpath.find('..') != -1:
 
235
            raise AssertionError('relpath contains ..')
 
236
        if relpath == '':
 
237
            return '/'
 
238
        if relpath[0] == '/':
265
239
            return relpath
266
 
        cwd_parts = self._cwd.split('/')
267
 
        rel_parts = relpath.split('/')
268
 
        r = []
269
 
        for i in cwd_parts + rel_parts:
270
 
            if i == '..':
271
 
                if not r:
272
 
                    raise ValueError("illegal relpath %r under %r"
273
 
                        % (relpath, self._cwd))
274
 
                r = r[:-1]
275
 
            elif i == '.' or i == '':
276
 
                pass
277
 
            else:
278
 
                r.append(i)
279
 
        return '/' + '/'.join(r)
 
240
        if relpath == '.':
 
241
            if (self._cwd == '/'):
 
242
                return self._cwd
 
243
            return self._cwd[:-1]
 
244
        if relpath.endswith('/'):
 
245
            relpath = relpath[:-1]
 
246
        if relpath.startswith('./'):
 
247
            relpath = relpath[2:]
 
248
        return self._cwd + relpath
280
249
 
281
250
 
282
251
class _MemoryLock(object):