Nah_kagz1092 commited on
Commit
5b531c1
·
verified ·
1 Parent(s): 9f3c802

Create DongBoHoaDuLieu.py

Browse files
Files changed (1) hide show
  1. DongBoHoaDuLieu.py +122 -0
DongBoHoaDuLieu.py ADDED
@@ -0,0 +1,122 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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):
16
+ exclude = [exclude]
17
+ self.exclude = exclude
18
+ self.command = ['rsync','-aP']
19
+
20
+ def alive(self):#Check if the thread is alive
21
+ if self.syncing_thread.is_alive():
22
+ return True
23
+ else:
24
+ return False
25
+
26
+ def _sync(self):#Sync constantly
27
+ command = self.command
28
+ for exclusion in self.exclude:
29
+ command.append(f'--exclude={exclusion}')
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
39
+ for exclusion in self.exclude:
40
+ command.append(f'--exclude={exclusion}')
41
+ command.extend([f'{self.source}/',f'{self.destination}/'])
42
+ if self.sync_deletions:
43
+ command.append('--delete')
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']}")