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
30
from bzrlib.errors import (
37
33
InProcessTransport,
37
from bzrlib.trace import mutter
40
38
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 = urlutils.URL._combine_paths(self._cwd, offset)
82
path = self._combine_paths(self._cwd, offset)
83
83
if len(path) == 0 or path[-1] != '/':
85
85
url = self._scheme + path
86
result = self.__class__(url)
86
result = MemoryTransport(url)
87
87
result._dirs = self._dirs
88
88
result._files = self._files
89
89
result._locks = self._locks
148
148
"""See Transport.put_file()."""
149
149
_abspath = self._abspath(relpath)
150
150
self._check_parent(_abspath)
152
self._files[_abspath] = (raw_bytes, mode)
153
return len(raw_bytes)
152
if type(bytes) is not str:
153
# Although not strictly correct, we raise UnicodeEncodeError to be
154
# compatible with other transports.
155
raise UnicodeEncodeError(
156
'undefined', bytes, 0, 1,
157
'put_file must be given a file of bytes, not unicode.')
158
self._files[_abspath] = (bytes, mode)
155
160
def mkdir(self, relpath, mode=None):
156
161
"""See Transport.mkdir()."""
160
165
raise FileExists(relpath)
161
166
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
168
def listable(self):
171
169
"""See Transport.listable."""
235
233
"""See Transport.stat()."""
236
234
_abspath = self._abspath(relpath)
237
235
if _abspath in self._files:
238
return MemoryStat(len(self._files[_abspath][0]), False,
236
return MemoryStat(len(self._files[_abspath][0]), False,
239
237
self._files[_abspath][1])
240
238
elif _abspath in self._dirs:
241
239
return MemoryStat(0, True, self._dirs[_abspath])
253
251
def _abspath(self, relpath):
254
252
"""Generate an internal absolute path."""
255
253
relpath = urlutils.unescape(relpath)
256
if relpath[:1] == '/':
254
if relpath.find('..') != -1:
255
raise AssertionError('relpath contains ..')
258
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)
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
274
271
class _MemoryLock(object):
275
272
"""This makes a lock."""
277
274
def __init__(self, path, transport):
275
assert isinstance(transport, MemoryTransport)
279
277
self.transport = transport
280
278
if self.path in self.transport._locks:
281
279
raise LockError('File %r already locked' % (self.path,))
282
280
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,))
284
288
def unlock(self):
285
289
del self.transport._locks[self.path]
286
290
self.transport = None
289
class MemoryServer(transport.Server):
293
class MemoryServer(Server):
290
294
"""Server for the MemoryTransport for testing with."""
292
def start_server(self):
297
"""See bzrlib.transport.Server.setUp."""
293
298
self._dirs = {'/':None}
296
301
self._scheme = "memory+%s:///" % id(self)
297
302
def memory_factory(url):
298
from bzrlib.transport import memory
299
result = memory.MemoryTransport(url)
303
result = MemoryTransport(url)
300
304
result._dirs = self._dirs
301
305
result._files = self._files
302
306
result._locks = self._locks
304
self._memory_factory = memory_factory
305
transport.register_transport(self._scheme, self._memory_factory)
308
register_transport(self._scheme, memory_factory)
307
def stop_server(self):
311
"""See bzrlib.transport.Server.tearDown."""
308
312
# unregister this server
309
transport.unregister_transport(self._scheme, self._memory_factory)
311
314
def get_url(self):
312
315
"""See bzrlib.transport.Server.get_url."""
313
316
return self._scheme
315
def get_bogus_url(self):
316
raise NotImplementedError
319
319
def get_test_permutations():
320
320
"""Return the permutations to be used in testing."""