1
# Copyright (C) 2005, 2006 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
"""Transport for the local filesystem.
19
This is a fairly thin wrapper on regular file IO."""
23
from stat import ST_MODE, S_ISDIR, ST_SIZE
27
from bzrlib.trace import mutter
28
from bzrlib.transport import Transport, Server
29
from bzrlib.osutils import abspath, realpath, normpath, pathjoin, rename
32
class LocalTransport(Transport):
33
"""This is the transport agent for local filesystem access."""
35
def __init__(self, base):
36
"""Set the base path where files will be stored."""
37
if base.startswith('file://'):
39
# realpath is incompatible with symlinks. When we traverse
40
# up we might be able to normpath stuff. RBC 20051003
41
base = normpath(abspath(base))
44
super(LocalTransport, self).__init__(base)
46
def should_cache(self):
49
def clone(self, offset=None):
50
"""Return a new LocalTransport with root at self.base + offset
51
Because the local filesystem does not require a connection,
52
we can just return a new object.
55
return LocalTransport(self.base)
57
return LocalTransport(self.abspath(offset))
59
def abspath(self, relpath):
60
"""Return the full url to the given relative URL.
61
This can be supplied with a string or a list
63
assert isinstance(relpath, basestring), (type(relpath), relpath)
64
return pathjoin(self.base, urllib.unquote(relpath))
66
def relpath(self, abspath):
67
"""Return the local path portion from a given absolute path.
69
from bzrlib.osutils import relpath
72
if abspath.endswith('/'):
73
abspath = abspath[:-1]
74
return relpath(self.base[:-1], abspath)
76
def has(self, relpath):
77
return os.access(self.abspath(relpath), os.F_OK)
79
def get(self, relpath):
80
"""Get the file at the given relative path.
82
:param relpath: The relative path to the file
85
path = self.abspath(relpath)
86
return open(path, 'rb')
87
except (IOError, OSError),e:
88
self._translate_error(e, path)
90
def put(self, relpath, f, mode=None):
91
"""Copy the file-like or string object into the location.
93
:param relpath: Location to put the contents, relative to base.
94
:param f: File-like or string object.
96
from bzrlib.atomicfile import AtomicFile
100
path = self.abspath(relpath)
101
fp = AtomicFile(path, 'wb', new_mode=mode)
102
except (IOError, OSError),e:
103
self._translate_error(e, path)
110
def iter_files_recursive(self):
111
"""Iter the relative paths of files in the transports sub-tree."""
112
queue = list(self.list_dir(u'.'))
114
relpath = urllib.quote(queue.pop(0))
115
st = self.stat(relpath)
116
if S_ISDIR(st[ST_MODE]):
117
for i, basename in enumerate(self.list_dir(relpath)):
118
queue.insert(i, relpath+'/'+basename)
122
def mkdir(self, relpath, mode=None):
123
"""Create a directory at the given path."""
126
path = self.abspath(relpath)
130
except (IOError, OSError),e:
131
self._translate_error(e, path)
133
def append(self, relpath, f):
134
"""Append the text in the file-like object into the final
138
fp = open(self.abspath(relpath), 'ab')
139
except (IOError, OSError),e:
140
self._translate_error(e, relpath)
145
def copy(self, rel_from, rel_to):
146
"""Copy the item at rel_from to the location at rel_to"""
148
path_from = self.abspath(rel_from)
149
path_to = self.abspath(rel_to)
151
shutil.copy(path_from, path_to)
152
except (IOError, OSError),e:
153
# TODO: What about path_to?
154
self._translate_error(e, path_from)
156
def rename(self, rel_from, rel_to):
157
path_from = self.abspath(rel_from)
159
# *don't* call bzrlib.osutils.rename, because we want to
160
# detect errors on rename
161
os.rename(path_from, self.abspath(rel_to))
162
except (IOError, OSError),e:
163
# TODO: What about path_to?
164
self._translate_error(e, path_from)
166
def move(self, rel_from, rel_to):
167
"""Move the item at rel_from to the location at rel_to"""
168
path_from = self.abspath(rel_from)
169
path_to = self.abspath(rel_to)
172
# this version will delete the destination if necessary
173
rename(path_from, path_to)
174
except (IOError, OSError),e:
175
# TODO: What about path_to?
176
self._translate_error(e, path_from)
178
def delete(self, relpath):
179
"""Delete the item at relpath"""
182
path = self.abspath(relpath)
184
except (IOError, OSError),e:
185
# TODO: What about path_to?
186
self._translate_error(e, path)
188
def copy_to(self, relpaths, other, mode=None, pb=None):
189
"""Copy a set of entries from self into another Transport.
191
:param relpaths: A list/generator of entries to be copied.
193
if isinstance(other, LocalTransport):
194
# Both from & to are on the local filesystem
195
# Unfortunately, I can't think of anything faster than just
196
# copying them across, one by one :(
199
total = self._get_total(relpaths)
201
for path in relpaths:
202
self._update_pb(pb, 'copy-to', count, total)
204
mypath = self.abspath(path)
205
otherpath = other.abspath(path)
206
shutil.copy(mypath, otherpath)
208
os.chmod(otherpath, mode)
209
except (IOError, OSError),e:
210
self._translate_error(e, path)
214
return super(LocalTransport, self).copy_to(relpaths, other, mode=mode, pb=pb)
217
"""See Transport.listable."""
220
def list_dir(self, relpath):
221
"""Return a list of all files at the given location.
222
WARNING: many transports do not support this, so trying avoid using
223
it if at all possible.
227
path = self.abspath(relpath)
228
return os.listdir(path)
229
except (IOError, OSError),e:
230
self._translate_error(e, path)
232
def stat(self, relpath):
233
"""Return the stat information for a file.
237
path = self.abspath(relpath)
239
except (IOError, OSError),e:
240
self._translate_error(e, path)
242
def lock_read(self, relpath):
243
"""Lock the given file for shared (read) access.
244
:return: A lock object, which should be passed to Transport.unlock()
246
from bzrlib.lock import ReadLock
249
path = self.abspath(relpath)
250
return ReadLock(path)
251
except (IOError, OSError), e:
252
self._translate_error(e, path)
254
def lock_write(self, relpath):
255
"""Lock the given file for exclusive (write) access.
256
WARNING: many transports do not support this, so trying avoid using it
258
:return: A lock object, which should be passed to Transport.unlock()
260
from bzrlib.lock import WriteLock
261
return WriteLock(self.abspath(relpath))
263
def rmdir(self, relpath):
264
"""See Transport.rmdir."""
267
path = self.abspath(relpath)
269
except (IOError, OSError),e:
270
self._translate_error(e, path)
273
class ScratchTransport(LocalTransport):
274
"""A transport that works in a temporary dir and cleans up after itself.
276
The dir only exists for the lifetime of the Python object.
277
Obviously you should not put anything precious in it.
280
def __init__(self, base=None):
282
base = tempfile.mkdtemp()
283
super(ScratchTransport, self).__init__(base)
286
shutil.rmtree(self.base, ignore_errors=True)
287
mutter("%r destroyed" % self)
290
class LocalRelpathServer(Server):
291
"""A pretend server for local transports, using relpaths."""
294
"""See Transport.Server.get_url."""
298
class LocalAbspathServer(Server):
299
"""A pretend server for local transports, using absolute paths."""
302
"""See Transport.Server.get_url."""
303
return os.path.abspath("")
306
class LocalURLServer(Server):
307
"""A pretend server for local transports, using file:// urls."""
310
"""See Transport.Server.get_url."""
311
# FIXME: \ to / on windows
312
return "file://%s" % os.path.abspath("")
315
def get_test_permutations():
316
"""Return the permutations to be used in testing."""
317
return [(LocalTransport, LocalRelpathServer),
318
(LocalTransport, LocalAbspathServer),
319
(LocalTransport, LocalURLServer),