这个脚本完成的内容是:读取一个log中的内容,将其中的部分内容获取,并按照一定规则将其填入excel
import re,os
import xlrd
import xlwt
from xlwt import *
from xlutils.copy import copy
gen_excel_file = "./find_force.log"
class gen_excel:
def __init__(self):
self._excel_list=[]
self._excel_dict = {}
self._tempcaseName = None
#在这里使用self定义的变量,实际上都是整个类都在使用的全局变量,所以这里不能将templine搞成全局变量
# self._tempLine = ''
def option_parse(self):
pass
def extract_excel(self,file):
for line in open(file):
if (line.startswith('/vobs/eagle2-xg746-prj/test/src') and line != self._tempcaseName):
tempcaseName = line
#这句是将上line指向的字符串按照/test/src/分割返回一个列表,0-第一个字符,-1最后一个字符
excel_element=line.split('/test/src/')[-1]
tempLine = ''
if ('force' in line):
tempLine = tempLine + line
forceContL = len(tempLine)
#这里用了这样的一个的if判断,好像是因为当字符串长度如果大于某一长度时,就无法放入excel的表格里了,这一长度是和excel 的版本有关系的
if(forceContL < 32767):
# print (excel_element)
self._excel_dict[excel_element] = tempLine +'\n'
latestforceCont = tempLine
for (k,v) in self._excel_dict.items():
print(k)
print(v)
print('-----------')
else:
print 'force contents have long characters pls go to testcase file... '
#self._excel_dict[excel_element] = 'force contents have long characters pls go to testcase file'+'\n'
self._excel_dict[excel_element] = latestforceCont
print self._excel_dict
def initial_excel(self):
nRow = 0
nCol = 0
Title_report=['CaseName','ForceContent','Comment']
pattern_title=xlwt.Pattern()
pattern_title.pattern=xlwt.Pattern.SOLID_PATTERN
pattern_title.pattern_fore_colour=5
style_title=xlwt.XFStyle()
style_title.pattern=pattern_title
#workbook 是xlwt模块内置的一种方法,大约是设置初始格式的方法
excel_table=Workbook()
excel_sheet_report=excel_table.add_sheet("Force_Report")
row=1
column_report=0
#write 是向excel表格中写数据的方法,其输入参数依次是,行、列、内容、style
for element in Title_report: excel_sheet_report.write(0,column_report,element,style_title)
column_report+=1
for (k,v) in self._excel_dict.items():
nRow +=1
nCol +=1
excel_sheet_report.write(nRow,0,k)
excel_sheet_report.write(nRow,1,v)
excel_table.save('force_grep.xls')
if __name__=="__main__":
excel_obj=gen_excel()
excel_obj.extract_excel(gen_excel_file)
excel_obj.initial_excel()
log内容
/vobs/eagle2-xg746-prj/test/src/host/flow/cp_phy_2g
140:force {/CHIP_TB_SOC/INST_CHIP/INST_BD_SCAN/EDT_TOP_CPI_I/INST_CORE/INST_PHY_TOP/INST_PHY_CORE/INST_PHY_PER_SHELL/INST_DSP_2G_SUPER/MOD_I_RESET_VEC_I(2 downto 0)} 111 -freeze
141:force {/CHIP_TB_SOC/INST_CHIP/INST_BD_SCAN/EDT_TOP_CPI_I/INST_CORE/INST_PHY_TOP/INST_PHY_CORE/INST_PHY_PER_SHELL/INST_DSP_2G_SUPER/MOD_O_RESET_VEC_I(2 downto 0)} 111 -freeze
/vobs/eagle2-xg746-prj/test/src/host/flow/l2c_noc_access
132:force /CHIP_TB_SOC/INST_CHIP/INST_BD_SCAN/EDT_TOP_CPI_I/INST_CORE/INST_CPI_L2C_NOC_SHELL/INST_CPI_L2C_NOC_TOP/INST_INTERCO_L2COPROC1_WRP/INST_INTERCO_L2COPROC1/l2c_noc1_inst/inst_pd_l2c_noc1_sw_emac_tile0_dig_main_wrapper/emac_tile0/fw_emac_tile0_slave_inst/mnoc_ni_fw_emac_tile0_slave_fw_rds_inst/fw_sec_check_err_o 0
/vobs/eagle2-xg746-prj/test/src/host/flow/flow_tbic_cjtag_00
184:force /CHIP_TB_SOC/INST_CHIP/INST_BD_SCAN/INST_CPI_TCU_TOP/JTAG_TRSTN_I 1
186:force /CHIP_TB_SOC/INST_CHIP/INST_BD_SCAN/EDT_TOP_CPI_I/INST_CORE/INST_DFX_AGG/dfxagg_fdfx_earlyboot_exit 1
187:force /CHIP_TB_SOC/INST_CHIP/INST_BD_SCAN/EDT_TOP_CPI_I/INST_CORE/INST_DFX_AGG/dfxagg_fdfx_policy_update_int 1
188:force {/CHIP_TB_SOC/INST_CHIP/INST_BD_SCAN/EDT_TOP_CPI_I/INST_CORE/INST_DFX_AGG/dfxagg_fdfx_secure_policy[3:0]} 0100
/vobs/eagle2-xg746-prj/test/src/host/toplevel/baseia/ia_flowtest_core1_lmuboot
118:force /CHIP_TB_SOC/INST_CHIP/INST_BD_SCAN/EDT_TOP_CPI_I/INST_CORE/INST_CPI_XBCPU_MACRO_SHELL/INST_CPI_XBCPU_MACRO_TOP/INST_XBCPU_MACRO_WRP/INST_XBCPU_MACRO/subsys_xg756_test_bootmode_cpu1_i 1
122:force /CHIP_TB_SOC/INST_CHIP/INST_BD_SCAN/EDT_TOP_CPI_I/INST_CORE/INST_CPI_XBCPU_MACRO_SHELL/INST_CPI_XBCPU_MACRO_TOP/INST_XBCPU_MACRO_WRP/INST_XBCPU_MACRO/inst_cpu_shell_vdd_wrapper/inst_cpu_shell/ia_subsys/bia/ssa_par/grp_ssa/ssa_core_top/ssa_core/bunit/bcfg_pmimr0_en 0
125:force /CHIP_TB_SOC/INST_CHIP/INST_BD_SCAN/EDT_TOP_CPI_I/INST_CORE/INST_CPI_XBCPU_MACRO_SHELL/INST_CPI_XBCPU_MACRO_TOP/INST_XBCPU_MACRO_WRP/INST_XBCPU_MACRO/inst_cpu_shell_vdd_wrapper/inst_cpu_shell/ia_subsys/bia/ssa_par/grp_ssa/ssa_core_top/ssa_core/bunit/bconfig/bmbound\[31:0\] 0
/vobs/eagle2-xg746-prj/test/src/host/xian/pac_chip/pac_tick_dly_common