~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/transport/memory.py

  • Committer: Robert Collins
  • Date: 2007-04-19 02:27:44 UTC
  • mto: This revision was merged to the branch mainline in revision 2426.
  • Revision ID: robertc@robertcollins.net-20070419022744-pfdqz42kp1wizh43
``make docs`` now creates a man page at ``man1/bzr.1`` fixing bug 107388.
(Robert Collins)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Canonical Ltd
 
1
# Copyright (C) 2005, 2006 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
12
12
#
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
16
16
 
17
17
"""Implementation of Transport that uses memory for its storage.
18
18
 
27
27
from cStringIO import StringIO
28
28
import warnings
29
29
 
30
 
from bzrlib import (
31
 
    urlutils,
32
 
    )
33
 
from bzrlib.errors import (
34
 
    FileExists,
35
 
    LockError,
36
 
    InProcessTransport,
37
 
    NoSuchFile,
38
 
    TransportError,
39
 
    )
 
30
from bzrlib.errors import TransportError, NoSuchFile, FileExists, LockError
40
31
from bzrlib.trace import mutter
41
 
from bzrlib.transport import (
42
 
    AppendBasedFileStream,
43
 
    _file_streams,
44
 
    LateReadError,
45
 
    register_transport,
46
 
    Server,
47
 
    Transport,
48
 
    unregister_transport,
49
 
    )
 
32
from bzrlib.transport import (Transport, register_transport, Server)
 
33
import bzrlib.urlutils as urlutils
50
34
 
51
35
 
52
36
 
88
72
        if len(path) == 0 or path[-1] != '/':
89
73
            path += '/'
90
74
        url = self._scheme + path
91
 
        result = self.__class__(url)
 
75
        result = MemoryTransport(url)
92
76
        result._dirs = self._dirs
93
77
        result._files = self._files
94
78
        result._locks = self._locks
133
117
            raise NoSuchFile(relpath)
134
118
        del self._files[_abspath]
135
119
 
136
 
    def external_url(self):
137
 
        """See bzrlib.transport.Transport.external_url."""
138
 
        # MemoryTransport's are only accessible in-process
139
 
        # so we raise here
140
 
        raise InProcessTransport(self)
141
 
 
142
120
    def get(self, relpath):
143
121
        """See Transport.get()."""
144
122
        _abspath = self._abspath(relpath)
145
123
        if not _abspath in self._files:
146
 
            if _abspath in self._dirs:
147
 
                return LateReadError(relpath)
148
 
            else:
149
 
                raise NoSuchFile(relpath)
 
124
            raise NoSuchFile(relpath)
150
125
        return StringIO(self._files[_abspath][0])
151
126
 
152
127
    def put_file(self, relpath, f, mode=None):
161
136
                'undefined', bytes, 0, 1,
162
137
                'put_file must be given a file of bytes, not unicode.')
163
138
        self._files[_abspath] = (bytes, mode)
164
 
        return len(bytes)
165
139
 
166
140
    def mkdir(self, relpath, mode=None):
167
141
        """See Transport.mkdir()."""
171
145
            raise FileExists(relpath)
172
146
        self._dirs[_abspath]=mode
173
147
 
174
 
    def open_write_stream(self, relpath, mode=None):
175
 
        """See Transport.open_write_stream."""
176
 
        self.put_bytes(relpath, "", mode)
177
 
        result = AppendBasedFileStream(self, relpath)
178
 
        _file_streams[self.abspath(relpath)] = result
179
 
        return result
180
 
 
181
148
    def listable(self):
182
149
        """See Transport.listable."""
183
150
        return True
186
153
        for file in self._files:
187
154
            if file.startswith(self._cwd):
188
155
                yield urlutils.escape(file[len(self._cwd):])
189
 
 
 
156
    
190
157
    def list_dir(self, relpath):
191
158
        """See Transport.list_dir()."""
192
159
        _abspath = self._abspath(relpath)
225
192
                    del container[path]
226
193
        do_renames(self._files)
227
194
        do_renames(self._dirs)
228
 
 
 
195
    
229
196
    def rmdir(self, relpath):
230
197
        """See Transport.rmdir."""
231
198
        _abspath = self._abspath(relpath)
246
213
        """See Transport.stat()."""
247
214
        _abspath = self._abspath(relpath)
248
215
        if _abspath in self._files:
249
 
            return MemoryStat(len(self._files[_abspath][0]), False,
 
216
            return MemoryStat(len(self._files[_abspath][0]), False, 
250
217
                              self._files[_abspath][1])
251
218
        elif _abspath in self._dirs:
252
219
            return MemoryStat(0, True, self._dirs[_abspath])
264
231
    def _abspath(self, relpath):
265
232
        """Generate an internal absolute path."""
266
233
        relpath = urlutils.unescape(relpath)
267
 
        if relpath[:1] == '/':
 
234
        if relpath.find('..') != -1:
 
235
            raise AssertionError('relpath contains ..')
 
236
        if relpath == '':
 
237
            return '/'
 
238
        if relpath[0] == '/':
268
239
            return relpath
269
 
        cwd_parts = self._cwd.split('/')
270
 
        rel_parts = relpath.split('/')
271
 
        r = []
272
 
        for i in cwd_parts + rel_parts:
273
 
            if i == '..':
274
 
                if not r:
275
 
                    raise ValueError("illegal relpath %r under %r"
276
 
                        % (relpath, self._cwd))
277
 
                r = r[:-1]
278
 
            elif i == '.' or i == '':
279
 
                pass
280
 
            else:
281
 
                r.append(i)
282
 
        return '/' + '/'.join(r)
 
240
        if relpath == '.':
 
241
            if (self._cwd == '/'):
 
242
                return self._cwd
 
243
            return self._cwd[:-1]
 
244
        if relpath.endswith('/'):
 
245
            relpath = relpath[:-1]
 
246
        if relpath.startswith('./'):
 
247
            relpath = relpath[2:]
 
248
        return self._cwd + relpath
283
249
 
284
250
 
285
251
class _MemoryLock(object):
286
252
    """This makes a lock."""
287
253
 
288
254
    def __init__(self, path, transport):
 
255
        assert isinstance(transport, MemoryTransport)
289
256
        self.path = path
290
257
        self.transport = transport
291
258
        if self.path in self.transport._locks:
306
273
class MemoryServer(Server):
307
274
    """Server for the MemoryTransport for testing with."""
308
275
 
309
 
    def start_server(self):
 
276
    def setUp(self):
 
277
        """See bzrlib.transport.Server.setUp."""
310
278
        self._dirs = {'/':None}
311
279
        self._files = {}
312
280
        self._locks = {}
317
285
            result._files = self._files
318
286
            result._locks = self._locks
319
287
            return result
320
 
        self._memory_factory = memory_factory
321
 
        register_transport(self._scheme, self._memory_factory)
 
288
        register_transport(self._scheme, memory_factory)
322
289
 
323
 
    def stop_server(self):
 
290
    def tearDown(self):
 
291
        """See bzrlib.transport.Server.tearDown."""
324
292
        # unregister this server
325
 
        unregister_transport(self._scheme, self._memory_factory)
326
293
 
327
294
    def get_url(self):
328
295
        """See bzrlib.transport.Server.get_url."""