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.transport import Transport, \
24
TransportError, NoSuchFile, FileExists
27
class MemoryStat(object):
29
def __init__(self, size):
33
class MemoryTransport(Transport):
34
"""This is the transport agent for local filesystem access."""
37
"""Set the 'base' path where files will be stored."""
38
super(MemoryTransport, self).__init__('in-memory:')
42
def clone(self, offset=None):
43
"""See Transport.clone()."""
46
def abspath(self, relpath):
47
"""See Transport.abspath()."""
48
return self.base + relpath
50
def append(self, relpath, f):
51
"""See Transport.append()."""
52
self._check_parent(relpath)
53
self._files[relpath] = self._files.get(relpath, "") + f.read()
55
def _check_parent(self, relpath):
56
dir = os.path.dirname(relpath)
58
if not dir in self._dirs:
59
raise NoSuchFile(relpath)
61
def has(self, relpath):
62
"""See Transport.has()."""
63
return relpath in self._files
65
def get(self, relpath):
66
"""See Transport.get()."""
67
if not relpath in self._files:
68
raise NoSuchFile(relpath)
69
return StringIO(self._files[relpath])
71
def put(self, relpath, f):
72
"""See Transport.put()."""
73
self._check_parent(relpath)
74
self._files[relpath] = f.read()
76
def mkdir(self, relpath):
77
"""See Transport.mkdir()."""
78
self._check_parent(relpath)
79
if relpath in self._dirs:
80
raise FileExists(relpath)
81
self._dirs.add(relpath)
84
"""See Transport.listable."""
87
def iter_files_recursive(self):
88
return iter(self._files)
90
# def list_dir(self, relpath):
93
def stat(self, relpath):
94
"""See Transport.stat()."""
95
return MemoryStat(len(self._files[relpath]))
97
# def lock_read(self, relpath):
100
# def lock_write(self, relpath):