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.
20
20
so this is primarily useful for testing.
23
from __future__ import absolute_import
26
27
from stat import S_IFREG, S_IFDIR
27
28
from cStringIO import StringIO
30
34
from bzrlib.errors import (
33
37
InProcessTransport,
37
from bzrlib.trace import mutter
38
40
from bzrlib.transport import (
41
AppendBasedFileStream,
44
import bzrlib.urlutils as urlutils
80
80
def clone(self, offset=None):
81
81
"""See Transport.clone()."""
82
path = self._combine_paths(self._cwd, offset)
82
path = urlutils.URL._combine_paths(self._cwd, offset)
83
83
if len(path) == 0 or path[-1] != '/':
85
85
url = self._scheme + path
86
result = MemoryTransport(url)
86
result = self.__class__(url)
87
87
result._dirs = self._dirs
88
88
result._files = self._files
89
89
result._locks = self._locks
156
156
'undefined', bytes, 0, 1,
157
157
'put_file must be given a file of bytes, not unicode.')
158
158
self._files[_abspath] = (bytes, mode)
160
161
def mkdir(self, relpath, mode=None):
161
162
"""See Transport.mkdir()."""
165
166
raise FileExists(relpath)
166
167
self._dirs[_abspath]=mode
169
def open_write_stream(self, relpath, mode=None):
170
"""See Transport.open_write_stream."""
171
self.put_bytes(relpath, "", mode)
172
result = AppendBasedFileStream(self, relpath)
173
_file_streams[self.abspath(relpath)] = result
168
176
def listable(self):
169
177
"""See Transport.listable."""
233
241
"""See Transport.stat()."""
234
242
_abspath = self._abspath(relpath)
235
243
if _abspath in self._files:
236
return MemoryStat(len(self._files[_abspath][0]), False,
244
return MemoryStat(len(self._files[_abspath][0]), False,
237
245
self._files[_abspath][1])
238
246
elif _abspath in self._dirs:
239
247
return MemoryStat(0, True, self._dirs[_abspath])
251
259
def _abspath(self, relpath):
252
260
"""Generate an internal absolute path."""
253
261
relpath = urlutils.unescape(relpath)
254
if relpath.find('..') != -1:
255
raise AssertionError('relpath contains ..')
258
if relpath[0] == '/':
262
if relpath[:1] == '/':
261
if (self._cwd == '/'):
263
return self._cwd[:-1]
264
if relpath.endswith('/'):
265
relpath = relpath[:-1]
266
if relpath.startswith('./'):
267
relpath = relpath[2:]
268
return self._cwd + relpath
264
cwd_parts = self._cwd.split('/')
265
rel_parts = relpath.split('/')
267
for i in cwd_parts + rel_parts:
270
raise ValueError("illegal relpath %r under %r"
271
% (relpath, self._cwd))
273
elif i == '.' or i == '':
277
return '/' + '/'.join(r)
271
280
class _MemoryLock(object):
272
281
"""This makes a lock."""
274
283
def __init__(self, path, transport):
275
assert isinstance(transport, MemoryTransport)
277
285
self.transport = transport
278
286
if self.path in self.transport._locks:
279
287
raise LockError('File %r already locked' % (self.path,))
280
288
self.transport._locks[self.path] = self
283
# Should this warn, or actually try to cleanup?
285
warnings.warn("MemoryLock %r not explicitly unlocked" % (self.path,))
288
290
def unlock(self):
289
291
del self.transport._locks[self.path]
290
292
self.transport = None
293
class MemoryServer(Server):
295
class MemoryServer(transport.Server):
294
296
"""Server for the MemoryTransport for testing with."""
297
"""See bzrlib.transport.Server.setUp."""
298
def start_server(self):
298
299
self._dirs = {'/':None}
301
302
self._scheme = "memory+%s:///" % id(self)
302
303
def memory_factory(url):
303
result = MemoryTransport(url)
304
from bzrlib.transport import memory
305
result = memory.MemoryTransport(url)
304
306
result._dirs = self._dirs
305
307
result._files = self._files
306
308
result._locks = self._locks
308
register_transport(self._scheme, memory_factory)
310
self._memory_factory = memory_factory
311
transport.register_transport(self._scheme, self._memory_factory)
311
"""See bzrlib.transport.Server.tearDown."""
313
def stop_server(self):
312
314
# unregister this server
315
transport.unregister_transport(self._scheme, self._memory_factory)
314
317
def get_url(self):
315
318
"""See bzrlib.transport.Server.get_url."""
316
319
return self._scheme
321
def get_bogus_url(self):
322
raise NotImplementedError
319
325
def get_test_permutations():
320
326
"""Return the permutations to be used in testing."""