-
Notifications
You must be signed in to change notification settings - Fork 3
/
mlp_common_checks.py
106 lines (74 loc) · 2.96 KB
/
mlp_common_checks.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
'''
Common checks to perform on all MLP Logs.
Convention is to raise Common Compliance Exception (CCError) if errors are found.
'''
import re
class CCError(Exception):
pass
def find_tag(loglines, tag, expect=None):
l = []
for ll in loglines:
if ll.tag == tag:
l.append(ll)
if expect is not None and len(l) != expect:
raise CCError('Expected exactly {} copy of tag \'{}\''.format(expect, tag))
return l
def check_clock(loglines):
''' Ensures start and stop exist and behave sanely '''
start = find_tag(loglines, 'run_start', expect=1)[0]
end = find_tag(loglines, 'run_stop', expect=1)[0]
delta_t = end.timestamp - start.timestamp
# Sanity check the runtine isn't off by a sign or orders of magnitude
if delta_t <= 0:
raise CCError('Runtime is less than or equal to zero. (time between run_start and run_stop)')
if delta_t > 60 * 60 * 24 * 365:
raise CCError('Runtime (time between run_start and run_stop) exceeds one year... assuming this is wrong.')
return delta_t
def check_exactly_one_tag(loglines, tag):
find_tag(loglines, tag, expect=1)
def check_at_least_one_tag(loglines, tag):
l = find_tag(loglines, tag)
if len(l) < 1:
raise CCError('Expected at least 1 copy of tag \'{}\''.format(tag))
def _check_eval(ll, name, tag, code):
try:
return eval(code.strip(), {}, {'ll': ll, 'v': ll.value})
except:
raise CCError('Failed evaluating {} on:\n{}'.format(name, ll.full_string))
def check_eval_tag(loglines, name, tag, code, expect):
count = 0
total = 0
for ll in loglines:
if re.match(tag, ll.tag):
total += 1
ok = _check_eval(ll, name, tag, code)
if not ok and expect == 'ALL':
raise CCError('Check {} failed on\n{}'.format(name, ll.full_string))
if ok:
count += 1
if expect == 'AT_LEAST_ONE':
if count == 0:
raise CCError('Check {} failed.'.format(name))
def check_tags_pair(loglines, first, second):
expect_first = True
for ll in loglines:
if ll.tag == first:
if not expect_first:
raise CCError('Expected a "{}" tag before:\n{}'.format(second, ll.full_string))
expect_first = False
if ll.tag == second:
if expect_first:
raise CCError('Expected a "{}" tag before:\n{}'.format(first, ll.full_string))
expect_first = True
if not expect_first:
raise CCError('Expected a "{}" tag before end.'.format(second, ll.full_string))
def check_tags_count_same(loglines, tags):
counts_per_tag = {}
for t in tags:
counts_per_tag[t] = 0
for ll in loglines:
for t in tags:
if ll.tag == t:
counts_per_tag[t] += 1
if len(set(counts_per_tag.values())) != 1:
raise CCError('Expected the following tags to have the same count: {}'.format(counts_per_tag))