| 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 |
Tools for syncing combinations of local and remote directories. |
|---|
| 36 |
|
|---|
| 37 |
*** WARNING: This is an unfinished in-development version! |
|---|
| 38 |
""" |
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
import os |
|---|
| 47 |
import shutil |
|---|
| 48 |
|
|---|
| 49 |
from ftputil import FTPHost |
|---|
| 50 |
import ftp_error |
|---|
| 51 |
|
|---|
| 52 |
__all__ = ['FTPHost', 'LocalHost', 'Syncer'] |
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 |
CHUNK_SIZE = 64*1024 |
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 |
class LocalHost(object): |
|---|
| 60 |
def open(self, path, mode): |
|---|
| 61 |
""" |
|---|
| 62 |
Return a Python file object for file name `path`, opened in |
|---|
| 63 |
mode `mode`. |
|---|
| 64 |
""" |
|---|
| 65 |
|
|---|
| 66 |
return open(path, mode) |
|---|
| 67 |
|
|---|
| 68 |
def time_shift(self): |
|---|
| 69 |
""" |
|---|
| 70 |
Return the time shift value (see methods `set_time_shift` |
|---|
| 71 |
and `time_shift` in class `FTPHost` for a definition). By |
|---|
| 72 |
definition, the value is zero for local file systems. |
|---|
| 73 |
""" |
|---|
| 74 |
return 0.0 |
|---|
| 75 |
|
|---|
| 76 |
def __getattr__(self, attr): |
|---|
| 77 |
return getattr(os, attr) |
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 |
class Syncer(object): |
|---|
| 81 |
def __init__(self, source, target): |
|---|
| 82 |
""" |
|---|
| 83 |
Init the `FTPSyncer` instance. |
|---|
| 84 |
|
|---|
| 85 |
Each of `source` and `target` is either an `FTPHost` or a |
|---|
| 86 |
`LocalHost` object. The source and target directories, resp. |
|---|
| 87 |
have to be set with the `chdir` command before passing them |
|---|
| 88 |
in. The semantics is so that the items under the source |
|---|
| 89 |
directory will show up under the target directory after the |
|---|
| 90 |
synchronization (unless there's an error). |
|---|
| 91 |
""" |
|---|
| 92 |
self._source = source |
|---|
| 93 |
self._target = target |
|---|
| 94 |
|
|---|
| 95 |
def _mkdir(self, target_dir): |
|---|
| 96 |
""" |
|---|
| 97 |
Try to create the target directory `target_dir`. If it already |
|---|
| 98 |
exists, don't do anything. If the directory is present but |
|---|
| 99 |
it's actually a file, raise a `SyncError`. |
|---|
| 100 |
""" |
|---|
| 101 |
|
|---|
| 102 |
|
|---|
| 103 |
|
|---|
| 104 |
if self._target.path.isfile(target_dir): |
|---|
| 105 |
raise ftp_error.SyncError("target dir '%s' is actually a file" % |
|---|
| 106 |
target_dir) |
|---|
| 107 |
if not self._target.path.isdir(target_dir): |
|---|
| 108 |
self._target.mkdir(target_dir) |
|---|
| 109 |
|
|---|
| 110 |
def _sync_file(self, source_file, target_file): |
|---|
| 111 |
|
|---|
| 112 |
|
|---|
| 113 |
|
|---|
| 114 |
|
|---|
| 115 |
|
|---|
| 116 |
|
|---|
| 117 |
|
|---|
| 118 |
print "Syncing", source_file, "->", target_file |
|---|
| 119 |
source = self._source.open(source_file, "rb") |
|---|
| 120 |
try: |
|---|
| 121 |
target = self._target.open(target_file, "wb") |
|---|
| 122 |
try: |
|---|
| 123 |
shutil.copyfileobj(source, target, length=CHUNK_SIZE) |
|---|
| 124 |
finally: |
|---|
| 125 |
target.close() |
|---|
| 126 |
finally: |
|---|
| 127 |
source.close() |
|---|
| 128 |
|
|---|
| 129 |
def _sync_tree(self, source_dir, target_dir): |
|---|
| 130 |
""" |
|---|
| 131 |
Synchronize the source and the target directory tree by |
|---|
| 132 |
updating the target to match the source as far as possible. |
|---|
| 133 |
|
|---|
| 134 |
Current limitations: |
|---|
| 135 |
- _don't_ delete items which are on the target path but not on the |
|---|
| 136 |
source path |
|---|
| 137 |
- files are always copied, the modification timestamps are not |
|---|
| 138 |
compared |
|---|
| 139 |
- all files are copied in binary mode, never in ASCII/text mode |
|---|
| 140 |
- incomplete error handling |
|---|
| 141 |
""" |
|---|
| 142 |
self._mkdir(target_dir) |
|---|
| 143 |
for dirpath, dirnames, filenames in self._source.walk(source_dir): |
|---|
| 144 |
for dirname in dirnames: |
|---|
| 145 |
inner_source_dir = self._source.path.join(dirpath, dirname) |
|---|
| 146 |
inner_target_dir = inner_source_dir.replace(source_dir, |
|---|
| 147 |
target_dir, 1) |
|---|
| 148 |
self._mkdir(inner_target_dir) |
|---|
| 149 |
for filename in filenames: |
|---|
| 150 |
source_file = self._source.path.join(dirpath, filename) |
|---|
| 151 |
target_file = source_file.replace(source_dir, target_dir, 1) |
|---|
| 152 |
self._sync_file(source_file, target_file) |
|---|
| 153 |
|
|---|
| 154 |
def sync(self, source_path, target_path): |
|---|
| 155 |
""" |
|---|
| 156 |
Synchronize `source_path` and `target_path` (both are strings, |
|---|
| 157 |
each denoting a directory or file path), i. e. update the |
|---|
| 158 |
target path so that it's a copy of the source path. |
|---|
| 159 |
|
|---|
| 160 |
This method handles both directory trees and single files. |
|---|
| 161 |
""" |
|---|
| 162 |
|
|---|
| 163 |
source_path = self._source.path.abspath(source_path) |
|---|
| 164 |
target_path = self._target.path.abspath(target_path) |
|---|
| 165 |
if self._source.path.isfile(source_path): |
|---|
| 166 |
self._sync_file(source_path, target_path) |
|---|
| 167 |
else: |
|---|
| 168 |
self._sync_tree(source_path, target_path) |
|---|
| 169 |
|
|---|