| 1 | #!/bin/env python |
|---|
| 2 | filenamein = "testsuite.log" |
|---|
| 3 | filenameout = "testsuite.xml" |
|---|
| 4 | verbose = 0 |
|---|
| 5 | |
|---|
| 6 | import os |
|---|
| 7 | import sys |
|---|
| 8 | |
|---|
| 9 | def getSourceDir(): |
|---|
| 10 | """Returns the location of the source code.""" |
|---|
| 11 | script = os.path.abspath(sys.argv[0]) |
|---|
| 12 | if os.path.islink(script): |
|---|
| 13 | script = os.path.realpath(script) |
|---|
| 14 | return os.path.dirname(script) |
|---|
| 15 | |
|---|
| 16 | class Test: |
|---|
| 17 | def __init__(self, text, verbose): |
|---|
| 18 | if verbose > 1: |
|---|
| 19 | print "*****", text |
|---|
| 20 | self.__time = 0. |
|---|
| 21 | self.__totaltime = 0. |
|---|
| 22 | self.__getStatus(text) |
|---|
| 23 | self.__getTimes(text) |
|---|
| 24 | |
|---|
| 25 | # trim down the text to just what we need for line number |
|---|
| 26 | start = text.find("testsuite.at") |
|---|
| 27 | stop = text.find(": ", start) |
|---|
| 28 | lineref = text[start:stop] |
|---|
| 29 | index = lineref.find(":") |
|---|
| 30 | self.__line = int(lineref[index+1:-1]) |
|---|
| 31 | |
|---|
| 32 | # get the group and name |
|---|
| 33 | self.__group = "unknown" |
|---|
| 34 | self.__name = "dob" |
|---|
| 35 | |
|---|
| 36 | text = text[:start-1] |
|---|
| 37 | index = text.find(" ") |
|---|
| 38 | text = text[index:].strip() |
|---|
| 39 | text = text.replace("Checking", "") |
|---|
| 40 | text = text.replace("Check the", "") |
|---|
| 41 | text = text.replace("C++", "CXX") |
|---|
| 42 | text = text.replace("->", "to") |
|---|
| 43 | text = text.replace("(", "") |
|---|
| 44 | text = text.replace(")", "") |
|---|
| 45 | text = text.replace("xml", "XML") |
|---|
| 46 | text = text.strip() |
|---|
| 47 | if text.endswith('.'): |
|---|
| 48 | text = text[:-1] |
|---|
| 49 | text = "_".join(text.split()) |
|---|
| 50 | |
|---|
| 51 | (self.__group, self.__name) = self.__splitGroup(text) |
|---|
| 52 | if verbose > 1: |
|---|
| 53 | print text, "->", (self.__group, self.__name) |
|---|
| 54 | |
|---|
| 55 | def __splitGroup(self, text): |
|---|
| 56 | # is it a binding |
|---|
| 57 | if "binding" in text: |
|---|
| 58 | splitted = text.split('_') |
|---|
| 59 | if splitted[0] in ("C", "CXX", "F77", "F90", "python", "IDL"): |
|---|
| 60 | return (splitted[0], "_".join(splitted[3:])) |
|---|
| 61 | |
|---|
| 62 | # is it an application |
|---|
| 63 | app = text.split('_')[0] |
|---|
| 64 | if app in ("nxconvert", "nxbrowse", "nxdir", "nxsummary"): |
|---|
| 65 | return (app, text.replace(app+"_", "")) |
|---|
| 66 | |
|---|
| 67 | # give up |
|---|
| 68 | return ("unknown", text) |
|---|
| 69 | |
|---|
| 70 | def __getStatus(self,text): |
|---|
| 71 | self.__msg = "<![CDATA[" + text + "]]>" |
|---|
| 72 | self.status = "failed" |
|---|
| 73 | if "skipped" in text: |
|---|
| 74 | self.status = "skipped" |
|---|
| 75 | elif "ok " in text: |
|---|
| 76 | self.status = "passed" |
|---|
| 77 | |
|---|
| 78 | def __getTimes(self, text): |
|---|
| 79 | if self.status == "skipped": |
|---|
| 80 | return # there is no time |
|---|
| 81 | |
|---|
| 82 | splitted = text.split()[-2:] |
|---|
| 83 | splitted[0] = splitted[0][1:] |
|---|
| 84 | splitted[1] = splitted[1][:-1] |
|---|
| 85 | splitted = [item[:-1] for item in splitted] |
|---|
| 86 | splitted = [item.split('m') for item in splitted] |
|---|
| 87 | self.__totaltime = float(splitted[0][0]) * 60. + float(splitted[0][1]) |
|---|
| 88 | self.__time = float(splitted[1][0]) * 60. + float(splitted[1][1]) |
|---|
| 89 | |
|---|
| 90 | def write(self, handle): |
|---|
| 91 | handle.write('<testcase classname="%s" name="%s" ' % \ |
|---|
| 92 | (self.__group, self.__name)) |
|---|
| 93 | handle.write('line="%d" ' % self.__line) |
|---|
| 94 | handle.write('time="%f" ' % self.__time) |
|---|
| 95 | handle.write('totalTime="%f"' % self.__totaltime) |
|---|
| 96 | if self.status == "passed": |
|---|
| 97 | handle.write('/>\n') |
|---|
| 98 | return |
|---|
| 99 | |
|---|
| 100 | handle.write('>\n') |
|---|
| 101 | |
|---|
| 102 | handle.write('<%s>\n' % self.status) |
|---|
| 103 | handle.write(self.__msg) |
|---|
| 104 | handle.write('</%s>\n' % self.status) |
|---|
| 105 | |
|---|
| 106 | handle.write('</testcase>\n') |
|---|
| 107 | |
|---|
| 108 | class TestSuite: |
|---|
| 109 | def __init__(self, data, verbose): |
|---|
| 110 | self.__suitename = "NeXus" |
|---|
| 111 | self.__tests = [] |
|---|
| 112 | self.__success = 0 |
|---|
| 113 | self.__failures = 0 |
|---|
| 114 | self.__skipped = 0 |
|---|
| 115 | self.__totaltime = 0 |
|---|
| 116 | |
|---|
| 117 | # find the total execution time |
|---|
| 118 | for (i, line) in zip(range(len(data)), data): |
|---|
| 119 | if line.startswith('testsuite:') and 'duration' in line: |
|---|
| 120 | splitted = line.split() |
|---|
| 121 | splitted = splitted[-3:] |
|---|
| 122 | splitted = [float(item[:-1]) for item in splitted] |
|---|
| 123 | (h,m,s) = splitted |
|---|
| 124 | self.__totaltime = s + 60 * m + 3600 * h |
|---|
| 125 | lastindex = i |
|---|
| 126 | |
|---|
| 127 | # trim the data down again |
|---|
| 128 | data = data[:lastindex-1] |
|---|
| 129 | |
|---|
| 130 | # create the individual tests |
|---|
| 131 | for line in data: |
|---|
| 132 | test = Test(line, verbose) |
|---|
| 133 | self.__tests.append(test) |
|---|
| 134 | if test.status == "skipped": |
|---|
| 135 | self.__skipped += 1 |
|---|
| 136 | elif test.status == "passed": |
|---|
| 137 | self.__success += 1 |
|---|
| 138 | else: |
|---|
| 139 | self.__failures += 1 |
|---|
| 140 | |
|---|
| 141 | def write(self, filename): |
|---|
| 142 | handle = file(filename, 'w') |
|---|
| 143 | handle.write('<?xml version="1.0" encoding="UTF-8" ?>\n') |
|---|
| 144 | |
|---|
| 145 | handle.write('<testsuite name="%s" ' % self.__suitename) |
|---|
| 146 | handle.write('tests="%d" ' % len(self.__tests)) |
|---|
| 147 | handle.write('failures="%d" ' % self.__failures) |
|---|
| 148 | handle.write('skipped="%d" ' % self.__skipped) |
|---|
| 149 | handle.write('package="%s" ' % self.__suitename) |
|---|
| 150 | handle.write('time="%f" ' % self.__totaltime) |
|---|
| 151 | handle.write('>\n') |
|---|
| 152 | |
|---|
| 153 | for test in self.__tests: |
|---|
| 154 | test.write(handle) |
|---|
| 155 | |
|---|
| 156 | handle.write('</testsuite>\n') |
|---|
| 157 | |
|---|
| 158 | |
|---|
| 159 | def getTests(filename, verbose): |
|---|
| 160 | handle = file(filename, 'r') |
|---|
| 161 | |
|---|
| 162 | data = handle.readlines() |
|---|
| 163 | data = [line.strip() for line in data] |
|---|
| 164 | handle.close() |
|---|
| 165 | for (i, line) in zip(range(len(data)), data): |
|---|
| 166 | if line.startswith("## Running the tests. ##"): |
|---|
| 167 | startindex = i |
|---|
| 168 | break |
|---|
| 169 | data = data[startindex+3:] |
|---|
| 170 | if verbose > 3: |
|---|
| 171 | print "\n".join(data) |
|---|
| 172 | |
|---|
| 173 | tests = TestSuite(data, verbose) |
|---|
| 174 | |
|---|
| 175 | return tests |
|---|
| 176 | |
|---|
| 177 | if __name__ == "__main__": |
|---|
| 178 | sourceDir = getSourceDir() |
|---|
| 179 | filenamein = os.path.join(sourceDir, filenamein) |
|---|
| 180 | filenameout = os.path.join(sourceDir, filenameout) |
|---|
| 181 | |
|---|
| 182 | print "Converting '%s' to '%s'" % (filenamein, filenameout) |
|---|
| 183 | if verbose: |
|---|
| 184 | print "Reading in", filenamein |
|---|
| 185 | tests = getTests(filenamein, verbose) |
|---|
| 186 | if verbose: |
|---|
| 187 | print "Writing out", filenameout |
|---|
| 188 | tests.write(filenameout) |
|---|