~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/transport/memory.py

  • Committer: Vincent Ladeuil
  • Date: 2007-07-18 09:43:41 UTC
  • mto: (2778.5.1 vila)
  • mto: This revision was merged to the branch mainline in revision 2789.
  • Revision ID: v.ladeuil+lp@free.fr-20070718094341-edmgsog3el06yqow
Add performance analysis of missing.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2010 Canonical Ltd
 
1
# Copyright (C) 2005, 2006 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
"""Implementation of Transport that uses memory for its storage.
18
18
 
27
27
from cStringIO import StringIO
28
28
import warnings
29
29
 
30
 
from bzrlib import (
31
 
    transport,
32
 
    urlutils,
33
 
    )
34
 
from bzrlib.errors import (
35
 
    FileExists,
36
 
    LockError,
37
 
    InProcessTransport,
38
 
    NoSuchFile,
39
 
    TransportError,
40
 
    )
 
30
from bzrlib.errors import TransportError, NoSuchFile, FileExists, LockError
41
31
from bzrlib.trace import mutter
42
32
from bzrlib.transport import (
43
 
    AppendBasedFileStream,
44
 
    _file_streams,
45
33
    LateReadError,
 
34
    register_transport,
 
35
    Server,
 
36
    Transport,
46
37
    )
 
38
import bzrlib.urlutils as urlutils
47
39
 
48
40
 
49
41
 
61
53
            self.st_mode = S_IFDIR | perms
62
54
 
63
55
 
64
 
class MemoryTransport(transport.Transport):
 
56
class MemoryTransport(Transport):
65
57
    """This is an in memory file system for transient data storage."""
66
58
 
67
59
    def __init__(self, url=""):
85
77
        if len(path) == 0 or path[-1] != '/':
86
78
            path += '/'
87
79
        url = self._scheme + path
88
 
        result = self.__class__(url)
 
80
        result = MemoryTransport(url)
89
81
        result._dirs = self._dirs
90
82
        result._files = self._files
91
83
        result._locks = self._locks
130
122
            raise NoSuchFile(relpath)
131
123
        del self._files[_abspath]
132
124
 
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
125
    def get(self, relpath):
140
126
        """See Transport.get()."""
141
127
        _abspath = self._abspath(relpath)
158
144
                'undefined', bytes, 0, 1,
159
145
                'put_file must be given a file of bytes, not unicode.')
160
146
        self._files[_abspath] = (bytes, mode)
161
 
        return len(bytes)
162
147
 
163
148
    def mkdir(self, relpath, mode=None):
164
149
        """See Transport.mkdir()."""
168
153
            raise FileExists(relpath)
169
154
        self._dirs[_abspath]=mode
170
155
 
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
156
    def listable(self):
179
157
        """See Transport.listable."""
180
158
        return True
183
161
        for file in self._files:
184
162
            if file.startswith(self._cwd):
185
163
                yield urlutils.escape(file[len(self._cwd):])
186
 
 
 
164
    
187
165
    def list_dir(self, relpath):
188
166
        """See Transport.list_dir()."""
189
167
        _abspath = self._abspath(relpath)
222
200
                    del container[path]
223
201
        do_renames(self._files)
224
202
        do_renames(self._dirs)
225
 
 
 
203
    
226
204
    def rmdir(self, relpath):
227
205
        """See Transport.rmdir."""
228
206
        _abspath = self._abspath(relpath)
243
221
        """See Transport.stat()."""
244
222
        _abspath = self._abspath(relpath)
245
223
        if _abspath in self._files:
246
 
            return MemoryStat(len(self._files[_abspath][0]), False,
 
224
            return MemoryStat(len(self._files[_abspath][0]), False, 
247
225
                              self._files[_abspath][1])
248
226
        elif _abspath in self._dirs:
249
227
            return MemoryStat(0, True, self._dirs[_abspath])
261
239
    def _abspath(self, relpath):
262
240
        """Generate an internal absolute path."""
263
241
        relpath = urlutils.unescape(relpath)
264
 
        if relpath[:1] == '/':
 
242
        if relpath.find('..') != -1:
 
243
            raise AssertionError('relpath contains ..')
 
244
        if relpath == '':
 
245
            return '/'
 
246
        if relpath[0] == '/':
265
247
            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)
 
248
        if relpath == '.':
 
249
            if (self._cwd == '/'):
 
250
                return self._cwd
 
251
            return self._cwd[:-1]
 
252
        if relpath.endswith('/'):
 
253
            relpath = relpath[:-1]
 
254
        if relpath.startswith('./'):
 
255
            relpath = relpath[2:]
 
256
        return self._cwd + relpath
280
257
 
281
258
 
282
259
class _MemoryLock(object):
283
260
    """This makes a lock."""
284
261
 
285
262
    def __init__(self, path, transport):
 
263
        assert isinstance(transport, MemoryTransport)
286
264
        self.path = path
287
265
        self.transport = transport
288
266
        if self.path in self.transport._locks:
300
278
        self.transport = None
301
279
 
302
280
 
303
 
class MemoryServer(transport.Server):
 
281
class MemoryServer(Server):
304
282
    """Server for the MemoryTransport for testing with."""
305
283
 
306
 
    def start_server(self):
 
284
    def setUp(self):
 
285
        """See bzrlib.transport.Server.setUp."""
307
286
        self._dirs = {'/':None}
308
287
        self._files = {}
309
288
        self._locks = {}
310
289
        self._scheme = "memory+%s:///" % id(self)
311
290
        def memory_factory(url):
312
 
            from bzrlib.transport import memory
313
 
            result = memory.MemoryTransport(url)
 
291
            result = MemoryTransport(url)
314
292
            result._dirs = self._dirs
315
293
            result._files = self._files
316
294
            result._locks = self._locks
317
295
            return result
318
 
        self._memory_factory = memory_factory
319
 
        transport.register_transport(self._scheme, self._memory_factory)
 
296
        register_transport(self._scheme, memory_factory)
320
297
 
321
 
    def stop_server(self):
 
298
    def tearDown(self):
 
299
        """See bzrlib.transport.Server.tearDown."""
322
300
        # unregister this server
323
 
        transport.unregister_transport(self._scheme, self._memory_factory)
324
301
 
325
302
    def get_url(self):
326
303
        """See bzrlib.transport.Server.get_url."""
327
304
        return self._scheme
328
305
 
329
 
    def get_bogus_url(self):
330
 
        raise NotImplementedError
331
 
 
332
306
 
333
307
def get_test_permutations():
334
308
    """Return the permutations to be used in testing."""