/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: Alexander Belchenko
  • Date: 2007-09-04 11:20:50 UTC
  • mto: This revision was merged to the branch mainline in revision 2800.
  • Revision ID: bialix@ukr.net-20070904112050-muj2o81u0xuaheoz
show path to python dll instead of bzr.exe for standalone application

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
30
from bzrlib.errors import (
35
31
    FileExists,
36
32
    LockError,
43
39
    AppendBasedFileStream,
44
40
    _file_streams,
45
41
    LateReadError,
 
42
    register_transport,
 
43
    Server,
 
44
    Transport,
46
45
    )
 
46
import bzrlib.urlutils as urlutils
47
47
 
48
48
 
49
49
 
61
61
            self.st_mode = S_IFDIR | perms
62
62
 
63
63
 
64
 
class MemoryTransport(transport.Transport):
 
64
class MemoryTransport(Transport):
65
65
    """This is an in memory file system for transient data storage."""
66
66
 
67
67
    def __init__(self, url=""):
85
85
        if len(path) == 0 or path[-1] != '/':
86
86
            path += '/'
87
87
        url = self._scheme + path
88
 
        result = self.__class__(url)
 
88
        result = MemoryTransport(url)
89
89
        result._dirs = self._dirs
90
90
        result._files = self._files
91
91
        result._locks = self._locks
158
158
                'undefined', bytes, 0, 1,
159
159
                'put_file must be given a file of bytes, not unicode.')
160
160
        self._files[_abspath] = (bytes, mode)
161
 
        return len(bytes)
162
161
 
163
162
    def mkdir(self, relpath, mode=None):
164
163
        """See Transport.mkdir()."""
183
182
        for file in self._files:
184
183
            if file.startswith(self._cwd):
185
184
                yield urlutils.escape(file[len(self._cwd):])
186
 
 
 
185
    
187
186
    def list_dir(self, relpath):
188
187
        """See Transport.list_dir()."""
189
188
        _abspath = self._abspath(relpath)
222
221
                    del container[path]
223
222
        do_renames(self._files)
224
223
        do_renames(self._dirs)
225
 
 
 
224
    
226
225
    def rmdir(self, relpath):
227
226
        """See Transport.rmdir."""
228
227
        _abspath = self._abspath(relpath)
243
242
        """See Transport.stat()."""
244
243
        _abspath = self._abspath(relpath)
245
244
        if _abspath in self._files:
246
 
            return MemoryStat(len(self._files[_abspath][0]), False,
 
245
            return MemoryStat(len(self._files[_abspath][0]), False, 
247
246
                              self._files[_abspath][1])
248
247
        elif _abspath in self._dirs:
249
248
            return MemoryStat(0, True, self._dirs[_abspath])
261
260
    def _abspath(self, relpath):
262
261
        """Generate an internal absolute path."""
263
262
        relpath = urlutils.unescape(relpath)
264
 
        if relpath[:1] == '/':
 
263
        if relpath.find('..') != -1:
 
264
            raise AssertionError('relpath contains ..')
 
265
        if relpath == '':
 
266
            return '/'
 
267
        if relpath[0] == '/':
265
268
            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)
 
269
        if relpath == '.':
 
270
            if (self._cwd == '/'):
 
271
                return self._cwd
 
272
            return self._cwd[:-1]
 
273
        if relpath.endswith('/'):
 
274
            relpath = relpath[:-1]
 
275
        if relpath.startswith('./'):
 
276
            relpath = relpath[2:]
 
277
        return self._cwd + relpath
280
278
 
281
279
 
282
280
class _MemoryLock(object):
283
281
    """This makes a lock."""
284
282
 
285
283
    def __init__(self, path, transport):
 
284
        assert isinstance(transport, MemoryTransport)
286
285
        self.path = path
287
286
        self.transport = transport
288
287
        if self.path in self.transport._locks:
300
299
        self.transport = None
301
300
 
302
301
 
303
 
class MemoryServer(transport.Server):
 
302
class MemoryServer(Server):
304
303
    """Server for the MemoryTransport for testing with."""
305
304
 
306
 
    def start_server(self):
 
305
    def setUp(self):
 
306
        """See bzrlib.transport.Server.setUp."""
307
307
        self._dirs = {'/':None}
308
308
        self._files = {}
309
309
        self._locks = {}
310
310
        self._scheme = "memory+%s:///" % id(self)
311
311
        def memory_factory(url):
312
 
            from bzrlib.transport import memory
313
 
            result = memory.MemoryTransport(url)
 
312
            result = MemoryTransport(url)
314
313
            result._dirs = self._dirs
315
314
            result._files = self._files
316
315
            result._locks = self._locks
317
316
            return result
318
 
        self._memory_factory = memory_factory
319
 
        transport.register_transport(self._scheme, self._memory_factory)
 
317
        register_transport(self._scheme, memory_factory)
320
318
 
321
 
    def stop_server(self):
 
319
    def tearDown(self):
 
320
        """See bzrlib.transport.Server.tearDown."""
322
321
        # unregister this server
323
 
        transport.unregister_transport(self._scheme, self._memory_factory)
324
322
 
325
323
    def get_url(self):
326
324
        """See bzrlib.transport.Server.get_url."""
327
325
        return self._scheme
328
326
 
329
 
    def get_bogus_url(self):
330
 
        raise NotImplementedError
331
 
 
332
327
 
333
328
def get_test_permutations():
334
329
    """Return the permutations to be used in testing."""