使用sys.argv可以传递参数
sys.argv[0]表示代码本身文件路径
所以传入的文件从sys.argv[1], 开始算起,传入内容大概是以空格进行分割
有时传入的内容中,有特殊字符,注意用反斜线转义
输入命令,如下
find_case.py '\\CLK38M_PROC\[115\].U_CLK38M_2FF_SYNC.INST_COMMON_SYNC_SVA_CHK .ipdb_common_sync_glitch_chk'
或者
phthon change.py /home/eagle2.work/regression_simfab/log
code如下
import sys
root=sys.argv[1]
或者,全代码
#!/usr/bin/env python
import os
import re
import sys
checker_log =sys.argv[1]
root='/home/eagle2.work3/regression_simfab/XG756_C0/XG756_C0_SPEC_3_3_SOC_TB_sva_case_search'
search_argu=[['check_logfiles.log.bz2'],['sf_tc.store','check_logfiles.log'],['<result>FAILED','<item>check_logs failed']]
def detect():#ergodic the files in path root #search_argu[1] is the two search search_arguments in file2
i=0
global check_file1
global check_file2
global rt
for rt,dirs,files in os.walk(root):#ergodic the files in path root
for file in files:
i+=1
#print(i)
#detect zip file
# print(rt)
path=rt+'/'
pathlist=os.listdir(path)
for filename in pathlist:
if filename.endswith(str(search_argu[0][0])):
# print("*******need to bunzip first********")
order1='cd '+path+' && bunzip2 '+filename
# print(rt)
os.system(order1)
else:
pass
if (pathlist.count(search_argu[1][0])==0) or (pathlist.count(search_argu[1][1])==0):
i+=1
# print('file not found')
# print(i)
break
try:
check_file1=path+search_argu[1][0]
check_file2=path+search_argu[1][1]
except FileNotFoundError:
# print('can not find files')
sys.exit()
check2(check_file2)
# print ("##################################################")
break
def check1(file):#check file st_tc.store has [<result>FAILED] [<item>check_logs failed] or not
a=0
b=0
#print('check1')
open_file1=open(file)
#print(open_file1)
for line in open_file1:
if(re.search(search_argu[2][0],line)):
a=1
break
for line in open_file1:
if (re.search(search_argu[2][1],line)):
b=1
break
open_file1.close()
if(a+b==2):
# print('check1 succeed')
return(1)
else:
# print('check1 failed')
return(0)
def check2(file):#check file check_logfiles.log
num=0
should_detect=[]
remain_file=[]
open_file2=open(file)
if(check1(check_file1)==1):
for line in open_file2:
num+=1
if(re.search(checker_log,line)!=None):
should_detect.append(line)
else:
pass
if len(should_detect) > 0:
# print(len(should_detect))
print(rt)
print ('%d times found in this case' %(len(should_detect)))
else:
# print ("can't find in this case")
pass
open_file2.close()
if __name__=='__main__':
detect()