report.py 4.58 KB
Newer Older
1 2
#!/usr/bin/env python3

3
import os, time
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
from pathlib import Path
 
def getListOfFiles(dirName):
    listOfFile = os.listdir(dirName)
    allFiles = list()
    for entry in listOfFile:
        fullPath = os.path.join(dirName, entry)
        if os.path.isdir(fullPath):
            allFiles = allFiles + getListOfFiles(fullPath)
        else:
            if Path(fullPath).suffix==".status":
                allFiles.append(fullPath)
                
    return allFiles        
 
 
def main():
    listOfFiles = getListOfFiles('.')
    listOfFiles.sort()
    testsuits = list()
    casenumber = dict()
    errors = dict()
    failures = dict()
    total_errors = 0
    total_failures = 0
29 30
    min_start_time = time.time()
    max_end_time = 0
31 32 33 34 35 36 37 38 39 40 41
    for elem in listOfFiles :
        st = elem.split('/')
        testsuit = st[1]
        testcase = st[-1].replace('.status','')
        if (testsuit not in testsuits):
            testsuits.append(testsuit)
            casenumber[testsuit] = 0
            errors[testsuit] = 0
            failures[testsuit] = 0
        casenumber[testsuit] += 1
        status = open(elem, 'r').read().strip()
42 43
        min_start_time = min(min_start_time, os.path.getmtime(os.path.join(os.path.dirname(elem),'.start')))
        max_end_time = max(max_end_time, os.path.getmtime(os.path.join(os.path.dirname(elem),'.stamp')))
44 45 46 47 48 49 50 51 52
        if (status=='ERROR'):
            errors[testsuit] += 1
            total_errors += 1
        if (status=='FAIL'):
            failures[testsuit] += 1
            total_failures += 1
    # Creating report
    with open("report.xml", "w") as f:
        print('<?xml version="1.0" encoding="UTF-8"?>', file=f)
53
        print('<testsuites disabled="0" errors="%d" failures="%d" tests="%d" time="%d">' % (total_errors, total_failures, len(listOfFiles), max_end_time - min_start_time), file=f)
54 55 56 57 58 59 60 61 62 63
        for suite in testsuits:
            print('    <testsuite disabled="0" errors="%d" failures="%d" name="%s" skipped="0" tests="%d" time="%d">' % (errors[suite], failures[suite], suite, casenumber[suite], 0), file=f)        
            for elem in listOfFiles :
                st = elem.split('/')
                testsuit = st[1]
                if (testsuit != suite):
                    continue
                testcase = st[-1].replace('.status','')
                casenumber[testsuit] += 1
                status = open(elem, 'r').read().strip()
64 65
                print('        <testcase classname="%s.%s" name="%s" status="%s" time="%d">' % (testsuit, st[2].replace('.status',''), testcase, status, 
                    os.path.getmtime(os.path.join(os.path.dirname(elem),'.stamp')) - os.path.getmtime(os.path.join(os.path.dirname(elem),'.start'))), file=f)
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
                if (status=='ERROR'):
                    print('            <error message="%s" type="%s"/>' % (status, status), file=f)
                if (status=='FAIL'):
                    print('            <failure message="%s" type="%s"/>' % (status, status), file=f)
                    file_tb = os.path.join(os.path.dirname(elem),'testbench.log')
                    file_re = os.path.join(os.path.dirname(elem),'result.log')
                    file_ys = os.path.join(os.path.dirname(elem),'yosys.log')
                    if (os.path.isfile(file_tb)):
                        print('<system-out>', end="", file=f)
                        with open(file_tb, "r") as logf:
                            for line in logf:
                                print(line.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;").replace("\"", "&quot;"), end="", file=f)
                        print('</system-out>', file=f)
                    elif (os.path.isfile(file_re)):
                        print('<system-out>', end="", file=f)
                        with open(file_re, "r") as logf:
                            for line in logf:
                                print(line.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;").replace("\"", "&quot;"), end="", file=f)
                        print('</system-out>', file=f)
                    elif (os.path.isfile(file_ys)):
                        print('<system-out>', end="", file=f)
                        with open(file_ys, "r") as logf:
                            for line in logf:
                                print(line.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;").replace("\"", "&quot;"), end="", file=f)
                        print('</system-out>', file=f)
                print('        </testcase>', file=f)
            
            print('    </testsuite>', file=f)

        print('</testsuites>', file=f)
if __name__ == '__main__':
    main()