Rejekts commited on
Commit
78a108d
1 Parent(s): 6978e41

Upload easy_sync.py

Browse files
Files changed (1) hide show
  1. easy_sync.py +50 -0
easy_sync.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import subprocess, time, threading
2
+
3
+ class SyncingDirectory:
4
+ def __init__(self,source,destination,sync_deletions=False):
5
+ self.source = source
6
+ self.destination = destination
7
+ self.stop = threading.Event()
8
+ self.syncing_thread = threading.Thread(target=self._sync,args=(),daemon=True)
9
+ self.sync_deletions = sync_deletions
10
+
11
+ def _sync(self):
12
+ if self.sync_deletions:
13
+ command = ['rsync','-aP',f'{self.source}/',f'{self.destination}','--delete']
14
+ else:
15
+ command = ['rsync','-aP',f'{self.source}/',f'{self.destination}']
16
+ while not self.stop.is_set():
17
+ subprocess.run(command)
18
+ time.sleep(5)
19
+
20
+ def quick_sync(self):
21
+ if self.sync_deletions:
22
+ command = ['rsync','-aP',f'{self.source}/',f'{self.destination}','--delete']
23
+ else:
24
+ command = ['rsync','-aP',f'{self.source}/',f'{self.destination}']
25
+ subprocess.run(command)
26
+ return True
27
+
28
+ def background_sync(self,verbose=False):
29
+ if self.syncing_thread.is_alive():
30
+ if verbose: print("Active thread detected... ",end="")
31
+ self.stop.set()
32
+ self.syncing_thread.join()
33
+ if verbose: print("Stopped.")
34
+ if self.syncing_thread._started.is_set():
35
+ if verbose: print("Creating a fresh new thread... ",end="")
36
+ self.syncing_thread = threading.Thread(target=self._sync,args=(),daemon=True)
37
+ if verbose: print("Done.")
38
+ if self.stop.is_set():
39
+ if verbose: print("Creating new stop event... ",end="")
40
+ self.stop.clear()
41
+ if verbose: print("Done.")
42
+ if verbose: print("Starting new thread...",end="")
43
+ self.syncing_thread.start()
44
+ if verbose: print("Done!")
45
+ return True
46
+
47
+ def print_status(self):
48
+ print(f"The background thread has been started: {self.syncing_thread._started.is_set()}")
49
+ print(f"The background thread is alive: {self.syncing_thread.is_alive()}")
50
+ print(f"The background thread is stopped: {self.stop.is_set()}")