~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/transport/memory.py

  • Committer: John Arbash Meinel
  • Author(s): Mark Hammond
  • Date: 2008-09-09 17:02:21 UTC
  • mto: This revision was merged to the branch mainline in revision 3697.
  • Revision ID: john@arbash-meinel.com-20080909170221-svim3jw2mrz0amp3
An updated transparent icon for bzr.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2011, 2016 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
 
20
20
so this is primarily useful for testing.
21
21
"""
22
22
 
23
 
from __future__ import absolute_import
24
 
 
25
23
import os
26
24
import errno
 
25
import re
27
26
from stat import S_IFREG, S_IFDIR
28
27
from cStringIO import StringIO
 
28
import warnings
29
29
 
30
 
from bzrlib import (
31
 
    transport,
32
 
    urlutils,
33
 
    )
34
30
from bzrlib.errors import (
35
31
    FileExists,
36
32
    LockError,
37
33
    InProcessTransport,
38
34
    NoSuchFile,
 
35
    TransportError,
39
36
    )
 
37
from bzrlib.trace import mutter
40
38
from bzrlib.transport import (
41
39
    AppendBasedFileStream,
42
40
    _file_streams,
43
41
    LateReadError,
 
42
    register_transport,
 
43
    Server,
 
44
    Transport,
44
45
    )
 
46
import bzrlib.urlutils as urlutils
45
47
 
46
48
 
47
49
 
59
61
            self.st_mode = S_IFDIR | perms
60
62
 
61
63
 
62
 
class MemoryTransport(transport.Transport):
 
64
class MemoryTransport(Transport):
63
65
    """This is an in memory file system for transient data storage."""
64
66
 
65
67
    def __init__(self, url=""):
79
81
 
80
82
    def clone(self, offset=None):
81
83
        """See Transport.clone()."""
82
 
        path = urlutils.URL._combine_paths(self._cwd, offset)
 
84
        path = self._combine_paths(self._cwd, offset)
83
85
        if len(path) == 0 or path[-1] != '/':
84
86
            path += '/'
85
87
        url = self._scheme + path
86
 
        result = self.__class__(url)
 
88
        result = MemoryTransport(url)
87
89
        result._dirs = self._dirs
88
90
        result._files = self._files
89
91
        result._locks = self._locks
148
150
        """See Transport.put_file()."""
149
151
        _abspath = self._abspath(relpath)
150
152
        self._check_parent(_abspath)
151
 
        raw_bytes = f.read()
152
 
        self._files[_abspath] = (raw_bytes, mode)
153
 
        return len(raw_bytes)
 
153
        bytes = f.read()
 
154
        if type(bytes) is not str:
 
155
            # Although not strictly correct, we raise UnicodeEncodeError to be
 
156
            # compatible with other transports.
 
157
            raise UnicodeEncodeError(
 
158
                'undefined', bytes, 0, 1,
 
159
                'put_file must be given a file of bytes, not unicode.')
 
160
        self._files[_abspath] = (bytes, mode)
 
161
        return len(bytes)
154
162
 
155
163
    def mkdir(self, relpath, mode=None):
156
164
        """See Transport.mkdir()."""
175
183
        for file in self._files:
176
184
            if file.startswith(self._cwd):
177
185
                yield urlutils.escape(file[len(self._cwd):])
178
 
 
 
186
    
179
187
    def list_dir(self, relpath):
180
188
        """See Transport.list_dir()."""
181
189
        _abspath = self._abspath(relpath)
214
222
                    del container[path]
215
223
        do_renames(self._files)
216
224
        do_renames(self._dirs)
217
 
 
 
225
    
218
226
    def rmdir(self, relpath):
219
227
        """See Transport.rmdir."""
220
228
        _abspath = self._abspath(relpath)
235
243
        """See Transport.stat()."""
236
244
        _abspath = self._abspath(relpath)
237
245
        if _abspath in self._files:
238
 
            return MemoryStat(len(self._files[_abspath][0]), False,
 
246
            return MemoryStat(len(self._files[_abspath][0]), False, 
239
247
                              self._files[_abspath][1])
240
248
        elif _abspath in self._dirs:
241
249
            return MemoryStat(0, True, self._dirs[_abspath])
281
289
            raise LockError('File %r already locked' % (self.path,))
282
290
        self.transport._locks[self.path] = self
283
291
 
 
292
    def __del__(self):
 
293
        # Should this warn, or actually try to cleanup?
 
294
        if self.transport:
 
295
            warnings.warn("MemoryLock %r not explicitly unlocked" % (self.path,))
 
296
            self.unlock()
 
297
 
284
298
    def unlock(self):
285
299
        del self.transport._locks[self.path]
286
300
        self.transport = None
287
301
 
288
302
 
289
 
class MemoryServer(transport.Server):
 
303
class MemoryServer(Server):
290
304
    """Server for the MemoryTransport for testing with."""
291
305
 
292
 
    def start_server(self):
 
306
    def setUp(self):
 
307
        """See bzrlib.transport.Server.setUp."""
293
308
        self._dirs = {'/':None}
294
309
        self._files = {}
295
310
        self._locks = {}
296
311
        self._scheme = "memory+%s:///" % id(self)
297
312
        def memory_factory(url):
298
 
            from bzrlib.transport import memory
299
 
            result = memory.MemoryTransport(url)
 
313
            result = MemoryTransport(url)
300
314
            result._dirs = self._dirs
301
315
            result._files = self._files
302
316
            result._locks = self._locks
303
317
            return result
304
 
        self._memory_factory = memory_factory
305
 
        transport.register_transport(self._scheme, self._memory_factory)
 
318
        register_transport(self._scheme, memory_factory)
306
319
 
307
 
    def stop_server(self):
 
320
    def tearDown(self):
 
321
        """See bzrlib.transport.Server.tearDown."""
308
322
        # unregister this server
309
 
        transport.unregister_transport(self._scheme, self._memory_factory)
310
323
 
311
324
    def get_url(self):
312
325
        """See bzrlib.transport.Server.get_url."""
313
326
        return self._scheme
314
327
 
315
 
    def get_bogus_url(self):
316
 
        raise NotImplementedError
317
 
 
318
328
 
319
329
def get_test_permutations():
320
330
    """Return the permutations to be used in testing."""