File size: 2,356 Bytes
78a108d
e62dba9
78a108d
e62dba9
 
78a108d
 
 
e62dba9
78a108d
f19c838
e62dba9
 
 
 
 
 
78a108d
e62dba9
 
 
 
 
 
 
 
 
 
 
 
 
78a108d
 
f19c838
78a108d
e62dba9
 
 
 
 
 
 
78a108d
 
 
e62dba9
 
78a108d
 
 
 
e62dba9
 
 
 
78a108d
e62dba9
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import subprocess, time, threading
from typing import List, Union

class Channel:
    def __init__(self,source,destination,sync_deletions=False,sync_time=60,exclude: Union[str, List, None] = None):#What args to take
        self.source = source
        self.destination = destination
        self.stop = threading.Event()
        self.syncing_thread = threading.Thread(target=self._sync,args=())
        self.sync_deletions = sync_deletions
        self.sync_time = sync_time
        if not exclude:
            exclude = []
        if isinstance(exclude,str):
            exclude = [exclude]
        self.exclude = exclude
        self.command = ['rsync','-aP']

    def alive(self):#Check if the thread is alive
        if self.syncing_thread.is_alive():
            return True
        else:
            return False

    def _sync(self):#Sync constantly
        command = self.command
        for exclusion in self.exclude:
            command.append(f'--exclude={exclusion}')
        command.extend([f'{self.source}/',f'{self.destination}/'])
        if self.sync_deletions:    
            command.append('--delete')
        while not self.stop.is_set():
            subprocess.run(command)
            time.sleep(self.sync_time)

    def copy(self):#Sync once
        command = self.command
        for exclusion in self.exclude:
            command.append(f'--exclude={exclusion}')
        command.extend([f'{self.source}/',f'{self.destination}/'])
        if self.sync_deletions:    
            command.append('--delete')
        subprocess.run(command)
        return True
    
    def open(self):#Handle threads
        if self.syncing_thread.is_alive():#Check if it's running
            self.stop.set()
            self.syncing_thread.join()
        if self.stop.is_set():
            self.stop.clear()
        if self.syncing_thread._started.is_set():#If it has been started before
            self.syncing_thread = threading.Thread(target=self._sync,args=())#Create a FRESH thread
        self.syncing_thread.start()#Start the thread
        return self.alive()

    def close(self):#Stop the thread and close the process
        if self.alive():
            self.stop.set()
            self.syncing_thread.join()
            while self.alive():
                if not self.alive():
                    break
        return not self.alive()