/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: Breezy landing bot
  • Author(s): Vincent Ladeuil
  • Date: 2019-03-06 14:58:32 UTC
  • mfrom: (7294.1.2 trunk)
  • Revision ID: breezy.the.bot@gmail.com-20190306145832-ghgjxu1recloriwh
Open trunk again as 3.1.0dev1

Merged from https://code.launchpad.net/~vila/brz/trunk/+merge/364041

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
from io import (
27
27
    BytesIO,
28
28
    )
 
29
import itertools
29
30
import os
30
31
import errno
31
 
from stat import S_IFREG, S_IFDIR, S_IFLNK
 
32
from stat import S_IFREG, S_IFDIR, S_IFLNK, S_ISDIR
32
33
 
33
34
from .. import (
34
35
    transport,
50
51
 
51
52
class MemoryStat(object):
52
53
 
53
 
    def __init__(self, size, kind, perms):
 
54
    def __init__(self, size, kind, perms=None):
54
55
        self.st_size = size
55
 
        if kind == 'file':
 
56
        if not S_ISDIR(kind):
56
57
            if perms is None:
57
58
                perms = 0o644
58
 
            self.st_mode = S_IFREG | perms
59
 
        elif kind == 'directory':
 
59
            self.st_mode = kind | perms
 
60
        else:
60
61
            if perms is None:
61
62
                perms = 0o755
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)
 
63
            self.st_mode = kind | perms
67
64
 
68
65
 
69
66
class MemoryTransport(transport.Transport):
111
108
 
112
109
    def append_file(self, relpath, f, mode=None):
113
110
        """See Transport.append_file()."""
114
 
        _abspath = self._abspath(relpath)
 
111
        _abspath = self._resolve_symlinks(relpath)
115
112
        self._check_parent(_abspath)
116
113
        orig_content, orig_mode = self._files.get(_abspath, (b"", None))
117
114
        if mode is None:
128
125
    def has(self, relpath):
129
126
        """See Transport.has()."""
130
127
        _abspath = self._abspath(relpath)
131
 
        return ((_abspath in self._files)
132
 
                or (_abspath in self._dirs)
133
 
                or (_abspath in self._symlinks))
 
128
        for container in (self._files, self._dirs, self._symlinks):
 
129
            if _abspath in container.keys():
 
130
                return True
 
131
        return False
134
132
 
135
133
    def delete(self, relpath):
136
134
        """See Transport.delete()."""
137
135
        _abspath = self._abspath(relpath)
138
 
        if _abspath not in self._files:
 
136
        if _abspath in self._files:
 
137
            del self._files[_abspath]
 
138
        elif _abspath in self._symlinks:
 
139
            del self._symlinks[_abspath]
 
140
        else:
139
141
            raise NoSuchFile(relpath)
140
 
        del self._files[_abspath]
141
142
 
142
143
    def external_url(self):
143
144
        """See breezy.transport.Transport.external_url."""
147
148
 
148
149
    def get(self, relpath):
149
150
        """See Transport.get()."""
150
 
        _abspath = self._abspath(relpath)
 
151
        _abspath = self._resolve_symlinks(relpath)
151
152
        if _abspath not in self._files:
152
153
            if _abspath in self._dirs:
153
154
                return LateReadError(relpath)
157
158
 
158
159
    def put_file(self, relpath, f, mode=None):
159
160
        """See Transport.put_file()."""
160
 
        _abspath = self._abspath(relpath)
 
161
        _abspath = self._resolve_symlinks(relpath)
161
162
        self._check_parent(_abspath)
162
163
        raw_bytes = f.read()
163
164
        self._files[_abspath] = (raw_bytes, mode)
164
165
        return len(raw_bytes)
165
166
 
 
167
    def symlink(self, source, target):
 
168
        _abspath = self._resolve_symlinks(target)
 
169
        self._check_parent(_abspath)
 
170
        self._symlinks[_abspath] = self._abspath(source)
 
171
 
166
172
    def mkdir(self, relpath, mode=None):
167
173
        """See Transport.mkdir()."""
168
 
        _abspath = self._abspath(relpath)
 
174
        _abspath = self._resolve_symlinks(relpath)
169
175
        self._check_parent(_abspath)
170
176
        if _abspath in self._dirs:
171
177
            raise FileExists(relpath)
183
189
        return True
184
190
 
185
191
    def iter_files_recursive(self):
186
 
        for file in self._files:
 
192
        for file in itertools.chain(self._files, self._symlinks):
187
193
            if file.startswith(self._cwd):
188
194
                yield urlutils.escape(file[len(self._cwd):])
189
195
 
190
196
    def list_dir(self, relpath):
191
197
        """See Transport.list_dir()."""
192
 
        _abspath = self._abspath(relpath)
 
198
        _abspath = self._resolve_symlinks(relpath)
193
199
        if _abspath != '/' and _abspath not in self._dirs:
194
200
            raise NoSuchFile(relpath)
195
201
        result = []
197
203
        if not _abspath.endswith('/'):
198
204
            _abspath += '/'
199
205
 
200
 
        for path_group in self._files, self._dirs:
 
206
        for path_group in self._files, self._dirs, self._symlinks:
201
207
            for path in path_group:
202
208
                if path.startswith(_abspath):
203
209
                    trailing = path[len(_abspath):]
207
213
 
208
214
    def rename(self, rel_from, rel_to):
209
215
        """Rename a file or directory; fail if the destination exists"""
210
 
        abs_from = self._abspath(rel_from)
211
 
        abs_to = self._abspath(rel_to)
 
216
        abs_from = self._resolve_symlinks(rel_from)
 
217
        abs_to = self._resolve_symlinks(rel_to)
212
218
 
213
219
        def replace(x):
214
220
            if x == abs_from:
233
239
        # fail differently depending on dict order. So work on copy, fail on
234
240
        # error on only replace dicts if all goes well.
235
241
        renamed_files = self._files.copy()
 
242
        renamed_symlinks = self._symlinks.copy()
236
243
        renamed_dirs = self._dirs.copy()
237
244
        do_renames(renamed_files)
 
245
        do_renames(renamed_symlinks)
238
246
        do_renames(renamed_dirs)
239
247
        # We may have been cloned so modify in place
240
248
        self._files.clear()
241
249
        self._files.update(renamed_files)
 
250
        self._symlinks.clear()
 
251
        self._symlinks.update(renamed_symlinks)
242
252
        self._dirs.clear()
243
253
        self._dirs.update(renamed_dirs)
244
254
 
245
255
    def rmdir(self, relpath):
246
256
        """See Transport.rmdir."""
247
 
        _abspath = self._abspath(relpath)
 
257
        _abspath = self._resolve_symlinks(relpath)
248
258
        if _abspath in self._files:
249
259
            self._translate_error(IOError(errno.ENOTDIR, relpath), relpath)
250
 
        for path in self._files:
 
260
        for path in itertools.chain(self._files, self._symlinks):
251
261
            if path.startswith(_abspath + '/'):
252
262
                self._translate_error(IOError(errno.ENOTEMPTY, relpath),
253
263
                                      relpath)
262
272
    def stat(self, relpath):
263
273
        """See Transport.stat()."""
264
274
        _abspath = self._abspath(relpath)
265
 
        if _abspath in self._files:
266
 
            return MemoryStat(len(self._files[_abspath][0]), 'file',
 
275
        if _abspath in self._files.keys():
 
276
            return MemoryStat(len(self._files[_abspath][0]), S_IFREG,
267
277
                              self._files[_abspath][1])
268
 
        elif _abspath in self._dirs:
269
 
            return MemoryStat(0, 'directory', self._dirs[_abspath])
270
 
        elif _abspath in self._symlinks:
271
 
            return MemoryStat(0, 'symlink', 0)
 
278
        elif _abspath in self._dirs.keys():
 
279
            return MemoryStat(0, S_IFDIR, self._dirs[_abspath])
 
280
        elif _abspath in self._symlinks.keys():
 
281
            return MemoryStat(0, S_IFLNK)
272
282
        else:
273
283
            raise NoSuchFile(_abspath)
274
284
 
280
290
        """See Transport.lock_write()."""
281
291
        return _MemoryLock(self._abspath(relpath), self)
282
292
 
 
293
    def _resolve_symlinks(self, relpath):
 
294
        path = self._abspath(relpath)
 
295
        while path in self._symlinks.keys():
 
296
            path = self._symlinks[path]
 
297
        return path
 
298
 
283
299
    def _abspath(self, relpath):
284
300
        """Generate an internal absolute path."""
285
301
        relpath = urlutils.unescape(relpath)
336
352
    def start_server(self):
337
353
        self._dirs = {'/': None}
338
354
        self._files = {}
 
355
        self._symlinks = {}
339
356
        self._locks = {}
340
357
        self._scheme = "memory+%s:///" % id(self)
341
358
 
344
361
            result = memory.MemoryTransport(url)
345
362
            result._dirs = self._dirs
346
363
            result._files = self._files
 
364
            result._symlinks = self._symlinks
347
365
            result._locks = self._locks
348
366
            return result
349
367
        self._memory_factory = memory_factory