1
# Copyright (C) 2005, 2006 Canonical Ltd
1
# Copyright (C) 2005-2010 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
17
"""Implementation of Transport that uses memory for its storage.
61
61
self.st_mode = S_IFDIR | perms
64
class MemoryTransport(Transport):
64
class MemoryTransport(transport.Transport):
65
65
"""This is an in memory file system for transient data storage."""
67
67
def __init__(self, url=""):
82
82
def clone(self, offset=None):
83
83
"""See Transport.clone()."""
84
path = self._combine_paths(self._cwd, offset)
84
path = urlutils.URL._combine_paths(self._cwd, offset)
85
85
if len(path) == 0 or path[-1] != '/':
87
87
url = self._scheme + path
88
result = MemoryTransport(url)
88
result = self.__class__(url)
89
89
result._dirs = self._dirs
90
90
result._files = self._files
91
91
result._locks = self._locks
183
183
for file in self._files:
184
184
if file.startswith(self._cwd):
185
185
yield urlutils.escape(file[len(self._cwd):])
187
187
def list_dir(self, relpath):
188
188
"""See Transport.list_dir()."""
189
189
_abspath = self._abspath(relpath)
243
243
"""See Transport.stat()."""
244
244
_abspath = self._abspath(relpath)
245
245
if _abspath in self._files:
246
return MemoryStat(len(self._files[_abspath][0]), False,
246
return MemoryStat(len(self._files[_abspath][0]), False,
247
247
self._files[_abspath][1])
248
248
elif _abspath in self._dirs:
249
249
return MemoryStat(0, True, self._dirs[_abspath])
283
283
"""This makes a lock."""
285
285
def __init__(self, path, transport):
286
assert isinstance(transport, MemoryTransport)
288
287
self.transport = transport
289
288
if self.path in self.transport._locks:
290
289
raise LockError('File %r already locked' % (self.path,))
291
290
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,))
299
292
def unlock(self):
300
293
del self.transport._locks[self.path]
301
294
self.transport = None
304
class MemoryServer(Server):
297
class MemoryServer(transport.Server):
305
298
"""Server for the MemoryTransport for testing with."""
308
"""See bzrlib.transport.Server.setUp."""
300
def start_server(self):
309
301
self._dirs = {'/':None}
312
304
self._scheme = "memory+%s:///" % id(self)
313
305
def memory_factory(url):
314
result = MemoryTransport(url)
306
from bzrlib.transport import memory
307
result = memory.MemoryTransport(url)
315
308
result._dirs = self._dirs
316
309
result._files = self._files
317
310
result._locks = self._locks
319
register_transport(self._scheme, memory_factory)
312
self._memory_factory = memory_factory
313
transport.register_transport(self._scheme, self._memory_factory)
322
"""See bzrlib.transport.Server.tearDown."""
315
def stop_server(self):
323
316
# unregister this server
317
transport.unregister_transport(self._scheme, self._memory_factory)
325
319
def get_url(self):
326
320
"""See bzrlib.transport.Server.get_url."""
327
321
return self._scheme
323
def get_bogus_url(self):
324
raise NotImplementedError
330
327
def get_test_permutations():
331
328
"""Return the permutations to be used in testing."""