/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 breezy/transport/memory.py

  • Committer: Jelmer Vernooij
  • Date: 2018-04-02 00:52:27 UTC
  • mfrom: (6939 work)
  • mto: This revision was merged to the branch mainline in revision 7274.
  • Revision ID: jelmer@jelmer.uk-20180402005227-pecflp1mvdjrjqd6
Merge trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2010 Canonical Ltd
 
1
# Copyright (C) 2005-2011, 2016 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
20
20
so this is primarily useful for testing.
21
21
"""
22
22
 
 
23
from __future__ import absolute_import
 
24
 
 
25
import contextlib
23
26
import os
24
27
import errno
25
 
import re
26
28
from stat import S_IFREG, S_IFDIR
27
 
from cStringIO import StringIO
28
 
import warnings
29
29
 
30
 
from bzrlib import (
 
30
from .. import (
31
31
    transport,
32
32
    urlutils,
33
33
    )
34
 
from bzrlib.errors import (
 
34
from ..errors import (
35
35
    FileExists,
36
36
    LockError,
37
37
    InProcessTransport,
38
38
    NoSuchFile,
39
 
    TransportError,
40
 
    )
41
 
from bzrlib.trace import mutter
42
 
from bzrlib.transport import (
 
39
    )
 
40
from ..sixish import (
 
41
    BytesIO,
 
42
    )
 
43
from ..transport import (
43
44
    AppendBasedFileStream,
44
45
    _file_streams,
45
46
    LateReadError,
53
54
        self.st_size = size
54
55
        if not is_dir:
55
56
            if perms is None:
56
 
                perms = 0644
 
57
                perms = 0o644
57
58
            self.st_mode = S_IFREG | perms
58
59
        else:
59
60
            if perms is None:
60
 
                perms = 0755
 
61
                perms = 0o755
61
62
            self.st_mode = S_IFDIR | perms
62
63
 
63
64
 
81
82
 
82
83
    def clone(self, offset=None):
83
84
        """See Transport.clone()."""
84
 
        path = self._combine_paths(self._cwd, offset)
 
85
        path = urlutils.URL._combine_paths(self._cwd, offset)
85
86
        if len(path) == 0 or path[-1] != '/':
86
87
            path += '/'
87
88
        url = self._scheme + path
131
132
        del self._files[_abspath]
132
133
 
133
134
    def external_url(self):
134
 
        """See bzrlib.transport.Transport.external_url."""
 
135
        """See breezy.transport.Transport.external_url."""
135
136
        # MemoryTransport's are only accessible in-process
136
137
        # so we raise here
137
138
        raise InProcessTransport(self)
144
145
                return LateReadError(relpath)
145
146
            else:
146
147
                raise NoSuchFile(relpath)
147
 
        return StringIO(self._files[_abspath][0])
 
148
        return BytesIO(self._files[_abspath][0])
148
149
 
149
150
    def put_file(self, relpath, f, mode=None):
150
151
        """See Transport.put_file()."""
151
152
        _abspath = self._abspath(relpath)
152
153
        self._check_parent(_abspath)
153
 
        bytes = f.read()
154
 
        if type(bytes) is not str:
155
 
            # Although not strictly correct, we raise UnicodeEncodeError to be
156
 
            # compatible with other transports.
157
 
            raise UnicodeEncodeError(
158
 
                'undefined', bytes, 0, 1,
159
 
                'put_file must be given a file of bytes, not unicode.')
160
 
        self._files[_abspath] = (bytes, mode)
161
 
        return len(bytes)
 
154
        raw_bytes = f.read()
 
155
        self._files[_abspath] = (raw_bytes, mode)
 
156
        return len(raw_bytes)
162
157
 
163
158
    def mkdir(self, relpath, mode=None):
164
159
        """See Transport.mkdir()."""
170
165
 
171
166
    def open_write_stream(self, relpath, mode=None):
172
167
        """See Transport.open_write_stream."""
173
 
        self.put_bytes(relpath, "", mode)
 
168
        self.put_bytes(relpath, b"", mode)
174
169
        result = AppendBasedFileStream(self, relpath)
175
170
        _file_streams[self.abspath(relpath)] = result
176
171
        return result
199
194
                if path.startswith(_abspath):
200
195
                    trailing = path[len(_abspath):]
201
196
                    if trailing and '/' not in trailing:
202
 
                        result.append(trailing)
203
 
        return map(urlutils.escape, result)
 
197
                        result.append(urlutils.escape(trailing))
 
198
        return result
204
199
 
205
200
    def rename(self, rel_from, rel_to):
206
201
        """Rename a file or directory; fail if the destination exists"""
289
284
            raise LockError('File %r already locked' % (self.path,))
290
285
        self.transport._locks[self.path] = self
291
286
 
292
 
    def __del__(self):
293
 
        # Should this warn, or actually try to cleanup?
294
 
        if self.transport:
295
 
            warnings.warn("MemoryLock %r not explicitly unlocked" % (self.path,))
296
 
            self.unlock()
297
 
 
298
287
    def unlock(self):
299
288
        del self.transport._locks[self.path]
300
289
        self.transport = None
309
298
        self._locks = {}
310
299
        self._scheme = "memory+%s:///" % id(self)
311
300
        def memory_factory(url):
312
 
            from bzrlib.transport import memory
 
301
            from . import memory
313
302
            result = memory.MemoryTransport(url)
314
303
            result._dirs = self._dirs
315
304
            result._files = self._files
323
312
        transport.unregister_transport(self._scheme, self._memory_factory)
324
313
 
325
314
    def get_url(self):
326
 
        """See bzrlib.transport.Server.get_url."""
 
315
        """See breezy.transport.Server.get_url."""
327
316
        return self._scheme
328
317
 
329
318
    def get_bogus_url(self):