~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/transport/local.py

  • Committer: Robert Collins
  • Date: 2006-05-23 02:12:20 UTC
  • mto: (1755.1.2 integration)
  • mto: This revision was merged to the branch mainline in revision 1757.
  • Revision ID: robertc@robertcollins.net-20060523021220-7aae296d0f330275
Make LocalTransport faster by not normpathing every internal path translation.

Show diffs side-by-side

added added

removed removed

Lines of Context:
58
58
        else:
59
59
            return LocalTransport(self.abspath(offset))
60
60
 
 
61
    def _abspath(self, relative_reference):
 
62
        """Return a path for use in os calls.
 
63
 
 
64
        Several assumptions are made:
 
65
         - relative_reference does not contain '..'
 
66
         - relative_reference is url escaped.
 
67
        """
 
68
        return pathjoin(self.base, urllib.unquote(relative_reference))
 
69
 
61
70
    def abspath(self, relpath):
62
71
        """Return the full url to the given relative URL."""
 
72
        # TODO: url escape the result. RBC 20060523.
63
73
        assert isinstance(relpath, basestring), (type(relpath), relpath)
64
74
        result = normpath(pathjoin(self.base, urllib.unquote(relpath)))
65
75
        #if result[-1] != '/':
77
87
                       strip_trailing_slash(abspath))
78
88
 
79
89
    def has(self, relpath):
80
 
        return os.access(self.abspath(relpath), os.F_OK)
 
90
        return os.access(self._abspath(relpath), os.F_OK)
81
91
 
82
92
    def get(self, relpath):
83
93
        """Get the file at the given relative path.
85
95
        :param relpath: The relative path to the file
86
96
        """
87
97
        try:
88
 
            path = self.abspath(relpath)
 
98
            path = self._abspath(relpath)
89
99
            return open(path, 'rb')
90
100
        except (IOError, OSError),e:
91
101
            self._translate_error(e, path)
100
110
 
101
111
        path = relpath
102
112
        try:
103
 
            path = self.abspath(relpath)
 
113
            path = self._abspath(relpath)
104
114
            check_legal_path(path)
105
115
            fp = AtomicFile(path, 'wb', new_mode=mode)
106
116
        except (IOError, OSError),e:
127
137
        """Create a directory at the given path."""
128
138
        path = relpath
129
139
        try:
130
 
            path = self.abspath(relpath)
 
140
            path = self._abspath(relpath)
131
141
            os.mkdir(path)
132
142
            if mode is not None:
133
143
                os.chmod(path, mode)
135
145
            self._translate_error(e, path)
136
146
 
137
147
    def append(self, relpath, f, mode=None):
138
 
        """Append the text in the file-like object into the final
139
 
        location.
140
 
        """
 
148
        """Append the text in the file-like object into the final location."""
 
149
        abspath = self._abspath(relpath)
141
150
        try:
142
 
            fp = open(self.abspath(relpath), 'ab')
 
151
            fp = open(abspath, 'ab')
 
152
            # FIXME should we really be chmodding every time ? RBC 20060523
143
153
            if mode is not None:
144
 
                os.chmod(self.abspath(relpath), mode)
 
154
                os.chmod(abspath, mode)
145
155
        except (IOError, OSError),e:
146
156
            self._translate_error(e, relpath)
147
157
        # win32 workaround (tell on an unwritten file returns 0)
152
162
 
153
163
    def copy(self, rel_from, rel_to):
154
164
        """Copy the item at rel_from to the location at rel_to"""
155
 
        path_from = self.abspath(rel_from)
156
 
        path_to = self.abspath(rel_to)
 
165
        path_from = self._abspath(rel_from)
 
166
        path_to = self._abspath(rel_to)
157
167
        try:
158
168
            shutil.copy(path_from, path_to)
159
169
        except (IOError, OSError),e:
161
171
            self._translate_error(e, path_from)
162
172
 
163
173
    def rename(self, rel_from, rel_to):
164
 
        path_from = self.abspath(rel_from)
 
174
        path_from = self._abspath(rel_from)
165
175
        try:
166
176
            # *don't* call bzrlib.osutils.rename, because we want to 
167
177
            # detect errors on rename
168
 
            os.rename(path_from, self.abspath(rel_to))
 
178
            os.rename(path_from, self._abspath(rel_to))
169
179
        except (IOError, OSError),e:
170
180
            # TODO: What about path_to?
171
181
            self._translate_error(e, path_from)
172
182
 
173
183
    def move(self, rel_from, rel_to):
174
184
        """Move the item at rel_from to the location at rel_to"""
175
 
        path_from = self.abspath(rel_from)
176
 
        path_to = self.abspath(rel_to)
 
185
        path_from = self._abspath(rel_from)
 
186
        path_to = self._abspath(rel_to)
177
187
 
178
188
        try:
179
189
            # this version will delete the destination if necessary
186
196
        """Delete the item at relpath"""
187
197
        path = relpath
188
198
        try:
189
 
            path = self.abspath(relpath)
 
199
            path = self._abspath(relpath)
190
200
            os.remove(path)
191
201
        except (IOError, OSError),e:
192
 
            # TODO: What about path_to?
193
202
            self._translate_error(e, path)
194
203
 
195
204
    def copy_to(self, relpaths, other, mode=None, pb=None):
206
215
            for path in relpaths:
207
216
                self._update_pb(pb, 'copy-to', count, total)
208
217
                try:
209
 
                    mypath = self.abspath(path)
210
 
                    otherpath = other.abspath(path)
 
218
                    mypath = self._abspath(path)
 
219
                    otherpath = other._abspath(path)
211
220
                    shutil.copy(mypath, otherpath)
212
221
                    if mode is not None:
213
222
                        os.chmod(otherpath, mode)
227
236
        WARNING: many transports do not support this, so trying avoid using
228
237
        it if at all possible.
229
238
        """
230
 
        path = self.abspath(relpath)
 
239
        path = self._abspath(relpath)
231
240
        try:
232
241
            return [urllib.quote(entry) for entry in os.listdir(path)]
233
242
        except (IOError, OSError), e:
238
247
        """
239
248
        path = relpath
240
249
        try:
241
 
            path = self.abspath(relpath)
 
250
            path = self._abspath(relpath)
242
251
            return os.stat(path)
243
252
        except (IOError, OSError),e:
244
253
            self._translate_error(e, path)
250
259
        from bzrlib.lock import ReadLock
251
260
        path = relpath
252
261
        try:
253
 
            path = self.abspath(relpath)
 
262
            path = self._abspath(relpath)
254
263
            return ReadLock(path)
255
264
        except (IOError, OSError), e:
256
265
            self._translate_error(e, path)
262
271
        :return: A lock object, which should be passed to Transport.unlock()
263
272
        """
264
273
        from bzrlib.lock import WriteLock
265
 
        return WriteLock(self.abspath(relpath))
 
274
        return WriteLock(self._abspath(relpath))
266
275
 
267
276
    def rmdir(self, relpath):
268
277
        """See Transport.rmdir."""
269
278
        path = relpath
270
279
        try:
271
 
            path = self.abspath(relpath)
 
280
            path = self._abspath(relpath)
272
281
            os.rmdir(path)
273
282
        except (IOError, OSError),e:
274
283
            self._translate_error(e, path)