|
| 1 | +#! /usr/bin/env python |
| 2 | +import re |
| 3 | +import os, os.path |
| 4 | +from collections import * |
| 5 | +import argparse |
| 6 | + |
| 7 | +cur_path = os.getcwd() |
| 8 | +match_ok = re.match("(/tmpdata/SD5886V100[_|2]*)",cur_path) |
| 9 | +if(match_ok): |
| 10 | + rpt_path = match_ok.group(1) + "/regress_report" |
| 11 | +else: |
| 12 | + print "You are not on /tmpdata/SD5886... dir, please rerun on it" |
| 13 | +#rpt_path = "/tmpdata/SD5886V100_2/regress_report" |
| 14 | + |
| 15 | +date_dict = {} |
| 16 | +blk2mod = OrderedDict() |
| 17 | + |
| 18 | +''' |
| 19 | +Add help information here. |
| 20 | +''' |
| 21 | + |
| 22 | +help_parser = argparse.ArgumentParser( |
| 23 | + formatter_class = argparse.RawDescriptionHelpFormatter, |
| 24 | + description = "Grs", |
| 25 | + epilog = 'More information and error report, please contact Chris lee' |
| 26 | +) |
| 27 | + |
| 28 | +help_paser.add_argument( |
| 29 | + '-s','--start', |
| 30 | + metavar = 'start', |
| 31 | + type = str, |
| 32 | + help = 'Set start date, eg. -s 20120101.' |
| 33 | +) |
| 34 | + |
| 35 | +help_paser.add_argument( |
| 36 | + '-e','--end', |
| 37 | + metavar = 'end', |
| 38 | + type = str, |
| 39 | + help = 'Set end date, eg. -s 20130101.' |
| 40 | +) |
| 41 | + |
| 42 | +help_paser.add_argument( |
| 43 | + '-b','--block', |
| 44 | + metavar = 'block', |
| 45 | + nargs = "+", |
| 46 | + type = str, |
| 47 | + help = 'Set which block need collect, default is all!' |
| 48 | +) |
| 49 | + |
| 50 | +help_paser.add_argument( |
| 51 | + '-p','--png', |
| 52 | + action = 'store_true', |
| 53 | + default = False, |
| 54 | + help = 'Auto generate png.' |
| 55 | +) |
| 56 | + |
| 57 | +args = help_parser.parse_args() |
| 58 | + |
| 59 | +def updateDateDictRpt(blk,pre_path,rpt): |
| 60 | + m = re.match("(\w+)_(\d+)_(\d+).rpt",rpt) |
| 61 | + if m: |
| 62 | + d = date_dict.setdefault(m.group(2),{}) #date |
| 63 | + b = d.setdefault(blk,{}) #block name |
| 64 | + mo = b.setdefault(m.group(1),[]) |
| 65 | + |
| 66 | + mo.append( |
| 67 | + [m.group(3), |
| 68 | + getCovInfo(os.path.sep.join([pre_path, rpt]))] |
| 69 | + ) |
| 70 | + |
| 71 | + b[m.group(1)] = sorted(mo, cmpTwoCov) |
| 72 | + |
| 73 | + if m.group(1) no in blk2mod[blk]: |
| 74 | + blk2mod[blk].append(m.group(1)) |
| 75 | + |
| 76 | + |
| 77 | +def updateDateDict(blk,ver_list,pre_path): |
| 78 | + for e in ver_list: |
| 79 | + ver_path = os.path.sep.join([pre_path, e]) |
| 80 | + if os.path.isdir(ver_path): |
| 81 | + for rpt in os.listdir(ver_path): |
| 82 | + updateDateDictRpt(blk,ver_path,rpt) |
| 83 | + else: |
| 84 | + updateDateDictRpt(blk,pre_path,e) |
| 85 | + |
| 86 | + |
| 87 | +def cmpTwoCov(a,b): |
| 88 | + sa = sum([float(x[:-1]) if x != "--" else 0 for x in a[1]:[:-1]]) #Don't compare TC nums |
| 89 | + sb = sum([float(x[:-1]) if x != "--" else 0 for x in b[1]:[:-1]]) |
| 90 | + if sa > sb: |
| 91 | + return 1 |
| 92 | + return -1 |
| 93 | + |
| 94 | + |
| 95 | +def getCovInfo(path): |
| 96 | + with open(path) as f: |
| 97 | + line_number = 0 |
| 98 | + st_num = 0 |
| 99 | + for a_line in f: |
| 100 | + line_number += 1 |
| 101 | + if(re.match("The Digest of the Report",a_line)): |
| 102 | + st_num = line_number |
| 103 | + if(re.match("\s*TOTAL",a_line)): |
| 104 | + break |
| 105 | + tc_num = str(line_number - st_num -6) |
| 106 | + f.seek(0) |
| 107 | + m = re.search( |
| 108 | + "TOTAL\s+\|\s*(\d+)\s*\|[0-9]+\|[0-9]+\|\s+([0-9.]+).+Code Coverage:\s+([0-9.%]*).+Functional Coverage:\s+([0-9.%]*).+Plan Coverage:\s+([0-9.%]*)", |
| 109 | + f.read(), |
| 110 | + flags = re.M|re.S |
| 111 | + ) |
| 112 | + if m: |
| 113 | + cov0 = m.group(3) |
| 114 | + cov1 = m.group(4) |
| 115 | + cov2 = m.group(5) |
| 116 | + if cov0 == "%": |
| 117 | + cov0 = "0.0%" |
| 118 | + if cov1 == "%": |
| 119 | + cov1 = "0.0%" |
| 120 | + if cov2 == "%": |
| 121 | + cov2 = "0.0%" |
| 122 | + return cov0,cov1,cov2,m.group(2),tc_num |
| 123 | + else: |
| 124 | + return "--","--","--","--","--" |
| 125 | + |
| 126 | + |
| 127 | + |
| 128 | + |
| 129 | +#Start parser all date report info. |
| 130 | +for e in os.listdir(rpt_path): |
| 131 | + pre_path = os.path.sep.join([rpt_path,e]) |
| 132 | + if os.path.isdir(pre_path): |
| 133 | + if args.block and e not in args.block: |
| 134 | + continue |
| 135 | + blk2mod[e] = [] |
| 136 | + updateDateDict(e,os.listdir(pre_path),pre_path) |
| 137 | + |
| 138 | + |
| 139 | +#Do some date filter |
| 140 | +_dates = date_dict.keys() |
| 141 | +_dates.sort() |
| 142 | +dates = [] |
| 143 | +for d in _dates: |
| 144 | + if args.start and d < args.start: |
| 145 | + continue |
| 146 | + if args.end and d > args.end: |
| 147 | + continue |
| 148 | + dates.append(d) |
| 149 | + |
| 150 | + |
| 151 | +import sys,time |
| 152 | +prefix_path = os.path.realpath(os.path.dirname(sys.argv[0])) |
| 153 | + |
| 154 | +#Generate the summary report.. |
| 155 | +def gen_report(): |
| 156 | + import jinja2 |
| 157 | + from jinja2 import Environment |
| 158 | + from jinja2.loaders import FileSystemLoader |
| 159 | + env = Environment(loader=FileSystemLoader(prefix_path + '/tpl')) |
| 160 | + tmpl = env.get_template('grs.rpt') |
| 161 | + |
| 162 | + grpt_name = "summary_report_%s.rpt" % time.strftime("%Y%m%d") |
| 163 | + with open(grpt_name, "w") as f: |
| 164 | + f.write( |
| 165 | + tmpl.render( |
| 166 | + time = time.strftime("%a, %d %b %Y %H:%M:%S"), |
| 167 | + dates = dates, |
| 168 | + date_dict = date_dict, |
| 169 | + blks = blk2mod |
| 170 | + ) |
| 171 | + ) |
| 172 | + |
| 173 | + print grpt_name + " is generated!" |
| 174 | + |
| 175 | + |
| 176 | +def gen_gui(fig,cc,dates,mode,blk): |
| 177 | + max_tc_nums = 0 |
| 178 | + min_tc_nums = 1000 |
| 179 | + max_rate = 0 |
| 180 | + min_rate = 100 |
| 181 | + colors = ['r','g','b','y','k'] |
| 182 | + shapes = ['o','s','^','d','*'] |
| 183 | + lstyle = ['-','-','-','-','-.'] |
| 184 | + labels = ['Code Cov','Func Cov','Plan Cov','Pass Rate','TC Nums'] |
| 185 | + cov_l_data = [[[]],[[]],[[]],[[]],[[]]] |
| 186 | + |
| 187 | + for x,d in enumerate(dates): |
| 188 | + if blk in date_dict[d] and mode in date_dict[d][blk]: |
| 189 | + cov = date_dict[d][blk][mode][-1][1] |
| 190 | + for i in range(4): |
| 191 | + tmp = float(cov[i][:-1]) if "-" not in cov[i] else 0 |
| 192 | + if tmp < min_rate: |
| 193 | + min_rate = tmp |
| 194 | + if tmp > max_rate: |
| 195 | + max_rate = tmp |
| 196 | + cov_l_data[i][-1].append([x,tmp]) |
| 197 | + tmp = int(cov[4]) if "-" not in cov[4] else 0 |
| 198 | + cov_l_data[4][-1].append([x,tmp]) |
| 199 | + if tmp > max_tc_nums: |
| 200 | + max_tc_nums = tmp |
| 201 | + if tmp < min_tc_nums: |
| 202 | + min_tc_nums = tmp |
| 203 | + |
| 204 | + else: |
| 205 | + for i in range(5): |
| 206 | + if cov_l_datap[i][-1]: |
| 207 | + cov_l_data[i].append([]) |
| 208 | + |
| 209 | + for i in range(5): |
| 210 | + if not cov_l_data[i][-1]: |
| 211 | + cov_l_data[i].pop(-1) |
| 212 | + line2d = [] |
| 213 | + for l in cov_l_data[i]: |
| 214 | + x = [] |
| 215 | + y = [] |
| 216 | + for e in l: |
| 217 | + x.append(e[0]) |
| 218 | + y.append(e[1]) |
| 219 | + line2d.append(Line2D(x,y,color = colors[i], linestyle = lstyle[i], marker = shapes[i])) |
| 220 | + cov_l_datap[i] = line2d |
| 221 | + |
| 222 | + |
| 223 | + def ddate_format(x,pos=None): |
| 224 | + ind = np.clip(int(x+0.5),0,len(dates)-1) |
| 225 | + if ind >= len(dates): |
| 226 | + return "" |
| 227 | + return dates[ind][4:] |
| 228 | + |
| 229 | + x = range(len(dates)) |
| 230 | + cc.xaxis.set_major_formatter(ticker.FuncFormatter(date_format)) |
| 231 | + cc.set_ylim([0,100]) |
| 232 | + cc.set_xlim([0,len(dates) - 1]) |
| 233 | + cc.grid(True) |
| 234 | + |
| 235 | + lines = [] |
| 236 | + tl = None |
| 237 | + for i in range(4): |
| 238 | + for l in cov_l_data[i]: |
| 239 | + tl = cc.add_line(l) |
| 240 | + if tl: |
| 241 | + lines.append(tl) |
| 242 | + |
| 243 | + cc.set_title(blk + "'s Cov(%s)" % mode) |
| 244 | + cc.set_xticks(x) |
| 245 | + cc.set_xlabel("Date") |
| 246 | + cc.set_ylabel("Coverage Date(%)") |
| 247 | + |
| 248 | + cc2 = cc.twinx() |
| 249 | + cc2.set_ylim(min_tc_nums - 1, max_tc_nums + 1) |
| 250 | + for l in cov_l_data[4]: |
| 251 | + tl = cc2.add_line(l) |
| 252 | + if tl: |
| 253 | + lines.append(tl) |
| 254 | + cc2.set_ylabel("TC Nums") |
| 255 | + |
| 256 | + font = FontProperties() |
| 257 | + font.set_size(9) |
| 258 | + l1 = fig.legend(lines[:4],labels[:4],"lower left",prop = font) |
| 259 | + l2 = fig.legend(lines[4:5],labels[4:5],"lower right",prop = font) |
| 260 | + plt.gca().add_artist(l1) |
| 261 | + plt.gca().add_artist(l2) |
| 262 | + |
| 263 | +if args.png: |
| 264 | + import subprocess as sp |
| 265 | + import os.path |
| 266 | + if not os.path.exists(prefix_path + "/matplotlib"): |
| 267 | + print "Initial ..., please wait!" |
| 268 | + sp.check_call(["tar","-xjf",prefix_path + "/matplotlib.tar.bz2","-C",prefix_path]) |
| 269 | + sys.path.insert(0,prefix_path + "/matplotlib") |
| 270 | + print "Please rerun!" |
| 271 | + quit(1) |
| 272 | + |
| 273 | + import matplotlib.pyplot as plt |
| 274 | + import matplotlib.ticker as ticker |
| 275 | + from matplotlib.collections import LineCollection |
| 276 | + from matplotlib.lines import Line2D |
| 277 | + from matplotlib.font_manger import FontProperties |
| 278 | + import numpy as np |
| 279 | + tmpString = '' |
| 280 | + for blk in blk2mod: |
| 281 | + for mod in blk2mod[blk]: |
| 282 | + fig = plt.figure() |
| 283 | + cc = fig.add_subplot(1,1,1) |
| 284 | + gen_gui(fig,cc,dates,mod,blk) |
| 285 | + fig.autofmt_xdate() |
| 286 | + plt.savefig("%s_%s.png" % (blk,mod), dpi = 100) |
| 287 | + plt.clf() |
| 288 | + tmpString += '<img src = "%s_%s.png" />\n' % (blk,mod) |
| 289 | + |
| 290 | + htmlString = '''<html>\n |
| 291 | + |
| 292 | + |
| 293 | + |
| 294 | + ''' % (tmpString) |
| 295 | + with open("grs_gng.html", "w") as f: |
| 296 | + f.write(htmlString) |
| 297 | +else: |
| 298 | + gen_report() |
| 299 | + |
| 300 | + |
| 301 | + |
| 302 | + |
| 303 | + |
| 304 | + |
| 305 | + |
| 306 | + |
| 307 | + |
| 308 | + |
| 309 | + |
| 310 | + |
| 311 | + |
| 312 | + |
| 313 | + |
| 314 | + |
| 315 | + |
| 316 | + |
| 317 | + |
| 318 | + |
| 319 | + |
0 commit comments