-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
634 lines (512 loc) · 20 KB
/
main.py
File metadata and controls
634 lines (512 loc) · 20 KB
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
"""
File Synchronization Project
This script provides functionality for synchronizing files between different storage locations,
including FTP servers, zip archives, and local folders. It supports automatic synchronization
of added, deleted, and modified files, as well as periodic monitoring of FTP and folder changes.
Modules:
- FTPLocation: Interacts with an FTP server for file operations.
- ZipLocation: Interacts with zip archives for file operations.
- FolderLocation: Interacts with local file systems for file operations.
- sync_files: Synchronizes files between two locations.
- monitor_ftp_changes: Periodically monitors an FTP server for changes and syncs them.
- monitor_folder: Monitors local folders or FTP servers for changes.
- create_location: Creates the appropriate location object based on a given string.
- calculate_checksum: Calculates the MD5 checksum of a file to detect modifications.
- MyHandler: A custom handler for monitoring file system events.
Dependencies:
- ftplib: For FTP interaction.
- zipfile: For zip file handling.
- os: For file and directory manipulation.
- hashlib: For checksum calculation.
- watchdog: For monitoring file system events.
"""
from abc import ABC, abstractmethod
import os
import zipfile
import ftplib
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
from urllib.parse import urlparse
import time
import hashlib
import sys
class Location(ABC):
"""
Abstract base class for different storage locations (FTP, Zip, Folder).
Methods:
list_files: Lists all files in the location.
read_file: Reads a file's content.
write_file: Writes content to a file.
delete_file: Deletes a file.
get_file_mod_time: Returns the modification time of a file.
"""
@abstractmethod
def list_files(self):
"""
Lists all files in the storage location.
Returns:
List of filenames in the location.
"""
pass
@abstractmethod
def read_file(self, file_name):
"""
Reads the content of a file.
Args:
file_name (str): The name of the file to read.
Returns:
bytes: The content of the file.
"""
pass
@abstractmethod
def write_file(self, file_name, content):
"""
Writes content to a file.
Args:
file_name (str): The name of the file to write to.
content (bytes): The content to write to the file.
"""
pass
@abstractmethod
def delete_file(self, file_name):
"""
Deletes a file.
Args:
file_name (str): The name of the file to delete.
"""
pass
@abstractmethod
def get_file_mod_time(self, file_name):
"""
Gets the modification time of a file.
Args:
file_name (str): The name of the file.
Returns:
float: The modification time as a Unix timestamp.
"""
pass
class FTPLocation(Location):
"""
Represents an FTP server location for file synchronization.
Args:
ftp_url (str): The FTP server URL.
username (str): The username for FTP login.
password (str): The password for FTP login.
folder_path (str, optional): The path to a specific folder on the FTP server.
Methods:
list_files: Lists files on the FTP server.
read_file: Reads a file from the FTP server.
write_file: Writes a file to the FTP server.
delete_file: Deletes a file from the FTP server.
get_file_mod_time: Gets the modification time of a file on the FTP server.
"""
def __init__(self, ftp_url, username, password, folder_path=None):
"""
Initializes the FTPLocation with FTP server credentials and optional folder path.
Args:
ftp_url (str): The FTP server URL.
username (str): The username for FTP login.
password (str): The password for FTP login.
folder_path (str, optional): The folder path to access on the FTP server.
"""
self.ftp_url = ftp_url
self.username = username
self.password = password
self.folder_path = folder_path
self.ftp = ftplib.FTP(self.ftp_url)
self.ftp.login(self.username, self.password)
if self.folder_path:
self.ftp.cwd(self.folder_path) # Navigate to the specified folder
def list_files(self):
"""
Lists all files in the current directory on the FTP server.
Returns:
list: A list of filenames in the current directory on the FTP server.
"""
return self.ftp.nlst() # List all files in the current directory on FTP
def read_file(self, file_name):
"""
Reads the content of a file from the FTP server.
Args:
file_name (str): The name of the file to read.
Returns:
bytes: The content of the file.
"""
content = bytearray()
self.ftp.retrbinary(f"RETR {file_name}", content.extend)
return bytes(content)
def write_file(self, file_name, content):
"""
Writes content to a file on the FTP server.
Args:
file_name (str): The name of the file to write to.
content (bytes): The content to write to the file.
"""
with open("tempfile", "wb") as temp:
temp.write(content)
with open("tempfile", "rb") as temp:
self.ftp.storbinary(f"STOR {file_name}", temp)
os.remove("tempfile")
def delete_file(self, file_name):
"""
Deletes a file from the FTP server.
Args:
file_name (str): The name of the file to delete.
"""
try:
self.ftp.delete(file_name)
print(f"Deleted {file_name} from FTP")
except ftplib.error_perm as e:
print(f"Error deleting file {file_name}: {e}")
def get_file_mod_time(self, file_name):
"""
Gets the modification time of a file on the FTP server.
Args:
file_name (str): The name of the file.
Returns:
float: The modification time as a Unix timestamp.
"""
try:
mod_time = self.ftp.sendcmd(f"MDTM {file_name}")
return time.mktime(time.strptime(mod_time[4:], "%Y%m%d%H%M%S"))
except ftplib.error_perm as e:
print(f"Error getting modification time for {file_name}: {e}")
return 0
class ZipLocation(Location):
"""
Represents a zip archive location for file synchronization.
Args:
zip_path (str): The path to the zip archive.
Methods:
list_files: Lists files in the zip archive.
read_file: Reads a file from the zip archive.
write_file: Writes content to a file in the zip archive.
delete_file: Deletes a file from the zip archive.
get_file_mod_time: Gets the modification time of a file in the zip archive.
"""
def __init__(self, zip_path):
"""
Initializes the ZipLocation with the path to the zip archive.
Args:
zip_path (str): The path to the zip archive.
"""
self.zip_path = zip_path
def list_files(self):
"""
Lists all files in the zip archive.
Returns:
list: A list of filenames in the zip archive.
"""
with zipfile.ZipFile(self.zip_path, 'r') as zipf:
return zipf.namelist()
def read_file(self, file_name):
"""
Reads the content of a file from the zip archive.
Args:
file_name (str): The name of the file to read.
Returns:
bytes: The content of the file.
"""
with zipfile.ZipFile(self.zip_path, 'r') as zipf:
return zipf.read(file_name)
def write_file(self, file_name, content):
"""
Writes content to a file in the zip archive.
Args:
file_name (str): The name of the file to write to.
content (bytes): The content to write to the file.
"""
temp_zip_path = self.zip_path + ".tmp"
with zipfile.ZipFile(self.zip_path, 'r') as zipf:
with zipfile.ZipFile(temp_zip_path, 'w') as temp_zip:
for item in zipf.infolist():
if item.filename != file_name:
temp_zip.writestr(item, zipf.read(item.filename))
# Adaugă fișierul nou
with zipfile.ZipFile(temp_zip_path, 'a') as temp_zip:
temp_zip.writestr(file_name, content)
# Înlocuiește arhiva originală cu cea temporară
os.replace(temp_zip_path, self.zip_path)
def delete_file(self, file_name):
"""
Deletes a file from the zip archive.
Args:
file_name (str): The name of the file to delete.
"""
temp_path = self.zip_path + "_temp"
with zipfile.ZipFile(self.zip_path, 'r') as zipf:
with zipfile.ZipFile(temp_path, 'w') as temp_zip:
for item in zipf.infolist():
if item.filename != file_name:
temp_zip.writestr(item.filename, zipf.read(item.filename))
os.replace(temp_path, self.zip_path)
def get_file_mod_time(self, file_name):
"""
Gets the modification time of a file in the zip archive.
Args:
file_name (str): The name of the file.
Returns:
float: The modification time as a Unix timestamp.
"""
with zipfile.ZipFile(self.zip_path, 'r') as zipf:
info = zipf.getinfo(file_name)
return time.mktime(info.date_time + (0, 0, -1))
class FolderLocation(Location):
"""
Represents a local folder location for file synchronization.
Args:
folder_path (str): The path to the folder.
Methods:
list_files: Lists files in the local folder.
read_file: Reads a file from the local folder.
write_file: Writes content to a file in the local folder.
delete_file: Deletes a file from the local folder.
get_file_mod_time: Gets the modification time of a file in the local folder.
"""
def __init__(self, folder_path):
"""
Initializes the FolderLocation with the path to the local folder.
Args:
folder_path (str): The path to the folder.
"""
self.folder_path = folder_path
def list_files(self):
"""
Lists all files in the local folder.
Returns:
list: A list of filenames in the local folder.
"""
return [f for f in os.listdir(self.folder_path) if os.path.isfile(os.path.join(self.folder_path, f))]
def read_file(self, file_name):
"""
Reads the content of a file from the local folder.
Args:
file_name (str): The name of the file to read.
Returns:
bytes: The content of the file.
"""
with open(os.path.join(self.folder_path, file_name), 'rb') as f:
return f.read()
def write_file(self, file_name, content):
"""
Writes content to a file in the local folder.
Args:
file_name (str): The name of the file to write to.
content (bytes): The content to write to the file.
"""
with open(os.path.join(self.folder_path, file_name), 'wb') as f:
f.write(content)
def delete_file(self, file_name):
"""
Deletes a file from the local folder.
Args:
file_name (str): The name of the file to delete.
"""
os.remove(os.path.join(self.folder_path, file_name))
def get_file_mod_time(self, file_name):
"""
Gets the modification time of a file in the local folder.
Args:
file_name (str): The name of the file.
Returns:
float: The modification time as a Unix timestamp.
"""
return os.path.getmtime(os.path.join(self.folder_path, file_name))
def sync_files(loc1, loc2):
"""
Synchronizes files between two locations.
Args:
loc1 (Location): The first storage location.
loc2 (Location): The second storage location.
"""
files1 = set(loc1.list_files())
files2 = set(loc2.list_files())
all_files = files1.union(files2)
for file in all_files:
in_loc1 = file in files1
in_loc2 = file in files2
if in_loc1 and in_loc2:
mod_time1 = loc1.get_file_mod_time(file)
mod_time2 = loc2.get_file_mod_time(file)
if mod_time1 > mod_time2:
content = loc1.read_file(file)
loc2.write_file(file, content)
elif mod_time2 > mod_time1:
content = loc2.read_file(file)
loc1.write_file(file, content)
if in_loc1 and not in_loc2:
content = loc1.read_file(file)
loc2.write_file(file, content)
if in_loc2 and not in_loc1:
content = loc2.read_file(file)
loc1.write_file(file, content)
class MyHandler(FileSystemEventHandler):
"""
Custom event handler for monitoring file system changes.
Args:
loc1 (Location): The first location to synchronize.
loc2 (Location): The second location to synchronize.
Methods:
on_created: Handles file creation events.
on_deleted: Handles file deletion events.
on_modified: Handles file modification events.
"""
def __init__(self, loc1, loc2):
"""
Initializes the event handler with two locations.
Args:
loc1 (Location): The first location to synchronize.
loc2 (Location): The second location to synchronize.
"""
self.loc1 = loc1
self.loc2 = loc2
def on_created(self, event):
"""
Handles file creation events.
Args:
event (FileSystemEvent): The event object containing event details.
"""
if event.is_directory:
return
print(f"File created: {event.src_path}")
sync_files(self.loc1, self.loc2)
def on_deleted(self, event):
"""
Handles file deletion events.
Args:
event (FileSystemEvent): The event object containing event details.
"""
if event.is_directory:
return
print(f"File deleted: {event.src_path}")
file_name = os.path.basename(event.src_path)
if file_name in self.loc1.list_files():
self.loc1.delete_file(file_name)
if file_name in self.loc2.list_files():
self.loc2.delete_file(file_name)
def on_modified(self, event):
"""
Handles file modification events.
Args:
event (FileSystemEvent): The event object containing event details.
"""
if event.is_directory:
return
print(f"File modified: {event.src_path}")
sync_files(self.loc1, self.loc2)
def calculate_checksum(file_content):
"""
Calculates the MD5 checksum of a file content.
Args:
file_content (bytes): The content of the file.
Returns:
str: The MD5 checksum of the file content.
"""
return hashlib.md5(file_content).hexdigest()
def monitor_ftp_changes(ftp_loc, other_loc, poll_interval=10):
"""
Monitors FTP for changes and synchronizes with the other location.
Args:
ftp_loc (FTPLocation): The FTP location to monitor.
other_loc (Location): The location to synchronize with.
poll_interval (int): The polling interval in seconds.
"""
print("Monitoring FTP for changes...")
previous_files = set(ftp_loc.list_files())
try:
while True:
time.sleep(poll_interval)
current_files = set(ftp_loc.list_files())
# Detect added files
added_files = current_files - previous_files
for file in added_files:
print(f"New file detected on FTP: {file}")
content = ftp_loc.read_file(file)
other_loc.write_file(file, content)
# Detect deleted files
deleted_files = previous_files - current_files
for file in deleted_files:
print(f"File deleted from FTP: {file}")
other_loc.delete_file(file)
# Detect modified files by comparing checksums
for file in current_files & previous_files:
# Fetch the file contents and calculate checksums
ftp_content = ftp_loc.read_file(file)
other_content = other_loc.read_file(file)
ftp_checksum = calculate_checksum(ftp_content)
other_checksum = calculate_checksum(other_content)
if ftp_checksum != other_checksum:
print(f"File modified on FTP: {file}")
other_loc.write_file(file, ftp_content)
elif ftp_checksum != other_checksum:
print(f"File modified locally: {file}")
ftp_loc.write_file(file, other_content)
previous_files = current_files
except KeyboardInterrupt:
print("Stopped monitoring FTP for changes.")
def monitor_folder(loc1, loc2):
"""
Monitors changes in local folders or FTP servers and synchronizes them.
Args:
loc1 (Location): The first location to monitor.
loc2 (Location): The second location to monitor.
"""
if isinstance(loc1, FTPLocation):
monitor_ftp_changes(loc1, loc2)
elif isinstance(loc2, FTPLocation):
monitor_ftp_changes(loc2, loc1)
else:
# Use filesystem event monitoring for local folders
event_handler = MyHandler(loc1, loc2)
observer = Observer()
if isinstance(loc1, FolderLocation):
observer.schedule(event_handler, loc1.folder_path, recursive=True)
if isinstance(loc2, FolderLocation):
observer.schedule(event_handler, loc2.folder_path, recursive=True)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
def create_location(location_str):
"""
Creates a location object based on a string representation.
Args:
location_str (str): The location string (e.g., "ftp://...").
Returns:
Location: The corresponding location object (FTPLocation, ZipLocation, or FolderLocation).
Raises:
ValueError: If the location string is not valid.
"""
if location_str.startswith("ftp:"):
ftp_url = location_str[4:]
parsed_url = urlparse(f"ftp://{ftp_url}")
user = parsed_url.username
password = parsed_url.password
host = parsed_url.hostname
path = parsed_url.path.lstrip("/") # folderul de pe FTP
if user and password and host:
return FTPLocation(host, user, password, path)
else:
raise ValueError("URL-ul FTP trebuie să conțină utilizator, parolă și host.")
elif location_str.startswith("zip:"):
return ZipLocation(location_str[4:])
elif location_str.startswith("folder:"):
return FolderLocation(location_str[7:])
else:
raise ValueError("Unknown location type")
if __name__ == "__main__":
"""
Main entry point for the script. Synchronizes files between two specified locations.
Command-line usage:
python main.py <location_1> <location_2>
"""
if len(sys.argv) != 3:
print("Usage: main.py <location_1> <location_2>")
sys.exit(1)
loc1 = create_location(sys.argv[1])
loc2 = create_location(sys.argv[2])
sync_files(loc1, loc2)
monitor_folder(loc1, loc2)