/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-11-18 18:23:32 UTC
  • mto: This revision was merged to the branch mainline in revision 7197.
  • Revision ID: jelmer@jelmer.uk-20181118182332-viz1qvqese2mo9i6
Fix some more Bazaar references.

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
 
26
from io import (
 
27
    BytesIO,
 
28
    )
23
29
import os
24
30
import errno
25
 
import re
26
 
from stat import S_IFREG, S_IFDIR
27
 
from cStringIO import StringIO
28
 
import warnings
 
31
from stat import S_IFREG, S_IFDIR, S_IFLNK
29
32
 
30
 
from bzrlib import (
 
33
from .. import (
31
34
    transport,
32
35
    urlutils,
33
36
    )
34
 
from bzrlib.errors import (
 
37
from ..errors import (
35
38
    FileExists,
36
39
    LockError,
37
40
    InProcessTransport,
38
41
    NoSuchFile,
39
 
    TransportError,
 
42
    TransportNotPossible,
40
43
    )
41
 
from bzrlib.trace import mutter
42
 
from bzrlib.transport import (
 
44
from ..transport import (
43
45
    AppendBasedFileStream,
44
46
    _file_streams,
45
47
    LateReadError,
46
48
    )
47
49
 
48
50
 
49
 
 
50
51
class MemoryStat(object):
51
52
 
52
 
    def __init__(self, size, is_dir, perms):
 
53
    def __init__(self, size, kind, perms):
53
54
        self.st_size = size
54
 
        if not is_dir:
 
55
        if kind == 'file':
55
56
            if perms is None:
56
 
                perms = 0644
 
57
                perms = 0o644
57
58
            self.st_mode = S_IFREG | perms
58
 
        else:
 
59
        elif kind == 'directory':
59
60
            if perms is None:
60
 
                perms = 0755
 
61
                perms = 0o755
61
62
            self.st_mode = S_IFDIR | perms
 
63
        elif kind == 'symlink':
 
64
            self.st_mode = S_IFLNK | 0o644
 
65
        else:
 
66
            raise AssertionError('unknown kind %r' % kind)
62
67
 
63
68
 
64
69
class MemoryTransport(transport.Transport):
75
80
        self._scheme = url[:split]
76
81
        self._cwd = url[split:]
77
82
        # dictionaries from absolute path to file mode
78
 
        self._dirs = {'/':None}
 
83
        self._dirs = {'/': None}
 
84
        self._symlinks = {}
79
85
        self._files = {}
80
86
        self._locks = {}
81
87
 
82
88
    def clone(self, offset=None):
83
89
        """See Transport.clone()."""
84
 
        path = self._combine_paths(self._cwd, offset)
 
90
        path = urlutils.URL._combine_paths(self._cwd, offset)
85
91
        if len(path) == 0 or path[-1] != '/':
86
92
            path += '/'
87
93
        url = self._scheme + path
88
94
        result = self.__class__(url)
89
95
        result._dirs = self._dirs
 
96
        result._symlinks = self._symlinks
90
97
        result._files = self._files
91
98
        result._locks = self._locks
92
99
        return result
106
113
        """See Transport.append_file()."""
107
114
        _abspath = self._abspath(relpath)
108
115
        self._check_parent(_abspath)
109
 
        orig_content, orig_mode = self._files.get(_abspath, ("", None))
 
116
        orig_content, orig_mode = self._files.get(_abspath, (b"", None))
110
117
        if mode is None:
111
118
            mode = orig_mode
112
119
        self._files[_abspath] = (orig_content + f.read(), mode)
115
122
    def _check_parent(self, _abspath):
116
123
        dir = os.path.dirname(_abspath)
117
124
        if dir != '/':
118
 
            if not dir in self._dirs:
 
125
            if dir not in self._dirs:
119
126
                raise NoSuchFile(_abspath)
120
127
 
121
128
    def has(self, relpath):
122
129
        """See Transport.has()."""
123
130
        _abspath = self._abspath(relpath)
124
 
        return (_abspath in self._files) or (_abspath in self._dirs)
 
131
        return ((_abspath in self._files)
 
132
                or (_abspath in self._dirs)
 
133
                or (_abspath in self._symlinks))
125
134
 
126
135
    def delete(self, relpath):
127
136
        """See Transport.delete()."""
128
137
        _abspath = self._abspath(relpath)
129
 
        if not _abspath in self._files:
 
138
        if _abspath not in self._files:
130
139
            raise NoSuchFile(relpath)
131
140
        del self._files[_abspath]
132
141
 
133
142
    def external_url(self):
134
 
        """See bzrlib.transport.Transport.external_url."""
 
143
        """See breezy.transport.Transport.external_url."""
135
144
        # MemoryTransport's are only accessible in-process
136
145
        # so we raise here
137
146
        raise InProcessTransport(self)
139
148
    def get(self, relpath):
140
149
        """See Transport.get()."""
141
150
        _abspath = self._abspath(relpath)
142
 
        if not _abspath in self._files:
 
151
        if _abspath not in self._files:
143
152
            if _abspath in self._dirs:
144
153
                return LateReadError(relpath)
145
154
            else:
146
155
                raise NoSuchFile(relpath)
147
 
        return StringIO(self._files[_abspath][0])
 
156
        return BytesIO(self._files[_abspath][0])
148
157
 
149
158
    def put_file(self, relpath, f, mode=None):
150
159
        """See Transport.put_file()."""
151
160
        _abspath = self._abspath(relpath)
152
161
        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)
 
162
        raw_bytes = f.read()
 
163
        self._files[_abspath] = (raw_bytes, mode)
 
164
        return len(raw_bytes)
162
165
 
163
166
    def mkdir(self, relpath, mode=None):
164
167
        """See Transport.mkdir()."""
166
169
        self._check_parent(_abspath)
167
170
        if _abspath in self._dirs:
168
171
            raise FileExists(relpath)
169
 
        self._dirs[_abspath]=mode
 
172
        self._dirs[_abspath] = mode
170
173
 
171
174
    def open_write_stream(self, relpath, mode=None):
172
175
        """See Transport.open_write_stream."""
173
 
        self.put_bytes(relpath, "", mode)
 
176
        self.put_bytes(relpath, b"", mode)
174
177
        result = AppendBasedFileStream(self, relpath)
175
178
        _file_streams[self.abspath(relpath)] = result
176
179
        return result
199
202
                if path.startswith(_abspath):
200
203
                    trailing = path[len(_abspath):]
201
204
                    if trailing and '/' not in trailing:
202
 
                        result.append(trailing)
203
 
        return map(urlutils.escape, result)
 
205
                        result.append(urlutils.escape(trailing))
 
206
        return result
204
207
 
205
208
    def rename(self, rel_from, rel_to):
206
209
        """Rename a file or directory; fail if the destination exists"""
207
210
        abs_from = self._abspath(rel_from)
208
211
        abs_to = self._abspath(rel_to)
 
212
 
209
213
        def replace(x):
210
214
            if x == abs_from:
211
215
                x = abs_to
212
216
            elif x.startswith(abs_from + '/'):
213
217
                x = abs_to + x[len(abs_from):]
214
218
            return x
 
219
 
215
220
        def do_renames(container):
 
221
            renames = []
216
222
            for path in container:
217
223
                new_path = replace(path)
218
224
                if new_path != path:
219
225
                    if new_path in container:
220
226
                        raise FileExists(new_path)
221
 
                    container[new_path] = container[path]
222
 
                    del container[path]
223
 
        do_renames(self._files)
224
 
        do_renames(self._dirs)
 
227
                    renames.append((path, new_path))
 
228
            for path, new_path in renames:
 
229
                container[new_path] = container[path]
 
230
                del container[path]
 
231
 
 
232
        # If we modify the existing dicts, we're not atomic anymore and may
 
233
        # fail differently depending on dict order. So work on copy, fail on
 
234
        # error on only replace dicts if all goes well.
 
235
        renamed_files = self._files.copy()
 
236
        renamed_dirs = self._dirs.copy()
 
237
        do_renames(renamed_files)
 
238
        do_renames(renamed_dirs)
 
239
        # We may have been cloned so modify in place
 
240
        self._files.clear()
 
241
        self._files.update(renamed_files)
 
242
        self._dirs.clear()
 
243
        self._dirs.update(renamed_dirs)
225
244
 
226
245
    def rmdir(self, relpath):
227
246
        """See Transport.rmdir."""
234
253
                                      relpath)
235
254
        for path in self._dirs:
236
255
            if path.startswith(_abspath + '/') and path != _abspath:
237
 
                self._translate_error(IOError(errno.ENOTEMPTY, relpath), relpath)
238
 
        if not _abspath in self._dirs:
 
256
                self._translate_error(
 
257
                    IOError(errno.ENOTEMPTY, relpath), relpath)
 
258
        if _abspath not in self._dirs:
239
259
            raise NoSuchFile(relpath)
240
260
        del self._dirs[_abspath]
241
261
 
243
263
        """See Transport.stat()."""
244
264
        _abspath = self._abspath(relpath)
245
265
        if _abspath in self._files:
246
 
            return MemoryStat(len(self._files[_abspath][0]), False,
 
266
            return MemoryStat(len(self._files[_abspath][0]), 'file',
247
267
                              self._files[_abspath][1])
248
268
        elif _abspath in self._dirs:
249
 
            return MemoryStat(0, True, self._dirs[_abspath])
 
269
            return MemoryStat(0, 'directory', self._dirs[_abspath])
 
270
        elif _abspath in self._symlinks:
 
271
            return MemoryStat(0, 'symlink', 0)
250
272
        else:
251
273
            raise NoSuchFile(_abspath)
252
274
 
270
292
            if i == '..':
271
293
                if not r:
272
294
                    raise ValueError("illegal relpath %r under %r"
273
 
                        % (relpath, self._cwd))
 
295
                                     % (relpath, self._cwd))
274
296
                r = r[:-1]
275
297
            elif i == '.' or i == '':
276
298
                pass
277
299
            else:
278
300
                r.append(i)
 
301
                r = self._symlinks.get('/'.join(r), r)
279
302
        return '/' + '/'.join(r)
280
303
 
 
304
    def symlink(self, source, link_name):
 
305
        """Create a symlink pointing to source named link_name."""
 
306
        _abspath = self._abspath(link_name)
 
307
        self._check_parent(_abspath)
 
308
        self._symlinks[_abspath] = source.split('/')
 
309
 
 
310
    def readlink(self, link_name):
 
311
        _abspath = self._abspath(link_name)
 
312
        try:
 
313
            return '/'.join(self._symlinks[_abspath])
 
314
        except KeyError:
 
315
            raise NoSuchFile(link_name)
 
316
 
281
317
 
282
318
class _MemoryLock(object):
283
319
    """This makes a lock."""
289
325
            raise LockError('File %r already locked' % (self.path,))
290
326
        self.transport._locks[self.path] = self
291
327
 
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
328
    def unlock(self):
299
329
        del self.transport._locks[self.path]
300
330
        self.transport = None
304
334
    """Server for the MemoryTransport for testing with."""
305
335
 
306
336
    def start_server(self):
307
 
        self._dirs = {'/':None}
 
337
        self._dirs = {'/': None}
308
338
        self._files = {}
309
339
        self._locks = {}
310
340
        self._scheme = "memory+%s:///" % id(self)
 
341
 
311
342
        def memory_factory(url):
312
 
            from bzrlib.transport import memory
 
343
            from . import memory
313
344
            result = memory.MemoryTransport(url)
314
345
            result._dirs = self._dirs
315
346
            result._files = self._files
323
354
        transport.unregister_transport(self._scheme, self._memory_factory)
324
355
 
325
356
    def get_url(self):
326
 
        """See bzrlib.transport.Server.get_url."""
 
357
        """See breezy.transport.Server.get_url."""
327
358
        return self._scheme
328
359
 
329
360
    def get_bogus_url(self):