lcgwaf /lcgcmt/pyLCG/python/lcg/aa/checkLogFiles.py

Language Python Lines 459
MD5 Hash 2f9babb8f77dfe6f5f24f0d1c4cbfd6a
Repository https://github.com/mana-fwk/lcgwaf.git View Raw File
  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
#!/usr/bin/env python
 
import os, sys, re, time, socket
import logging, logging.config

spiScriptDir = os.path.abspath( os.path.dirname( sys.argv[0] ) )
#logging.config.fileConfig(os.path.join(spiScriptDir,"loggers"))
log=logging.getLogger("nightlies.console")
if len(log.handlers) == 0:
	handler = logging.StreamHandler()
	handler.setFormatter(logging.Formatter('%(asctime)s %(levelname)s %(message)s'))
	log.addHandler(handler)
	log.setLevel(logging.INFO)




class LogChecker(object) :

    # --------------------------------------------------------------------------------

    def __init__(self,html,file,platform) :

        self.htmlOut = None
        self.sumLog  = None
        self.logDir  = os.getcwd()+"/www"
        if not os.path.exists(os.getcwd()+"/www"):
            os.makedirs(os.getcwd()+"/www")
        self.errFiles = []
        self.htmlOut = html
        self.files=file
        #self.files.append(file)
        self.verbose = 1
        self.slot    = None
        self.plat=platform
        return

    # --------------------------------------------------------------------------------
    def setHtml(self, html) :
        self.htmlOut = html
        self.verbose = 0
        return
    # --------------------------------------------------------------------------------
    def setSlot(self, slot) :
        self.slot = slot
        return
    # --------------------------------------------------------------------------------

    def checkLog(self,file):

        pkgName = ""
        import re
        # on windows we don't start with a "/"
        prjAfs  = re.compile(".*../../../../LCG_Builders"+"/.*/([A-Za-z].*)/([A-Za-z].*)/logs/(.*)\.log")
        prjAfsMatch = prjAfs.match( os.path.abspath(file) )
        # set defaults for "any" logfile
        pkgName = file
        #plat = "unknown"
        if prjAfsMatch :  # it matches the path, so we can get more info
            pkgName = prjAfsMatch.group(2)
            plat = prjAfsMatch.group(3)
        # ignore logfiles from qmtest
        if pkgName.find("-qmtest") != -1 :
            nWarn, nErr = self.handleQMTestFile(pkgName)
            return nWarn, nErr, 0
        htmlFileName =  file.split("/")[9]+"-log.html"
        if self.verbose > 0 :
            log.info("================================================================================")
            log.info("checking " + pkgName + " on " +  self.plat)
            log.info("    file " +  os.path.abspath(file))
            log.info("================================================================================")
        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 for ' + pkgName + ' on ' + self.plat + '</a></h3>\n' )
            self.sumLog.write( '\n' )
            self.sumLog.write( "<p>\n")

        logFile = open(file, "r")
        lines = logFile.readlines()
        logFile.close()

        shortLog = False
	if ( (len(lines) < 200) and
             (pkgName.find("LCGCMT") == -1) ):
            if (self.htmlOut and self.sumLog ) :
                self.sumLog.write( '<font color="#ff0000"><b>Warning:</b> suspiciously short log file!</font>\n')
                shortLog = True

        reCMTErr = []
        reCMTErr.append( re.compile("Cannot read the requirements file") )

        matchMkErr1 = re.compile('.*make.*\WError\s*1.*')
        matchError  = re.compile('.*\Werror[: ].*', re.I)
        matchError1 = re.compile('^error .*', re.I)
        matchWarn   = re.compile('.*warning[: ].*', re.I)
        matchFailB  = re.compile('\.\.\.failed .*', re.I)  # boost's "...failed" messages
        matchDiskQ  = re.compile('.*isk quota exceeded.*', re.I)
        matchConf  = re.compile('Configuring Project .*', )
        matchBuild = re.compile('Building Project .*', )
        matchTest  = re.compile('Executing all tests .*', )
        matchInst  = re.compile('Installing Project .*', )

        nErr    = 0
        nMkErr  = 0
        nWarn   = 0
        nCMTErr = 0
        errorList = {}
        mkErrList = [] # make is "build" :-)
        warnList  = {}
        actualStep = "init"
        index = -1
        for line in lines:
            index += 1

            mMk = matchConf.match(line)
            if mMk : actualStep = "conf"
            mMk = matchBuild.match(line)
            if mMk : actualStep = "build"
            mMk = matchTest.match(line)
            if mMk : actualStep = "test"
            mMk = matchInst.match(line)
            if mMk : actualStep = "inst"

            # count failed make's (by package)
            mMkE1 = matchMkErr1.match(line)
            if mMkE1 :
                nMkErr += 1
                mkErrList.append(index)

            mErr  = matchError.match(line)
            mErr1 = matchError1.match(line)
            mDskQ = matchDiskQ.match(line)
            if mErr or mErr1 or mDskQ :          # treat errors and disk quota exceeded basically the same way ...

                if mErr or mErr1:               # ... but check the following only for real error messages
                    # ignore compiler flag (this needs more sophistication to not skip real errors)
                    if line.find("-Wno-error") != -1 : continue

                nErr += 1
                if actualStep not in errorList.keys() :
                    errorList[actualStep] = []
                errorList[actualStep].append(index)

            for reCMT in reCMTErr:
                matchCMTErr = reCMT.match(line)
                if matchCMTErr :
                    nCMTErr += 1
                    nErr += 1
                    if actualStep not in errorList.keys() :
                        errorList[actualStep] = []
                    errorList[actualStep].append(index)

            mWarn = matchWarn.match(line)
            if mWarn :
                # ignore genreflex warnings about not supported references
		ignoreWarnings = [
			"genreflex: WARNING: References are not supported as data members",
			"include/boost-1_34_1/boost/date_time/time_facet.hpp:202: warning: unused parameter 'a_ref'",
			"include/boost-1_34_1/boost/date_time/time.hpp:81: warning: unused parameter 'as_offset'",
			"include/boost-1_34_1/boost/date_time/time.hpp:88: warning: unused parameter 'as_offset'",
			"WARNING >> You should provide a target for",
			]


                nWarn += 1
                if actualStep not in warnList.keys() :
                    warnList[actualStep] = []
                warnList[actualStep].append(index)

		for x in ignoreWarnings:
			if line.find(x) != -1:
				nWarn -= 1
				if actualStep in warnList.keys() and index in warnList[actualStep]:
					warnList[actualStep].remove(index)


        # ---------- end analysis of logfile, now prepare output
        summaryFileName =   file.split("/")[9]+"-log.summary"
        sumFil = open( os.path.join(self.logDir, summaryFileName), 'w')
        now = time.time()
        sumFil.write(str(now) + " (" + time.ctime(now) + ") " +  " " + pkgName + " " + self.plat + "\n")
        if shortLog :
            sumFil.write(str(nWarn) + ", " + str(nErr+1) + ", " + str(nMkErr) + ", " + str(nCMTErr) + "\n")
        else:
            sumFil.write(str(nWarn) + ", " + str(nErr) + ", " + str(nMkErr) + ", " + str(nCMTErr) + "\n")
        sumFil.close()
        # print "Summary written to :", summaryFileName

        if self.htmlOut :
            errLines = []
            for key, value in errorList.items():
                for i in value:
                    if i not in errLines:
                        errLines.append(int(i))

            warnLines = []
            groupedWarnings = []
            for key, value in warnList.items():
                for i in value:
                    if i not in warnLines:
                    	warnLines.append(int(i))
                    	if int(i)-1 in warnLines: groupedWarnings.append(int(i))

            htmlFile = open(self.logDir + "/" + htmlFileName, 'w')
            htmlFile.write("<html>\n<head><title>LogCheck for package " + pkgName + " </title></head>\n<body>\n")
            htmlFile.write("<h3>LogCheck for package " + pkgName + " on " + socket.gethostname() +"</h3>\n")

            htmlFile.write('<p>\n')
            htmlFile.write('Warnings   : '+ str(nWarn) +' <br /> ')
            htmlFile.write('Errors     : '+str(nErr)   +' <br /> ')
            htmlFile.write('Make Errors: '+str(nMkErr) +' <br /> ')
            htmlFile.write('CMT  Errors: '+str(nCMTErr)+' <br /> ')
            htmlFile.write('</p>\n')
            # if there were make errors, list a summary of them
            if len(mkErrList) > 0 :
                htmlFile.write("<h3>Summary of make errors:</h3>\n")
                for index in mkErrList:
                    if (self.htmlOut and self.sumLog ) :
                        # htmlFile.write( '<a name="' + pkgName + '"></a>\n')
                        htmlFile.write( "<hr />\n")
                        htmlFile.write( '<a href="'+htmlFileName+'#line_' + str(index) + '">\n' )
                    try:
                        for delta in range(-2,2) :
                            if (self.htmlOut and self.sumLog ) :
                                htmlFile.write( str(index+delta) + " : " + lines[index+delta])
                            # print " ", index+delta, ":", lines[index+delta],
                    except IndexError:
                        pass
                    if (self.htmlOut and self.sumLog ) :
                        htmlFile.write( '</a>\n')
                htmlFile.write("<hr />\n")
            elif len(warnList.keys()) > 0:
            	htmlFile.write("<h3>Summary of warnings:</h3>\n")
                for index in warnList.values()[0]:
                    if index not in groupedWarnings:
	                    if (self.htmlOut and self.sumLog):
	                        htmlFile.write( "<hr />\n")
	                        htmlFile.write( '<a href="'+htmlFileName+'#line_' + str(index) + '">\n' )
	                    try:
	                        for delta in range(-2,2) :
	                            if (self.htmlOut and self.sumLog ) :
	                                htmlFile.write( str(index+delta) + " : " + lines[index+delta])
	                            # print " ", index+delta, ":", lines[index+delta],
	                    except IndexError:
	                        pass
	                    if (self.htmlOut and self.sumLog ) :
	                        htmlFile.write( '</a>\n')
                htmlFile.write("<hr />\n")



            htmlFile.write('<pre>\n')
            for index in range(len(lines)):
                if index in errLines :
                    htmlFile.write('</pre>\n')
                    htmlFile.write('<a name="line_' + str(index) + '">')
                    htmlFile.write('<font color="red" size+=3><b>\n')
                    htmlFile.write(lines[index])
                    htmlFile.write('</b></font>')
                    htmlFile.write('</a> \n')
                    htmlFile.write('<pre>\n')
                elif index in warnLines :
                    htmlFile.write('</pre>\n')
                    htmlFile.write('<a name="line_' + str(index) + '">')
                    htmlFile.write('<font color="blue" size+=3><b>\n')
                    htmlFile.write(lines[index])
                    htmlFile.write('</b></font>')
                    htmlFile.write('</a> \n')
                    htmlFile.write('<pre>\n')
                else:
                    htmlFile.write(lines[index])
            htmlFile.write("</pre>\n</body>\n</html>")
            htmlFile.close()

        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 ("+len(errorList.items())+"), further printout suppressed !!</font>\n')
                errLimit = True

        if self.verbose > 10 and not errLimit :
            for key, value in errorList.items() :
                log.debug("++++++++++" +  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="'+htmlFileName+'#line_' + str(index) + '">\n' )
                    try:
                        log.debug("------------------------------------------")
                        for delta in range(-2,2) :
                            if (self.htmlOut and self.sumLog ) :
                                self.sumLog.write( str(index+delta) + " : " + lines[index+delta])
                            log.debug(" " + str(index+delta) + ":" + lines[index+delta])
                    except IndexError:
                        pass
                    if (self.htmlOut and self.sumLog ) :
                        self.sumLog.write( '</a>\n')

        msg = "In total: "

        if (nErr == 0) :
            msg += "no errors, "
        else:
            if (nMkErr > 0) :
                msg += str(nMkErr) + " make errors, "
            msg += str(nErr) + " errors, "
            self.errFiles.append(htmlFileName)

        if ( nWarn == 0):
            msg += "no warnings"
        else:
            msg += str(nWarn) + " warnings"

        msg += " found in " + str(len(lines)) + " lines."

        if self.verbose > 0 :
            log.debug(msg)
        if (self.htmlOut and self.sumLog ) :
            self.sumLog.write( msg + "\n")
            self.sumLog.write( '</p>\n' )

        if prjAfsMatch :
            self.handleQmtestFile(file)

        return nMkErr, nErr, nWarn

    # --------------------------------------------------------------------------------

    def handleQmtestFile(self, file):
        log.debug("ignoring qmtest file:" + file)
        return 0,0

    # --------------------------------------------------------------------------------

    def checkFiles(self, fileList=[]) :

        import socket
        hostName = socket.gethostname().lower()

        import time
        date = time.ctime()
        if self.htmlOut and not self.sumLog :
            summaryFileName =  self.files.split("/")[9]+"-log.summary"
            self.sumLog = open(summaryFileName,'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
        totMkErr = 0
        totWarn = 0

        errFiles = []

        nFiles = 0
        nFilErr  = 0
        nFilWarn = 0
        if self.files :

            try:
                nMkErr, nErr, nWarn = self.checkLog(self.files)
            except IOError:
                sys.exit(-1)
            nFiles += 1
            totMkErr += nMkErr
            totErr += nErr
            totWarn += nWarn

            if nErr > 0 : nFilErr += 1
            if nWarn > 0 : nFilWarn += 1

        if self.verbose > 0 :
            print "\n================================================================================\n"

            if nFiles > 0 :
                print "Checked a total of ", nFiles, "log files."
                print "A total of ", totMkErr," make errors in ", nFilErr, '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 usage():

    print sys.argv[0],"[--hmtl] <logFile> [<logFile> ...]"

    return

# ================================================================================

if __name__ == "__main__" :

    import getopt
    options = sys.argv[1:]
    try:
        opts, args = getopt.getopt(options, 'h',
                                   ['help','html','verbose=','topDir='])
    except getopt.GetoptError:
        usage()
        sys.exit(-2)

    html = None
    verb = 0
    slot = None
    top  = None

    for o, a in opts:
        if o in ('-h', '--help'):
            usage()
            sys.exit()

        if o in ('--html',):
            html = True
        if o in ('--verbose',):
            verb = a
        #if o in ('--slot',):
           # slot = a
        if o in ('--topDir',):
            top = a

    checker = LogChecker()

    if html : checker.setHtml(html)
    if top  : checker.topDir = top
    #checker.setSlot(slot)

    checker.verbose = verb
    checker.checkFiles(args)
Back to Top