import shutil import string import re import os import xlwt import xlrd import xlutils from xlwt import XFStyle, Pattern from xlwt.Workbook import Workbook from xlwt.Worksheet import Worksheet from xlwt.Style import easyxf filename = /home/ubuntu/Downloads/log.txt #filename = "/var/lib/jenkins/jobs/hsail2brig/builds/" + os.environ["BUILD_NUMBER"] + "/log" resultlog = readlog(filename) totalinfo = gettotalinfo(resultlog) resultlog = trimlog(resultlog) casesinfo = getcases(resultlog) def readlog(filename): logfile = open(filename) log = [] log = [l for l in logfile.readlines() if l.strip()] logfile.close() return log def gettotalinfo(log) totalinfo = {} foundtests = 0 foundnumbers = 0 foundendnumbers = 0 foundpassed = 0 for line in log if foundnumbers == 0: testsinfo = re.search("\[==========\] Running (\d+) tests from (\d+) test cases", line) if testsinfo: totalinfo['totaltests'] = testsinfo.group(1) totalinfo['totaltestcases'] = testsinfo.group(2) foundnumbers = 1 elif foundendnumbers == 0: endtestsinfo = re.search("\[==========\] (\d+) tests from (\d+) test cases ran. \((\d+) ms total\)", line) if endtestsinfo: totalinfo['totaltime'] = endtestsinfo.group(3) foundendnumbers = 1 elif foundpassed == 0: passednum = re.search("\[\s+PASSED\s+\] (\d+) tests.", line) if passednum: totalinfo['totalpassed'] = passednum.group(1) foundpassed = 1 else: testsdisabled = re.search("YOU HAVE (\d+) DISABLED TESTS", line) if testsdisabled: totalinfo['disabled'] = testsdisabled.group(1) totalinfo['totalfailed'] = str(int(totalinfo['totaltests']) - int(totalinfo['totalpassed'])) return totalinfo def trimlog(log): del log[0:log.index("[----------] Global test environment set-up.\n",) + 1] del log[log.index("[----------] Global test environment tear-down\n"):len(log)] return log def getcases(log): testcases = [] caseflag = 0 for line in log: if caseflag == 0: beginline = re.search("\[----------\] (\d+) tests from (.*)", line) if beginline: testcase={} testcase['casetests'] = beginline.group(1) testcase['casename'] = beginline.group(2) beginlinenumber = log.index(line,) caseflag = 1 else: endline = re.search("\[----------\] \d+ tests from \S+ \((\d+) ms total\)", line) if endline: testcase['casetime'] = endline.group(1) endlinenumber = log.index(line,) + 1 testcase['caselog'] = log[beginlinenumber + 1:endlinenumber - 1] testcase['casepassed'] = 0 testcase['casefailed'] = 0 testcase['casetestinfo'] = [] testcase = gettests(testcase) testcases.append(testcase) caseflag = 0 return testcases def gettests(testcase): testflag = 0 for line in testcase['caselog']: if testflag == 0: beginline = re.search("\[\s*RUN\s*\] .+?\.(.+)", line) if beginline: test={} test['testname'] = beginline.group(1) beginlinenumber = testcase['caselog'].index(line,) testflag = 1 else: endline = re.search("\[\s*(FAILED|OK)\s*\] .+?\..+ \((\d+) ms\)", l) if endline: if endline.group(1) == "FAILED": test['testresult'] = "fail" testcase['casefailed'] += 1 else: test['testresult'] = "pass" testcase['casepassed'] +=1 test['testtime'] = endline.group(2) endlinenumber = caselog.index(line,) + 1 test['testlog'] = caselog[beginlinenumber:endlinenumber] testcase['casetestinfo'].append(test) testflag = 0 return testcase wb = Workbook() ws = xlwt.Worksheet ws = wb.add_sheet("hsail2brig", True) headerstyle = easyxf( 'font: color black, bold true, height 240, name Ubuntu Mono;' 'borders: left thin, right thin, top thin, bottom thin,' 'left_color black, right_color black, top_color black, bottom_color black;') casestyle = easyxf( 'pattern: pattern solid, fore_colour pale_blue;' 'font: color black, bold true, name Ubuntu Mono;' 'borders: left thin, right thin, top thin, bottom thin,' 'left_color black, right_color black, top_color black, bottom_color black;') passstyle = easyxf( 'pattern: pattern solid, fore_colour dark_green_ega;' 'font: color black, name Ubuntu Mono;' 'borders: left thin, right thin, top thin, bottom thin,' 'left_color black, right_color black, top_color black, bottom_color black;') failstyle = easyxf( 'pattern: pattern solid, fore_colour dark_red;' 'font: color white, name Ubuntu Mono;' 'borders: left thin, right thin, top thin, bottom thin,' 'left_color black, right_color black, top_color black, bottom_color black;') rownum = 0 namecolumn = 0 resultcolumn = 1 timecolumn = 2 logcolumn = 3 namewidth = 0 resultwidth = 0 timewidth = 0 logwidth = 0 namestring = totaltestcasesnum + " test cases" if len(namestring) * 256 > namewidth: namewidth = len(namestring) * 256 ws.write(rownum, namecolumn, namestring, headerstyle) resultstring = totaltestnum + " tests, " + totalpassed + " passed, " + totalfailed + " failed" if len(resultstring) * 300 > resultwidth: resultwidth = len(resultstring) * 300 ws.write(rownum, resultcolumn, resultstring, headerstyle) timestring = totaltime + " ms" if len(timestring) * 400 > timewidth: timewidth = len(timestring) * 400 ws.write(rownum, timecolumn, timestring, headerstyle) logstring = disabled + " tests disabled" if len(logstring) * 256 > logwidth: logwidth = len(logstring) * 256 ws.write(rownum, logcolumn, logstring, headerstyle) rownum += 1 for case in testcases: namestring = case['name'] if len(namestring) * 256 > namewidth: namewidth = len(namestring) * 256 ws.write(rownum, namecolumn, namestring, casestyle) resultstring = case['testnum'] + " tests, " + case['passed'] + " passed, " + case['failed'] + " failed" if len(resultstring) * 256 > resultwidth: resultwidth = len(resultstring) * 256 ws.write(rownum, resultcolumn, resultstring, casestyle) timestring = case['time'] + "ms" if len(timestring) * 256 > timewidth: timewidth = len(timestring) * 256 ws.write(rownum, timecolumn, timestring, casestyle) ws.write(rownum, logcolumn, '', casestyle) rownum += 1 for test in case['tests']: if test['result'] == 'fail': teststyle = failstyle else: teststyle = passstyle namestring = test['name'] if len(namestring) * 256 > namewidth: namewidth = len(namestring) * 256 ws.write(rownum, namecolumn, namestring, teststyle) resultstring = test['result'] if len(resultstring) * 256 > resultwidth: resultwidth = len(resultstring) * 256 ws.write(rownum, resultcolumn, resultstring, teststyle) timestring = test['time'] + "ms" if len(timestring) * 256 > timewidth: timewidth = len(timestring) * 256 ws.write(rownum, timecolumn, timestring, teststyle) tlog = "" rowheight = 0 for logline in test['log']: rowheight += 1 logl = logline.strip() if len(logl) * 256 > logwidth: logwidth = len(logl) * 256 tlog = tlog + "\n" + logl ws.write(rownum, logcolumn, tlog.strip(), teststyle) ws.row(rownum).height_mismatch = 1 ws.row(rownum).height = 195 * rowheight + 20 rownum += 1 ws.col(namecolumn).width = namewidth ws.col(resultcolumn).width = resultwidth ws.col(timecolumn).width = timewidth ws.col(logcolumn).width = logwidth wb.save("/var/lib/jenkins/jobs/hsail2brig/workspace/" + os.environ["BUILD_ID"] + ".xlsx")
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question