/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« 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-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
 
    transport,
32
 
    urlutils,
33
 
    )
34
 
from bzrlib.errors import (
35
 
    FileExists,
36
 
    LockError,
37
 
    InProcessTransport,
38
 
    NoSuchFile,
39
 
    TransportError,
40
 
    )
 
30
from bzrlib.errors import TransportError, NoSuchFile, FileExists, LockError
41
31
from bzrlib.trace import mutter
42
 
from bzrlib.transport import (
43
 
    AppendBasedFileStream,
44
 
    _file_streams,
45
 
    LateReadError,
46
 
    )
 
32
from bzrlib.transport import (Transport, register_transport, Server)
 
33
import bzrlib.urlutils as urlutils
47
34
 
48
35
 
49
36
 
61
48
            self.st_mode = S_IFDIR | perms
62
49
 
63
50
 
64
 
class MemoryTransport(transport.Transport):
 
51
class MemoryTransport(Transport):
65
52
    """This is an in memory file system for transient data storage."""
66
53
 
67
54
    def __init__(self, url=""):
85
72
        if len(path) == 0 or path[-1] != '/':
86
73
            path += '/'
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]
132
119
 
133
 
    def external_url(self):
134
 
        """See bzrlib.transport.Transport.external_url."""
135
 
        # MemoryTransport's are only accessible in-process
136
 
        # so we raise here
137
 
        raise InProcessTransport(self)
138
 
 
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)
145
 
            else:
146
 
                raise NoSuchFile(relpath)
 
124
            raise NoSuchFile(relpath)
147
125
        return StringIO(self._files[_abspath][0])
148
126
 
149
127
    def put_file(self, relpath, f, mode=None):
158
136
                'undefined', bytes, 0, 1,
159
137
                'put_file must be given a file of bytes, not unicode.')
160
138
        self._files[_abspath] = (bytes, mode)
161
 
        return len(bytes)
162
139
 
163
140
    def mkdir(self, relpath, mode=None):
164
141
        """See Transport.mkdir()."""
168
145
            raise FileExists(relpath)
169
146
        self._dirs[_abspath]=mode
170
147
 
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
176
 
        return result
177
 
 
178
148
    def listable(self):
179
149
        """See Transport.listable."""
180
150
        return True
183
153
        for file in self._files:
184
154
            if file.startswith(self._cwd):
185
155
                yield urlutils.escape(file[len(self._cwd):])
186
 
 
 
156
    
187
157
    def list_dir(self, relpath):
188
158
        """See Transport.list_dir()."""
189
159
        _abspath = self._abspath(relpath)
222
192
                    del container[path]
223
193
        do_renames(self._files)
224
194
        do_renames(self._dirs)
225
 
 
 
195
    
226
196
    def rmdir(self, relpath):
227
197
        """See Transport.rmdir."""
228
198
        _abspath = self._abspath(relpath)
243
213
        """See Transport.stat()."""
244
214
        _abspath = self._abspath(relpath)
245
215
        if _abspath in self._files:
246
 
            return MemoryStat(len(self._files[_abspath][0]), False,
 
216
            return MemoryStat(len(self._files[_abspath][0]), False, 
247
217
                              self._files[_abspath][1])
248
218
        elif _abspath in self._dirs:
249
219
            return MemoryStat(0, True, self._dirs[_abspath])
261
231
    def _abspath(self, relpath):
262
232
        """Generate an internal absolute path."""
263
233
        relpath = urlutils.unescape(relpath)
264
 
        if relpath[:1] == '/':
 
234
        if relpath.find('..') != -1:
 
235
            raise AssertionError('relpath contains ..')
 
236
        if relpath == '':
 
237
            return '/'
 
238
        if relpath[0] == '/':
265
239
            return relpath
266
 
        cwd_parts = self._cwd.split('/')
267
 
        rel_parts = relpath.split('/')
268
 
        r = []
269
 
        for i in cwd_parts + rel_parts:
270
 
            if i == '..':
271
 
                if not r:
272
 
                    raise ValueError("illegal relpath %r under %r"
273
 
                        % (relpath, self._cwd))
274
 
                r = r[:-1]
275
 
            elif i == '.' or i == '':
276
 
                pass
277
 
            else:
278
 
                r.append(i)
279
 
        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
280
249
 
281
250
 
282
251
class _MemoryLock(object):
283
252
    """This makes a lock."""
284
253
 
285
254
    def __init__(self, path, transport):
 
255
        assert isinstance(transport, MemoryTransport)
286
256
        self.path = path
287
257
        self.transport = transport
288
258
        if self.path in self.transport._locks:
300
270
        self.transport = None
301
271
 
302
272
 
303
 
class MemoryServer(transport.Server):
 
273
class MemoryServer(Server):
304
274
    """Server for the MemoryTransport for testing with."""
305
275
 
306
 
    def start_server(self):
 
276
    def setUp(self):
 
277
        """See bzrlib.transport.Server.setUp."""
307
278
        self._dirs = {'/':None}
308
279
        self._files = {}
309
280
        self._locks = {}
310
281
        self._scheme = "memory+%s:///" % id(self)
311
282
        def memory_factory(url):
312
 
            from bzrlib.transport import memory
313
 
            result = memory.MemoryTransport(url)
 
283
            result = MemoryTransport(url)
314
284
            result._dirs = self._dirs
315
285
            result._files = self._files
316
286
            result._locks = self._locks
317
287
            return result
318
 
        self._memory_factory = memory_factory
319
 
        transport.register_transport(self._scheme, self._memory_factory)
 
288
        register_transport(self._scheme, memory_factory)
320
289
 
321
 
    def stop_server(self):
 
290
    def tearDown(self):
 
291
        """See bzrlib.transport.Server.tearDown."""
322
292
        # unregister this server
323
 
        transport.unregister_transport(self._scheme, self._memory_factory)
324
293
 
325
294
    def get_url(self):
326
295
        """See bzrlib.transport.Server.get_url."""
327
296
        return self._scheme
328
297
 
329
 
    def get_bogus_url(self):
330
 
        raise NotImplementedError
331
 
 
332
298
 
333
299
def get_test_permutations():
334
300
    """Return the permutations to be used in testing."""