| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
""" |
|---|
| 35 |
ftputil - high-level FTP client library |
|---|
| 36 |
|
|---|
| 37 |
FTPHost objects |
|---|
| 38 |
This class resembles the `os` module's interface to ordinary file |
|---|
| 39 |
systems. In addition, it provides a method `file` which will |
|---|
| 40 |
return file-objects corresponding to remote files. |
|---|
| 41 |
|
|---|
| 42 |
# example session |
|---|
| 43 |
host = ftputil.FTPHost('ftp.domain.com', 'me', 'secret') |
|---|
| 44 |
print host.getcwd() # e. g. '/home/me' |
|---|
| 45 |
source = host.file('sourcefile', 'r') |
|---|
| 46 |
host.mkdir('newdir') |
|---|
| 47 |
host.chdir('newdir') |
|---|
| 48 |
target = host.file('targetfile', 'w') |
|---|
| 49 |
host.copyfileobj(source, target) |
|---|
| 50 |
source.close() |
|---|
| 51 |
target.close() |
|---|
| 52 |
host.remove('targetfile') |
|---|
| 53 |
host.chdir(host.pardir) |
|---|
| 54 |
host.rmdir('newdir') |
|---|
| 55 |
host.close() |
|---|
| 56 |
|
|---|
| 57 |
There are also shortcuts for uploads and downloads: |
|---|
| 58 |
|
|---|
| 59 |
host.upload(local_file, remote_file) |
|---|
| 60 |
host.download(remote_file, local_file) |
|---|
| 61 |
|
|---|
| 62 |
Both accept an additional mode parameter. If it is 'b', the |
|---|
| 63 |
transfer mode will be for binary files. |
|---|
| 64 |
|
|---|
| 65 |
For even more functionality refer to the documentation in |
|---|
| 66 |
`ftputil.txt`. |
|---|
| 67 |
|
|---|
| 68 |
FTPFile objects |
|---|
| 69 |
`FTPFile` objects are constructed via the `file` method (`open` |
|---|
| 70 |
is an alias) of `FTPHost` objects. `FTPFile` objects support the |
|---|
| 71 |
usual file operations for non-seekable files (`read`, `readline`, |
|---|
| 72 |
`readlines`, `xreadlines`, `write`, `writelines`, `close`). |
|---|
| 73 |
|
|---|
| 74 |
Note: ftputil currently is not threadsafe. More specifically, you can |
|---|
| 75 |
use different `FTPHost` objects in different threads but not |
|---|
| 76 |
using a single `FTPHost` object in different threads. |
|---|
| 77 |
""" |
|---|
| 78 |
|
|---|
| 79 |
import ftplib |
|---|
| 80 |
import os |
|---|
| 81 |
import stat |
|---|
| 82 |
import sys |
|---|
| 83 |
import time |
|---|
| 84 |
|
|---|
| 85 |
import ftp_error |
|---|
| 86 |
import ftp_file |
|---|
| 87 |
import ftp_path |
|---|
| 88 |
import ftp_stat |
|---|
| 89 |
import ftputil_version |
|---|
| 90 |
|
|---|
| 91 |
|
|---|
| 92 |
|
|---|
| 93 |
from ftp_error import FTPError, FTPIOError, FTPOSError, \ |
|---|
| 94 |
InaccessibleLoginDirError, InternalError, \ |
|---|
| 95 |
ParserError, PermanentError, RootDirError, \ |
|---|
| 96 |
TemporaryError, TimeShiftError |
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 |
|
|---|
| 100 |
__all__ = ['FTPError', 'FTPOSError', 'TemporaryError', |
|---|
| 101 |
'PermanentError', 'ParserError', 'FTPIOError', |
|---|
| 102 |
'RootDirError', 'FTPHost'] |
|---|
| 103 |
|
|---|
| 104 |
__version__ = ftputil_version.__version__ |
|---|
| 105 |
|
|---|
| 106 |
|
|---|
| 107 |
|
|---|
| 108 |
|
|---|
| 109 |
|
|---|
| 110 |
class FTPHost(object): |
|---|
| 111 |
"""FTP host class.""" |
|---|
| 112 |
|
|---|
| 113 |
|
|---|
| 114 |
|
|---|
| 115 |
|
|---|
| 116 |
|
|---|
| 117 |
|
|---|
| 118 |
|
|---|
| 119 |
|
|---|
| 120 |
|
|---|
| 121 |
|
|---|
| 122 |
|
|---|
| 123 |
|
|---|
| 124 |
|
|---|
| 125 |
|
|---|
| 126 |
|
|---|
| 127 |
|
|---|
| 128 |
|
|---|
| 129 |
|
|---|
| 130 |
|
|---|
| 131 |
|
|---|
| 132 |
|
|---|
| 133 |
def __init__(self, *args, **kwargs): |
|---|
| 134 |
"""Abstract initialization of `FTPHost` object.""" |
|---|
| 135 |
|
|---|
| 136 |
self._args = args |
|---|
| 137 |
self._kwargs = kwargs |
|---|
| 138 |
|
|---|
| 139 |
self._session = self._make_session() |
|---|
| 140 |
|
|---|
| 141 |
self.path = ftp_path._Path(self) |
|---|
| 142 |
|
|---|
| 143 |
self._stat = ftp_stat._Stat(self) |
|---|
| 144 |
self.stat_cache = self._stat._lstat_cache |
|---|
| 145 |
self.stat_cache.enable() |
|---|
| 146 |
|
|---|
| 147 |
self._current_dir = ftp_error._try_with_oserror(self._session.pwd) |
|---|
| 148 |
|
|---|
| 149 |
self._children = [] |
|---|
| 150 |
|
|---|
| 151 |
self._file = None |
|---|
| 152 |
|
|---|
| 153 |
self.closed = False |
|---|
| 154 |
|
|---|
| 155 |
|
|---|
| 156 |
|
|---|
| 157 |
|
|---|
| 158 |
self.curdir, self.pardir, self.sep = '.', '..', '/' |
|---|
| 159 |
|
|---|
| 160 |
|
|---|
| 161 |
self.set_time_shift(0.0) |
|---|
| 162 |
|
|---|
| 163 |
|
|---|
| 164 |
|
|---|
| 165 |
|
|---|
| 166 |
|
|---|
| 167 |
def _make_session(self): |
|---|
| 168 |
""" |
|---|
| 169 |
Return a new session object according to the current state of |
|---|
| 170 |
this `FTPHost` instance. |
|---|
| 171 |
""" |
|---|
| 172 |
|
|---|
| 173 |
args = self._args[:] |
|---|
| 174 |
kwargs = self._kwargs.copy() |
|---|
| 175 |
|
|---|
| 176 |
|
|---|
| 177 |
|
|---|
| 178 |
factory = kwargs.pop('session_factory', ftplib.FTP) |
|---|
| 179 |
return ftp_error._try_with_oserror(factory, *args, **kwargs) |
|---|
| 180 |
|
|---|
| 181 |
def _copy(self): |
|---|
| 182 |
"""Return a copy of this `FTPHost` object.""" |
|---|
| 183 |
|
|---|
| 184 |
|
|---|
| 185 |
return FTPHost(*self._args, **self._kwargs) |
|---|
| 186 |
|
|---|
| 187 |
def _available_child(self): |
|---|
| 188 |
""" |
|---|
| 189 |
Return an available (i. e. one whose `_file` object is closed) |
|---|
| 190 |
child (`FTPHost` object) from the pool of children or `None` |
|---|
| 191 |
if there aren't any. |
|---|
| 192 |
""" |
|---|
| 193 |
for host in self._children: |
|---|
| 194 |
if host._file.closed: |
|---|
| 195 |
return host |
|---|
| 196 |
|
|---|
| 197 |
return None |
|---|
| 198 |
|
|---|
| 199 |
def file(self, path, mode='r'): |
|---|
| 200 |
""" |
|---|
| 201 |
Return an open file(-like) object which is associated with |
|---|
| 202 |
this `FTPHost` object. |
|---|
| 203 |
|
|---|
| 204 |
This method tries to reuse a child but will generate a new one |
|---|
| 205 |
if none is available. |
|---|
| 206 |
""" |
|---|
| 207 |
host = self._available_child() |
|---|
| 208 |
if host is None: |
|---|
| 209 |
host = self._copy() |
|---|
| 210 |
self._children.append(host) |
|---|
| 211 |
host._file = ftp_file._FTPFile(host) |
|---|
| 212 |
basedir = self.getcwd() |
|---|
| 213 |
|
|---|
| 214 |
|
|---|
| 215 |
if host.path.isabs(path): |
|---|
| 216 |
effective_path = path |
|---|
| 217 |
else: |
|---|
| 218 |
effective_path = host.path.join(basedir, path) |
|---|
| 219 |
effective_dir, effective_file = host.path.split(effective_path) |
|---|
| 220 |
try: |
|---|
| 221 |
|
|---|
| 222 |
host.chdir(effective_dir) |
|---|
| 223 |
except ftp_error.PermanentError: |
|---|
| 224 |
|
|---|
| 225 |
|
|---|
| 226 |
raise ftp_error.FTPIOError("remote directory '%s' doesn't exist " |
|---|
| 227 |
"or has insufficient access rights" % effective_dir) |
|---|
| 228 |
host._file._open(effective_file, mode) |
|---|
| 229 |
if 'w' in mode: |
|---|
| 230 |
self.stat_cache.invalidate(effective_path) |
|---|
| 231 |
return host._file |
|---|
| 232 |
|
|---|
| 233 |
|
|---|
| 234 |
open = file |
|---|
| 235 |
|
|---|
| 236 |
def close(self): |
|---|
| 237 |
"""Close host connection.""" |
|---|
| 238 |
if self.closed: |
|---|
| 239 |
return |
|---|
| 240 |
|
|---|
| 241 |
for host in self._children: |
|---|
| 242 |
|
|---|
| 243 |
host._file.close() |
|---|
| 244 |
host.close() |
|---|
| 245 |
|
|---|
| 246 |
try: |
|---|
| 247 |
ftp_error._try_with_oserror(self._session.close) |
|---|
| 248 |
finally: |
|---|
| 249 |
|
|---|
| 250 |
|
|---|
| 251 |
|
|---|
| 252 |
|
|---|
| 253 |
self.stat_cache.clear() |
|---|
| 254 |
self._children = [] |
|---|
| 255 |
self.closed = True |
|---|
| 256 |
|
|---|
| 257 |
def __del__(self): |
|---|
| 258 |
|
|---|
| 259 |
|
|---|
| 260 |
try: |
|---|
| 261 |
self.close() |
|---|
| 262 |
except: |
|---|
| 263 |
|
|---|
| 264 |
pass |
|---|
| 265 |
|
|---|
| 266 |
|
|---|
| 267 |
|
|---|
| 268 |
|
|---|
| 269 |
def set_parser(self, parser): |
|---|
| 270 |
""" |
|---|
| 271 |
Set the parser for extracting stat results from directory |
|---|
| 272 |
listings. |
|---|
| 273 |
|
|---|
| 274 |
The parser interface is described in the documentation, but |
|---|
| 275 |
here are the most important things: |
|---|
| 276 |
|
|---|
| 277 |
- A parser should derive from `ftp_stat.Parser`. |
|---|
| 278 |
|
|---|
| 279 |
- The parser has to implement two methods, `parse_line` and |
|---|
| 280 |
`ignores_line`. For the latter, there's a probably useful |
|---|
| 281 |
default in the class `ftp_stat.Parser`. |
|---|
| 282 |
|
|---|
| 283 |
- `parse_line` should try to parse a line of a directory |
|---|
| 284 |
listing and return a `ftp_stat.StatResult` instance. If |
|---|
| 285 |
parsing isn't possible, raise `ftp_error.ParserError` with |
|---|
| 286 |
a useful error message. |
|---|
| 287 |
|
|---|
| 288 |
- `ignores_line` should return a true value if the line isn't |
|---|
| 289 |
assumed to contain stat information. |
|---|
| 290 |
""" |
|---|
| 291 |
|
|---|
| 292 |
self.stat_cache.clear() |
|---|
| 293 |
|
|---|
| 294 |
self._stat._parser = parser |
|---|
| 295 |
|
|---|
| 296 |
self._stat._allow_parser_switching = False |
|---|
| 297 |
|
|---|
| 298 |
|
|---|
| 299 |
|
|---|
| 300 |
|
|---|
| 301 |
def set_time_shift(self, time_shift): |
|---|
| 302 |
""" |
|---|
| 303 |
Set the time shift value (i. e. the time difference between |
|---|
| 304 |
client and server) for this `FTPHost` object. By (my) |
|---|
| 305 |
definition, the time shift value is positive if the local |
|---|
| 306 |
time of the server is greater than the local time of the |
|---|
| 307 |
client (for the same physical time), i. e. |
|---|
| 308 |
|
|---|
| 309 |
time_shift =def= t_server - t_client |
|---|
| 310 |
<=> t_server = t_client + time_shift |
|---|
| 311 |
<=> t_client = t_server - time_shift |
|---|
| 312 |
|
|---|
| 313 |
The time shift is measured in seconds. |
|---|
| 314 |
""" |
|---|
| 315 |
self._time_shift = time_shift |
|---|
| 316 |
|
|---|
| 317 |
def time_shift(self): |
|---|
| 318 |
""" |
|---|
| 319 |
Return the time shift between FTP server and client. See the |
|---|
| 320 |
docstring of `set_time_shift` for more on this value. |
|---|
| 321 |
""" |
|---|
| 322 |
return self._time_shift |
|---|
| 323 |
|
|---|
| 324 |
def __rounded_time_shift(self, time_shift): |
|---|
| 325 |
""" |
|---|
| 326 |
Return the given time shift in seconds, but rounded to |
|---|
| 327 |
full hours. The argument is also assumed to be given in |
|---|
| 328 |
seconds. |
|---|
| 329 |
""" |
|---|
| 330 |
minute = 60.0 |
|---|
| 331 |
hour = 60.0 * minute |
|---|
| 332 |
|
|---|
| 333 |
if time_shift == 0: |
|---|
| 334 |
return 0.0 |
|---|
| 335 |
|
|---|
| 336 |
absolute_time_shift = abs(time_shift) |
|---|
| 337 |
signum = time_shift / absolute_time_shift |
|---|
| 338 |
|
|---|
| 339 |
|
|---|
| 340 |
absolute_rounded_time_shift = \ |
|---|
| 341 |
int( (absolute_time_shift + 30*minute) / hour ) * hour |
|---|
| 342 |
|
|---|
| 343 |
return signum * absolute_rounded_time_shift |
|---|
| 344 |
|
|---|
| 345 |
def __assert_valid_time_shift(self, time_shift): |
|---|
| 346 |
""" |
|---|
| 347 |
Perform sanity checks on the time shift value (given in |
|---|
| 348 |
seconds). If the value is invalid, raise a `TimeShiftError`, |
|---|
| 349 |
else simply return `None`. |
|---|
| 350 |
""" |
|---|
| 351 |
minute = 60.0 |
|---|
| 352 |
hour = 60.0 * minute |
|---|
| 353 |
absolute_rounded_time_shift = abs(self.__rounded_time_shift(time_shift)) |
|---|
| 354 |
|
|---|
| 355 |
|
|---|
| 356 |
if absolute_rounded_time_shift > 24 * hour: |
|---|
| 357 |
raise ftp_error.TimeShiftError( |
|---|
| 358 |
"time shift (%.2f s) > 1 day" % time_shift) |
|---|
| 359 |
|
|---|
| 360 |
|
|---|
| 361 |
maximum_deviation = 5 * minute |
|---|
| 362 |
if abs(time_shift - self.__rounded_time_shift(time_shift)) > \ |
|---|
| 363 |
maximum_deviation: |
|---|
| 364 |
raise ftp_error.TimeShiftError( |
|---|
| 365 |
"time shift (%.2f s) deviates more than %d s from full hours" |
|---|
| 366 |
% (time_shift, maximum_deviation)) |
|---|
| 367 |
|
|---|
| 368 |
def synchronize_times(self): |
|---|
| 369 |
""" |
|---|
| 370 |
Synchronize the local times of FTP client and server. This |
|---|
| 371 |
is necessary to let `upload_if_newer` and `download_if_newer` |
|---|
| 372 |
work correctly. |
|---|
| 373 |
|
|---|
| 374 |
This implementation of `synchronize_times` requires _all_ of |
|---|
| 375 |
the following: |
|---|
| 376 |
|
|---|
| 377 |
- The connection between server and client is established. |
|---|
| 378 |
- The client has write access to the directory that is |
|---|
| 379 |
current when `synchronize_times` is called. |
|---|
| 380 |
|
|---|
| 381 |
The usual usage pattern of `synchronize_times` is to call it |
|---|
| 382 |
directly after the connection is established. (As can be |
|---|
| 383 |
concluded from the points above, this requires write access |
|---|
| 384 |
to the login directory.) |
|---|
| 385 |
|
|---|
| 386 |
If `synchronize_times` fails, it raises a `TimeShiftError`. |
|---|
| 387 |
""" |
|---|
| 388 |
helper_file_name = "_ftputil_sync_" |
|---|
| 389 |
|
|---|
| 390 |
|
|---|
| 391 |
try: |
|---|
| 392 |
file_ = self.file(helper_file_name, 'w') |
|---|
| 393 |
file_.close() |
|---|
| 394 |
server_time = self.path.getmtime(helper_file_name) |
|---|
| 395 |
finally: |
|---|
| 396 |
|
|---|
| 397 |
self.unlink(helper_file_name) |
|---|
| 398 |
|
|---|
| 399 |
time_shift = server_time - time.time() |
|---|
| 400 |
|
|---|
| 401 |
self.__assert_valid_time_shift(time_shift) |
|---|
| 402 |
|
|---|
| 403 |
self.set_time_shift(self.__rounded_time_shift(time_shift)) |
|---|
| 404 |
|
|---|
| 405 |
|
|---|
| 406 |
|
|---|
| 407 |
|
|---|
| 408 |
def copyfileobj(self, source, target, length=64*1024): |
|---|
| 409 |
"Copy data from file-like object source to file-like object target." |
|---|
| 410 |
|
|---|
| 411 |
|
|---|
| 412 |
while True: |
|---|
| 413 |
buffer_ = source.read(length) |
|---|
| 414 |
if not buffer_: |
|---|
| 415 |
break |
|---|
| 416 |
target.write(buffer_) |
|---|
| 417 |
|
|---|
| 418 |
def __get_modes(self, mode): |
|---|
| 419 |
"""Return modes for source and target file.""" |
|---|
| 420 |
if mode == 'b': |
|---|
| 421 |
return 'rb', 'wb' |
|---|
| 422 |
else: |
|---|
| 423 |
return 'r', 'w' |
|---|
| 424 |
|
|---|
| 425 |
def __copy_file(self, source, target, mode, source_open, target_open): |
|---|
| 426 |
""" |
|---|
| 427 |
Copy a file from source to target. Which of both is a local |
|---|
| 428 |
or a remote file, respectively, is determined by the arguments. |
|---|
| 429 |
""" |
|---|
| 430 |
source_mode, target_mode = self.__get_modes(mode) |
|---|
| 431 |
source = source_open(source, source_mode) |
|---|
| 432 |
try: |
|---|
| 433 |
target = target_open(target, target_mode) |
|---|
| 434 |
try: |
|---|
| 435 |
self.copyfileobj(source, target) |
|---|
| 436 |
finally: |
|---|
| 437 |
target.close() |
|---|
| 438 |
finally: |
|---|
| 439 |
source.close() |
|---|
| 440 |
|
|---|
| 441 |
def upload(self, source, target, mode=''): |
|---|
| 442 |
""" |
|---|
| 443 |
Upload a file from the local source (name) to the remote |
|---|
| 444 |
target (name). The argument mode is an empty string or 'a' for |
|---|
| 445 |
text copies, or 'b' for binary copies. |
|---|
| 446 |
""" |
|---|
| 447 |
self.__copy_file(source, target, mode, open, self.file) |
|---|
| 448 |
|
|---|
| 449 |
|
|---|
| 450 |
|
|---|
| 451 |
def download(self, source, target, mode=''): |
|---|
| 452 |
""" |
|---|
| 453 |
Download a file from the remote source (name) to the local |
|---|
| 454 |
target (name). The argument mode is an empty string or 'a' for |
|---|
| 455 |
text copies, or 'b' for binary copies. |
|---|
| 456 |
""" |
|---|
| 457 |
self.__copy_file(source, target, mode, self.file, open) |
|---|
| 458 |
|
|---|
| 459 |
|
|---|
| 460 |
|
|---|
| 461 |
|
|---|
| 462 |
def __copy_file_if_newer(self, source, target, mode, |
|---|
| 463 |
source_mtime, target_mtime, target_exists, copy_method): |
|---|
| 464 |
""" |
|---|
| 465 |
Copy a source file only if it's newer than the target. The |
|---|
| 466 |
direction of the copy operation is determined by the |
|---|
| 467 |
arguments. See methods `upload_if_newer` and |
|---|
| 468 |
`download_if_newer` for examples. |
|---|
| 469 |
|
|---|
| 470 |
If the copy was necessary, return `True`, else return `False`. |
|---|
| 471 |
""" |
|---|
| 472 |
source_timestamp = source_mtime(source) |
|---|
| 473 |
if target_exists(target): |
|---|
| 474 |
target_timestamp = target_mtime(target) |
|---|
| 475 |
else: |
|---|
| 476 |
|
|---|
| 477 |
target_timestamp = 0.0 |
|---|
| 478 |
if source_timestamp > target_timestamp: |
|---|
| 479 |
copy_method(source, target, mode) |
|---|
| 480 |
return True |
|---|
| 481 |
else: |
|---|
| 482 |
return False |
|---|
| 483 |
|
|---|
| 484 |
def __shifted_local_mtime(self, file_name): |
|---|
| 485 |
""" |
|---|
| 486 |
Return last modification of a local file, corrected with |
|---|
| 487 |
respect to the time shift between client and server. |
|---|
| 488 |
""" |
|---|
| 489 |
local_mtime = os.path.getmtime(file_name) |
|---|
| 490 |
|
|---|
| 491 |
return local_mtime + self.time_shift() |
|---|
| 492 |
|
|---|
| 493 |
def upload_if_newer(self, source, target, mode=''): |
|---|
| 494 |
""" |
|---|
| 495 |
Upload a file only if it's newer than the target on the |
|---|
| 496 |
remote host or if the target file does not exist. See the |
|---|
| 497 |
method `upload` for the meaning of the parameters. |
|---|
| 498 |
|
|---|
| 499 |
If an upload was necessary, return `True`, else return |
|---|
| 500 |
`False`. |
|---|
| 501 |
""" |
|---|
| 502 |
return self.__copy_file_if_newer(source, target, mode, |
|---|
| 503 |
self.__shifted_local_mtime, self.path.getmtime, |
|---|
| 504 |
self.path.exists, self.upload) |
|---|
| 505 |
|
|---|
| 506 |
def download_if_newer(self, source, target, mode=''): |
|---|
| 507 |
""" |
|---|
| 508 |
Download a file only if it's newer than the target on the |
|---|
| 509 |
local host or if the target file does not exist. See the |
|---|
| 510 |
method `download` for the meaning of the parameters. |
|---|
| 511 |
|
|---|
| 512 |
If a download was necessary, return `True`, else return |
|---|
| 513 |
`False`. |
|---|
| 514 |
""" |
|---|
| 515 |
return self.__copy_file_if_newer(source, target, mode, |
|---|
| 516 |
self.path.getmtime, self.__shifted_local_mtime, |
|---|
| 517 |
os.path.exists, self.download) |
|---|
| 518 |
|
|---|
| 519 |
|
|---|
| 520 |
|
|---|
| 521 |
|
|---|
| 522 |
def _check_inaccessible_login_directory(self): |
|---|
| 523 |
""" |
|---|
| 524 |
Raise an `InaccessibleLoginDirError` exception if we can't |
|---|
| 525 |
change to the login directory. This test is only reliable if |
|---|
| 526 |
the current directory is the login directory. |
|---|
| 527 |
""" |
|---|
| 528 |
presumable_login_dir = self.getcwd() |
|---|
| 529 |
|
|---|
| 530 |
|
|---|
| 531 |
try: |
|---|
| 532 |
self.chdir(presumable_login_dir) |
|---|
| 533 |
except ftp_error.PermanentError: |
|---|
| 534 |
|
|---|
| 535 |
raise ftp_error.InaccessibleLoginDirError( |
|---|
| 536 |
"directory '%s' is not accessible" % presumable_login_dir) |
|---|
| 537 |
|
|---|
| 538 |
def _robust_ftp_command(self, command, path, descend_deeply=False): |
|---|
| 539 |
""" |
|---|
| 540 |
Run an FTP command on a path. |
|---|
| 541 |
|
|---|
| 542 |
If the path doesn't contain whitespace, run it (the |
|---|
| 543 |
overwritten `_command` method) with the instance (`self`) as |
|---|
| 544 |
first and the `path` as second argument. |
|---|
| 545 |
|
|---|
| 546 |
If the path contains whitespace, split it into a head and a |
|---|
| 547 |
tail part where the tail is the last component of the path. |
|---|
| 548 |
Change into the head directory, then execute the command on |
|---|
| 549 |
the tail component. |
|---|
| 550 |
|
|---|
| 551 |
The return value of the method is the return value of the |
|---|
| 552 |
command. |
|---|
| 553 |
|
|---|
| 554 |
If `descend_deeply` is true (the default is false), descend |
|---|
| 555 |
deeply, i. e. change the directory to the end of the path. |
|---|
| 556 |
""" |
|---|
| 557 |
head, tail = self.path.split(path) |
|---|
| 558 |
if descend_deeply: |
|---|
| 559 |
special_case = " " in path |
|---|
| 560 |
else: |
|---|
| 561 |
special_case = " " in head |
|---|
| 562 |
if not special_case: |
|---|
| 563 |
|
|---|
| 564 |
return command(self, path) |
|---|
| 565 |
else: |
|---|
| 566 |
self._check_inaccessible_login_directory() |
|---|
| 567 |
|
|---|
| 568 |
|
|---|
| 569 |
|
|---|
| 570 |
|
|---|
| 571 |
|
|---|
| 572 |
|
|---|
| 573 |
|
|---|
| 574 |
old_dir = self.getcwd() |
|---|
| 575 |
try: |
|---|
| 576 |
if descend_deeply: |
|---|
| 577 |
|
|---|
| 578 |
self.chdir(path) |
|---|
| 579 |
return command(self, self.curdir) |
|---|
| 580 |
else: |
|---|
| 581 |
|
|---|
| 582 |
self.chdir(head) |
|---|
| 583 |
return command(self, tail) |
|---|
| 584 |
finally: |
|---|
| 585 |
|
|---|
| 586 |
self.chdir(old_dir) |
|---|
| 587 |
|
|---|
| 588 |
|
|---|
| 589 |
|
|---|
| 590 |
|
|---|
| 591 |
def getcwd(self): |
|---|
| 592 |
"""Return the current path name.""" |
|---|
| 593 |
return self._current_dir |
|---|
| 594 |
|
|---|
| 595 |
def chdir(self, path): |
|---|
| 596 |
"""Change the directory on the host.""" |
|---|
| 597 |
ftp_error._try_with_oserror(self._session.cwd, path) |
|---|
| 598 |
self._current_dir = self.path.normpath(self.path.join( |
|---|
| 599 |
|
|---|
| 600 |
self._current_dir, path)) |
|---|
| 601 |
|
|---|
| 602 |
def mkdir(self, path, mode=None): |
|---|
| 603 |
""" |
|---|
| 604 |
Make the directory path on the remote host. The argument |
|---|
| 605 |
`mode` is ignored and only "supported" for similarity with |
|---|
| 606 |
`os.mkdir`. |
|---|
| 607 |
""" |
|---|
| 608 |
|
|---|
| 609 |
|
|---|
| 610 |
def command(self, path): |
|---|
| 611 |
"""Callback function.""" |
|---|
| 612 |
return ftp_error._try_with_oserror(self._session.mkd, path) |
|---|
| 613 |
self._robust_ftp_command(command, path) |
|---|
| 614 |
|
|---|
| 615 |
def makedirs(self, path, mode=None): |
|---|
| 616 |
""" |
|---|
| 617 |
Make the directory `path`, but also make not yet existing |
|---|
| 618 |
intermediate directories, like `os.makedirs`. The value |
|---|
| 619 |
of `mode` is only accepted for compatibility with |
|---|
| 620 |
`os.makedirs` but otherwise ignored. |
|---|
| 621 |
""" |
|---|
| 622 |
|
|---|
| 623 |
|
|---|
| 624 |
path = self.path.abspath(path) |
|---|
| 625 |
directories = path.split(self.sep) |
|---|
| 626 |
|
|---|
| 627 |
|
|---|
| 628 |
for index in range(1, len(directories)): |
|---|
| 629 |
|
|---|
| 630 |
next_directory = self.sep + self.path.join(*directories[:index+1]) |
|---|
| 631 |
try: |
|---|
| 632 |
self.mkdir(next_directory) |
|---|
| 633 |
except ftp_error.PermanentError: |
|---|
| 634 |
|
|---|
| 635 |
|
|---|
| 636 |
|
|---|
| 637 |
|
|---|
| 638 |
if not self.path.isdir(next_directory): |
|---|
| 639 |
raise |
|---|
| 640 |
|
|---|
| 641 |
def rmdir(self, path): |
|---|
| 642 |
""" |
|---|
| 643 |
Remove the _empty_ directory `path` on the remote host. |
|---|
| 644 |
|
|---|
| 645 |
Compatibility note: |
|---|
| 646 |
|
|---|
| 647 |
Previous versions of ftputil could possibly delete non- |
|---|
| 648 |
empty directories as well, - if the server allowed it. This |
|---|
| 649 |
is no longer supported. |
|---|
| 650 |
""" |
|---|
| 651 |
path = self.path.abspath(path) |
|---|
| 652 |
if self.listdir(path): |
|---|
| 653 |
raise ftp_error.PermanentError("directory '%s' not empty" % path) |
|---|
| 654 |
|
|---|
| 655 |
def command(self, path): |
|---|
| 656 |
"""Callback function.""" |
|---|
| 657 |
ftp_error._try_with_oserror(self._session.rmd, path) |
|---|
| 658 |
self._robust_ftp_command(command, path) |
|---|
| 659 |
self.stat_cache.invalidate(path) |
|---|
| 660 |
|
|---|
| 661 |
def remove(self, path): |
|---|
| 662 |
"""Remove the given file or link.""" |
|---|
| 663 |
path = self.path.abspath(path) |
|---|
| 664 |
|
|---|
| 665 |
|
|---|
| 666 |
if self.path.isfile(path) or self.path.islink(path): |
|---|
| 667 |
def command(self, path): |
|---|
| 668 |
"""Callback function.""" |
|---|
| 669 |
ftp_error._try_with_oserror(self._session.delete, path) |
|---|
| 670 |
self._robust_ftp_command(command, path) |
|---|
| 671 |
else: |
|---|
| 672 |
raise ftp_error.PermanentError("remove/unlink can only delete " |
|---|
| 673 |
"files and links, not directories") |
|---|
| 674 |
self.stat_cache.invalidate(path) |
|---|
| 675 |
|
|---|
| 676 |
def unlink(self, path): |
|---|
| 677 |
""" |
|---|
| 678 |
Remove the given file given by `path`. |
|---|
| 679 |
|
|---|
| 680 |
Raise a `PermanentError` if the path doesn't exist, raise a |
|---|
| 681 |
`PermanentError`, but maybe raise other exceptions depending |
|---|
| 682 |
on the state of the server (e. g. timeout). |
|---|
| 683 |
""" |
|---|
| 684 |
self.remove(path) |
|---|
| 685 |
|
|---|
| 686 |
def rmtree(self, path, ignore_errors=False, onerror=None): |
|---|
| 687 |
""" |
|---|
| 688 |
Remove the given remote, possibly non-empty, directory tree. |
|---|
| 689 |
The interface of this method is rather complex, in favor of |
|---|
| 690 |
compatibility with `shutil.rmtree`. |
|---|
| 691 |
|
|---|
| 692 |
If `ignore_errors` is set to a true value, errors are ignored. |
|---|
| 693 |
If `ignore_errors` is a false value _and_ `onerror` isn't set, |
|---|
| 694 |
all exceptions occuring during the tree iteration and |
|---|
| 695 |
processing are raised. These exceptions are all of type |
|---|
| 696 |
`PermanentError`. |
|---|
| 697 |
|
|---|
| 698 |
To distinguish between error situations, pass in a callable |
|---|
| 699 |
for `onerror`. This callable must accept three arguments: |
|---|
| 700 |
`func`, `path` and `exc_info`). `func` is a bound method |
|---|
| 701 |
object, _for example_ `your_host_object.listdir`. `path` is |
|---|
| 702 |
the path that was the recent argument of the respective method |
|---|
| 703 |
(`listdir`, `remove`, `rmdir`). `exc_info` is the exception |
|---|
| 704 |
info as it's got from `sys.exc_info`. |
|---|
| 705 |
|
|---|
| 706 |
Implementation note: The code is copied from `shutil.rmtree` |
|---|
| 707 |
in Python 2.4 and adapted to ftputil. |
|---|
| 708 |
""" |
|---|
| 709 |
|
|---|
| 710 |
|
|---|
| 711 |
if ignore_errors: |
|---|
| 712 |
def new_onerror(*args): |
|---|
| 713 |
"""Do nothing.""" |
|---|
| 714 |
|
|---|
| 715 |
|
|---|
| 716 |
pass |
|---|
| 717 |
elif onerror is None: |
|---|
| 718 |
def new_onerror(*args): |
|---|
| 719 |
"""Re-raise exception.""" |
|---|
| 720 |
|
|---|
| 721 |
|
|---|
| 722 |
raise |
|---|
| 723 |
else: |
|---|
| 724 |
new_onerror = onerror |
|---|
| 725 |
names = [] |
|---|
| 726 |
try: |
|---|
| 727 |
names = self.listdir(path) |
|---|
| 728 |
except ftp_error.PermanentError: |
|---|
| 729 |
new_onerror(self.listdir, path, sys.exc_info()) |
|---|
| 730 |
for name in names: |
|---|
| 731 |
full_name = self.path.join(path, name) |
|---|
| 732 |
try: |
|---|
| 733 |
mode = self.lstat(full_name).st_mode |
|---|
| 734 |
except ftp_error.PermanentError: |
|---|
| 735 |
mode = 0 |
|---|
| 736 |
if stat.S_ISDIR(mode): |
|---|
| 737 |
self.rmtree(full_name, ignore_errors, new_onerror) |
|---|
| 738 |
else: |
|---|
| 739 |
try: |
|---|
| 740 |
self.remove(full_name) |
|---|
| 741 |
except ftp_error.PermanentError: |
|---|
| 742 |
new_onerror(self.remove, full_name, sys.exc_info()) |
|---|
| 743 |
try: |
|---|
| 744 |
self.rmdir(path) |
|---|
| 745 |
except ftp_error.FTPOSError: |
|---|
| 746 |
new_onerror(self.rmdir, path, sys.exc_info()) |
|---|
| 747 |
|
|---|
| 748 |
def rename(self, source, target): |
|---|
| 749 |
"""Rename the source on the FTP host to target.""" |
|---|
| 750 |
|
|---|
| 751 |
|
|---|
| 752 |
|
|---|
| 753 |
self._check_inaccessible_login_directory() |
|---|
| 754 |
source_head, source_tail = self.path.split(source) |
|---|
| 755 |
target_head, target_tail = self.path.split(target) |
|---|
| 756 |
paths_contain_whitespace = (" " in source_head) or (" " in target_head) |
|---|
| 757 |
if paths_contain_whitespace and source_head == target_head: |
|---|
| 758 |
|
|---|