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.
27
27
from cStringIO import StringIO
34
from bzrlib.errors import (
30
from bzrlib.errors import TransportError, NoSuchFile, FileExists, LockError
41
31
from bzrlib.trace import mutter
42
from bzrlib.transport import (
43
AppendBasedFileStream,
32
from bzrlib.transport import (Transport, register_transport, Server)
33
import bzrlib.urlutils as urlutils
82
69
def clone(self, offset=None):
83
70
"""See Transport.clone()."""
84
path = urlutils.URL._combine_paths(self._cwd, offset)
71
path = self._combine_paths(self._cwd, offset)
85
72
if len(path) == 0 or path[-1] != '/':
87
74
url = self._scheme + path
88
result = self.__class__(url)
75
result = MemoryTransport(url)
89
76
result._dirs = self._dirs
90
77
result._files = self._files
91
78
result._locks = self._locks
130
117
raise NoSuchFile(relpath)
131
118
del self._files[_abspath]
133
def external_url(self):
134
"""See bzrlib.transport.Transport.external_url."""
135
# MemoryTransport's are only accessible in-process
137
raise InProcessTransport(self)
139
120
def get(self, relpath):
140
121
"""See Transport.get()."""
141
122
_abspath = self._abspath(relpath)
142
123
if not _abspath in self._files:
143
if _abspath in self._dirs:
144
return LateReadError(relpath)
146
raise NoSuchFile(relpath)
124
raise NoSuchFile(relpath)
147
125
return StringIO(self._files[_abspath][0])
149
127
def put_file(self, relpath, f, mode=None):
150
128
"""See Transport.put_file()."""
151
129
_abspath = self._abspath(relpath)
152
130
self._check_parent(_abspath)
154
if type(bytes) is not str:
155
# Although not strictly correct, we raise UnicodeEncodeError to be
156
# compatible with other transports.
157
raise UnicodeEncodeError(
158
'undefined', bytes, 0, 1,
159
'put_file must be given a file of bytes, not unicode.')
160
self._files[_abspath] = (bytes, mode)
131
self._files[_abspath] = (f.read(), mode)
163
133
def mkdir(self, relpath, mode=None):
164
134
"""See Transport.mkdir()."""
168
138
raise FileExists(relpath)
169
139
self._dirs[_abspath]=mode
171
def open_write_stream(self, relpath, mode=None):
172
"""See Transport.open_write_stream."""
173
self.put_bytes(relpath, "", mode)
174
result = AppendBasedFileStream(self, relpath)
175
_file_streams[self.abspath(relpath)] = result
178
141
def listable(self):
179
142
"""See Transport.listable."""
222
185
del container[path]
223
186
do_renames(self._files)
224
187
do_renames(self._dirs)
226
189
def rmdir(self, relpath):
227
190
"""See Transport.rmdir."""
228
191
_abspath = self._abspath(relpath)
229
192
if _abspath in self._files:
230
193
self._translate_error(IOError(errno.ENOTDIR, relpath), relpath)
231
194
for path in self._files:
232
if path.startswith(_abspath + '/'):
195
if path.startswith(_abspath):
233
196
self._translate_error(IOError(errno.ENOTEMPTY, relpath),
235
198
for path in self._dirs:
236
if path.startswith(_abspath + '/') and path != _abspath:
199
if path.startswith(_abspath) and path != _abspath:
237
200
self._translate_error(IOError(errno.ENOTEMPTY, relpath), relpath)
238
201
if not _abspath in self._dirs:
239
202
raise NoSuchFile(relpath)
243
206
"""See Transport.stat()."""
244
207
_abspath = self._abspath(relpath)
245
208
if _abspath in self._files:
246
return MemoryStat(len(self._files[_abspath][0]), False,
209
return MemoryStat(len(self._files[_abspath][0]), False,
247
210
self._files[_abspath][1])
248
211
elif _abspath in self._dirs:
249
212
return MemoryStat(0, True, self._dirs[_abspath])
261
224
def _abspath(self, relpath):
262
225
"""Generate an internal absolute path."""
263
226
relpath = urlutils.unescape(relpath)
264
if relpath[:1] == '/':
227
if relpath.find('..') != -1:
228
raise AssertionError('relpath contains ..')
231
if relpath[0] == '/':
266
cwd_parts = self._cwd.split('/')
267
rel_parts = relpath.split('/')
269
for i in cwd_parts + rel_parts:
272
raise ValueError("illegal relpath %r under %r"
273
% (relpath, self._cwd))
275
elif i == '.' or i == '':
279
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
282
244
class _MemoryLock(object):
283
245
"""This makes a lock."""
285
247
def __init__(self, path, transport):
248
assert isinstance(transport, MemoryTransport)
287
250
self.transport = transport
288
251
if self.path in self.transport._locks:
289
252
raise LockError('File %r already locked' % (self.path,))
290
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,))
292
261
def unlock(self):
293
262
del self.transport._locks[self.path]
294
263
self.transport = None
297
class MemoryServer(transport.Server):
266
class MemoryServer(Server):
298
267
"""Server for the MemoryTransport for testing with."""
300
def start_server(self):
270
"""See bzrlib.transport.Server.setUp."""
301
271
self._dirs = {'/':None}
304
274
self._scheme = "memory+%s:///" % id(self)
305
275
def memory_factory(url):
306
from bzrlib.transport import memory
307
result = memory.MemoryTransport(url)
276
result = MemoryTransport(url)
308
277
result._dirs = self._dirs
309
278
result._files = self._files
310
279
result._locks = self._locks
312
self._memory_factory = memory_factory
313
transport.register_transport(self._scheme, self._memory_factory)
281
register_transport(self._scheme, memory_factory)
315
def stop_server(self):
284
"""See bzrlib.transport.Server.tearDown."""
316
285
# unregister this server
317
transport.unregister_transport(self._scheme, self._memory_factory)
319
287
def get_url(self):
320
288
"""See bzrlib.transport.Server.get_url."""
321
289
return self._scheme
323
def get_bogus_url(self):
324
raise NotImplementedError
327
292
def get_test_permutations():
328
293
"""Return the permutations to be used in testing."""