~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/transport/memory.py

  • Committer: Launchpad Translations on behalf of bzr-core
  • Date: 2012-11-25 04:30:35 UTC
  • mto: (6581.1.1 trunk)
  • mto: This revision was merged to the branch mainline in revision 6582.
  • Revision ID: launchpad_translations_on_behalf_of_bzr-core-20121125043035-9abma98g1j6p5uox
Launchpad automatic translations update.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006 Canonical Ltd
 
1
# Copyright (C) 2005-2010 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
"""Implementation of Transport that uses memory for its storage.
18
18
 
20
20
so this is primarily useful for testing.
21
21
"""
22
22
 
 
23
from __future__ import absolute_import
 
24
 
23
25
import os
24
26
import errno
25
 
import re
26
27
from stat import S_IFREG, S_IFDIR
27
28
from cStringIO import StringIO
28
 
import warnings
29
29
 
30
 
from bzrlib.errors import TransportError, NoSuchFile, FileExists, LockError
31
 
from bzrlib.trace import mutter
 
30
from bzrlib import (
 
31
    transport,
 
32
    urlutils,
 
33
    )
 
34
from bzrlib.errors import (
 
35
    FileExists,
 
36
    LockError,
 
37
    InProcessTransport,
 
38
    NoSuchFile,
 
39
    )
32
40
from bzrlib.transport import (
 
41
    AppendBasedFileStream,
 
42
    _file_streams,
33
43
    LateReadError,
34
 
    register_transport,
35
 
    Server,
36
 
    Transport,
37
44
    )
38
 
import bzrlib.urlutils as urlutils
39
45
 
40
46
 
41
47
 
53
59
            self.st_mode = S_IFDIR | perms
54
60
 
55
61
 
56
 
class MemoryTransport(Transport):
 
62
class MemoryTransport(transport.Transport):
57
63
    """This is an in memory file system for transient data storage."""
58
64
 
59
65
    def __init__(self, url=""):
73
79
 
74
80
    def clone(self, offset=None):
75
81
        """See Transport.clone()."""
76
 
        path = self._combine_paths(self._cwd, offset)
 
82
        path = urlutils.URL._combine_paths(self._cwd, offset)
77
83
        if len(path) == 0 or path[-1] != '/':
78
84
            path += '/'
79
85
        url = self._scheme + path
80
 
        result = MemoryTransport(url)
 
86
        result = self.__class__(url)
81
87
        result._dirs = self._dirs
82
88
        result._files = self._files
83
89
        result._locks = self._locks
122
128
            raise NoSuchFile(relpath)
123
129
        del self._files[_abspath]
124
130
 
 
131
    def external_url(self):
 
132
        """See bzrlib.transport.Transport.external_url."""
 
133
        # MemoryTransport's are only accessible in-process
 
134
        # so we raise here
 
135
        raise InProcessTransport(self)
 
136
 
125
137
    def get(self, relpath):
126
138
        """See Transport.get()."""
127
139
        _abspath = self._abspath(relpath)
144
156
                'undefined', bytes, 0, 1,
145
157
                'put_file must be given a file of bytes, not unicode.')
146
158
        self._files[_abspath] = (bytes, mode)
 
159
        return len(bytes)
147
160
 
148
161
    def mkdir(self, relpath, mode=None):
149
162
        """See Transport.mkdir()."""
153
166
            raise FileExists(relpath)
154
167
        self._dirs[_abspath]=mode
155
168
 
 
169
    def open_write_stream(self, relpath, mode=None):
 
170
        """See Transport.open_write_stream."""
 
171
        self.put_bytes(relpath, "", mode)
 
172
        result = AppendBasedFileStream(self, relpath)
 
173
        _file_streams[self.abspath(relpath)] = result
 
174
        return result
 
175
 
156
176
    def listable(self):
157
177
        """See Transport.listable."""
158
178
        return True
161
181
        for file in self._files:
162
182
            if file.startswith(self._cwd):
163
183
                yield urlutils.escape(file[len(self._cwd):])
164
 
    
 
184
 
165
185
    def list_dir(self, relpath):
166
186
        """See Transport.list_dir()."""
167
187
        _abspath = self._abspath(relpath)
200
220
                    del container[path]
201
221
        do_renames(self._files)
202
222
        do_renames(self._dirs)
203
 
    
 
223
 
204
224
    def rmdir(self, relpath):
205
225
        """See Transport.rmdir."""
206
226
        _abspath = self._abspath(relpath)
221
241
        """See Transport.stat()."""
222
242
        _abspath = self._abspath(relpath)
223
243
        if _abspath in self._files:
224
 
            return MemoryStat(len(self._files[_abspath][0]), False, 
 
244
            return MemoryStat(len(self._files[_abspath][0]), False,
225
245
                              self._files[_abspath][1])
226
246
        elif _abspath in self._dirs:
227
247
            return MemoryStat(0, True, self._dirs[_abspath])
239
259
    def _abspath(self, relpath):
240
260
        """Generate an internal absolute path."""
241
261
        relpath = urlutils.unescape(relpath)
242
 
        if relpath.find('..') != -1:
243
 
            raise AssertionError('relpath contains ..')
244
 
        if relpath == '':
245
 
            return '/'
246
 
        if relpath[0] == '/':
 
262
        if relpath[:1] == '/':
247
263
            return relpath
248
 
        if relpath == '.':
249
 
            if (self._cwd == '/'):
250
 
                return self._cwd
251
 
            return self._cwd[:-1]
252
 
        if relpath.endswith('/'):
253
 
            relpath = relpath[:-1]
254
 
        if relpath.startswith('./'):
255
 
            relpath = relpath[2:]
256
 
        return self._cwd + relpath
 
264
        cwd_parts = self._cwd.split('/')
 
265
        rel_parts = relpath.split('/')
 
266
        r = []
 
267
        for i in cwd_parts + rel_parts:
 
268
            if i == '..':
 
269
                if not r:
 
270
                    raise ValueError("illegal relpath %r under %r"
 
271
                        % (relpath, self._cwd))
 
272
                r = r[:-1]
 
273
            elif i == '.' or i == '':
 
274
                pass
 
275
            else:
 
276
                r.append(i)
 
277
        return '/' + '/'.join(r)
257
278
 
258
279
 
259
280
class _MemoryLock(object):
260
281
    """This makes a lock."""
261
282
 
262
283
    def __init__(self, path, transport):
263
 
        assert isinstance(transport, MemoryTransport)
264
284
        self.path = path
265
285
        self.transport = transport
266
286
        if self.path in self.transport._locks:
267
287
            raise LockError('File %r already locked' % (self.path,))
268
288
        self.transport._locks[self.path] = self
269
289
 
270
 
    def __del__(self):
271
 
        # Should this warn, or actually try to cleanup?
272
 
        if self.transport:
273
 
            warnings.warn("MemoryLock %r not explicitly unlocked" % (self.path,))
274
 
            self.unlock()
275
 
 
276
290
    def unlock(self):
277
291
        del self.transport._locks[self.path]
278
292
        self.transport = None
279
293
 
280
294
 
281
 
class MemoryServer(Server):
 
295
class MemoryServer(transport.Server):
282
296
    """Server for the MemoryTransport for testing with."""
283
297
 
284
 
    def setUp(self):
285
 
        """See bzrlib.transport.Server.setUp."""
 
298
    def start_server(self):
286
299
        self._dirs = {'/':None}
287
300
        self._files = {}
288
301
        self._locks = {}
289
302
        self._scheme = "memory+%s:///" % id(self)
290
303
        def memory_factory(url):
291
 
            result = MemoryTransport(url)
 
304
            from bzrlib.transport import memory
 
305
            result = memory.MemoryTransport(url)
292
306
            result._dirs = self._dirs
293
307
            result._files = self._files
294
308
            result._locks = self._locks
295
309
            return result
296
 
        register_transport(self._scheme, memory_factory)
 
310
        self._memory_factory = memory_factory
 
311
        transport.register_transport(self._scheme, self._memory_factory)
297
312
 
298
 
    def tearDown(self):
299
 
        """See bzrlib.transport.Server.tearDown."""
 
313
    def stop_server(self):
300
314
        # unregister this server
 
315
        transport.unregister_transport(self._scheme, self._memory_factory)
301
316
 
302
317
    def get_url(self):
303
318
        """See bzrlib.transport.Server.get_url."""
304
319
        return self._scheme
305
320
 
 
321
    def get_bogus_url(self):
 
322
        raise NotImplementedError
 
323
 
306
324
 
307
325
def get_test_permutations():
308
326
    """Return the permutations to be used in testing."""