Skip to content

Commit

Permalink
Issue #3 consider filestamp of file before downloading
Browse files Browse the repository at this point in the history
  • Loading branch information
ALee008 committed Mar 25, 2022
1 parent 517c599 commit 3e9cc77
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions sync_sftp.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import os
import logging
import sys
import pathlib
import datetime
import logging

import tqdm
import paramiko.util
Expand All @@ -23,14 +25,29 @@ 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

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()

0 comments on commit 3e9cc77

Please sign in to comment.