Implemented reporting callbacks.
Both the dependency Tracer class and the Packer class now support a callback object, where the latter is a subclass of the former. For file transfers running in a separate thread, there is a thread-safe wrapper for progress callbacks. This wrapper can be called from any thread, and calls the wrapped callback object from the main thread. This way the callback implementation itself doesn't have to worry about threading issues.
This commit is contained in:
@@ -7,17 +7,9 @@ from . import transfer
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class FileCopier(threading.Thread, transfer.FileTransferer):
|
||||
class FileCopier(transfer.FileTransferer):
|
||||
"""Copies or moves files in source directory order."""
|
||||
|
||||
def __init__(self) -> None:
|
||||
# Stupid Thread.__init__ doesn't call super().__init__(),
|
||||
# so it doesn't get chained to transfer.FileTransferer.__init__().
|
||||
# However, I want to have Thread as first subclass so that its
|
||||
# start() and join() methods Just Work™.
|
||||
threading.Thread.__init__(self)
|
||||
transfer.FileTransferer.__init__(self)
|
||||
|
||||
def run(self) -> None:
|
||||
files_transferred = 0
|
||||
files_skipped = 0
|
||||
@@ -29,8 +21,8 @@ class FileCopier(threading.Thread, transfer.FileTransferer):
|
||||
|
||||
for src, dst, act in self.iter_queue():
|
||||
try:
|
||||
st_src = src.stat() # must exist, or it wouldn't be queued.
|
||||
if dst.exists():
|
||||
st_src = src.stat()
|
||||
st_dst = dst.stat()
|
||||
if st_dst.st_size == st_src.st_size and st_dst.st_mtime >= st_src.st_mtime:
|
||||
log.info('SKIP %s; already exists', src)
|
||||
@@ -46,6 +38,7 @@ class FileCopier(threading.Thread, transfer.FileTransferer):
|
||||
# TODO(Sybren): when we target Py 3.6+, remove the str() calls.
|
||||
tfunc = transfer_funcs[act]
|
||||
tfunc(str(src), str(dst)) # type: ignore
|
||||
self.report_transferred(st_src.st_size)
|
||||
|
||||
files_transferred += 1
|
||||
except Exception:
|
||||
|
||||
Reference in New Issue
Block a user