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
from bzrlib.errors import TransportError, NoSuchFile, FileExists, LockError
31
from bzrlib.trace import mutter
34
from bzrlib.errors import (
32
40
from bzrlib.transport import (
41
AppendBasedFileStream,
38
import bzrlib.urlutils as urlutils
74
80
def clone(self, offset=None):
75
81
"""See Transport.clone()."""
76
path = self._combine_paths(self._cwd, offset)
82
path = urlutils.URL._combine_paths(self._cwd, offset)
77
83
if len(path) == 0 or path[-1] != '/':
79
85
url = self._scheme + path
80
result = MemoryTransport(url)
86
result = self.__class__(url)
81
87
result._dirs = self._dirs
82
88
result._files = self._files
83
89
result._locks = self._locks
122
128
raise NoSuchFile(relpath)
123
129
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)
125
137
def get(self, relpath):
126
138
"""See Transport.get()."""
127
139
_abspath = self._abspath(relpath)
144
156
'undefined', bytes, 0, 1,
145
157
'put_file must be given a file of bytes, not unicode.')
146
158
self._files[_abspath] = (bytes, mode)
148
161
def mkdir(self, relpath, mode=None):
149
162
"""See Transport.mkdir()."""
153
166
raise FileExists(relpath)
154
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
156
176
def listable(self):
157
177
"""See Transport.listable."""
221
241
"""See Transport.stat()."""
222
242
_abspath = self._abspath(relpath)
223
243
if _abspath in self._files:
224
return MemoryStat(len(self._files[_abspath][0]), False,
244
return MemoryStat(len(self._files[_abspath][0]), False,
225
245
self._files[_abspath][1])
226
246
elif _abspath in self._dirs:
227
247
return MemoryStat(0, True, self._dirs[_abspath])
239
259
def _abspath(self, relpath):
240
260
"""Generate an internal absolute path."""
241
261
relpath = urlutils.unescape(relpath)
242
if relpath.find('..') != -1:
243
raise AssertionError('relpath contains ..')
246
if relpath[0] == '/':
262
if relpath[:1] == '/':
249
if (self._cwd == '/'):
251
return self._cwd[:-1]
252
if relpath.endswith('/'):
253
relpath = relpath[:-1]
254
if relpath.startswith('./'):
255
relpath = relpath[2:]
256
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)
259
280
class _MemoryLock(object):
260
281
"""This makes a lock."""
262
283
def __init__(self, path, transport):
263
assert isinstance(transport, MemoryTransport)
265
285
self.transport = transport
266
286
if self.path in self.transport._locks:
267
287
raise LockError('File %r already locked' % (self.path,))
268
288
self.transport._locks[self.path] = self
271
# Should this warn, or actually try to cleanup?
273
warnings.warn("MemoryLock %r not explicitly unlocked" % (self.path,))
276
290
def unlock(self):
277
291
del self.transport._locks[self.path]
278
292
self.transport = None
281
class MemoryServer(Server):
295
class MemoryServer(transport.Server):
282
296
"""Server for the MemoryTransport for testing with."""
285
"""See bzrlib.transport.Server.setUp."""
298
def start_server(self):
286
299
self._dirs = {'/':None}
289
302
self._scheme = "memory+%s:///" % id(self)
290
303
def memory_factory(url):
291
result = MemoryTransport(url)
304
from bzrlib.transport import memory
305
result = memory.MemoryTransport(url)
292
306
result._dirs = self._dirs
293
307
result._files = self._files
294
308
result._locks = self._locks
296
register_transport(self._scheme, memory_factory)
310
self._memory_factory = memory_factory
311
transport.register_transport(self._scheme, self._memory_factory)
299
"""See bzrlib.transport.Server.tearDown."""
313
def stop_server(self):
300
314
# unregister this server
315
transport.unregister_transport(self._scheme, self._memory_factory)
302
317
def get_url(self):
303
318
"""See bzrlib.transport.Server.get_url."""
304
319
return self._scheme
321
def get_bogus_url(self):
322
raise NotImplementedError
307
325
def get_test_permutations():
308
326
"""Return the permutations to be used in testing."""