当前位置: 首页>編程日記>正文

testbed代码审查信息提取

testbed代码审查信息提取

testbed代码审查信息提取
test编码规则检查出来的结果需要人力去收集,整理,尤其是一个缺陷在很多文件的很多行,整理起来非常费事。
写个小程序,刚好练练python。
代码写的早,最近没再用testbed,使用方法大致为为:
1.testbed静态分析后,将代码审查结果保存为1.html;
2.将代码保存为1,.py (要与1.html同目录);
3.执行1.py。
程序代码如下:

import requests
from bs4 import BeautifulSoup as bs
import re
import csv
from collections import Counter#将每个规则在一个文件中的所在行号进行排序
# 排序关键字匹配
# 匹配开头数字序号
def sort_key(s):if s:try:c = re.findall('^\d+', s)[0]except:c = -1return int(c)#将读取的代码审查问题写入文件
#参数 dic 为要输出的字典信息
#参数 output_mid_code_review_filename  要输出的详细的审查报告的文件名称
def write_code_rewivew_file(dic,output_mid_code_review_filename):#打开文件,追加afile = open(output_mid_code_review_filename,'w',newline='')#设定写入模式csv_write = csv.writer(file,dialect='excel')#写入具体内容line=['code','line','violation','standard',"filename","funname","fun_start_line","fun_end_line"]csv_write.writerow(line)for item in dic:line=[item.get('code',""),item.get('line',""),\item.get('violation',""),item.get('standard',""),\item.get('filename',""),item.get('funname',""),\item.get('fun_start_line',""),item.get('fun_end_line',"")]csv_write.writerow(line)print ("write_code_rewivew_file write over")file.close()#参数 sum_dic 为要输出的字典信息
#参数 output_sum_code_review_file  要输出的行汇总过的的审查报告的文件名称
def write_sum_code_rewivew_file(sum_dic,output_sum_code_review_file):file = open(output_sum_code_review_file,'w',newline='')#设定写入模式csv_write = csv.writer(file,dialect='excel')#写入具体内容line=['code','standard','violation',"filename",'lines']csv_write.writerow(line)standards=list(sum_dic.keys())standards.sort()for item in standards:#统计各行出现的次数line_count = Counter(sum_dic[item][4])line_count_with_times = []print("88888888888888888888888888888")print(item)line_nums = list(line_count.keys())line_nums.sort(key=sort_key)for line in line_nums:times=line_count[line]if times < 2:line_count_with_times.append(line)else:line_count_with_times.append(line+"("+str(times)+"次)")print(line_count_with_times)# 排序#d = sorted(result.items(), key=lambda x: x[1], reverse=True)line=[sum_dic[item][0],sum_dic[item][1],sum_dic[item][2],sum_dic[item][3],",".join(line_count_with_times)]       #line=[sum_dic[item][0],sum_dic[item][1],sum_dic[item][2],sum_dic[item][3],",".join(list[sum_dic[item][4]])]csv_write.writerow(line)print ("write_sum_code_rewivew_file write over")file.close()def code_review_substract(testbed_code_review_report_filename):file = open(testbed_code_review_report_filename, 'r', encoding='gb2312')soup = bs(file, 'lxml')tables_erro_info=[]    #用于汇总所有表格中的错误信息table_erro_info=[]     #用于临时保存单个表中的错误信息all_table = soup.find_all("table")for table in all_table:filename=""funname=""fun_start_line=""fun_end_line=""table_erro_info=[]head=table.find_all("th")#情况1:全局错误信息,若表格列有4列,且标题为 code line violaion sandad,则认为找到表格if len(head)==4 and head[0].getText().strip()=="Code" \and head[1].getText().strip()=="File: Src Line" and head[2].getText().strip()=="Violation" \and head[3].getText().strip()=="Standard":print(table)#提取表格中的所有违规记录rows=table.find_all("tr")table_erro_info=[]for row in rows:error={}cols=row.find_all("td")#表中存在列数不等于4的情况,该表数据丢弃if len(cols) == 4 :error['code']=cols[0].getText().strip()error['filename']=cols[1].getText().strip().split(":",1)[0].strip()error['line']=cols[1].getText().strip().split(":",1)[1].strip()              error['violation']=cols[2].getText().strip()error['standard']=cols[3].getText().strip()            table_erro_info.append(error)print(error)#若函数违规信息表格列有4列,且标题为 code line violaion sandad,则认为找到表格if len(head)==4 and head[0].getText().strip()=="Code" \and head[1].getText().strip()=="Line" and head[2].getText().strip()=="Violation" \and head[3].getText().strip()=="Standard":print(table)#提取表格上方的函数名以及起始行、终止行信息if table.find_previous("table",attrs={"width":"50%"}) is not None:if len(table.find_previous("table",attrs={"width":"50%"}).find_all("tr")) == 1:whole_fun_info=table.find_previous("table",attrs={"width":"50%"}).getText().strip()       fun_list=whole_fun_info.replace("("," ").replace(")"," ").split()filename,funname,fun_start_line,fun_end_line=[fun_list[i] for i in [4,0,1,3]]print(filename,funname,fun_start_line,fun_end_line)#提取表格中的所有违规记录rows=table.find_all("tr")table_erro_info=[]for row in rows:error={}cols=row.find_all("td")#表中存在列数不等于4的情况,该表数据丢弃if len(cols) == 4 and cols[1].getText().strip().isdigit():error['code']=cols[0].getText().strip()error['line']=cols[1].getText().strip()error['violation']=cols[2].getText().strip()error['standard']=cols[3].getText().strip()error['filename']=filenameerror['funname']=funnameerror['fun_start_line']=fun_start_lineerror['fun_end_line']=fun_end_line               table_erro_info.append(error)print(error)#若函数违规信息表格列有3列,且标题为 code violaion sandad,则认为找到表格if len(head)==3 and head[0].getText().strip()=="Code" \and head[1].getText().strip()=="Violation" and head[2].getText().strip()=="Standard":print(table)#提取表格上方的函数名以及起始行、终止行信息if table.find_previous("table",attrs={"width":"50%"}) is not None:if len(table.find_previous("table",attrs={"width":"50%"}).find_all("tr")) == 1:whole_fun_info=table.find_previous("table",attrs={"width":"50%"}).getText().strip()       fun_list=whole_fun_info.replace("("," ").replace(")"," ").split()filename,funname,fun_start_line,fun_end_line=[fun_list[i] for i in [4,0,1,3]]print(filename,funname,fun_start_line,fun_end_line)#提取表格中的所有违规记录rows=table.find_all("tr")table_erro_info=[]for row in rows:error={}cols=row.find_all("td")#表中存在列数不等于3的情况,该表数据丢弃if len(cols) ==3 :error['code']=cols[0].getText().strip()error['violation']=cols[1].getText().strip().split(":")[0].strip()error['line']=funname #为了汇总时行号存在,用函数名代替行号error['standard']=cols[2].getText().strip()error['filename']=filenameerror['funname']=funnameerror['fun_start_line']=fun_start_lineerror['fun_end_line']=fun_end_line               table_erro_info.append(error)print(error)tables_erro_info=tables_erro_info+table_erro_infoprint(len(tables_erro_info))    sum_dic={}for error in tables_erro_info:if error['standard']+"#"+error['filename'] not in sum_dic:if "line" not in error:sum_dic[error['standard']+"#"+error['filename']]=[error['code'],error['standard'],error['violation'],error['filename'],['-1']]else:sum_dic[error['standard']+"#"+error['filename']]=[error['code'],error['standard'],error['violation'],error['filename'],[error['line']]]else:if "line" in error:sum_dic[error['standard']+"#"+error['filename']][4].append(error['line'])           print("*********sum_dic:*******************")for i in sum_dic:print(i,sum_dic[i])return tables_erro_info,sum_dictables_erro_info,sum_dic=code_review_substract("1.htm")
write_code_rewivew_file(tables_erro_info,'代码审查_明细.csv')
write_sum_code_rewivew_file(sum_dic,'代码审查-汇总.csv')


https://www.fengoutiyan.com/post/14295.html

相关文章:

  • testbed
  • 如何使用审查元素提取网页图片
  • 代码审查的内容
  • idle代码提示
  • testbed安装教程
  • 网页审查
  • 如何更有效的实施代码审查
  • php代码审查
  • 鏡像模式如何設置在哪,圖片鏡像操作
  • 什么軟件可以把圖片鏡像翻轉,C#圖片處理 解決左右鏡像相反(旋轉圖片)
  • 手機照片鏡像翻轉,C#圖像鏡像
  • 視頻鏡像翻轉軟件,python圖片鏡像翻轉_python中鏡像實現方法
  • 什么軟件可以把圖片鏡像翻轉,利用PS實現圖片的鏡像處理
  • 照片鏡像翻轉app,java實現圖片鏡像翻轉
  • 什么軟件可以把圖片鏡像翻轉,python圖片鏡像翻轉_python圖像處理之鏡像實現方法
  • matlab下載,matlab如何鏡像處理圖片,matlab實現圖像鏡像
  • 圖片鏡像翻轉,MATLAB:鏡像圖片
  • 鏡像翻轉圖片的軟件,圖像處理:實現圖片鏡像(基于python)
  • canvas可畫,JavaScript - canvas - 鏡像圖片
  • 圖片鏡像翻轉,UGUI優化:使用鏡像圖片
  • Codeforces,CodeForces 1253C
  • MySQL下載安裝,Mysql ERROR: 1253 解決方法
  • 勝利大逃亡英雄逃亡方案,HDU - 1253 勝利大逃亡 BFS
  • 大一c語言期末考試試題及答案匯總,電大計算機C語言1253,1253《C語言程序設計》電大期末精彩試題及其問題詳解
  • lu求解線性方程組,P1253 [yLOI2018] 扶蘇的問題 (線段樹)
  • c語言程序設計基礎題庫,1253號C語言程序設計試題,2016年1月試卷號1253C語言程序設計A.pdf
  • 信奧賽一本通官網,【信奧賽一本通】1253:抓住那頭牛(詳細代碼)
  • c語言程序設計1253,1253c語言程序設計a(2010年1月)
  • 勝利大逃亡英雄逃亡方案,BFS——1253 勝利大逃亡
  • 直流電壓測量模塊,IM1253B交直流電能計量模塊(艾銳達光電)
  • c語言程序設計第三版課后答案,【渝粵題庫】國家開放大學2021春1253C語言程序設計答案
  • 18轉換為二進制,1253. 將數字轉換為16進制
  • light-emitting diode,LightOJ-1253 Misere Nim
  • masterroyale魔改版,1253 Dungeon Master
  • codeformer官網中文版,codeforces.1253 B
  • c語言程序設計考研真題及答案,2020C語言程序設計1253,1253計算機科學與技術專業C語言程序設計A科目2020年09月國家開 放大學(中央廣播電視大學)
  • c語言程序設計基礎題庫,1253本科2016c語言程序設計試題,1253電大《C語言程序設計A》試題和答案200901
  • 肇事逃逸車輛無法聯系到車主怎么辦,1253尋找肇事司機