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
from bzrlib.errors import (
40
from bzrlib.transport import (
41
AppendBasedFileStream,
30
from bzrlib.errors import TransportError, NoSuchFile, FileExists, LockError
31
from bzrlib.trace import mutter
32
from bzrlib.transport import (Transport, register_transport, Server)
33
import bzrlib.urlutils as urlutils
80
69
def clone(self, offset=None):
81
70
"""See Transport.clone()."""
82
path = urlutils.URL._combine_paths(self._cwd, offset)
71
path = self._combine_paths(self._cwd, offset)
83
72
if len(path) == 0 or path[-1] != '/':
85
74
url = self._scheme + path
86
result = self.__class__(url)
75
result = MemoryTransport(url)
87
76
result._dirs = self._dirs
88
77
result._files = self._files
89
78
result._locks = self._locks
128
117
raise NoSuchFile(relpath)
129
118
del self._files[_abspath]
131
def external_url(self):
132
"""See bzrlib.transport.Transport.external_url."""
133
# MemoryTransport's are only accessible in-process
135
raise InProcessTransport(self)
137
120
def get(self, relpath):
138
121
"""See Transport.get()."""
139
122
_abspath = self._abspath(relpath)
140
123
if not _abspath in self._files:
141
if _abspath in self._dirs:
142
return LateReadError(relpath)
144
raise NoSuchFile(relpath)
124
raise NoSuchFile(relpath)
145
125
return StringIO(self._files[_abspath][0])
147
127
def put_file(self, relpath, f, mode=None):
148
128
"""See Transport.put_file()."""
149
129
_abspath = self._abspath(relpath)
150
130
self._check_parent(_abspath)
152
self._files[_abspath] = (raw_bytes, mode)
153
return len(raw_bytes)
131
self._files[_abspath] = (f.read(), mode)
155
133
def mkdir(self, relpath, mode=None):
156
134
"""See Transport.mkdir()."""
160
138
raise FileExists(relpath)
161
139
self._dirs[_abspath]=mode
163
def open_write_stream(self, relpath, mode=None):
164
"""See Transport.open_write_stream."""
165
self.put_bytes(relpath, "", mode)
166
result = AppendBasedFileStream(self, relpath)
167
_file_streams[self.abspath(relpath)] = result
170
141
def listable(self):
171
142
"""See Transport.listable."""
235
206
"""See Transport.stat()."""
236
207
_abspath = self._abspath(relpath)
237
208
if _abspath in self._files:
238
return MemoryStat(len(self._files[_abspath][0]), False,
209
return MemoryStat(len(self._files[_abspath][0]), False,
239
210
self._files[_abspath][1])
240
211
elif _abspath in self._dirs:
241
212
return MemoryStat(0, True, self._dirs[_abspath])
253
224
def _abspath(self, relpath):
254
225
"""Generate an internal absolute path."""
255
226
relpath = urlutils.unescape(relpath)
256
if relpath[:1] == '/':
227
if relpath.find('..') != -1:
228
raise AssertionError('relpath contains ..')
231
if relpath[0] == '/':
258
cwd_parts = self._cwd.split('/')
259
rel_parts = relpath.split('/')
261
for i in cwd_parts + rel_parts:
264
raise ValueError("illegal relpath %r under %r"
265
% (relpath, self._cwd))
267
elif i == '.' or i == '':
271
return '/' + '/'.join(r)
234
if (self._cwd == '/'):
236
return self._cwd[:-1]
237
if relpath.endswith('/'):
238
relpath = relpath[:-1]
239
if relpath.startswith('./'):
240
relpath = relpath[2:]
241
return self._cwd + relpath
274
244
class _MemoryLock(object):
275
245
"""This makes a lock."""
277
247
def __init__(self, path, transport):
248
assert isinstance(transport, MemoryTransport)
279
250
self.transport = transport
280
251
if self.path in self.transport._locks:
281
252
raise LockError('File %r already locked' % (self.path,))
282
253
self.transport._locks[self.path] = self
256
# Should this warn, or actually try to cleanup?
258
warnings.warn("MemoryLock %r not explicitly unlocked" % (self.path,))
284
261
def unlock(self):
285
262
del self.transport._locks[self.path]
286
263
self.transport = None
289
class MemoryServer(transport.Server):
266
class MemoryServer(Server):
290
267
"""Server for the MemoryTransport for testing with."""
292
def start_server(self):
270
"""See bzrlib.transport.Server.setUp."""
293
271
self._dirs = {'/':None}
296
274
self._scheme = "memory+%s:///" % id(self)
297
275
def memory_factory(url):
298
from bzrlib.transport import memory
299
result = memory.MemoryTransport(url)
276
result = MemoryTransport(url)
300
277
result._dirs = self._dirs
301
278
result._files = self._files
302
279
result._locks = self._locks
304
self._memory_factory = memory_factory
305
transport.register_transport(self._scheme, self._memory_factory)
281
register_transport(self._scheme, memory_factory)
307
def stop_server(self):
284
"""See bzrlib.transport.Server.tearDown."""
308
285
# unregister this server
309
transport.unregister_transport(self._scheme, self._memory_factory)
311
287
def get_url(self):
312
288
"""See bzrlib.transport.Server.get_url."""
313
289
return self._scheme
315
def get_bogus_url(self):
316
raise NotImplementedError
319
292
def get_test_permutations():
320
293
"""Return the permutations to be used in testing."""