Update easy_sync.py
Browse files- easy_sync.py +44 -31
easy_sync.py
CHANGED
|
@@ -1,51 +1,64 @@
|
|
| 1 |
import subprocess, time, threading
|
|
|
|
| 2 |
|
| 3 |
-
class
|
| 4 |
-
def __init__(self,source,destination,sync_deletions=False,sync_time=60)
|
| 5 |
self.source = source
|
| 6 |
self.destination = destination
|
| 7 |
self.stop = threading.Event()
|
| 8 |
-
self.syncing_thread = threading.Thread(target=self._sync,args=()
|
| 9 |
self.sync_deletions = sync_deletions
|
| 10 |
self.sync_time = sync_time
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
def
|
| 13 |
-
if self.
|
| 14 |
-
|
| 15 |
-
else:
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
while not self.stop.is_set():
|
| 18 |
subprocess.run(command)
|
| 19 |
time.sleep(self.sync_time)
|
| 20 |
|
| 21 |
-
def
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
| 26 |
subprocess.run(command)
|
| 27 |
return True
|
| 28 |
|
| 29 |
-
def
|
| 30 |
-
if self.syncing_thread.is_alive()
|
| 31 |
-
if verbose: print("Active thread detected... ",end="")
|
| 32 |
self.stop.set()
|
| 33 |
self.syncing_thread.join()
|
| 34 |
-
if verbose: print("Stopped.")
|
| 35 |
-
if self.syncing_thread._started.is_set():
|
| 36 |
-
if verbose: print("Creating a fresh new thread... ",end="")
|
| 37 |
-
self.syncing_thread = threading.Thread(target=self._sync,args=(),daemon=True)
|
| 38 |
-
if verbose: print("Done.")
|
| 39 |
if self.stop.is_set():
|
| 40 |
-
if verbose: print("Creating new stop event... ",end="")
|
| 41 |
self.stop.clear()
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
self.syncing_thread.start()
|
| 45 |
-
|
| 46 |
-
return True
|
| 47 |
|
| 48 |
-
def
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import subprocess, time, threading
|
| 2 |
+
from typing import List, Union
|
| 3 |
|
| 4 |
+
class Channel:
|
| 5 |
+
def __init__(self,source,destination,sync_deletions=False,sync_time=60,exclude: Union[str, List, None] = None):#What args to take
|
| 6 |
self.source = source
|
| 7 |
self.destination = destination
|
| 8 |
self.stop = threading.Event()
|
| 9 |
+
self.syncing_thread = threading.Thread(target=self._sync,args=())
|
| 10 |
self.sync_deletions = sync_deletions
|
| 11 |
self.sync_time = sync_time
|
| 12 |
+
if not exclude:
|
| 13 |
+
exclude = []
|
| 14 |
+
if isinstance(exclude,str):
|
| 15 |
+
exclude = [exclude]
|
| 16 |
+
self.exclude = exclude
|
| 17 |
+
self.command = ['rsync','-aP']
|
| 18 |
|
| 19 |
+
def alive(self):#Check if the thread is alive
|
| 20 |
+
if self.syncing_thread.is_alive():
|
| 21 |
+
return True
|
| 22 |
+
else:
|
| 23 |
+
return False
|
| 24 |
+
|
| 25 |
+
def _sync(self):#Sync constantly
|
| 26 |
+
command = self.command
|
| 27 |
+
for exclusion in self.exclude:
|
| 28 |
+
command.append(f'--exclude={exclusion}')
|
| 29 |
+
command.extend([f'{self.source}/',f'{self.destination}/'])
|
| 30 |
+
if self.sync_deletions:
|
| 31 |
+
command.append('--delete')
|
| 32 |
while not self.stop.is_set():
|
| 33 |
subprocess.run(command)
|
| 34 |
time.sleep(self.sync_time)
|
| 35 |
|
| 36 |
+
def copy(self):#Sync once
|
| 37 |
+
command = self.command
|
| 38 |
+
for exclusion in self.exclude:
|
| 39 |
+
command.append(f'--exclude={exclusion}')
|
| 40 |
+
command.extend([f'{self.source}/',f'{self.destination}/'])
|
| 41 |
+
if self.sync_deletions:
|
| 42 |
+
command.append('--delete')
|
| 43 |
subprocess.run(command)
|
| 44 |
return True
|
| 45 |
|
| 46 |
+
def open(self):#Handle threads
|
| 47 |
+
if self.syncing_thread.is_alive():#Check if it's running
|
|
|
|
| 48 |
self.stop.set()
|
| 49 |
self.syncing_thread.join()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
if self.stop.is_set():
|
|
|
|
| 51 |
self.stop.clear()
|
| 52 |
+
if self.syncing_thread._started.is_set():#If it has been started before
|
| 53 |
+
self.syncing_thread = threading.Thread(target=self._sync,args=())#Create a FRESH thread
|
| 54 |
+
self.syncing_thread.start()#Start the thread
|
| 55 |
+
return self.alive()
|
|
|
|
| 56 |
|
| 57 |
+
def close(self):#Stop the thread and close the process
|
| 58 |
+
if self.alive():
|
| 59 |
+
self.stop.set()
|
| 60 |
+
self.syncing_thread.join()
|
| 61 |
+
while self.alive():
|
| 62 |
+
if not self.alive():
|
| 63 |
+
break
|
| 64 |
+
return not self.alive()
|