-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync.py
94 lines (81 loc) · 2.74 KB
/
sync.py
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import os
import subprocess
import json
from multiprocessing import Process,Lock
"""
Upload and download, API handling services provided by rclone.org
VERY IMPORTANT:
it reads config from a JSON file: config.json
drawback: no exception catching
Example:
from:**here**:/home/exampleuser/examplerfolder
to:**there**:
filename(type EOF to stop):myfile
**here** is the name of the local drive you created in the rclone
**there** is another drive
"""
config = json.load(open('config/sync.json'))
class CopyTask:
def __init__(self, filename, frompath, topath):
self.filename = filename
self.frompath = frompath
self.topath = topath
"""
return a nonzero value for success
needs revision
"""
def copy(copytask):
status = subprocess.call(["rclone", "copy", f"{copytask.frompath}/{copytask.filename}", f"{copytask.topath}"])
if status < 0:
print(f"Copy process terminated abnormally(status {status})")
return 0
else:
print(f"Copy from {copytask.frompath}/{copytask.filename} to {copytask.topath}/{copytask.filename} completed successfully")
return 1
"""
Concurrently manage copy processes which run in parallel
Remove it?
"""
def batch_copy(file_list):
count = 0
count_lock = Lock()
# alive indicates if any process is alive
alive = False
proc_list = [None] * config["MAX_PROCESSES"]
while count < len(file_list) or alive:
alive = False
for i in range(config["MAX_PROCESSES"]):
try:
# Maybe here will be a TypeError or something
if proc_list[i] and proc_list[i].is_alive():
alive = True
continue
else:
proc_list[i].join(0)
count_lock.acquire()
if count < len(file_list):
proc_list[i] = Process(target=copy, args=(file_list[count]))
proc_list[i].start()
alive = True
count += 1
count_lock.release()
except Exception:
count_lock.acquire()
if count < len(file_list):
proc_list[i] = Process(target=copy, args=(file_list[count]))
alive = True
count += 1
proc_list[i].start()
count_lock.release()
print("Batch copy complete")
if __name__=="__main__":
frompath = input("from:")
topath = input("to:")
filelist = []
try:
filename = input("filename(type EOF to stop):")
if filename:
filelist.append(CopyTask(filename, frompath, topath))
except EOFError:
pass
batch_copy(filelist)