1185.11.19
by John Arbash Meinel
Testing put and append, also testing agaist file-like objects as well as strings. |
1 |
# Copyright (C) 2005 Canonical Ltd
|
2 |
||
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.
|
|
7 |
||
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.
|
|
12 |
||
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
|
|
1185.16.72
by Martin Pool
[merge] from robert and fix up tests |
16 |
|
17 |
"""Transport for the local filesystem.
|
|
18 |
||
19 |
This is a fairly thin wrapper on regular file IO."""
|
|
907.1.1
by John Arbash Meinel
Reworking the Branch and Store code to support an abstracted filesystem layer. |
20 |
|
1442.1.42
by Robert Collins
rebuild ScratchBranch on top of ScratchTransport |
21 |
import os |
22 |
import shutil |
|
1442.1.44
by Robert Collins
Many transport related tweaks: |
23 |
from stat import ST_MODE, S_ISDIR, ST_SIZE |
1442.1.42
by Robert Collins
rebuild ScratchBranch on top of ScratchTransport |
24 |
import tempfile |
1469
by Robert Collins
Change Transport.* to work with URL's. |
25 |
import urllib |
1442.1.42
by Robert Collins
rebuild ScratchBranch on top of ScratchTransport |
26 |
|
27 |
from bzrlib.trace import mutter |
|
1185.31.44
by John Arbash Meinel
Cleaned up Exceptions for all transports. |
28 |
from bzrlib.transport import Transport |
1185.31.58
by John Arbash Meinel
Updating for new transport tests so that they pass on win32 |
29 |
from bzrlib.osutils import abspath, realpath, normpath, pathjoin, rename |
907.1.1
by John Arbash Meinel
Reworking the Branch and Store code to support an abstracted filesystem layer. |
30 |
|
1442.1.42
by Robert Collins
rebuild ScratchBranch on top of ScratchTransport |
31 |
|
907.1.1
by John Arbash Meinel
Reworking the Branch and Store code to support an abstracted filesystem layer. |
32 |
class LocalTransport(Transport): |
33 |
"""This is the transport agent for local filesystem access."""
|
|
34 |
||
35 |
def __init__(self, base): |
|
36 |
"""Set the base path where files will be stored."""
|
|
1185.11.1
by John Arbash Meinel
(broken) Transport work is merged in. Tests do not pass yet. |
37 |
if base.startswith('file://'): |
38 |
base = base[7:] |
|
1092.2.24
by Robert Collins
merge from martins newformat branch - brings in transport abstraction |
39 |
# realpath is incompatible with symlinks. When we traverse
|
40 |
# up we might be able to normpath stuff. RBC 20051003
|
|
1185.31.38
by John Arbash Meinel
Changing os.path.normpath to osutils.normpath |
41 |
super(LocalTransport, self).__init__(normpath(abspath(base))) |
907.1.1
by John Arbash Meinel
Reworking the Branch and Store code to support an abstracted filesystem layer. |
42 |
|
907.1.32
by John Arbash Meinel
Renaming is_remote to should_cache as it is more appropriate. |
43 |
def should_cache(self): |
907.1.22
by John Arbash Meinel
Fixed some encoding issues, added is_remote function for Transport objects. |
44 |
return False |
45 |
||
907.1.2
by John Arbash Meinel
Working on making Branch() do all of it's work over a Transport. |
46 |
def clone(self, offset=None): |
47 |
"""Return a new LocalTransport with root at self.base + offset
|
|
48 |
Because the local filesystem does not require a connection,
|
|
49 |
we can just return a new object.
|
|
50 |
"""
|
|
51 |
if offset is None: |
|
52 |
return LocalTransport(self.base) |
|
53 |
else: |
|
54 |
return LocalTransport(self.abspath(offset)) |
|
55 |
||
907.1.8
by John Arbash Meinel
Changed the format for abspath. Updated branch to use a hidden _transport |
56 |
def abspath(self, relpath): |
1469
by Robert Collins
Change Transport.* to work with URL's. |
57 |
"""Return the full url to the given relative URL.
|
907.1.8
by John Arbash Meinel
Changed the format for abspath. Updated branch to use a hidden _transport |
58 |
This can be supplied with a string or a list
|
907.1.2
by John Arbash Meinel
Working on making Branch() do all of it's work over a Transport. |
59 |
"""
|
1185.12.70
by Aaron Bentley
Removed b |
60 |
assert isinstance(relpath, basestring), (type(relpath), relpath) |
1185.31.33
by John Arbash Meinel
A couple more path.join statements needed changing. |
61 |
return pathjoin(self.base, urllib.unquote(relpath)) |
907.1.1
by John Arbash Meinel
Reworking the Branch and Store code to support an abstracted filesystem layer. |
62 |
|
907.1.24
by John Arbash Meinel
Remote functionality work. |
63 |
def relpath(self, abspath): |
64 |
"""Return the local path portion from a given absolute path.
|
|
65 |
"""
|
|
1457.1.2
by Robert Collins
move branch._relpath into osutils as relpath |
66 |
from bzrlib.osutils import relpath |
1442.1.64
by Robert Collins
Branch.open_containing now returns a tuple (Branch, relative-path). |
67 |
if abspath is None: |
1185.33.66
by Martin Pool
[patch] use unicode literals for all hardcoded paths (Alexander Belchenko) |
68 |
abspath = u'.' |
1457.1.2
by Robert Collins
move branch._relpath into osutils as relpath |
69 |
return relpath(self.base, abspath) |
907.1.24
by John Arbash Meinel
Remote functionality work. |
70 |
|
907.1.1
by John Arbash Meinel
Reworking the Branch and Store code to support an abstracted filesystem layer. |
71 |
def has(self, relpath): |
907.1.2
by John Arbash Meinel
Working on making Branch() do all of it's work over a Transport. |
72 |
return os.access(self.abspath(relpath), os.F_OK) |
907.1.1
by John Arbash Meinel
Reworking the Branch and Store code to support an abstracted filesystem layer. |
73 |
|
907.1.50
by John Arbash Meinel
Removed encode/decode from Transport.put/get, added more exceptions that can be thrown. |
74 |
def get(self, relpath): |
907.1.1
by John Arbash Meinel
Reworking the Branch and Store code to support an abstracted filesystem layer. |
75 |
"""Get the file at the given relative path.
|
907.1.20
by John Arbash Meinel
Removed Transport.open(), making get + put encode/decode to utf-8 |
76 |
|
77 |
:param relpath: The relative path to the file
|
|
78 |
"""
|
|
907.1.48
by John Arbash Meinel
Updated LocalTransport by passing it through the transport_test suite, and got it to pass. |
79 |
try: |
907.1.50
by John Arbash Meinel
Removed encode/decode from Transport.put/get, added more exceptions that can be thrown. |
80 |
path = self.abspath(relpath) |
81 |
return open(path, 'rb') |
|
1185.31.44
by John Arbash Meinel
Cleaned up Exceptions for all transports. |
82 |
except (IOError, OSError),e: |
83 |
self._translate_error(e, path) |
|
907.1.20
by John Arbash Meinel
Removed Transport.open(), making get + put encode/decode to utf-8 |
84 |
|
1185.58.2
by John Arbash Meinel
Added mode to the appropriate transport functions, and tests to make sure they work. |
85 |
def put(self, relpath, f, mode=None): |
907.1.20
by John Arbash Meinel
Removed Transport.open(), making get + put encode/decode to utf-8 |
86 |
"""Copy the file-like or string object into the location.
|
87 |
||
88 |
:param relpath: Location to put the contents, relative to base.
|
|
89 |
:param f: File-like or string object.
|
|
90 |
"""
|
|
907.1.1
by John Arbash Meinel
Reworking the Branch and Store code to support an abstracted filesystem layer. |
91 |
from bzrlib.atomicfile import AtomicFile |
92 |
||
1185.31.44
by John Arbash Meinel
Cleaned up Exceptions for all transports. |
93 |
path = relpath |
907.1.48
by John Arbash Meinel
Updated LocalTransport by passing it through the transport_test suite, and got it to pass. |
94 |
try: |
907.1.50
by John Arbash Meinel
Removed encode/decode from Transport.put/get, added more exceptions that can be thrown. |
95 |
path = self.abspath(relpath) |
1185.58.2
by John Arbash Meinel
Added mode to the appropriate transport functions, and tests to make sure they work. |
96 |
fp = AtomicFile(path, 'wb', new_mode=mode) |
1185.31.44
by John Arbash Meinel
Cleaned up Exceptions for all transports. |
97 |
except (IOError, OSError),e: |
98 |
self._translate_error(e, path) |
|
907.1.2
by John Arbash Meinel
Working on making Branch() do all of it's work over a Transport. |
99 |
try: |
100 |
self._pump(f, fp) |
|
101 |
fp.commit() |
|
102 |
finally: |
|
103 |
fp.close() |
|
104 |
||
1442.1.44
by Robert Collins
Many transport related tweaks: |
105 |
def iter_files_recursive(self): |
106 |
"""Iter the relative paths of files in the transports sub-tree."""
|
|
1185.33.66
by Martin Pool
[patch] use unicode literals for all hardcoded paths (Alexander Belchenko) |
107 |
queue = list(self.list_dir(u'.')) |
1442.1.44
by Robert Collins
Many transport related tweaks: |
108 |
while queue: |
1479
by Robert Collins
More quoting at the transport layer bugfixes. |
109 |
relpath = urllib.quote(queue.pop(0)) |
1442.1.44
by Robert Collins
Many transport related tweaks: |
110 |
st = self.stat(relpath) |
111 |
if S_ISDIR(st[ST_MODE]): |
|
112 |
for i, basename in enumerate(self.list_dir(relpath)): |
|
113 |
queue.insert(i, relpath+'/'+basename) |
|
114 |
else: |
|
115 |
yield relpath |
|
116 |
||
1185.58.2
by John Arbash Meinel
Added mode to the appropriate transport functions, and tests to make sure they work. |
117 |
def mkdir(self, relpath, mode=None): |
907.1.2
by John Arbash Meinel
Working on making Branch() do all of it's work over a Transport. |
118 |
"""Create a directory at the given path."""
|
1185.31.44
by John Arbash Meinel
Cleaned up Exceptions for all transports. |
119 |
path = relpath |
907.1.48
by John Arbash Meinel
Updated LocalTransport by passing it through the transport_test suite, and got it to pass. |
120 |
try: |
1185.31.44
by John Arbash Meinel
Cleaned up Exceptions for all transports. |
121 |
path = self.abspath(relpath) |
122 |
os.mkdir(path) |
|
1185.58.2
by John Arbash Meinel
Added mode to the appropriate transport functions, and tests to make sure they work. |
123 |
if mode is not None: |
124 |
os.chmod(path, mode) |
|
1185.31.44
by John Arbash Meinel
Cleaned up Exceptions for all transports. |
125 |
except (IOError, OSError),e: |
126 |
self._translate_error(e, path) |
|
907.1.2
by John Arbash Meinel
Working on making Branch() do all of it's work over a Transport. |
127 |
|
907.1.50
by John Arbash Meinel
Removed encode/decode from Transport.put/get, added more exceptions that can be thrown. |
128 |
def append(self, relpath, f): |
907.1.1
by John Arbash Meinel
Reworking the Branch and Store code to support an abstracted filesystem layer. |
129 |
"""Append the text in the file-like object into the final
|
130 |
location.
|
|
131 |
"""
|
|
907.1.50
by John Arbash Meinel
Removed encode/decode from Transport.put/get, added more exceptions that can be thrown. |
132 |
fp = open(self.abspath(relpath), 'ab') |
907.1.1
by John Arbash Meinel
Reworking the Branch and Store code to support an abstracted filesystem layer. |
133 |
self._pump(f, fp) |
134 |
||
135 |
def copy(self, rel_from, rel_to): |
|
136 |
"""Copy the item at rel_from to the location at rel_to"""
|
|
137 |
import shutil |
|
907.1.2
by John Arbash Meinel
Working on making Branch() do all of it's work over a Transport. |
138 |
path_from = self.abspath(rel_from) |
139 |
path_to = self.abspath(rel_to) |
|
907.1.48
by John Arbash Meinel
Updated LocalTransport by passing it through the transport_test suite, and got it to pass. |
140 |
try: |
141 |
shutil.copy(path_from, path_to) |
|
1185.31.44
by John Arbash Meinel
Cleaned up Exceptions for all transports. |
142 |
except (IOError, OSError),e: |
143 |
# TODO: What about path_to?
|
|
144 |
self._translate_error(e, path_from) |
|
907.1.1
by John Arbash Meinel
Reworking the Branch and Store code to support an abstracted filesystem layer. |
145 |
|
146 |
def move(self, rel_from, rel_to): |
|
147 |
"""Move the item at rel_from to the location at rel_to"""
|
|
907.1.2
by John Arbash Meinel
Working on making Branch() do all of it's work over a Transport. |
148 |
path_from = self.abspath(rel_from) |
149 |
path_to = self.abspath(rel_to) |
|
907.1.1
by John Arbash Meinel
Reworking the Branch and Store code to support an abstracted filesystem layer. |
150 |
|
907.1.48
by John Arbash Meinel
Updated LocalTransport by passing it through the transport_test suite, and got it to pass. |
151 |
try: |
1185.31.58
by John Arbash Meinel
Updating for new transport tests so that they pass on win32 |
152 |
rename(path_from, path_to) |
1185.31.44
by John Arbash Meinel
Cleaned up Exceptions for all transports. |
153 |
except (IOError, OSError),e: |
154 |
# TODO: What about path_to?
|
|
155 |
self._translate_error(e, path_from) |
|
907.1.1
by John Arbash Meinel
Reworking the Branch and Store code to support an abstracted filesystem layer. |
156 |
|
157 |
def delete(self, relpath): |
|
158 |
"""Delete the item at relpath"""
|
|
1185.31.44
by John Arbash Meinel
Cleaned up Exceptions for all transports. |
159 |
path = relpath |
907.1.48
by John Arbash Meinel
Updated LocalTransport by passing it through the transport_test suite, and got it to pass. |
160 |
try: |
1185.31.44
by John Arbash Meinel
Cleaned up Exceptions for all transports. |
161 |
path = self.abspath(relpath) |
162 |
os.remove(path) |
|
163 |
except (IOError, OSError),e: |
|
164 |
# TODO: What about path_to?
|
|
165 |
self._translate_error(e, path) |
|
907.1.1
by John Arbash Meinel
Reworking the Branch and Store code to support an abstracted filesystem layer. |
166 |
|
1185.58.2
by John Arbash Meinel
Added mode to the appropriate transport functions, and tests to make sure they work. |
167 |
def copy_to(self, relpaths, other, mode=None, pb=None): |
907.1.28
by John Arbash Meinel
Added pb to function that were missing, implemented a basic double-dispatch copy_to function. |
168 |
"""Copy a set of entries from self into another Transport.
|
169 |
||
170 |
:param relpaths: A list/generator of entries to be copied.
|
|
171 |
"""
|
|
172 |
if isinstance(other, LocalTransport): |
|
173 |
# Both from & to are on the local filesystem
|
|
174 |
# Unfortunately, I can't think of anything faster than just
|
|
175 |
# copying them across, one by one :(
|
|
176 |
import shutil |
|
177 |
||
178 |
total = self._get_total(relpaths) |
|
179 |
count = 0 |
|
180 |
for path in relpaths: |
|
181 |
self._update_pb(pb, 'copy-to', count, total) |
|
1185.16.158
by John Arbash Meinel
Added a test that copy_to raises NoSuchFile when a directory is missing (not IOError) |
182 |
try: |
1185.58.2
by John Arbash Meinel
Added mode to the appropriate transport functions, and tests to make sure they work. |
183 |
mypath = self.abspath(path) |
184 |
otherpath = other.abspath(path) |
|
185 |
shutil.copy(mypath, otherpath) |
|
186 |
if mode is not None: |
|
187 |
os.chmod(otherpath, mode) |
|
1185.31.44
by John Arbash Meinel
Cleaned up Exceptions for all transports. |
188 |
except (IOError, OSError),e: |
189 |
self._translate_error(e, path) |
|
907.1.28
by John Arbash Meinel
Added pb to function that were missing, implemented a basic double-dispatch copy_to function. |
190 |
count += 1 |
191 |
return count |
|
192 |
else: |
|
1185.58.2
by John Arbash Meinel
Added mode to the appropriate transport functions, and tests to make sure they work. |
193 |
return super(LocalTransport, self).copy_to(relpaths, other, mode=mode, pb=pb) |
907.1.28
by John Arbash Meinel
Added pb to function that were missing, implemented a basic double-dispatch copy_to function. |
194 |
|
1400.1.1
by Robert Collins
implement a basic test for the ui branch command from http servers |
195 |
def listable(self): |
196 |
"""See Transport.listable."""
|
|
197 |
return True |
|
907.1.28
by John Arbash Meinel
Added pb to function that were missing, implemented a basic double-dispatch copy_to function. |
198 |
|
907.1.2
by John Arbash Meinel
Working on making Branch() do all of it's work over a Transport. |
199 |
def list_dir(self, relpath): |
200 |
"""Return a list of all files at the given location.
|
|
201 |
WARNING: many transports do not support this, so trying avoid using
|
|
202 |
it if at all possible.
|
|
203 |
"""
|
|
1185.31.44
by John Arbash Meinel
Cleaned up Exceptions for all transports. |
204 |
path = relpath |
907.1.48
by John Arbash Meinel
Updated LocalTransport by passing it through the transport_test suite, and got it to pass. |
205 |
try: |
1185.31.44
by John Arbash Meinel
Cleaned up Exceptions for all transports. |
206 |
path = self.abspath(relpath) |
207 |
return os.listdir(path) |
|
208 |
except (IOError, OSError),e: |
|
209 |
self._translate_error(e, path) |
|
907.1.2
by John Arbash Meinel
Working on making Branch() do all of it's work over a Transport. |
210 |
|
211 |
def stat(self, relpath): |
|
212 |
"""Return the stat information for a file.
|
|
213 |
"""
|
|
1185.31.44
by John Arbash Meinel
Cleaned up Exceptions for all transports. |
214 |
path = relpath |
907.1.48
by John Arbash Meinel
Updated LocalTransport by passing it through the transport_test suite, and got it to pass. |
215 |
try: |
1185.31.44
by John Arbash Meinel
Cleaned up Exceptions for all transports. |
216 |
path = self.abspath(relpath) |
217 |
return os.stat(path) |
|
218 |
except (IOError, OSError),e: |
|
219 |
self._translate_error(e, path) |
|
907.1.2
by John Arbash Meinel
Working on making Branch() do all of it's work over a Transport. |
220 |
|
907.1.24
by John Arbash Meinel
Remote functionality work. |
221 |
def lock_read(self, relpath): |
222 |
"""Lock the given file for shared (read) access.
|
|
223 |
:return: A lock object, which should be passed to Transport.unlock()
|
|
224 |
"""
|
|
225 |
from bzrlib.lock import ReadLock |
|
226 |
return ReadLock(self.abspath(relpath)) |
|
227 |
||
228 |
def lock_write(self, relpath): |
|
229 |
"""Lock the given file for exclusive (write) access.
|
|
230 |
WARNING: many transports do not support this, so trying avoid using it
|
|
231 |
||
232 |
:return: A lock object, which should be passed to Transport.unlock()
|
|
233 |
"""
|
|
234 |
from bzrlib.lock import WriteLock |
|
235 |
return WriteLock(self.abspath(relpath)) |
|
236 |
||
1442.1.41
by Robert Collins
move duplicate scratch logic into a scratch transport |
237 |
|
238 |
class ScratchTransport(LocalTransport): |
|
239 |
"""A transport that works in a temporary dir and cleans up after itself.
|
|
240 |
|
|
241 |
The dir only exists for the lifetime of the Python object.
|
|
242 |
Obviously you should not put anything precious in it.
|
|
243 |
"""
|
|
244 |
||
1442.1.42
by Robert Collins
rebuild ScratchBranch on top of ScratchTransport |
245 |
def __init__(self, base=None): |
246 |
if base is None: |
|
247 |
base = tempfile.mkdtemp() |
|
1442.1.41
by Robert Collins
move duplicate scratch logic into a scratch transport |
248 |
super(ScratchTransport, self).__init__(base) |
249 |
||
250 |
def __del__(self): |
|
1442.1.42
by Robert Collins
rebuild ScratchBranch on top of ScratchTransport |
251 |
shutil.rmtree(self.base, ignore_errors=True) |
1442.1.41
by Robert Collins
move duplicate scratch logic into a scratch transport |
252 |
mutter("%r destroyed" % self) |