-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
226 lines (166 loc) · 7.11 KB
/
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import sys
import time
from pathlib import Path
from threading import Thread, Lock
from multiprocessing import cpu_count
from typing import List, Tuple
from utils import create_folder, delete_path, get_items_chunks, replicate_file
from logging_config import logger, setup_logging
# Create a lock to synchronize access to shared resources across threads
lock: Lock = Lock()
def synchronize_folders(item_list: List[str], source_path: Path, replica_path: Path) -> None:
"""
Synchronizes files and folders from a source directory to a replica directory.
Args:
item_list (list): List of relative paths of items within the source directory.
source_path (Path): Path to the source directory.
replica_path (Path): Path to the replica directory where items will be synchronized.
Returns:
None
"""
for item_path in item_list:
item = source_path / item_path
if item.is_dir():
create_folder(replica_path / item_path)
elif item.is_file():
with lock:
replicate_file(item, replica_path / item_path)
def clean_up_replica(item_list: List[str], source_path: Path, replica_path: Path) -> None:
"""
Cleans up the replica directory by deleting items that no longer exist in the source directory.
Args:
item_list (list): List of relative paths of items within the replica directory.
source_path (Path): Path to the source directory.
replica_path (Path): Path to the replica directory.
Returns:
None
"""
for item_path in item_list:
item = replica_path / item_path
with lock:
if not (source_path / item_path).exists():
delete_path(item)
def process_without_threads(source_items: List[str], replica_items: List[str],
source_path: Path, replica_path: Path) -> None:
"""
Synchronize folders and clean up replica items without using threads.
Args:
source_items (list): List of chunks of source items.
replica_items (list): List of chunks of replica items.
source_path (Path): Path object representing the source folder.
replica_path (Path): Path object representing the replica folder.
Returns:
None
"""
synchronize_folders(source_items, source_path, replica_path)
clean_up_replica(replica_items, source_path, replica_path)
def process_with_threads(source_items: List[List[str]], replica_items: List[List[str]],
source_path: Path, replica_path: Path) -> None:
"""
Synchronize folders and clean up replica items using multiple threads.
Args:
source_items (list): List of chunks of source items.
replica_items (list): List of chunks of replica items.
source_path (Path): Path object representing the source folder.
replica_path (Path): Path object representing the replica folder.
Returns:
None
"""
threads = []
# Create threads for synchronizing folders from source to replica
for chunk in source_items:
t = Thread(target=synchronize_folders, args=(chunk, source_path, replica_path,))
t.start()
threads.append(t)
# Create threads for cleaning up replica items that are no longer in source
for chunk in replica_items:
t = Thread(target=clean_up_replica, args=(chunk, source_path, replica_path,))
t.start()
threads.append(t)
# Wait for all threads to complete
for t in threads:
t.join()
threads = []
# Create threads for synchronizing folders from source to replica
for chunk in source_items:
t = Thread(target=synchronize_folders, args=(chunk, source_path, replica_path,))
t.start()
threads.append(t)
# Create threads for cleaning up replica items that are no longer in source
for chunk in replica_items:
t = Thread(target=clean_up_replica, args=(chunk, source_path, replica_path,))
t.start()
threads.append(t)
# Wait for all threads to complete
for t in threads:
t.join()
def process_syncronization(source_folder: str, replica_folder: str) -> None:
"""
Synchronize the source folder with the replica folder using multiple threads if available.
Args:
source_folder (str): Path to the source folder.
replica_folder (str): Path to the replica folder.
Returns:
None
"""
source_path = Path(source_folder)
replica_path = Path(replica_folder)
number_of_threads = cpu_count()
# Uncomment to run without threading
"""
number_of_threads = 1
"""
# Divide source and replica items into chunks for concurrent processing
source_items = get_items_chunks(source_path, number_of_threads)
replica_items = get_items_chunks(replica_path, number_of_threads)
# Determine whether to use threads based on the number of available CPUs
if number_of_threads > 1:
process_with_threads(source_items, replica_items, source_path, replica_path)
else:
process_without_threads(source_items[0], replica_items[0], source_path, replica_path)
def parse_arguments() -> Tuple[str, str, int, str]:
"""
Parses command-line arguments for source folder, replica folder, synchronization interval, and log file.
Returns:
tuple: A tuple containing parsed values:
- source_folder (str): Path to the source folder.
- replica_folder (str): Path to the replica folder.
- sync_interval (int): Interval in seconds for synchronization.
- log_file (str): Path to the log file.
"""
if len(sys.argv) < 5:
print("Usage: python main.py <source_folder> <replica_folder> <sync_interval> <log_file>")
sys.exit(1)
# Assign command-line arguments to variables
source_folder = sys.argv[1]
replica_folder = sys.argv[2]
sync_interval = int(sys.argv[3])
log_file = sys.argv[4]
return source_folder, replica_folder, sync_interval, log_file
def main() -> None:
"""
Entry point of the synchronization script.
Parses command-line arguments for source folder, replica folder, synchronization interval, and log file,
sets up logging, and continuously creates threads for synchronization based on the specified interval.
Returns:
None
"""
if len(sys.argv) < 5:
print("Usage: python main.py <source_folder> <replica_folder> <sync_interval> <log.file>")
sys.exit(1)
# Parse command-line arguments
source_folder, replica_folder, sync_interval, log_file = parse_arguments()
# Set up logging configuration
setup_logging(log_file)
try:
# Continuously synchronize folders using threads based on the sync interval
while True:
process_syncronization(source_folder, replica_folder)
logger.info("Synchronization process completed successfully.")
time.sleep(sync_interval)
except KeyboardInterrupt:
logger.info("Program interrupted. Exiting...")
except Exception as e:
logger.error(f"Unexpected error occurred: {e}")
if __name__ == "__main__":
main()