-
Notifications
You must be signed in to change notification settings - Fork 51
/
utils.py
executable file
·61 lines (45 loc) · 1.47 KB
/
utils.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
#!/usr/bin/env python3
from subprocess import run, PIPE
from shutil import which
# Logging utils
def color_string(string, fg=None):
COLORS = { # List some colors that may be needed
"red": "\033[31m",
"pink": "\033[38;5;206m",
"green": "\033[32m",
"orange": "\033[33m",
"blue": "\033[34m",
"cyan": "\033[36m",
"lightred": "\033[91m",
"lightgreen": "\033[92m",
"yellow": "\033[93m",
"lightblue": "\033[94m",
"lightcyan": "\033[96m",
"brightwhite": "\u001b[37;1m",
"brightmagenta": "\u001b[35;1m",
}
endcolor = "\033[0m"
return f"{COLORS.get(fg, '')}{string}{endcolor}"
def info(*lines):
for line in lines:
print(f"{color_string('[I] ', fg='cyan')}{line}")
def error(*lines, should_exit=True):
for line in lines:
print(f"{color_string('[E] ', fg='lightred')}{line}")
if should_exit:
exit(1)
def warning(*lines):
for line in lines:
print(f"{color_string('[W] ', fg='yellow')}{line}")
# Shell / external utils
def sh(command):
"Executes command in shell and returns its exit status"
return run(command, shell=True).returncode
def shout(command, silence=False):
"Executes command in shell and returns its stdout"
stdout = run(command, shell=True, stdout=PIPE).stdout.decode("utf-8")
if not silence:
print(stdout)
return stdout
def has_command(command):
return which(command) is not None