Delete py/sync.py
Browse files- py/sync.py +0 -70
py/sync.py
DELETED
@@ -1,70 +0,0 @@
|
|
1 |
-
import time
|
2 |
-
import threading
|
3 |
-
import subprocess
|
4 |
-
|
5 |
-
class Channel:
|
6 |
-
def __init__(self, source, destination, sync_deletions=False, every=60, exclude = 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 |
-
|
14 |
-
if not exclude: exclude = []
|
15 |
-
if isinstance(exclude, str): exclude = [exclude]
|
16 |
-
|
17 |
-
self.exclude = exclude
|
18 |
-
self.command = ['rsync', '-aP']
|
19 |
-
|
20 |
-
def alive(self):
|
21 |
-
if self.syncing_thread.is_alive(): return True
|
22 |
-
else: return False
|
23 |
-
|
24 |
-
def _sync(self):
|
25 |
-
command = self.command
|
26 |
-
|
27 |
-
for exclusion in self.exclude:
|
28 |
-
command.append(f'--exclude={exclusion}')
|
29 |
-
|
30 |
-
command.extend([f'{self.source}/', f'{self.destination}/'])
|
31 |
-
|
32 |
-
if self.sync_deletions: command.append('--delete')
|
33 |
-
|
34 |
-
while not self.event.is_set():
|
35 |
-
subprocess.run(command)
|
36 |
-
time.sleep(self.every)
|
37 |
-
|
38 |
-
def copy(self):
|
39 |
-
command = self.command
|
40 |
-
|
41 |
-
for exclusion in self.exclude:
|
42 |
-
command.append(f'--exclude={exclusion}')
|
43 |
-
|
44 |
-
command.extend([f'{self.source}/', f'{self.destination}/'])
|
45 |
-
|
46 |
-
if self.sync_deletions: command.append('--delete')
|
47 |
-
subprocess.run(command)
|
48 |
-
|
49 |
-
return True
|
50 |
-
|
51 |
-
def start(self):
|
52 |
-
if self.syncing_thread.is_alive():
|
53 |
-
self.event.set()
|
54 |
-
self.syncing_thread.join()
|
55 |
-
|
56 |
-
if self.event.is_set(): self.event.clear()
|
57 |
-
if self.syncing_thread._started.is_set(): self.syncing_thread = threading.Thread(target=self._sync, args=())
|
58 |
-
|
59 |
-
self.syncing_thread.start()
|
60 |
-
return self.alive()
|
61 |
-
|
62 |
-
def stop(self):
|
63 |
-
if self.alive():
|
64 |
-
self.event.set()
|
65 |
-
self.syncing_thread.join()
|
66 |
-
|
67 |
-
while self.alive():
|
68 |
-
if not self.alive(): break
|
69 |
-
|
70 |
-
return not self.alive()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|