Update easy_sync.py
Browse files- easy_sync.py +70 -12
easy_sync.py
CHANGED
@@ -1,14 +1,15 @@
|
|
1 |
import subprocess, time, threading
|
2 |
from typing import List, Union
|
|
|
3 |
|
4 |
class Channel:
|
5 |
-
def __init__(self,source,destination,sync_deletions=False,
|
6 |
self.source = source
|
7 |
self.destination = destination
|
8 |
-
self.
|
9 |
self.syncing_thread = threading.Thread(target=self._sync,args=())
|
10 |
self.sync_deletions = sync_deletions
|
11 |
-
self.
|
12 |
if not exclude:
|
13 |
exclude = []
|
14 |
if isinstance(exclude,str):
|
@@ -29,9 +30,9 @@ class Channel:
|
|
29 |
command.extend([f'{self.source}/',f'{self.destination}/'])
|
30 |
if self.sync_deletions:
|
31 |
command.append('--delete')
|
32 |
-
while not self.
|
33 |
subprocess.run(command)
|
34 |
-
time.sleep(self.
|
35 |
|
36 |
def copy(self):#Sync once
|
37 |
command = self.command
|
@@ -43,22 +44,79 @@ class Channel:
|
|
43 |
subprocess.run(command)
|
44 |
return True
|
45 |
|
46 |
-
def
|
47 |
if self.syncing_thread.is_alive():#Check if it's running
|
48 |
-
self.
|
49 |
self.syncing_thread.join()
|
50 |
-
if self.
|
51 |
-
self.
|
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
|
58 |
if self.alive():
|
59 |
-
self.
|
60 |
self.syncing_thread.join()
|
61 |
while self.alive():
|
62 |
if not self.alive():
|
63 |
break
|
64 |
-
return not self.alive()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import subprocess, time, threading
|
2 |
from typing import List, Union
|
3 |
+
import os, shutil, fnmatch
|
4 |
|
5 |
class Channel:
|
6 |
+
def __init__(self,source,destination,sync_deletions=False,every=60,exclude: Union[str, List, None] = None):
|
7 |
self.source = source
|
8 |
self.destination = destination
|
9 |
+
self.event = threading.Event()
|
10 |
self.syncing_thread = threading.Thread(target=self._sync,args=())
|
11 |
self.sync_deletions = sync_deletions
|
12 |
+
self.every = every
|
13 |
if not exclude:
|
14 |
exclude = []
|
15 |
if isinstance(exclude,str):
|
|
|
30 |
command.extend([f'{self.source}/',f'{self.destination}/'])
|
31 |
if self.sync_deletions:
|
32 |
command.append('--delete')
|
33 |
+
while not self.event.is_set():
|
34 |
subprocess.run(command)
|
35 |
+
time.sleep(self.every)
|
36 |
|
37 |
def copy(self):#Sync once
|
38 |
command = self.command
|
|
|
44 |
subprocess.run(command)
|
45 |
return True
|
46 |
|
47 |
+
def start(self):#Handle threads
|
48 |
if self.syncing_thread.is_alive():#Check if it's running
|
49 |
+
self.event.set()
|
50 |
self.syncing_thread.join()
|
51 |
+
if self.event.is_set():
|
52 |
+
self.event.clear()
|
53 |
if self.syncing_thread._started.is_set():#If it has been started before
|
54 |
self.syncing_thread = threading.Thread(target=self._sync,args=())#Create a FRESH thread
|
55 |
self.syncing_thread.start()#Start the thread
|
56 |
return self.alive()
|
57 |
|
58 |
+
def stop(self):#Stop the thread and close the process
|
59 |
if self.alive():
|
60 |
+
self.event.set()
|
61 |
self.syncing_thread.join()
|
62 |
while self.alive():
|
63 |
if not self.alive():
|
64 |
break
|
65 |
+
return not self.alive()
|
66 |
+
|
67 |
+
class GarbageMan:
|
68 |
+
def __init__(self) -> None:
|
69 |
+
self.thread = threading.Thread(target=self.take_out,args=())
|
70 |
+
self.event = threading.Event()
|
71 |
+
|
72 |
+
def destroy(self, trash):
|
73 |
+
if not isinstance(trash,dict):
|
74 |
+
if os.path.isdir(os.path.join(self.path,trash)):
|
75 |
+
shutil.rmtree(os.path.join(self.path,trash))
|
76 |
+
elif os.path.isfile(os.path.join(self.path,trash)):
|
77 |
+
os.remove(os.path.join(self.path,trash))
|
78 |
+
else:
|
79 |
+
trash.Delete()
|
80 |
+
|
81 |
+
def take_out(self) -> None:
|
82 |
+
while not self.event.is_set():
|
83 |
+
for object in self.garbage:
|
84 |
+
trash = object["title"] if isinstance(object,dict) else object
|
85 |
+
if fnmatch.fnmatch(trash,self.pattern):
|
86 |
+
self.destroy(object)
|
87 |
+
time.sleep(self.every)
|
88 |
+
|
89 |
+
def stop(self) -> None:
|
90 |
+
if not self.event.is_set():
|
91 |
+
self.event.set()
|
92 |
+
self.thread.join()
|
93 |
+
self.event.clear()
|
94 |
+
if self.thread._started.is_set():
|
95 |
+
self.thread = threading.Thread(target=self.take_out,args=())
|
96 |
+
|
97 |
+
def start(self,path: Union[str,List],every:int=30,pattern: str='') -> None:
|
98 |
+
if isinstance(path,list):
|
99 |
+
self.path = None
|
100 |
+
self.garbage = path
|
101 |
+
elif isinstance(path,str):
|
102 |
+
self.path = path
|
103 |
+
self.garbage = os.listdir(path)
|
104 |
+
else:
|
105 |
+
return "Error"
|
106 |
+
self.every = every
|
107 |
+
self.pattern = pattern
|
108 |
+
if self.thread.is_alive():
|
109 |
+
self.stop()
|
110 |
+
self.thread.start()
|
111 |
+
|
112 |
+
def _fake(self, trash):
|
113 |
+
if not isinstance(trash,dict):
|
114 |
+
if os.path.isdir(os.path.join(self.path,trash)):
|
115 |
+
with open("log.txt","a") as f:
|
116 |
+
f.write(f"Fake deleted dir: {trash}")
|
117 |
+
elif os.path.isfile(os.path.join(self.path,trash)):
|
118 |
+
with open("log.txt","a") as f:
|
119 |
+
f.write(f"Fake deleted file: {trash}")
|
120 |
+
else:
|
121 |
+
with open("log.txt","a") as f:
|
122 |
+
f.write(f"Fake permanently deleted: {trash['title']}")
|