~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/transport/memory.py

  • Committer: John Arbash Meinel
  • Date: 2006-03-08 14:31:23 UTC
  • mfrom: (1598 +trunk)
  • mto: (1685.1.1 bzr-encoding)
  • mto: This revision was merged to the branch mainline in revision 1752.
  • Revision ID: john@arbash-meinel.com-20060308143123-448308b0db4de410
[merge] bzr.dev 1573, lots of updates

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
 
50
56
            url = url + '/'
51
57
        super(MemoryTransport, self).__init__(url)
52
58
        self._cwd = url[url.find(':') + 1:]
 
59
        # dictionaries from absolute path to file mode
53
60
        self._dirs = {}
54
61
        self._files = {}
55
62
        self._locks = {}
86
93
        self._check_parent(_abspath)
87
94
        orig_content, orig_mode = self._files.get(_abspath, ("", None))
88
95
        self._files[_abspath] = (orig_content + f.read(), orig_mode)
 
96
        return len(orig_content)
89
97
 
90
98
    def _check_parent(self, _abspath):
91
99
        dir = os.path.dirname(_abspath)
153
161
                path[len(_abspath)] == '/'):
154
162
                result.append(path[len(_abspath) + 1:])
155
163
        return result
 
164
 
 
165
    def rename(self, rel_from, rel_to):
 
166
        """Rename a file or directory; fail if the destination exists"""
 
167
        abs_from = self._abspath(rel_from)
 
168
        abs_to = self._abspath(rel_to)
 
169
        def replace(x):
 
170
            if x == abs_from:
 
171
                x = abs_to
 
172
            elif x.startswith(abs_from + '/'):
 
173
                x = abs_to + x[len(abs_from):]
 
174
            return x
 
175
        def do_renames(container):
 
176
            for path in container:
 
177
                new_path = replace(path)
 
178
                if new_path != path:
 
179
                    if new_path in container:
 
180
                        raise FileExists(new_path)
 
181
                    container[new_path] = container[path]
 
182
                    del container[path]
 
183
        do_renames(self._files)
 
184
        do_renames(self._dirs)
156
185
    
157
186
    def rmdir(self, relpath):
158
187
        """See Transport.rmdir."""
161
190
            self._translate_error(IOError(errno.ENOTDIR, relpath), relpath)
162
191
        for path in self._files:
163
192
            if path.startswith(_abspath):
164
 
                self._translate_error(IOError(errno.EBUSY, relpath), relpath)
 
193
                self._translate_error(IOError(errno.ENOTEMPTY, relpath),
 
194
                                      relpath)
165
195
        for path in self._dirs:
166
196
            if path.startswith(_abspath) and path != _abspath:
167
 
                self._translate_error(IOError(errno.EBUSY, relpath), relpath)
 
197
                self._translate_error(IOError(errno.ENOTEMPTY, relpath), relpath)
168
198
        if not _abspath in self._dirs:
169
199
            raise NoSuchFile(relpath)
170
200
        del self._dirs[_abspath]