![]() |
|
|
Generated: 8 Jan 2009 |
00001 #!/usr/bin/env python 00002 # 00003 # Simple script to loop over the packages providing tests and collect 00004 # the result summaries 00005 # 00006 # @author: Marco Clemencic <marco.clemencic@cern.ch> 00007 # @date: 3/11/2007 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 # I assume that this script is run from a CMT action, which implies few other 00036 # things: 00037 # - we are in the cmt directory of a package 00038 # - CMTCONFIG etc. are correctly defined 00039 # - we can call cmt 00040 00041 # Directory where to store the report 00042 if "CMTINSTALLAREA" not in os.environ: 00043 os.environ["CMTINSTALLAREA"] = os.popen("cmt show macro_value CMTINSTALLAREA","r").read().strip() 00044 00045 # Find the packages (their cmt dirs) 00046 cmt_br_header_re = re.compile(r"^# Now trying.*in (.*) \([0-9]+/[0-9]+\)") 00047 dirs = [] 00048 00049 # Hack to work around a problem with cmt broadcast and CMTUSERCONTEXT. 00050 old_CMTUSERCONTEXT = None 00051 if "CMTUSERCONTEXT" in os.environ: 00052 old_CMTUSERCONTEXT = os.environ["CMTUSERCONTEXT"] 00053 del os.environ["CMTUSERCONTEXT"] 00054 00055 for l in os.popen("cmt broadcast","r"): 00056 m = cmt_br_header_re.match(l) 00057 if m: 00058 dirs.append(m.group(1)) 00059 00060 # Restore original CMTUSERCONTEXT, if it was set. 00061 if old_CMTUSERCONTEXT is not None: 00062 os.environ["CMTUSERCONTEXT"] = old_CMTUSERCONTEXT 00063 00064 # Find the results.qmr files 00065 results = filter(os.path.isfile,[ os.path.realpath(os.path.join(d,'..',os.environ["CMTCONFIG"],"results.qmr")) for d in dirs ]) 00066 00067 # Check if the result files have been found 00068 if len(results) == 0: 00069 print "Warning: no result file found! (Did you run the tests?)" 00070 sys.exit() 00071 00072 # parse and collect the summaries 00073 # FIXME: (MCl) with QMTest 2.3 we need a test database to be able to run "qmtest summarize", while it 00074 # is not needed with QMTest 2.4 00075 from tempfile import mkdtemp 00076 from shutil import rmtree 00077 tmpdir = mkdtemp() 00078 origdir = os.getcwd() 00079 report = None 00080 try: 00081 os.chdir(tmpdir) 00082 os.popen("qmtest create-tdb","r").read() 00083 00084 for r in results: 00085 out = os.popen("qmtest summarize -f brief %s"%r,"r").read() 00086 rep = parse_summary(out) 00087 if report is None: 00088 report = rep 00089 else: 00090 report["results"] += rep["results"] 00091 if rep["not_passed"]: 00092 report["not_passed"] += rep["not_passed"] 00093 for k in rep["statistics"]: 00094 if k in report["statistics"]: 00095 report["statistics"][k] += rep["statistics"][k] 00096 else: 00097 report["statistics"][k] = rep["statistics"][k] 00098 finally: 00099 os.chdir(origdir) 00100 rmtree(tmpdir,ignore_errors=True) 00101 00102 if report is None: 00103 print "Warning: I could not generate the report" 00104 sys.exit() 00105 00106 # Finalize the report 00107 report["results"].append('') 00108 if not report["not_passed"]: 00109 report["not_passed"] = ['',' None.'] 00110 report["not_passed"] += ['',''] 00111 00112 statistics_output = ['--- STATISTICS ---------------------------------------------------------------',''] 00113 # order: total, FAIL, UNTESTED, PASS 00114 #" 2 tests total" 00115 tot = report["statistics"]["total"] 00116 statistics_output.append("%8d tests total"%(tot)) 00117 for k in [ "ERROR", "FAIL", "UNTESTED", "PASS" ]: 00118 if k in report["statistics"]: 00119 n = report["statistics"][k] 00120 p = round(100. * n / tot) 00121 statistics_output.append("%8d (%3d%%) tests %s"%(n,p,k)) 00122 statistics_output.append('') 00123 00124 results_output = ['--- TEST RESULTS -------------------------------------------------------------'] 00125 results_output += report["results"] 00126 00127 not_passed_output = ['--- TESTS THAT DID NOT PASS --------------------------------------------------'] 00128 if not report["not_passed"]: 00129 not_passed_output 00130 not_passed_output += report["not_passed"] 00131 00132 output = statistics_output + not_passed_output + results_output + not_passed_output + statistics_output 00133 print '\n'.join(output)