-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathformatting.py
120 lines (105 loc) · 3.25 KB
/
formatting.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
from colorama import Fore, Back, Style
from errors import wrap_exception, USBCommsError
from time import strftime
import colorama
import state
import os
import re
import sys
colorama.init()
if filename := os.environ.get('CYNTHION_TEST_LOG'):
logfile = open(filename, 'a')
ansi_escape = re.compile(r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])')
print(strftime("%Y-%m-%d %H:%M:%S ") + ' '.join(sys.argv), file=logfile)
else:
logfile = None
def log(*args, **kwargs):
kwargs['flush'] = True
print(*args, **kwargs)
if logfile is not None:
print(*(strip(arg) for arg in args), file=logfile, **kwargs)
def strip(text):
return ansi_escape.sub('', text)
def enable_numbering(enable):
state.numbering = enable
def msg(text, end):
if state.numbering:
state.step[-1] += 1
step_text = ".".join(str(s) for s in state.step)
step_text += " " * (11 - len(step_text))
prefix = Fore.YELLOW + step_text + Style.RESET_ALL + "│ "
else:
prefix = ""
log(prefix + (" " * state.indent ) + "• " + text + Style.RESET_ALL, end=end)
def item(text):
msg(text, "\n")
def todo(text):
item(Fore.YELLOW + "TODO" + Style.RESET_ALL + ": " + text)
def info(text):
return Fore.CYAN + str(text) + Style.RESET_ALL
def result(text):
log(info(text) + ", ", end="")
def ask(text):
log()
log(
Style.BRIGHT +
Fore.CYAN + " === Please " + text + " and press " +
Fore.GREEN + "PASS" + Fore.CYAN + " or " +
Fore.RED + "FAIL" + Fore.CYAN + " === " +
Style.RESET_ALL)
log()
def ok(text):
log()
log(Fore.GREEN + "PASS" + Style.RESET_ALL + ": " + text)
log()
def fail(err):
if state.numbering:
step_text = err.step + '-'
else:
step_text = ''
log()
log(Style.BRIGHT + Fore.RED + "FAIL " + Fore.YELLOW + step_text + err.code + Style.RESET_ALL)
log()
log(err.msg)
log()
if isinstance(err, USBCommsError):
logfile = '/var/log/kern.log'
prefix = 'kernel: '
count = 10
try:
log_lines = open(logfile, 'r').readlines()
log(f"Last {count} lines of {logfile}:\n")
for line in log_lines[-count:]:
start = line.find(prefix) + len(prefix)
log(line.rstrip()[start:])
except IOError as e:
log(f"Failed to read {logfile}: {e.strerror}")
log()
class group():
def __init__(self, text):
self.text = text
def __enter__(self):
msg(self.text, ":\n")
state.indent += 1
state.step.append(0)
return self
def __exit__(self, exc_type, exc_value, exc_tb):
# If we got an exception, wrap it into a CynthionTestError now,
# before we lose the step information.
if exc_value is not None:
wrap_exception(exc_value)
state.step.pop()
state.indent -= 1
return False
class task():
def __init__(self, text):
self.text = text
def __enter__(self):
msg(self.text, "... ")
return self
def __exit__(self, exc_type, exc_value, exc_tb):
if exc_type is None:
log(Fore.GREEN + "OK" + Style.RESET_ALL)
else:
log(Fore.RED + "FAIL" + Style.RESET_ALL)
return False