-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFileSyncPRO.py
More file actions
554 lines (331 loc) · 12.8 KB
/
FileSyncPRO.py
File metadata and controls
554 lines (331 loc) · 12.8 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
# ==========================================================
# FileSync PRO - Enterprise Backup & Sync Suite
# Professional Desktop Tool
# ==========================================================
import os
import sys
import shutil
import threading
import traceback
import hashlib
import zipfile
from datetime import datetime
from queue import Queue, Empty
from concurrent.futures import ThreadPoolExecutor
import tkinter as tk
from tkinter import filedialog, messagebox
import ttkbootstrap as tb
from ttkbootstrap.constants import *
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
from cryptography.fernet import Fernet
# =================== APP CONFIG ===================
APP_NAME = "FileSync PRO"
APP_VERSION = "4.0.0"
# =================== APP ===================
app = tk.Tk()
app.title(f"{APP_NAME} {APP_VERSION}")
app.geometry("1200x640")
tb.Style("darkly")
# =================== FLAGS ===================
ui_queue = Queue()
source_folder = tb.StringVar()
target_folder = tb.StringVar()
filter_ext = tb.StringVar(value="")
mirror_mode = tb.BooleanVar(value=False)
verify_hash = tb.BooleanVar(value=True)
zip_backup = tb.BooleanVar(value=False)
encrypt_backup = tb.BooleanVar(value=False)
sync_running = False
observer = None
files_synced = 0
bytes_copied = 0
# =================== UTIL ===================
def resource_path(file_name):
base = getattr(sys, "_MEIPASS", os.path.dirname(os.path.abspath(__file__)))
return os.path.join(base, file_name)
def log(msg):
ui_queue.put(("log", msg))
def update_progress(v):
ui_queue.put(("progress", v))
def update_stats():
ui_queue.put(("stats", (files_synced, bytes_copied)))
def log_error():
with open("error.log","a",encoding="utf-8") as f:
f.write(traceback.format_exc()+"\n")
def hash_file(path):
md5 = hashlib.md5()
with open(path,"rb") as f:
for chunk in iter(lambda:f.read(4096),b""):
md5.update(chunk)
return md5.hexdigest()
def allowed_file(file):
ext = filter_ext.get().strip()
if not ext:
return True
allowed = [e.strip().lower() for e in ext.split(",")]
return os.path.splitext(file)[1].lower() in allowed
# =================== ENCRYPTION ===================
def generate_key():
return Fernet.generate_key()
def encrypt_file(path):
key = generate_key()
fernet = Fernet(key)
with open(path,"rb") as f:
data = f.read()
encrypted = fernet.encrypt(data)
with open(path+".enc","wb") as f:
f.write(encrypted)
os.remove(path)
return key
# =================== FILE COPY ===================
def sync_file(src_file, dst_file):
global files_synced, bytes_copied
try:
os.makedirs(os.path.dirname(dst_file),exist_ok=True)
copy_required = False
if not os.path.exists(dst_file):
copy_required = True
else:
if os.path.getmtime(src_file) > os.path.getmtime(dst_file):
if verify_hash.get():
if hash_file(src_file) != hash_file(dst_file):
copy_required = True
else:
copy_required = True
if copy_required:
shutil.copy2(src_file,dst_file)
size = os.path.getsize(src_file)
files_synced += 1
bytes_copied += size
log(f"✔ Synced: {os.path.basename(src_file)}")
except:
log_error()
# =================== MULTI THREAD SYNC ===================
def full_sync():
global files_synced, bytes_copied
if not source_folder.get() or not target_folder.get():
messagebox.showerror("Error","Select folders first")
return
files_synced = 0
bytes_copied = 0
src = source_folder.get()
dst = target_folder.get()
log("🔄 Multi-thread synchronization started")
file_list = []
for root,dirs,files in os.walk(src):
for f in files:
if allowed_file(f):
file_list.append(os.path.join(root,f))
total = len(file_list)
executor = ThreadPoolExecutor(max_workers=6)
for i,src_file in enumerate(file_list):
rel = os.path.relpath(src_file,src)
dst_file = os.path.join(dst,rel)
executor.submit(sync_file,src_file,dst_file)
progress = int((i+1)/total*100)
update_progress(progress)
executor.shutdown(wait=True)
update_stats()
if mirror_mode.get():
for root,dirs,files in os.walk(dst):
for f in files:
dst_file = os.path.join(root,f)
rel = os.path.relpath(dst_file,dst)
src_file = os.path.join(src,rel)
if not os.path.exists(src_file):
os.remove(dst_file)
log(f"🗑 Deleted: {rel}")
if zip_backup.get():
create_zip_backup(dst)
log("✅ Sync finished")
update_progress(0)
# =================== ZIP BACKUP ===================
def create_zip_backup(folder):
try:
ts = datetime.now().strftime("%Y%m%d_%H%M%S")
zip_path = f"{folder}_backup_{ts}.zip"
log("📦 Creating ZIP backup")
with zipfile.ZipFile(zip_path,"w",zipfile.ZIP_DEFLATED) as z:
for root,dirs,files in os.walk(folder):
for f in files:
file_path = os.path.join(root,f)
z.write(file_path,os.path.relpath(file_path,folder))
log(f"📦 Backup created: {os.path.basename(zip_path)}")
if encrypt_backup.get():
encrypt_file(zip_path)
log("🔐 Backup encrypted")
except:
log_error()
# =================== REAL TIME ===================
class SyncHandler(FileSystemEventHandler):
def on_modified(self,event):
if event.is_directory:
return
src = event.src_path
if not allowed_file(src):
return
dst = os.path.join(
target_folder.get(),
os.path.relpath(src,source_folder.get())
)
sync_file(src,dst)
# =================== WATCH ===================
def start_realtime():
global observer
if not source_folder.get():
return
handler = SyncHandler()
observer = Observer()
observer.schedule(handler,source_folder.get(),recursive=True)
observer.start()
log("⚡ Real-time monitoring started")
def stop_realtime():
global observer
if observer:
observer.stop()
observer.join()
observer = None
log("🛑 Real-time monitoring stopped")
# =================== BROWSE ===================
def browse_source():
folder = filedialog.askdirectory()
if folder:
source_folder.set(folder)
def browse_target():
folder = filedialog.askdirectory()
if folder:
target_folder.set(folder)
# =================== ABOUT ===================
def show_about():
messagebox.showinfo(
f"About {APP_NAME}",
f"{APP_NAME} v{APP_VERSION}\n\n"
"Enterprise Backup & Synchronization Suite\n\n"
"FileSync PRO is a professional desktop tool designed for\n"
"developers, IT professionals, and power users who need\n"
"fast, reliable, and automated file synchronization.\n\n"
"Core Features:\n"
"• ⚡ Real-time folder monitoring & instant sync\n"
"• 🚀 High-performance multi-thread copy engine\n"
"• 🧠 Smart hash verification for data integrity\n"
"• 📦 Automatic ZIP archive backup creation\n"
"• 🔐 Optional encrypted backup protection\n"
"• 🎛 Advanced file extension filtering\n"
"• 🗑 Mirror mode for full directory replication\n"
"• 📊 Built-in synchronization analytics dashboard\n\n"
"Built with:\n"
"Python • Tkinter • ttkbootstrap • Watchdog\n\n"
"Designed for backup automation, development workflows,\n"
"server mirroring, and professional file management.\n\n"
"© 2026 Mate Technologies\n"
"https://matetools.gumroad.com"
)
# =================== MENU ===================
menubar = tb.Menu(app)
help_menu = tb.Menu(menubar,tearoff=0)
help_menu.add_command(label="About",command=show_about)
menubar.add_cascade(label="Help",menu=help_menu)
app.config(menu=menubar)
try:
app.iconbitmap(resource_path("logo.ico"))
except:
pass
# =================== TITLE ===================
title_frame = tb.Frame(app)
title_frame.pack(pady=(12, 10))
tb.Label(
title_frame,
text=APP_NAME,
font=("Segoe UI", 28, "bold"),
bootstyle="primary"
).pack()
tb.Label(
title_frame,
text=f"v{APP_VERSION} • Enterprise Backup & Sync Suite",
font=("Segoe UI", 10, "italic"),
foreground="#9ca3af"
).pack(pady=(2, 6))
tb.Label(
title_frame,
text="Real-time file synchronization, automated backups, encryption, and high-performance multi-thread file transfer",
font=("Segoe UI", 9),
foreground="#6b7280"
).pack()
# =================== CONTROLS ===================
controls = tb.Labelframe(app,text="Controls",padding=10)
controls.pack(fill="x",padx=10,pady=6)
tb.Button(
controls,
text="🔄 Full Sync",
bootstyle="success",
command=lambda: threading.Thread(target=full_sync,daemon=True).start()
).pack(side="left",padx=5)
tb.Button(
controls,
text="⚡ Start Real-Time",
bootstyle="warning",
command=start_realtime
).pack(side="left",padx=5)
tb.Button(
controls,
text="🛑 Stop",
bootstyle="danger",
command=stop_realtime
).pack(side="left",padx=5)
# =================== SETTINGS ===================
settings = tb.Labelframe(app,text="Settings",padding=10)
settings.pack(fill="x",padx=10,pady=6)
tb.Label(settings,text="Source").pack(side="left")
tb.Entry(settings,textvariable=source_folder,width=35).pack(side="left",padx=5)
tb.Button(settings,text="Browse",command=browse_source).pack(side="left")
tb.Label(settings,text="Target").pack(side="left",padx=10)
tb.Entry(settings,textvariable=target_folder,width=35).pack(side="left",padx=5)
tb.Button(settings,text="Browse",command=browse_target).pack(side="left")
tb.Checkbutton(settings,text="Mirror",variable=mirror_mode).pack(side="left",padx=10)
tb.Checkbutton(settings,text="Verify Hash",variable=verify_hash).pack(side="left")
tb.Checkbutton(settings,text="ZIP Backup",variable=zip_backup).pack(side="left",padx=10)
tb.Checkbutton(settings,text="Encrypt Backup",variable=encrypt_backup).pack(side="left")
# =================== FILTER ===================
filter_frame = tb.Labelframe(app,text="File Filters",padding=10)
filter_frame.pack(fill="x",padx=10,pady=6)
tb.Label(filter_frame,text="Extensions (.txt,.pdf,.py)").pack(side="left")
tb.Entry(filter_frame,textvariable=filter_ext,width=40).pack(side="left",padx=10)
# =================== LOG ===================
log_frame = tb.Labelframe(app,text="Activity Log",padding=10)
log_frame.pack(fill="both",expand=True,padx=10,pady=6)
log_text = tk.Text(log_frame, height=10)
log_text.pack(side="left",fill="both",expand=True)
scroll = tk.Scrollbar(log_frame,command=log_text.yview)
scroll.pack(side="right",fill="y")
log_text.config(yscrollcommand=scroll.set,state="disabled")
# =================== PROGRESS ===================
progress = tb.Progressbar(app,bootstyle="success-striped")
progress.pack(fill="x",padx=10,pady=5)
# =================== STATS ===================
stats_label = tb.Label(app,text="Files synced: 0 | Data copied: 0 MB")
stats_label.pack()
# =================== UI QUEUE ===================
def process_ui_queue():
try:
while True:
cmd,data = ui_queue.get_nowait()
if cmd=="log":
log_text.config(state="normal")
log_text.insert("end",data+"\n")
log_text.see("end")
log_text.config(state="disabled")
elif cmd=="progress":
progress["value"] = data
elif cmd=="stats":
files,data_bytes = data
mb = round(data_bytes/1024/1024,2)
stats_label.config(
text=f"Files synced: {files} | Data copied: {mb} MB"
)
except Empty:
pass
app.after(100,process_ui_queue)
# =================== START ===================
app.after(100,process_ui_queue)
app.mainloop()