-
Notifications
You must be signed in to change notification settings - Fork 1
/
watcher_main.py
88 lines (60 loc) · 1.92 KB
/
watcher_main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
"""
usage example
"""
import os
import time
import shutil
import pathlib
from file_sync_s3_test import *
from file_sync_s3.watcher import *
from file_sync_s3.aws_s3 import AuthBucketS3, ConfigTransferS3
logger = logging.getLogger(__name__)
this_dir = os.path.dirname(os.path.abspath(__file__))
file_sync_dir = f"{this_dir}/tmp/file_sync"
def produce_name(index:int) -> str:
return f"watcher-tester-{index}.binary"
def produce_path(file_name:str) -> str:
return f"{file_sync_dir}/{file_name}"
def produce_file(file_name:str) -> None:
file_path = produce_path(file_name)
with open(file_path, "wb") as file_unit:
file_unit.truncate(1 * 1024 * 1024)
def watcher_main():
logger.info(f"INITIATE")
shutil.rmtree(file_sync_dir, ignore_errors=True)
os.makedirs(file_sync_dir)
config_access = AuthBucketS3.default()
config_transfer = ConfigTransferS3.default()
bucket_operator = BucketOperatorS3(
config_access=config_access,
config_transfer=config_transfer,
)
folder_config = FolderConfig(
folder_path=file_sync_dir,
watcher_timeout=1,
watcher_recursive=True,
regex_include_list=[".+"],
regex_exclude_list=[],
keeper_expire=True,
keeper_diem_span=3,
keeper_scan_period=timedelta(seconds=3),
)
watcher_operator = WatcherOperator(
bucket_operator=bucket_operator,
folder_config=folder_config,
)
watcher_operator.initiate()
for index in range(3):
file_name = produce_name(index)
produce_file(file_name)
for index in range(3):
file_name = produce_name(index)
file_path = produce_path(file_name)
pathlib.Path(file_path).touch()
time.sleep(10)
shutil.rmtree(file_sync_dir, ignore_errors=True)
time.sleep(5)
watcher_operator.terminate()
logger.info(f"TERMINATE")
if __name__ == "__main__":
watcher_main()