diff --git a/sync_sftp.py b/sync_sftp.py index 794ad5b..ed33d6a 100644 --- a/sync_sftp.py +++ b/sync_sftp.py @@ -1,6 +1,8 @@ import os -import logging import sys +import pathlib +import datetime +import logging import tqdm import paramiko.util @@ -23,7 +25,13 @@ def download_files(): for file_name in tqdm.tqdm(settings.files, file=sys.stdout): source = os.path.join(settings.paths['source'], file_name) destination = os.path.join(settings.paths['destination'], file_name) - sftp.get(source, destination) + remote_timestamp, destination_timestamp = sftp.stat(source).st_mtime, pathlib.Path( + destination).stat().st_mtime + if remote_timestamp > destination_timestamp: + sftp.get(source, destination) + else: + logging.info(f"Remote file {file_name} - {ts(remote_timestamp)} has lower or equal timestamp " + f"as destination file {destination} - {ts(destination_timestamp)}. Download skipped.") except socket.timeout as msg: logging.warning(f"A timeout was ignored: Actual exception message: {msg}") pass @@ -31,6 +39,15 @@ def download_files(): return None -if __name__ == '__main__': +def ts(timestamp: float) -> str: + """Return human readable time from `timestamp`. + + :param timestamp: st_mtime from file. + """ + dt = datetime.datetime.fromtimestamp(timestamp) + return dt.strftime("%Y-%m-%d %H:%M:%S") + + +if __name__ == '__main__': download_files()