self-hosted-deployment/sync_files/sync_files.py
2023-01-01 18:43:58 -05:00

45 lines
1.4 KiB
Python

from shutil import copyfile
import os
import time
import uuid
def is_done_being_written(file_path):
file_size_before = os.path.getsize(file_path)
print('Size before: ', file_size_before)
time.sleep(1) # 1s
file_size_after = os.path.getsize(file_path)
print('Size after: ', file_size_after)
return file_size_before == file_size_after
def copy(source_file_path):
destination_dir = '/target'
destination_file_path = os.path.join(destination_dir, str(uuid.uuid4()) + '.pdf')
print(source_file_path, ' -> ', destination_file_path)
print('Checking if the file is done being written...')
while not is_done_being_written(source_file_path):
continue
print('File is stable, copying...')
copyfile(source_file_path, destination_file_path)
print('Deleting original...')
os.remove(source_file_path)
def main():
while True:
try:
source_dir = '/source'
for file_name in os.listdir(source_dir):
file_path = os.path.join(source_dir, file_name)
try:
copy(file_path)
except Exception as err:
print('Failed to copy ', file_path)
print(err)
except Exception as err:
print(err)
print('Done!')
print('Sleeping for 10m...')
time.sleep(60 * 10) #10m
if __name__ == "__main__":
main()