def get_data_list(path_input): log = [] log_list = [] weight = [] weight_list = [] reference = [] reference_list = [] path = kpi_path() #取出所有数据至log_list try: file1 = open(path_input,'rb') except Exception: print "Performance.py get_data_list() kpi_log.Qsst lose" exit(1) lines = file1.readlines() for line in lines: if line.find('#') > -1: log.append(line) #数据长度 x = len(log) i = 0 j = 0 #数据分类 for i in range(x): log_list.append(log[i].split(":")) #取出所有数据至weight_list try: file2 = open(path + "weight.txt",'rb') except Exception: print " Performance.py get_data_list() weight.text lose" lines = file2.readlines() for line in lines: if line.find("#") > -1: weight.append(line) #数据分类 z =len(weight) for i in range(z): weight_list.append(weight[i].split(":")) #添加权重到log_list for i in range(x): for j in range(z): if log_list[i][0] == weight_list[j][0] and log_list[i][1] == weight_list[j][1]: log_list[i].append(weight_list[j][2]) #取出所有数据至reference_list try: file3 = open(path + "reference_data.txt",'rb') except Exception: print " Performance.py get_data_list() reference_data.text lose" lines = file3.readlines() for line in lines: if line.find('#') > -1: reference.append(line) #数据分类 w =len(weight) for i in range(w): reference_list.append(reference[i].split(":")) #添加对比到log_list for i in range(x): for j in range(w): if log_list[i][0] == reference_list[j][0] and log_list[i][1] == reference_list[j][1]: log_list[i].append(reference_list[j][2]) #计算diff for i in range(x): log_list[i].append(str(((float(log_list[i][4][:-1])-float(log_list[i][2][:-1]))/float(log_list[i][4][:-1])))) #计算succeed for i in range(x): if float(log_list[i][5]) > float(0.05): log_list[i].append("succeed") else: log_list[i].append("failed") #计算测试分数 for i in range(x): log_list[i].append(float(log_list[i][2][:-1])*float(log_list[i][3][:-1])) #计算参考分数 for i in range(x): log_list[i].append(float(log_list[i][4][:-1])*float(log_list[i][3][:-1])) list = log_list return list
def html(path_input,path_output): ''' Rewrite the html. Read-in the data to the report. ''' list = get_data_list(path_input) dict = get_list(path_input) score = get_score(path_input) dict_html = get_html_list(path_input) category_list = [] category = [] #保存路径 path = kpi_path() #检查目录 if not os.path.exists(path): os.makedirs(path) i = 0 x = len(list) j = 0 for i in range(x): category_list.append(list[i][0]) #判断category数量 category = [i for i in set(category_list)] y =len(category) #创建详细列表 try: test_title= open(path+'Test_title.html','rb') except Exception: print "Performance.py html() Test_title.html lose" exit(1) return try: detal_report= open(path+'Detal_report.html','wb+') except Exception: print " Performance.py html() Detal_report.html lose" lines = test_title.readlines() for j in range(y): for line in lines: #替换开始测试CATEGORY if line.find('#category#') > -1: afertstr = line.replace('#category#',str(category[j][1:])) detal_report.write(afertstr) #替换data elif line.find('#replace#') > -1: afertstr = line.replace('#replace#',str(dict_html.items()[j][1])) detal_report.write(afertstr) else: detal_report.write(line) test_title.close() detal_report.close() #去除标点等等不需要元素 detal_report= open(path+'Detal_report.html','rb') detal_report1= open(path+'Detal_report1.html','wb+') lines = detal_report.readlines() for line in lines: if line.find("['") > -1: afertstr = line.replace("['",' ') detal_report1.write(afertstr) else: detal_report1.write(line) detal_report1= open(path+'Detal_report1.html','rb') detal_report2= open(path+'Detal_report2.html','wb+') lines = detal_report1.readlines() for line in lines: if line.find("']") > -1: afertstr = line.replace("']",' ') detal_report2.write(afertstr) else: detal_report2.write(line) detal_report2= open(path+'Detal_report2.html','rb') detal_report3= open(path+'Detal_report3.html','wb+') lines = detal_report2.readlines() for line in lines: if line.find("', '") > -1: afertstr = line.replace("', '",' ') detal_report3.write(afertstr) else: detal_report3.write(line) #正文 try: html = open(path_output,'wb+') except Exception: print "Performance.py html() Performance.html lose" exit(1) return #替换summary try: summary= open(path+'Summary.html','rb') except Exception: print "Performance.py html() Summary.html lose" exit(1) return lines = summary.readlines() for line in lines: if line.find('#replace#') > -1: for j in range(y): afertstr = line.replace('#replace#',score[j]) html.write(afertstr) else: html.write(line) #加入详细内容 try: detal_report3= open(path+'Detal_report3.html','rb') except Exception: print "Performance.py html() Detal_report3.html lose" exit(1) return lines = detal_report3.readlines() for line in lines: html.write(line) html.close() detal_report.close() detal_report1.close() detal_report2.close() detal_report3.close() summary.close() #删除html等 os.remove(path+'Detal_report.html') os.remove(path+'Detal_report1.html') os.remove(path+'Detal_report2.html') os.remove(path+'Detal_report3.html')