This repository has been archived by the owner on May 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scow-sync-query
executable file
·86 lines (69 loc) · 2.82 KB
/
scow-sync-query
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
#!/usr/bin/python3
'''
query the progress of file transfered
'''
import json
import os
import sys
from subprocess import PIPE, CalledProcessError, Popen
def process_file(file_path):
'''
parse the output of rsync
'''
if os.path.exists(file_path):
try:
tail_progress = Popen(['tail', '-n', '2', file_path], stdout=PIPE, stderr=PIPE)
stdout, stderr = tail_progress.communicate()
head_progress = Popen(['head', '-n', '1', file_path], stdout=PIPE, stderr=PIPE)
stdout_head, stderr_head = head_progress.communicate()
sys.stderr.write(stderr.decode('utf-8'))
sys.stderr.flush()
# stdout: filename\n\r xxx \r xxx \r xxx \r
if stdout != '':
lines = stdout.decode('utf-8')
if '%' not in lines: # 还没开始打印进度
return None
lines_list = lines.split('\n')
base_name = lines_list[0]
recv_address = ''
father_path = ''
if stdout_head != '':
head_lines = stdout_head.decode('utf-8')
head_lines_list = head_lines.split()
recv_address = head_lines_list[0]
father_path = head_lines_list[1]
## DEBUG
# progess_list = lines_list[1].split('\r')
# progress_line = progess_list[-1]
# progress_parts = progress_line.split()
progress_parts = lines_list[1].split('\r')[-1].split()
file_path = os.path.join(father_path, base_name)
transfer_size = progress_parts[0]
progress = progress_parts[1]
speed = progress_parts[2]
time = progress_parts[3]
return {"recvAddress":recv_address, "filePath": file_path, "transferSize": transfer_size, "progress": progress, "speed": speed, "leftTime": time}
else:
return None
except CalledProcessError:
return None
def traverse_folder(folder_path):
'''
traverse ~/scow/.scow-sync
'''
progresses = []
for root, dirs, files in os.walk(folder_path):
for dir in dirs:
transfer_id = os.path.join(root, dir)
for file in os.listdir(transfer_id):
file_path = os.path.join(transfer_id, file)
progress = process_file(file_path)
if progress:
progresses.append(progress)
return progresses
if __name__ == '__main__':
TRANSFER_PATH = os.path.expanduser('~/scow/.scow-sync')
progress = traverse_folder(TRANSFER_PATH)
sys.stdout.write(json.dumps(traverse_folder(TRANSFER_PATH)))
sys.stdout.flush()
sys.exit()