1
# Copyright (C) 2005 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
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
16
"""Implementation of Transport that uses memory for its storage."""
20
from cStringIO import StringIO
22
from bzrlib.trace import mutter
23
from bzrlib.errors import TransportError, NoSuchFile, FileExists
24
from bzrlib.transport import Transport
26
class MemoryStat(object):
28
def __init__(self, size):
32
class MemoryTransport(Transport):
33
"""This is the transport agent for local filesystem access."""
36
"""Set the 'base' path where files will be stored."""
37
super(MemoryTransport, self).__init__('in-memory:')
41
def clone(self, offset=None):
42
"""See Transport.clone()."""
45
def abspath(self, relpath):
46
"""See Transport.abspath()."""
47
return self.base + relpath
49
def append(self, relpath, f):
50
"""See Transport.append()."""
51
self._check_parent(relpath)
52
self._files[relpath] = self._files.get(relpath, "") + f.read()
54
def _check_parent(self, relpath):
55
dir = os.path.dirname(relpath)
57
if not dir in self._dirs:
58
raise NoSuchFile(relpath)
60
def has(self, relpath):
61
"""See Transport.has()."""
62
return relpath in self._files
64
def get(self, relpath):
65
"""See Transport.get()."""
66
if not relpath in self._files:
67
raise NoSuchFile(relpath)
68
return StringIO(self._files[relpath])
70
def put(self, relpath, f, mode=None):
71
"""See Transport.put()."""
72
self._check_parent(relpath)
73
self._files[relpath] = f.read()
75
def mkdir(self, relpath, mode=None):
76
"""See Transport.mkdir()."""
77
self._check_parent(relpath)
78
if relpath in self._dirs:
79
raise FileExists(relpath)
80
self._dirs.add(relpath)
83
"""See Transport.listable."""
86
def iter_files_recursive(self):
87
return iter(self._files)
89
# def list_dir(self, relpath):
92
def stat(self, relpath):
93
"""See Transport.stat()."""
94
return MemoryStat(len(self._files[relpath]))
96
# def lock_read(self, relpath):
99
# def lock_write(self, relpath):