~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-07-30 13:54:37 UTC
  • mto: (1946.2.6 reduce-knit-churn)
  • mto: This revision was merged to the branch mainline in revision 1898.
  • Revision ID: john@arbash-meinel.com-20060730135437-1d722abdb14bff76
(jelmer) Install new intertree tests

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
so this is primarily useful for testing.
21
21
"""
22
22
 
 
23
from copy import copy
23
24
import os
24
25
import errno
25
26
import re
58
59
        if url[-1] != '/':
59
60
            url = url + '/'
60
61
        super(MemoryTransport, self).__init__(url)
61
 
        split = url.find(':') + 3
62
 
        self._scheme = url[:split]
63
 
        self._cwd = url[split:]
 
62
        self._cwd = url[url.find(':') + 3:]
64
63
        # dictionaries from absolute path to file mode
65
64
        self._dirs = {'/':None}
66
65
        self._files = {}
68
67
 
69
68
    def clone(self, offset=None):
70
69
        """See Transport.clone()."""
71
 
        path = self._combine_paths(self._cwd, offset)
72
 
        if len(path) == 0 or path[-1] != '/':
73
 
            path += '/'
74
 
        url = self._scheme + path
 
70
        if offset is None or offset == '':
 
71
            return copy(self)
 
72
        segments = offset.split('/')
 
73
        cwdsegments = self._cwd.split('/')[:-1]
 
74
        while len(segments):
 
75
            segment = segments.pop(0)
 
76
            if segment == '.':
 
77
                continue
 
78
            if segment == '..':
 
79
                if len(cwdsegments) > 1:
 
80
                    cwdsegments.pop()
 
81
                continue
 
82
            cwdsegments.append(segment)
 
83
        url = self.base[:self.base.find(':') + 3] + '/'.join(cwdsegments) + '/'
75
84
        result = MemoryTransport(url)
76
85
        result._dirs = self._dirs
77
86
        result._files = self._files
89
98
        else:
90
99
            return temp_t.base[:-1]
91
100
 
92
 
    def append_file(self, relpath, f, mode=None):
93
 
        """See Transport.append_file()."""
 
101
    def append(self, relpath, f, mode=None):
 
102
        """See Transport.append()."""
94
103
        _abspath = self._abspath(relpath)
95
104
        self._check_parent(_abspath)
96
105
        orig_content, orig_mode = self._files.get(_abspath, ("", None))
108
117
    def has(self, relpath):
109
118
        """See Transport.has()."""
110
119
        _abspath = self._abspath(relpath)
111
 
        return (_abspath in self._files) or (_abspath in self._dirs)
 
120
        return _abspath in self._files or _abspath in self._dirs
112
121
 
113
122
    def delete(self, relpath):
114
123
        """See Transport.delete()."""
124
133
            raise NoSuchFile(relpath)
125
134
        return StringIO(self._files[_abspath][0])
126
135
 
127
 
    def put_file(self, relpath, f, mode=None):
128
 
        """See Transport.put_file()."""
 
136
    def put(self, relpath, f, mode=None):
 
137
        """See Transport.put()."""
129
138
        _abspath = self._abspath(relpath)
130
139
        self._check_parent(_abspath)
131
140
        self._files[_abspath] = (f.read(), mode)
145
154
    def iter_files_recursive(self):
146
155
        for file in self._files:
147
156
            if file.startswith(self._cwd):
148
 
                yield urlutils.escape(file[len(self._cwd):])
 
157
                yield file[len(self._cwd):]
149
158
    
150
159
    def list_dir(self, relpath):
151
160
        """See Transport.list_dir()."""
164
173
                len(path) > len(_abspath) and
165
174
                path[len(_abspath)] == '/'):
166
175
                result.append(path[len(_abspath) + 1:])
167
 
        return map(urlutils.escape, result)
 
176
        return result
168
177
 
169
178
    def rename(self, rel_from, rel_to):
170
179
        """Rename a file or directory; fail if the destination exists"""
227
236
        relpath = urlutils.unescape(relpath)
228
237
        if relpath.find('..') != -1:
229
238
            raise AssertionError('relpath contains ..')
230
 
        if relpath == '':
231
 
            return '/'
232
 
        if relpath[0] == '/':
233
 
            return relpath
234
239
        if relpath == '.':
235
240
            if (self._cwd == '/'):
236
241
                return self._cwd