qmtest_summarize.py
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009 import os, sys, re
00010
00011 def parse_summary(output):
00012 report = { "results":[], "not_passed":[], "statistics": {} }
00013 current_block = None
00014 for l in output.splitlines():
00015 if l.startswith("--- TEST RESULTS"):
00016 current_block = "results"
00017 elif l.startswith("--- TESTS THAT DID NOT PASS"):
00018 current_block = "not_passed"
00019 elif l.startswith("--- STATISTICS"):
00020 current_block = "statistics"
00021 else:
00022 if current_block in ["results","not_passed"]:
00023 report[current_block].append(l)
00024 elif current_block == "statistics":
00025 tkns = l.split()
00026 if tkns:
00027 report["statistics"][tkns[-1]] = int(tkns[0])
00028
00029 report["results"] = report["results"][:-1]
00030 report["not_passed"] = report["not_passed"][:-2]
00031 if report["not_passed"][-1].strip() == "None.":
00032 report["not_passed"] = []
00033 return report
00034
00035 def main():
00036
00037
00038
00039
00040
00041
00042
00043 if "CMTINSTALLAREA" not in os.environ:
00044 os.environ["CMTINSTALLAREA"] = os.popen("cmt show macro_value CMTINSTALLAREA","r").read().strip()
00045
00046
00047 cmt_br_header_re = re.compile(r"^# Now trying.*in (.*) \([0-9]+/[0-9]+\)")
00048 dirs = []
00049
00050
00051 old_CMTUSERCONTEXT = None
00052 if "CMTUSERCONTEXT" in os.environ:
00053 old_CMTUSERCONTEXT = os.environ["CMTUSERCONTEXT"]
00054 del os.environ["CMTUSERCONTEXT"]
00055
00056 for l in os.popen("cmt broadcast","r"):
00057 m = cmt_br_header_re.match(l)
00058 if m:
00059 dirs.append(m.group(1))
00060
00061
00062 if old_CMTUSERCONTEXT is not None:
00063 os.environ["CMTUSERCONTEXT"] = old_CMTUSERCONTEXT
00064
00065
00066 results = filter(os.path.isfile,[ os.path.realpath(os.path.join(d,'..',os.environ["CMTCONFIG"],"results.qmr")) for d in dirs ])
00067
00068
00069 if len(results) == 0:
00070 print "Warning: no result file found! (Did you run the tests?)"
00071 sys.exit()
00072
00073
00074
00075
00076 from tempfile import mkdtemp
00077 from shutil import rmtree
00078 tmpdir = mkdtemp()
00079 origdir = os.getcwd()
00080 report = None
00081 try:
00082 os.chdir(tmpdir)
00083 os.popen("qmtest create-tdb","r").read()
00084
00085 for r in results:
00086 out = os.popen("qmtest summarize -f brief %s"%r,"r").read()
00087 rep = parse_summary(out)
00088 if report is None:
00089 report = rep
00090 else:
00091 report["results"] += rep["results"]
00092 if rep["not_passed"]:
00093 report["not_passed"] += rep["not_passed"]
00094 for k in rep["statistics"]:
00095 if k in report["statistics"]:
00096 report["statistics"][k] += rep["statistics"][k]
00097 else:
00098 report["statistics"][k] = rep["statistics"][k]
00099 finally:
00100 os.chdir(origdir)
00101 rmtree(tmpdir,ignore_errors=True)
00102
00103 if report is None:
00104 print "Warning: I could not generate the report"
00105 sys.exit()
00106
00107
00108 report["results"].append('')
00109 if not report["not_passed"]:
00110 report["not_passed"] = ['',' None.']
00111 report["not_passed"] += ['','']
00112
00113 statistics_output = ['--- STATISTICS ---------------------------------------------------------------','']
00114
00115
00116 tot = report["statistics"]["total"]
00117 statistics_output.append("%8d tests total"%(tot))
00118 for k in [ "ERROR", "FAIL", "UNTESTED", "PASS" ]:
00119 if k in report["statistics"]:
00120 n = report["statistics"][k]
00121 p = round(100. * n / tot)
00122 statistics_output.append("%8d (%3d%%) tests %s"%(n,p,k))
00123 statistics_output.append('')
00124
00125 results_output = ['--- TEST RESULTS -------------------------------------------------------------']
00126 results_output += report["results"]
00127
00128 not_passed_output = ['--- TESTS THAT DID NOT PASS --------------------------------------------------']
00129 if not report["not_passed"]:
00130 not_passed_output
00131 not_passed_output += report["not_passed"]
00132
00133 output = statistics_output + not_passed_output + results_output + not_passed_output + statistics_output
00134 print '\n'.join(output)
00135
00136 if __name__ == '__main__':
00137 main()