-
Notifications
You must be signed in to change notification settings - Fork 14
/
utils.py
141 lines (106 loc) · 3.42 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
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
import tempfile
import os
import time
from colorama import Fore, Back, Style
import subprocess
def success(message: str):
"""
Displays a message in a success fashion
:param str message: the message
"""
print("")
print(Style.BRIGHT + Fore.GREEN + message + Style.RESET_ALL)
def warning(message: str):
"""
Displays a message in a warning fashion
:param str message: the message
"""
print("")
print(Style.BRIGHT + Fore.YELLOW + message + Style.RESET_ALL)
def error(message: str):
"""
Displays a message in a error fashion
:param str message: the message
"""
print("")
print(Style.BRIGHT + Fore.RED + message + Style.RESET_ALL)
def bright(message: str):
"""
Displays a message in a bright fashion
:param str message: the message
"""
print(Style.BRIGHT + message + Style.RESET_ALL)
def step_title(message: str):
"""
Displays a message in a bright and cyan fashion
:param str message: the message
"""
print("")
print(Style.BRIGHT + Fore.CYAN + message + Style.RESET_ALL)
def step_msg_check(message: str):
"""
Displays a message in a bright and cyan fashion
:param str message: the message
"""
print("")
print("-> " + Style.BRIGHT + Fore.YELLOW + message + Style.RESET_ALL)
def press_enter():
"""
Displays "Press enter when done..." in a bright fashion
"""
print("")
print(Style.BRIGHT + "Press enter when done..." + Style.RESET_ALL)
input()
def question_answers(question: str, answers: list, default: str = "") -> str:
"""
Asks a question and expect one of the given answers
:param str question: askes question
:param dict answers: possible answers
:return str: given answer
"""
possibilities = ",".join(answers)
if default != "":
possibilities += f" [{default}]"
while True:
print("")
answer = input(Style.BRIGHT + f"{question} ({possibilities}): " + Style.RESET_ALL).strip()
if answer == "":
answer = default
if answer in answers:
return answer
else:
warning(f"Possible answers are: {possibilities}" + Fore.RESET)
def yes_no(question: str, default: str = "") -> bool:
"""
Asks a yes/no question
:param str question: Question to ask
:param str default: default value (yes or no)
:return bool: True if yes elese no
"""
return question_answers(question, ["y", "n"], default) == "y"
def rhio(hostname: str, command: str) -> str:
"""
Sends a rhio command to a given host and returns its result
:param str hostname: remote hostname
:param str command: command to execute
:return str: the command result
"""
return subprocess.check_output(["rhio", hostname, *command.split(" ")]).decode("utf-8").strip()
def rhio_get_value(hostname: str, node: str) -> str:
"""
Gets a value from RhIO
:param str hostname: the remote host
:param str node: the node name (ex: /referee/teamId)
:return str: the value
"""
return "=".join(rhio(hostname, node).split("=")[1:])
def rhio_commands(hostname: str, commands: str) -> None:
"""
Execute some commands in batch
:param str hostname: the remote host
:param str commands: list of commands
"""
tmpfile = tempfile.NamedTemporaryFile()
tmpfile.write(commands.encode("utf-8"))
tmpfile.flush()
os.system(f"rhio {hostname} < {tmpfile.name}")