forked from cms-sw/cms-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkLogFile.py
executable file
·541 lines (467 loc) · 19.7 KB
/
checkLogFile.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
#!/usr/bin/env python2
from __future__ import print_function
import os
import re
import time
import sys
try:
scriptPath = os.path.dirname(os.path.abspath(__file__))
except NameError: # __file__ not defined in interactive mode
scriptPath = os.path.dirname(os.path.abspath(sys.argv[0]))
if scriptPath not in sys.path:
sys.path.append(scriptPath)
sys.path.append(os.path.join(scriptPath, "python"))
class LogChecker(object):
# --------------------------------------------------------------------------------
def __init__(self):
self.htmlOut = None
self.sumLog = None
self.logDir = os.path.join(os.getcwd(), "www")
self.fileIndex = 0
self.errFiles = []
self.pkgVers = {}
self.verbose = 1
if not os.path.exists(self.logDir):
os.makedirs(self.logDir)
return
# --------------------------------------------------------------------------------
def setHtml(self, html):
self.htmlOut = html
self.verbose = 0
return
# --------------------------------------------------------------------------------
def getTags(self):
try:
prepFile = open("nohup.out", "r")
except IOError:
try:
prepFile = open("prebuild.log", "r")
except IOError:
print("no nohup.out or prebuild.log found in . ")
raise
lines = prepFile.readlines()
prepFile.close()
pkgTagRe = re.compile(r"Package\s*([a-zA-Z].*)\s*version\s*(.*)\s*checkout\s*(.*)")
for line in lines:
pkgTagMatch = pkgTagRe.match(line)
if pkgTagMatch:
pkg = pkgTagMatch.group(1).strip()
vers = pkgTagMatch.group(2).strip()
stat = pkgTagMatch.group(3).strip()
self.pkgVers[pkg] = vers
if stat.lower() != "successful":
print("WARNING: problems checking out ", pkg, vers, stat, "(" + line + ")")
# print "found ", str(len(self.pkgVers.keys())), 'tags'
# for key, val in self.pkgVers.items():
# print "'"+key+"' " + val
return
# --------------------------------------------------------------------------------
def checkLog(self, logFileName):
plat = os.environ["SCRAM_ARCH"]
pkgName = ""
import re
gmakeCompRe = re.compile(
"gmake:.*/" + plat + r"/src/([a-zA-Z].*)/([a-zA-Z].*)/src/([a-zA-Z].*)\.o.*"
)
gmakeTestRe = re.compile(
"gmake:.*/" + plat + r"/src/([a-zA-Z].*)/([a-zA-Z].*)/test/([a-zA-Z].*)\.o.*"
)
gmakeLinkRe = re.compile(
"gmake:.*/" + plat + r"/src/([a-zA-Z].*)/([a-zA-Z].*)/(lib[a-zA-Z].*\.so).*"
)
gmakeGeneric = re.compile("gmake:.*")
self.fileIndex += 1
print("\n================================================================================")
print("in :", os.getcwd())
print("checking file ", logFileName)
print("================================================================================\n")
htmlFileName = logFileName.replace("/", "_").replace(".", "-") + ".html"
if self.htmlOut and self.sumLog:
self.sumLog.write(
'<image src="http://cern.ch/pfeiffer/aaLibrarian/colline.gif" width="90%" height="3"></image>\n'
)
self.sumLog.write(
'<h3>Checking <a href="'
+ htmlFileName
+ '">log file '
+ logFileName
+ "</a></h3>\n"
)
self.sumLog.write("\n")
self.sumLog.write("<p>\n")
self.getTags()
# analyze log file
logFile = open(logFileName, "r")
lines = logFile.readlines()
logFile.close()
if len(lines) < 200:
if self.htmlOut and self.sumLog:
self.sumLog.write(
'<font color="#ff0000"><b>Warning:</b> suspiciously short log file!</font>\n'
)
nErr = 0
nWarn = 0
errorList = {"make": []}
subSysCompErr = {}
subSysTestErr = {}
subSysLinkErr = {}
subSysGenErr = {}
index = -1
for line in lines:
index += 1
errFound = False
mMk = gmakeCompRe.match(line)
if mMk:
errFound = True
nErr += 1
subsys = mMk.group(1)
pkg = mMk.group(2)
fileName = mMk.group(3)
# print "error found for ", subsys, pkg, ":\n ", line
errorList["make"].append(index)
if subsys in subSysCompErr:
subSysCompErr[subsys].append((pkg, fileName, index, line))
else:
subSysCompErr[subsys] = [(pkg, fileName, index, line)]
mMk = gmakeTestRe.match(line)
if mMk:
errFound = True
nErr += 1
subsys = mMk.group(1)
pkg = mMk.group(2)
fileName = mMk.group(3)
# print "error found for ", subsys, pkg, ":\n ", line
errorList["make"].append(index)
if subsys in subSysTestErr:
subSysTestErr[subsys].append((pkg, fileName, index, line))
else:
subSysTestErr[subsys] = [(pkg, fileName, index, line)]
mMk = gmakeLinkRe.match(line)
if mMk:
errFound = True
nErr += 1
subsys = mMk.group(1)
pkg = mMk.group(2)
libName = mMk.group(3)
# print "error found for ", subsys, pkg, ":\n ", line
errorList["make"].append(index)
if subsys in subSysLinkErr:
subSysLinkErr[subsys].append((pkg, libName, index, line))
else:
subSysLinkErr[subsys] = [(pkg, libName, index, line)]
if not errFound: # no specific error was found, check generic
mMk = gmakeGeneric.match(line)
if mMk:
nErr += 1
subsys = "unknown"
pkg = "unknown"
libName = "unknown"
# print "UNKNOWN error found for ", subsys, pkg, ":\n ", line
errorList["make"].append(index)
if subsys in subSysGenErr:
subSysGenErr[subsys].append((pkg, libName, index, line))
else:
subSysGenErr[subsys] = [(pkg, libName, index, line)]
print("--------------------------------------------------------------------------------")
compErrPkg = []
for key, val in subSysCompErr.items():
print(str(len(val)) + " ERRORs building lib found for subsystem", key)
for item in val:
if item[0] not in compErrPkg:
compErrPkg.append(item[0])
startIndex = len(key) + len(item[0]) + 1
print(" " + item[0] + " (" + str(item[1])[startIndex:] + ")")
print("--------------------------------------------------------------------------------")
testErrPkg = []
for key, val in subSysTestErr.items():
print(str(len(val)) + " ERRORs building tests found for subsystem", key)
for item in val:
if item[0] not in testErrPkg:
testErrPkg.append(item[0])
print(" " + item[0] + " (" + str(item[1]) + ")")
print("--------------------------------------------------------------------------------")
nLinkErr = 0
for key, val in subSysLinkErr.items():
subSys = key.split("/")[0]
if (subSys not in subSysCompErr.keys()) and (subSys not in subSysTestErr.keys()):
nLinkErr += 1
print(str(len(val)) + " ERRORs in link-step found for subsystem", subSys)
for item in val:
print(" " + item[0] + " (" + str(item[1]) + ")")
print("--------------------------------------------------------------------------------")
genErrPkg = []
for key, val in list(subSysGenErr.items()):
print(str(len(val)) + " UNKNOWN ERRORs found ")
for item in val:
if item[0] not in genErrPkg:
genErrPkg.append(item[0])
print(" " + item[0] + " (" + str(item[3]) + ")")
print("--------------------------------------------------------------------------------")
print("\nA total of ", len(compErrPkg), " packages failed compilation.")
print("\nA total of ", len(testErrPkg), " packages failed compiling tests.")
print("\nA total of ", nLinkErr, " packages failed linking(only).")
print("\nA total of ", len(genErrPkg), " unknown errors.")
print("\n")
# file analyzed, now prepreare printout
if self.htmlOut:
errLines = []
for key, value in errorList.items():
for i in value:
if i not in errLines:
errLines.append(int(i))
try:
htmlFile = open(os.path.join(self.logDir, htmlFileName), "w")
except IOError:
print("ERROR opening htmlFile ", os.path.join(self.logDir, htmlFileName))
raise
htmlFile.write(
"<html>\n<head><title>LogCheck for " + logFileName + "</title></head>\n<body>\n"
)
htmlFile.write("<h1>LogCheck for " + logFileName + "</h1>\n")
htmlFile.write("<h2>Analysis from " + time.asctime() + "</h2>\n")
for key, val in subSysCompErr.items():
htmlFile.write("<p>\n")
htmlFile.write(
'<a href="'
+ htmlFileName
+ "#"
+ key
+ '">'
+ str(len(val))
+ " Compile ERRORs found for subsystem "
+ key
)
htmlFile.write("</a> <br />\n")
htmlFile.write("</p>\n")
for key, val in subSysLinkErr.items():
htmlFile.write("<p>\n")
htmlFile.write(
'<a href="'
+ htmlFileName
+ "#"
+ key
+ '">'
+ str(len(val))
+ " Linker ERRORs found for subsystem "
+ key
)
htmlFile.write("</a> <br />\n")
htmlFile.write("</p>\n")
for key, val in subSysCompErr.items():
htmlFile.write("<p>\n")
htmlFile.write("<a name=" + key + ">\n")
htmlFile.write(str(len(val)) + " ERRORs found for subsystem " + key + "<br />\n")
htmlFile.write("<ol>\n")
for item in val:
pkg = item[0]
try:
htmlFile.write(
"<li>package "
+ pkg
+ " file "
+ item[1]
+ " Tag: "
+ self.pkgVers[key + "/" + pkg]
+ " <br />\n"
)
except KeyError:
htmlFile.write(
"<li>package "
+ pkg
+ " file "
+ item[1]
+ " Tag: <unknown> ??? <br />\n"
)
index = item[2]
htmlFile.write('<a href="' + htmlFileName + "#line_" + str(index) + '">\n')
htmlFile.write("<pre>\n")
try:
for delta in range(-5, 1):
if self.htmlOut:
htmlFile.write(str(index + delta) + " : " + lines[index + delta])
# print " ", index+delta, ":", lines[index+delta],
except IndexError:
pass
htmlFile.write("</pre>\n")
htmlFile.write("</a></li>\n")
htmlFile.write("<hr />\n")
htmlFile.write("</ol>\n")
htmlFile.write("</p>\n")
for key, val in subSysGenErr.items():
htmlFile.write("<p>\n")
htmlFile.write(
'<a href="'
+ htmlFileName
+ "#"
+ key
+ '">'
+ str(len(val))
+ " UNKNOWN ERRORs found "
)
htmlFile.write("</a> <br />\n")
htmlFile.write("</p>\n")
htmlFile.write("<pre>\n")
for index in range(len(lines)):
# html-ify:
line = lines[index]
line = line.replace("&", "&")
line = line.replace("<", "<")
line = line.replace(">", ">")
if index in errLines:
htmlFile.write('<a name="line_' + str(index) + '">')
htmlFile.write('<font color="red" size+=3><b>\n')
htmlFile.write(line)
htmlFile.write("</b></font>")
htmlFile.write("</a> \n")
else:
htmlFile.write(line)
htmlFile.write("</pre>\n</body>\n</html>")
htmlFile.close()
print("\nhtml-ified log file created at:", htmlFileName, " (in", self.logDir, ")\n")
# check if we have too many errors ....
errLimit = False
if len(errorList.items()) > 500:
if self.htmlOut and self.sumLog:
self.sumLog.write(
'<font color="#ff0000"><b>Caution:</b> Too many errors found ('
+ str(len(errorList.items()))
+ "), printout suppressed !!</font>\n"
)
errLimit = True
if self.verbose > 0 and not errLimit:
for key, value in errorList.items():
print("++++++++++", key)
for index in value:
if self.htmlOut and self.sumLog:
self.sumLog.write('<a name="' + pkgName + '"></a>\n')
self.sumLog.write("<hr />\n")
self.sumLog.write(
'<a href="file'
+ str(self.fileIndex)
+ ".html#line_"
+ str(index)
+ '">\n'
)
try:
print("------------------------------------------")
for delta in range(-2, 2):
if self.htmlOut and self.sumLog:
self.sumLog.write(
str(index + delta) + " : " + lines[index + delta]
)
print(" ", index + delta, ":", lines[index + delta], end=" ")
except IndexError:
pass
if self.htmlOut and self.sumLog:
self.sumLog.write("</a>\n")
msg = "In total: "
if nErr == 0:
msg += "no errors, "
else:
msg += str(nErr) + " errors, "
self.errFiles.append(pkgName)
if nWarn == 0:
msg += "no warnings"
else:
msg += str(nWarn) + " warnings"
msg += " found in " + str(len(lines)) + " lines."
print(msg)
if self.htmlOut and self.sumLog:
self.sumLog.write(msg + "\n")
self.sumLog.write("</p>\n")
return nErr, nWarn
# --------------------------------------------------------------------------------
def checkFiles(self, fileList=None):
if fileList is None:
fileList = []
print("going to check ", len(fileList), " files:", fileList)
import socket
hostName = socket.gethostname().lower()
import time
date = time.ctime()
if self.htmlOut and not self.sumLog:
self.sumLog = open(os.path.join(self.logDir, "CheckLog-summary.html"), "w")
self.sumLog.write(
"<html>\n<head><title>Summary of logfiles</title></head>\n<body>\n<pre>\n"
)
self.sumLog.write("<h2>Check of logfiles</h2>\n")
self.sumLog.write("<p>\n")
self.sumLog.write("Checking done on " + hostName + " at " + date)
self.sumLog.write("</p>\n")
self.sumLog.write("<p>\n")
self.sumLog.write('<a href="#summary">Summary of checks</a>\n')
self.sumLog.write("</p>\n")
totErr = 0
totWarn = 0
nFiles = 0
nFilErr = 0
nFilWarn = 0
for fileName in fileList:
file = fileName
if fileName[:2] == "./":
file = fileName[2:]
try:
nErr, nWarn = self.checkLog(file)
except IOError:
print(" IOError found !?!? ")
raise
nFiles += 1
totErr += nErr
totWarn += nWarn
if nErr > 0:
nFilErr += 1
if nWarn > 0:
nFilWarn += 1
print(
"\n================================================================================\n"
)
if nFiles > 0:
print("Checked a total of ", nFiles, "log files.")
print("A total of ", totErr, "errors in ", nFilErr, "files.")
print("A total of ", totWarn, "warnings in ", nFilWarn, "files.")
# print "Files with errors: "
# for f in self.errFiles:
# print "\t", f
# print
else:
print("No files found to check")
print("")
if self.htmlOut and self.sumLog:
self.sumLog.write('<a name="summary"></a><hr />\n')
self.sumLog.write("Checked a total of " + str(nFiles) + " log files.\n")
self.sumLog.write(
"A total of " + str(totErr) + " errors in " + str(nFilErr) + " files.\n"
)
self.sumLog.write(
"A total of " + str(totWarn) + " warnings in " + str(nFilWarn) + " files.\n"
)
self.sumLog.write("Files with errors: \n")
self.sumLog.write("<table>\n")
for f in self.errFiles:
self.sumLog.write('<tr><td><a href="#' + f + '">' + f + "</a> </td></tr>\n")
self.sumLog.write("</table>\n")
self.sumLog.write("\n")
self.sumLog.write("</pre>\n</body>\n</html>")
self.sumLog.close()
return
# ================================================================================
def main():
try:
import argparse
except ImportError:
import archived_argparse as argparse
parser = argparse.ArgumentParser()
parser.add_argument("--html", default=False, action="store_true")
parser.add_argument("--verbose", default=0)
parser.add_argument("files", nargs="+")
args = parser.parse_args()
html = args.html
verb = args.verb
checker = LogChecker()
if html:
checker.setHtml(html)
checker.verbose = verb
files = args.files
checker.checkFiles(files)
if __name__ == "__main__":
main()