~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/transport/memory.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2006-02-22 07:59:56 UTC
  • mfrom: (1553.5.33 bzr.mbp.locks)
  • Revision ID: pqm@pqm.ubuntu.com-20060222075956-fb281c427e571da6
add LockDir and related fixes

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005 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
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
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
 
"""Implementation of Transport that uses memory for its storage."""
 
16
 
 
17
"""Implementation of Transport that uses memory for its storage.
 
18
 
 
19
The contents of the transport will be lost when the object is discarded,
 
20
so this is primarily useful for testing.
 
21
"""
17
22
 
18
23
from copy import copy
19
24
import os
20
25
import errno
 
26
import re
21
27
from stat import *
22
28
from cStringIO import StringIO
23
29
 
48
54
            url = url + '/'
49
55
        super(MemoryTransport, self).__init__(url)
50
56
        self._cwd = url[url.find(':') + 1:]
 
57
        # dictionaries from absolute path to file mode
51
58
        self._dirs = {}
52
59
        self._files = {}
53
60
        self._locks = {}
151
158
                path[len(_abspath)] == '/'):
152
159
                result.append(path[len(_abspath) + 1:])
153
160
        return result
 
161
 
 
162
    def rename(self, rel_from, rel_to):
 
163
        """Rename a file or directory; fail if the destination exists"""
 
164
        abs_from = self._abspath(rel_from)
 
165
        abs_to = self._abspath(rel_to)
 
166
        def replace(x):
 
167
            if x == abs_from:
 
168
                x = abs_to
 
169
            elif x.startswith(abs_from + '/'):
 
170
                x = abs_to + x[len(abs_from):]
 
171
            return x
 
172
        def do_renames(container):
 
173
            for path in container:
 
174
                new_path = replace(path)
 
175
                if new_path != path:
 
176
                    if new_path in container:
 
177
                        raise FileExists(new_path)
 
178
                    container[new_path] = container[path]
 
179
                    del container[path]
 
180
        do_renames(self._files)
 
181
        do_renames(self._dirs)
154
182
    
155
183
    def rmdir(self, relpath):
156
184
        """See Transport.rmdir."""
159
187
            self._translate_error(IOError(errno.ENOTDIR, relpath), relpath)
160
188
        for path in self._files:
161
189
            if path.startswith(_abspath):
162
 
                self._translate_error(IOError(errno.EBUSY, relpath), relpath)
 
190
                self._translate_error(IOError(errno.ENOTEMPTY, relpath),
 
191
                                      relpath)
163
192
        for path in self._dirs:
164
193
            if path.startswith(_abspath) and path != _abspath:
165
 
                self._translate_error(IOError(errno.EBUSY, relpath), relpath)
 
194
                self._translate_error(IOError(errno.ENOTEMPTY, relpath), relpath)
166
195
        if not _abspath in self._dirs:
167
196
            raise NoSuchFile(relpath)
168
197
        del self._dirs[_abspath]