~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/transport/memory.py

  • Committer: John Arbash Meinel
  • Date: 2008-03-20 15:10:05 UTC
  • mto: This revision was merged to the branch mainline in revision 3299.
  • Revision ID: john@arbash-meinel.com-20080320151005-z9lajjy69m20of17
uncommit --local in an unbound branch raises the same exception as commit --local

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])
275
283
    """This makes a lock."""
276
284
 
277
285
    def __init__(self, path, transport):
 
286
        assert isinstance(transport, MemoryTransport)
278
287
        self.path = path
279
288
        self.transport = transport
280
289
        if self.path in self.transport._locks:
281
290
            raise LockError('File %r already locked' % (self.path,))
282
291
        self.transport._locks[self.path] = self
283
292
 
 
293
    def __del__(self):
 
294
        # Should this warn, or actually try to cleanup?
 
295
        if self.transport:
 
296
            warnings.warn("MemoryLock %r not explicitly unlocked" % (self.path,))
 
297
            self.unlock()
 
298
 
284
299
    def unlock(self):
285
300
        del self.transport._locks[self.path]
286
301
        self.transport = None
287
302
 
288
303
 
289
 
class MemoryServer(transport.Server):
 
304
class MemoryServer(Server):
290
305
    """Server for the MemoryTransport for testing with."""
291
306
 
292
 
    def start_server(self):
 
307
    def setUp(self):
 
308
        """See bzrlib.transport.Server.setUp."""
293
309
        self._dirs = {'/':None}
294
310
        self._files = {}
295
311
        self._locks = {}
296
312
        self._scheme = "memory+%s:///" % id(self)
297
313
        def memory_factory(url):
298
 
            from bzrlib.transport import memory
299
 
            result = memory.MemoryTransport(url)
 
314
            result = MemoryTransport(url)
300
315
            result._dirs = self._dirs
301
316
            result._files = self._files
302
317
            result._locks = self._locks
303
318
            return result
304
 
        self._memory_factory = memory_factory
305
 
        transport.register_transport(self._scheme, self._memory_factory)
 
319
        register_transport(self._scheme, memory_factory)
306
320
 
307
 
    def stop_server(self):
 
321
    def tearDown(self):
 
322
        """See bzrlib.transport.Server.tearDown."""
308
323
        # unregister this server
309
 
        transport.unregister_transport(self._scheme, self._memory_factory)
310
324
 
311
325
    def get_url(self):
312
326
        """See bzrlib.transport.Server.get_url."""
313
327
        return self._scheme
314
328
 
315
 
    def get_bogus_url(self):
316
 
        raise NotImplementedError
317
 
 
318
329
 
319
330
def get_test_permutations():
320
331
    """Return the permutations to be used in testing."""