1
# Copyright (C) 2005-2010 Canonical Ltd
1
# Copyright (C) 2005, 2006 Canonical Ltd
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
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
17
17
"""Implementation of Transport that uses memory for its storage.
20
20
so this is primarily useful for testing.
23
from __future__ import absolute_import
27
26
from stat import S_IFREG, S_IFDIR
28
27
from cStringIO import StringIO
34
30
from bzrlib.errors import (
37
33
InProcessTransport,
37
from bzrlib.trace import mutter
40
38
from bzrlib.transport import (
41
39
AppendBasedFileStream,
46
import bzrlib.urlutils as urlutils
59
61
self.st_mode = S_IFDIR | perms
62
class MemoryTransport(transport.Transport):
64
class MemoryTransport(Transport):
63
65
"""This is an in memory file system for transient data storage."""
65
67
def __init__(self, url=""):
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] != '/':
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
181
183
for file in self._files:
182
184
if file.startswith(self._cwd):
183
185
yield urlutils.escape(file[len(self._cwd):])
185
187
def list_dir(self, relpath):
186
188
"""See Transport.list_dir()."""
187
189
_abspath = self._abspath(relpath)
241
243
"""See Transport.stat()."""
242
244
_abspath = self._abspath(relpath)
243
245
if _abspath in self._files:
244
return MemoryStat(len(self._files[_abspath][0]), False,
246
return MemoryStat(len(self._files[_abspath][0]), False,
245
247
self._files[_abspath][1])
246
248
elif _abspath in self._dirs:
247
249
return MemoryStat(0, True, self._dirs[_abspath])
281
283
"""This makes a lock."""
283
285
def __init__(self, path, transport):
286
assert isinstance(transport, MemoryTransport)
285
288
self.transport = transport
286
289
if self.path in self.transport._locks:
287
290
raise LockError('File %r already locked' % (self.path,))
288
291
self.transport._locks[self.path] = self
294
# Should this warn, or actually try to cleanup?
296
warnings.warn("MemoryLock %r not explicitly unlocked" % (self.path,))
290
299
def unlock(self):
291
300
del self.transport._locks[self.path]
292
301
self.transport = None
295
class MemoryServer(transport.Server):
304
class MemoryServer(Server):
296
305
"""Server for the MemoryTransport for testing with."""
298
def start_server(self):
308
"""See bzrlib.transport.Server.setUp."""
299
309
self._dirs = {'/':None}
302
312
self._scheme = "memory+%s:///" % id(self)
303
313
def memory_factory(url):
304
from bzrlib.transport import memory
305
result = memory.MemoryTransport(url)
314
result = MemoryTransport(url)
306
315
result._dirs = self._dirs
307
316
result._files = self._files
308
317
result._locks = self._locks
310
self._memory_factory = memory_factory
311
transport.register_transport(self._scheme, self._memory_factory)
319
register_transport(self._scheme, memory_factory)
313
def stop_server(self):
322
"""See bzrlib.transport.Server.tearDown."""
314
323
# unregister this server
315
transport.unregister_transport(self._scheme, self._memory_factory)
317
325
def get_url(self):
318
326
"""See bzrlib.transport.Server.get_url."""
319
327
return self._scheme
321
def get_bogus_url(self):
322
raise NotImplementedError
325
330
def get_test_permutations():
326
331
"""Return the permutations to be used in testing."""