-
Notifications
You must be signed in to change notification settings - Fork 8
/
rsyncy.py
executable file
·315 lines (270 loc) · 8.86 KB
/
rsyncy.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#!/usr/bin/env python3
import os
import queue
import re
import select
import subprocess
import sys
import time
import types
from threading import Thread
from datetime import datetime
_re_chk = re.compile(r"(..)-.+=(\d+)/(\d+)")
# start inline from laktakpy
class CLI:
NO_COLOR = os.environ.get("NO_COLOR", "")
class style:
reset = "\033[0m"
bold = "\033[01m"
class esc:
right = "\033[C"
def clear_line(opt=0):
# 0=to end, 1=from start, 2=all
return "\033[" + str(opt) + "K"
def get_col_bits():
if CLI.NO_COLOR:
return 1
c, t = os.environ.get("COLORTERM", ""), os.environ.get("TERM", "")
if c in ["truecolor", "24bit"]:
return 24
elif c == "8bit" or "256" in t:
return 8
else:
return 4
# 4bit system colors
def fg4(col):
# black=0,red=1,green=2,orange=3,blue=4,purple=5,cyan=6,lightgrey=7
# darkgrey=8,lightred=9,lightgreen=10,yellow=11,lightblue=12,pink=13,lightcyan=14
if CLI.NO_COLOR:
return ""
else:
return f"\033[{(30+col) if col<8 else (90-8+col)}m"
def bg4(col):
# black=0,red=1,green=2,orange=3,blue=4,purple=5,cyan=6,lightgrey=7
if CLI.NO_COLOR:
return ""
else:
return f"\033[{40+col}m"
# 8bit xterm colors
def fg8(col):
if CLI.NO_COLOR:
return ""
else:
return f"\033[38;5;{col}m"
def bg8(col):
if CLI.NO_COLOR:
return ""
else:
return f"\033[48;5;{col}m"
def write(*text):
for t in text:
sys.stdout.write(str(t))
sys.stdout.flush()
def printline(*text, sep=" ", end="\r\n"):
CLI.write("\r", sep.join([str(t) for t in text]), CLI.esc.clear_line(), end)
def get_size():
# returns {columns, lines}
if sys.stdout.isatty():
return os.get_terminal_size()
else:
return types.SimpleNamespace(columns=80, lines=40)
# end inline from laktakpy
class Rsyncy:
def __init__(self, rstyle):
self.bg = rstyle["bg"]
self.cdim = rstyle["dim"]
self.ctext = rstyle["text"]
self.cbar1 = rstyle["bar1"]
self.cbar2 = rstyle["bar2"]
self.cspin = rstyle["spin"]
self.spinner = rstyle["spinner"]
self.trans = 0
self.percent = 0
self.speed = ""
self.xfr = ""
self.chk = ""
self.chk_finished = False
self.start = datetime.now()
self.stat_mode = False
def parse_stat(self, line):
# sample: 6,672,528 96% 1.04MB/s 0:00:06 (xfr#1, to-chk=7/12)
data = [s for s in line.split(" ") if s]
if len(data) >= 4:
self.trans, percent, self.speed, timing, *_ = data
try:
self.percent = int(percent.strip("%")) / 100
except Exception as e:
print("ERROR - can't parse#1:", line, data, e, sep="\n> ")
# timing is remaining with 4 args, or elapsed with 6
if len(data) == 6:
try:
xfr, chk = data[4:6]
self.xfr = xfr.strip(",").split("#")[1]
if self.xfr:
self.xfr = "#" + self.xfr
m = _re_chk.match(chk)
if m:
self.chk_finished = m[1] == "to"
todo = int(m[2])
total = int(m[3])
done = total - todo
self.chk = f"{(done/total if total else 0):2.0%} ({total})"
else:
self.chk = ""
except Exception as e:
print("ERROR - can't parse#2:", line, data, e, sep="\n> ")
def draw_stat(self):
cols = CLI.get_size().columns
elapsed = datetime.now() - self.start
if self.chk_finished:
spin = ""
else:
spin = self.spinner[round(elapsed.total_seconds()) % len(self.spinner)]
# define status (excl. bar)
# use \xff as a placeholder for the spinner
parts = [
o
for o in [
f"{self.trans:>11}",
f"{self.speed:>14}",
f"{str(elapsed).split('.')[0]}",
f"{self.xfr}",
f"scan {self.chk}\xff",
]
if o
]
# reduce to fit
plen = lambda: sum(len(s) for s in parts) + len(parts)
while parts and plen() > cols:
parts.pop(0)
# add bar in remaining space
pc = 0
rcols = cols - plen()
if rcols > 12:
pc_width = min(rcols - 7, 30)
pc = round(self.percent * pc_width)
parts.insert(
0,
f"{self.cbar1}[{self.cbar2}{'#' * pc}{self.cbar1}{':' * (pc_width-pc)}]{self.ctext}"
+ f"{self.percent:>5.0%}",
)
rcols -= pc_width + 7
elif rcols > 5:
parts.insert(0, f"{self.percent:>5.0%}")
rcols -= 5
# get delimiter size
delim = f"{self.cdim}|{self.ctext}"
if rcols > (len(parts) - 1) * 2:
delim = " " + delim + " "
# render with delimiter
status = delim.join(parts).replace("\xff", f"{self.cspin}{spin}{self.ctext}")
if not self.stat_mode:
# write and position cursor on bar
CLI.write(
"\r",
self.bg,
status,
CLI.esc.clear_line(),
"\r",
f"{CLI.esc.right * pc}",
CLI.style.reset,
)
else:
CLI.write("\r\n", self.bg, status, CLI.style.reset)
def parse_line(self, line, is_stat):
line = line.decode().strip(" ")
if not line:
return
is_stat = is_stat or line[0] == "\r"
line = line.replace("\r", "")
if is_stat:
self.parse_stat(line)
self.draw_stat()
elif line[-1] == "/":
# skip directories
pass
elif not self.stat_mode:
CLI.printline(line)
self.draw_stat()
def read(self, fd):
line = b""
while True:
stat = select.select([fd], [], [], 0.2)
if fd in stat[0]:
ch = os.read(fd, 1)
if ch == b"":
# exit
self.parse_line(line, False)
break
elif ch == b"\r":
self.parse_line(line, True)
line = b"\r"
elif ch == b"\n":
self.parse_line(line, False)
line = b""
else:
line += ch
else:
# no new input
if line:
# assume this is a status update
self.parse_line(line, True)
line = b""
else:
# waiting for input
self.draw_stat()
time.sleep(0.5)
CLI.printline("")
def run_rsync(args, write_pipe, rc):
# prefix rsync and add args required for progress
args = ["rsync"] + args + ["--info=progress2", "--no-v", "-hv"]
p = None
try:
p = subprocess.Popen(args, stdout=write_pipe)
p.wait()
finally:
os.close(write_pipe)
rc.put(p.returncode if p else 1)
if __name__ == "__main__":
if CLI.get_col_bits() >= 8:
rstyle = {
"bg": CLI.bg8(238),
"dim": CLI.fg8(241),
"text": CLI.fg8(250),
"bar1": CLI.fg8(243),
"bar2": CLI.fg8(43),
"spin": CLI.fg8(228) + CLI.style.bold,
"spinner": ["-", "\\", "|", "/"],
}
else:
rstyle = {
"bg": CLI.bg4(7),
"dim": CLI.fg4(8),
"text": CLI.fg4(0),
"bar1": CLI.fg4(8),
"bar2": CLI.fg4(0),
"spin": CLI.fg4(14) + CLI.style.bold,
"spinner": ["-", "\\", "|", "/"],
}
rsyncy = Rsyncy(rstyle)
rsyncy.stat_mode = os.path.basename(sys.argv[0]) == "rsyncy-stat"
try:
if len(sys.argv) == 1:
if sys.stdin.isatty():
print("rsyncy is an rsync wrapper with a progress bar.")
print(
"Please specify your rsync options as you normally would but use rsyncy instead of rsync."
)
else:
# receive pipe from rsync
rsyncy.read(sys.stdin.fileno())
else:
read_pipe, write_pipe = os.pipe()
rc = queue.Queue()
t = Thread(target=run_rsync, args=(sys.argv[1:], write_pipe, rc))
t.start()
rsyncy.read(read_pipe)
t.join()
sys.exit(rc.get())
except KeyboardInterrupt:
sys.exit(1)