27
27
from cStringIO import StringIO
30
from bzrlib.errors import (
30
from bzrlib.errors import TransportError, NoSuchFile, FileExists, LockError
37
31
from bzrlib.trace import mutter
38
from bzrlib.transport import (
32
from bzrlib.transport import (Transport, register_transport, Server)
44
33
import bzrlib.urlutils as urlutils
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
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)
131
self._files[_abspath] = (f.read(), mode)
160
133
def mkdir(self, relpath, mode=None):
161
134
"""See Transport.mkdir()."""
180
153
if _abspath != '/' and _abspath not in self._dirs:
181
154
raise NoSuchFile(relpath)
184
if not _abspath.endswith('/'):
187
for path_group in self._files, self._dirs:
188
for path in path_group:
189
if path.startswith(_abspath):
190
trailing = path[len(_abspath):]
191
if trailing and '/' not in trailing:
192
result.append(trailing)
156
for path in self._files:
157
if (path.startswith(_abspath) and
158
path[len(_abspath) + 1:].find('/') == -1 and
159
len(path) > len(_abspath)):
160
result.append(path[len(_abspath) + 1:])
161
for path in self._dirs:
162
if (path.startswith(_abspath) and
163
path[len(_abspath) + 1:].find('/') == -1 and
164
len(path) > len(_abspath) and
165
path[len(_abspath)] == '/'):
166
result.append(path[len(_abspath) + 1:])
193
167
return map(urlutils.escape, result)
195
169
def rename(self, rel_from, rel_to):
219
193
if _abspath in self._files:
220
194
self._translate_error(IOError(errno.ENOTDIR, relpath), relpath)
221
195
for path in self._files:
222
if path.startswith(_abspath + '/'):
196
if path.startswith(_abspath):
223
197
self._translate_error(IOError(errno.ENOTEMPTY, relpath),
225
199
for path in self._dirs:
226
if path.startswith(_abspath + '/') and path != _abspath:
200
if path.startswith(_abspath) and path != _abspath:
227
201
self._translate_error(IOError(errno.ENOTEMPTY, relpath), relpath)
228
202
if not _abspath in self._dirs:
229
203
raise NoSuchFile(relpath)