-
Notifications
You must be signed in to change notification settings - Fork 0
/
reporter.py
executable file
·279 lines (235 loc) · 10.4 KB
/
reporter.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
#!/usr/bin/env python3
import json
import sys
import os
import inspect
import argparse
from enum import Enum
class OutputType( Enum ):
STANDARD = "STANDARD"
GITHUB = "GITHUB"
def __str__( self ) :
return self.value
def dumpFile( filename, errorLabel, success=False, bannerMsg="", banner="!" * 80 ) :
print( "\nOpening logfile {0}".format( filename ) )
print( "{msg}\n{banner}".format( msg=bannerMsg, banner=banner ) )
with open( filename, 'r') as f:
current = next( f, None )
last = None
for line in f.readlines() :
print( current, end="" )
current = line
# last line
if current is not None and not success :
print( errorLabel.format( title=filename, message=current ) )
else :
print( current )
print( banner )
print( "\nClosing logfile {0}".format( filename ) )
def getLogsPrintedStr( masterDict, masterLog ) :
indent = " "
outputFormat = "{desc:=<32}=> {file}\n"
output = outputFormat.format( desc="master log [developers only]", file=masterLog )
for test, testlog in masterDict.items() :
if not testlog["success"] :
output += outputFormat.format( desc=indent * 1 + "{0} stdout ".format( test ), file=testlog["stdout"] )
for step, steplog in testlog["steps"].items() :
if not steplog["success"] :
output += outputFormat.format( desc=indent * 2 + "{0} stdout ".format( step ), file=steplog["logfile"] )
return output
def getSummaryPrintedStr( masterDict, metadata ) :
indent = " "
outputFormat = "{name:<24} {reason:<40}\n"
cmdFormat = "{runner} {file} -t {test} {offset} -s LOCAL"
output = "SUMMARY OF TEST FAILURES\n" + outputFormat.format( name="NAME", reason="REASON" )
for test, testlog in masterDict.items() :
if not testlog["success"] :
testCmd = cmdFormat.format(
runner=metadata["rel_exec"],
file=metadata["rel_file"],
test=test,
offset="" if metadata["rel_offset"] == "" else "-d " + metadata["rel_offset"]
)
output += outputFormat.format( name=test, reason=testlog[ "line" ] ) + "[TO REPRODUCE LOCALLY] : " + testCmd + "\n"
for step, steplog in testlog["steps"].items() :
if not steplog["success"] :
output += outputFormat.format( name=indent * 1 + step, reason=steplog[ "line" ] )
return output
def getOptionsParser():
parser = argparse.ArgumentParser(
description="Outputs summary and logs for failed tests",
)
parser.add_argument(
"masterLog",
help="Master logfile output from runner.py",
type=str,
default=""
)
# parser.add_argument(
# "-t", "--tests",
# help="Test names matching respective JSON test name to run",
# dest="tests",
# type=str,
# nargs="+",
# default=[]
# )
parser.add_argument(
"-o", "--outputType",
dest="outputType",
help="Set the output type style for enhanced readability",
type=OutputType,
choices=list( OutputType ),
default=OutputType.STANDARD
)
parser.add_argument(
"-e", "--exec",
dest="exec",
help="Exec to use when outputting instructions on reproduction",
type=str,
default="<location to hpc-workflows/.ci/runner.py>"
)
parser.add_argument(
"-f", "--failedStepsOnly",
dest="failedStepsOnly",
help="Only output the failed steps instead of the whole test",
default=False,
const=True,
action='store_const'
)
parser.add_argument(
"-m", "--markStepsOnly",
dest="markStepsOnly",
help="Only mark failur in the failed steps",
default=False,
const=True,
action='store_const'
)
parser.add_argument(
"-s", "--summaryOnly",
dest="summaryOnly",
help="Only output the summary and no logs",
default=False,
const=True,
action='store_const'
)
parser.add_argument(
"-n", "--noExitCode",
dest="noExitCode",
help="Always exit normally without exit code reflecting success",
default=False,
const=True,
action='store_const'
)
return parser
class Options(object):
"""Empty namespace"""
pass
def main() :
parser = getOptionsParser()
options = Options()
parser.parse_args( namespace=options )
fp = open( options.masterLog )
logs = json.load( fp )
metadata = logs.pop( "metadata", None )
metadata["rel_exec"] = options.exec
failure = False
startGroup = None
stopGroup = None
noticeLabel = None
errorLabel = None
testErrorLabel = None
if options.outputType == OutputType.STANDARD :
startGroup = ""
stopGroup = ""
noticeLabel = ""
errorLabel = "{{message}}"
elif options.outputType == OutputType.GITHUB :
startGroup = "::group::{title}"
stopGroup = "::endgroup::"
noticeLabel = "::notice title={title}::{message}"
errorLabel = "::error title={title}::{{message}}"
if options.markStepsOnly :
testErrorLabel = "{{message}}"
else :
testErrorLabel = errorLabel
if not options.summaryOnly :
print( "Finding tests that failed..." )
for test, testlog in logs.items() :
if not testlog["success"] :
failure = True
testTitle = "STDOUT FOR TEST {test}".format( test=test )
print( startGroup.format( title=testTitle ) )
print( "\n".join([( "#" * 80 )]*3 ) )
print( "Test {test} failed, printing stdout".format( test=test ) )
dumpFile( testlog["stdout"], testErrorLabel.format( title=test ), bannerMsg=testTitle, banner="\n".join([( "!#!#" * 20 )]*2 ) )
print( "Finding logs for steps that failed..." )
print( stopGroup )
longestStep = len( max( testlog["steps"].keys(), key=len ) ) + len( test ) + 1
for step, steplog in testlog["steps"].items() :
stepAncestry = "{test}.{step}".format( test=test, step=step )
stepTitle = " > STDOUT FOR STEP {step}".format( step=stepAncestry )
if not options.failedStepsOnly or not steplog["success"] :
# print( noticeLabel.format( title=stepAncestry, message=stepTitle ) )
if not steplog["success"] :
print( startGroup.format( title="{0:<{1}}".format( stepTitle, longestStep + 20 ) + " <- CLICK HERER !!! ERROR !!!" ) )
print( "Step {step} failed, printing stdout".format( step=stepAncestry ) )
else :
print( startGroup.format( title=stepTitle ) )
dumpFile( steplog["logfile"], errorLabel.format( title=stepAncestry ), steplog["success"], bannerMsg=stepTitle )
print( stopGroup )
print( "\n", flush=True, end="" )
print( startGroup.format( title="Summary" ) )
hereThereBeLogs = "^^ !!! ALL LOG FILES ARE PRINTED TO SCREEN ABOVE FOR REFERENCE !!! ^^"
refLogs = getLogsPrintedStr( logs, os.path.abspath( options.masterLog ) )
howToUse = ""
if options.outputType == OutputType.STANDARD :
howToUse = inspect.cleandoc(
hereThereBeLogs +
"""
To find when a logfile is printed search for (remove single quotes) :
'Opening logfile <logfile>'
OR
'Closing logfile <logfile>'
Replacing <logfile> with the logfile you wish to see
""" )
elif options.outputType == OutputType.GITHUB :
howToUse = inspect.cleandoc(
hereThereBeLogs +
"""
To find when a logfile is printed use the above sections to view the output.
They are labeled as TEST|STEP then <test> or <test>.<step>, respectively.
Click on the above section to expand or collapse it.
Sections with the errors summarized below are labelled above.
If logs are too long and/or truncated, please download the logfiles via the artifacts
found via the summary page if available, otherwise download normally
""" )
print( "~ How to use brief ~\n{help}\n\nOr refer to log files : \n{reflogs}\n{addendum}\n".format(
help=howToUse,
reflogs=refLogs,
addendum=hereThereBeLogs
),
flush=True )
if options.summaryOnly :
print( startGroup.format( title="Summary" ) )
# Now do executive summary
print( getSummaryPrintedStr( logs, metadata ) )
note = inspect.cleandoc(
"""
Note: HPC users should use '-s LOCAL' in reproduce command with caution
as it will run directly where you are, consider using an interactive node
"""
)
print( note )
# Force wait for stdout to be finished
try:
os.fsync( sys.stdout.fileno() )
except:
pass
if failure :
print( "FAILURE!" )
if not options.noExitCode :
# Exit with bad status so people know where to look since that might be
# an issue as this will look "successful"
exit( 1 )
if __name__ == '__main__' :
main()