Package: circadian_scp_upload
📮 Circadian SCP UploadResumable, interruptible, well-tested SCP upload client for any files or directories generated day by day.Use CaseYou have a local directory that generates daily data on your local machine. The directory looks like this:📁 data-directory-1
├── 📁 20190101
│   ├── 📄 file1.txt
│   ├── 📄 file2.txt
│   └── 📄 file3.txt
└── 📁 20190102
    ├── 📄 file1.txt
    ├── 📄 file2.txt
    └── 📄 file3.txtOr like this:📁 data-directory-2
├── 📄 20190101.txt
├── 📄 20190102-a.txt
├── 📄 20190102-b.txt
└── 📄 20190103.txtYou want to upload that data to a server, but only after the day of creation. Additionally, you want to mark the directories as "in progress" on the remote server so that subsequent processing steps will not touch unfinished days of data while uploading.This tool usesSCPvia the Python libraryparamikoto do that. It will write files named.do-not-touchin the local and remote directories during the upload process and delete them afterward.Below is a code snippet that defines a specific directory/file naming scheme (for example,%Y%m%d-(\.txt|\.csv)). The client uses this information to tellwhena specific file or directory was generated. It will only upload files when at least one hour of the following day has passed.Can't I usersyncor a similar CLI tool for that?Yes, of course. However, the actual copying logic of individual files or directories is just 130 lines of code of this repository. The rest of this library is dedicated to being a plug-and-play solution for any Python codebase: logging, regex filters, being interruptable, in-progress markers, and so on.One should be able topip install/poetry add/... and call a well-documented and typed upload client class instead of manually connecting each codebase to rsync and doing all the pattern and scheduling logic repeatedly.How do you make sure that the upload works correctly?First, the whole codebase has type hints and is strictly checked withMypy- even the snippet in the usage section below is tye checked with Mypy.Secondly, the date patterning is tested extensively, and the upload process of the files and directories is tested with an actual remote server by generating a bunch of sample files and directories and uploading them to that server. One can check out the output of the test runs in theGitHub Actionsof this repository - in the "Run pytests" step.Thirdly, after the upload, the checksum of the local and the remote directories/files is compared to ensure the upload was successful. Only if those checksums match will the client delete the local files. The file removal has to be actively enabled or disabled.UsageInstall into any Python^3.10project:pipinstallcircadian_scp_upload# orpoetryaddcircadian_scp_uploadConfigure and use the upload client:importcircadian_scp_upload# Use the callbacks to customize the upload process# and integrate it into your own codebase. All callbacks# are optional and the callback object does not need to be# passed to the upload client. The lambda functions below# are the default values.upload_client_callbacks=circadian_scp_upload.UploadClientCallbacks(# which directories/files to consider in the upload process; only# supports %Y/%y/%m/%d - does not support parentheses in the stringdated_regex=r"^"+"%Y%m%d"+r"$",# use your own logger instead of print statementslog_info=lambdamessage:print(f"INFO -{message}"),log_error=lambdamessage:print(f"ERROR -{message}"),# callback that is called periodically during the upload# process to check if the upload should be abortedshould_abort_upload=lambda:False,)# teardown happens automatically when leaving the "with"-blockwithcircadian_scp_upload.RemoteConnection("1.2.3.4","someusername","somepassword")asremote_connection:# upload a directory full of directories "YYYYMMDD/"circadian_scp_upload.DailyTransferClient(remote_connection=remote_connection,src_path="/path/to/local/data-directory-1",dst_path="/path/to/remote/data-directory-1",remove_files_after_upload=True,variant="directories",callbacks=upload_client_callbacks,).run()# upload a directory full of files "YYYYMMDD.txt"circadian_scp_upload.DailyTransferClient(remote_connection=remote_connection,src_path="/path/to/local/data-directory-2",dst_path="/path/to/remote/data-directory-2",remove_files_after_upload=True,variant="files",callbacks=upload_client_callbacks,).run()The client will produce an informational output wherever one directs the log output - the progress is only logged at steps of 10%:INFO - 2005-06-20: found 1 paths for this date: ['/tmp/circadian_scp_upload_test_1693053096_3.10.12/20050620']
INFO - 2005-06-20: starting to upload directory local directory '/tmp/circadian_scp_upload_test_1693053096_3.10.12/20050620' to remote directory '/tmp/circadian_scp_upload_test_1693053096_3.10.12/20050620'
INFO - 2005-06-20: found 5 files in src directory
INFO - 2005-06-20: 5 files missing in dst
INFO - 2005-06-20: created remote directory
INFO - 2005-06-20:   0 % (1/5) uploaded
INFO - 2005-06-20:  20 % (2/5) uploaded
INFO - 2005-06-20:  40 % (3/5) uploaded
INFO - 2005-06-20:  60 % (4/5) uploaded
INFO - 2005-06-20:  80 % (5/5) uploaded
INFO - 2005-06-20: 100 % (5/5) uploaded (finished)
INFO - 2005-06-20: checksums match
INFO - 2005-06-20: finished removing source
INFO - 2005-06-20: done (successful)
INFO - 2023-08-23: found 1 paths for this date: ['/tmp/circadian_scp_upload_test_1693053096_3.10.12/20230823']
INFO - 2023-08-23: starting to upload directory local directory '/tmp/circadian_scp_upload_test_1693053096_3.10.12/20230823' to remote directory '/tmp/circadian_scp_upload_test_1693053096_3.10.12/20230823'
INFO - 2023-08-23: found 5 files in src directory
INFO - 2023-08-23: 5 files missing in dst
INFO - 2023-08-23: created remote directory
INFO - 2023-08-23:   0 % (1/5) uploaded
INFO - 2023-08-23:  20 % (2/5) uploaded
INFO - 2023-08-23:  40 % (3/5) uploaded
INFO - 2023-08-23:  60 % (4/5) uploaded
INFO - 2023-08-23:  80 % (5/5) uploaded
INFO - 2023-08-23: 100 % (5/5) uploaded (finished)
INFO - 2023-08-23: checksums match
INFO - 2023-08-23: finished removing source
INFO - 2023-08-23: done (successful)