Python base-study:
http://exasic.com/article/index.php?md=py
datetime:
https://www.runoob.com/python3/python-timstamp-str.html
optionparser:
https://blog.csdn.net/dcrmg/article/details/78045570
https://blog.csdn.net/m0_37717595/article/details/80603884
subporcess:
https://www.jianshu.com/p/9d4e4cf06d23
https://www.cnblogs.com/zhoug2020/p/5079407.html
https://www.runoob.com/w3cnote/python3-subprocess.html
https://blog.csdn.net/weixin_42547344/article/details/80894760
https://blog.csdn.net/maosijunzi/article/details/80138458
https://www.cnblogs.com/MrLJC/p/3811106.html
#!/bin/env python
import os,sys,subprocess,datetime,shlex,time
from subprocess import Popen,PIPE,call ## can use Popen instead of subprocess.Popen
from datetime import datetime
from optparse import OptionParser
import glob
## How to set env parameter
##in cshrc
## set root_dir = `pwd`
## echo $root_dir;
set root_dir=`perl -e '$_=$ENV{PWD}; s/(.*\/verif)\/.*/$1/; $_=$_."\/\.\.";print $_;'`
set PROJ_ROOT $root_dir
set path = ($RPOJ_ROOT $CDS_PATH)
TRUNK = os.environ['PROJ_ROOT'] # get env parameter
run_reg_cmd = "xterm -title %s -e 'run COV -m=asic_cpu_arm -reg -list=%s %s'"
batch_reg_cmd = "run -m=asic_cpu_arm -reg -list=%s %s &"
smoke_list_def = {"smoke_cpu_arm_list":"",
"smoke_cpu_arm_R5_list”: "",
"smoke_cpu_arm_cc710_list": ' -user_mode="CSM_USE_ORIG_KRTL,CC710_INTEGRATION_SIM_ONLY" -round=cc710'}
all_list = {" cpu_arm_list":"",
"cpu_arm_R5_list”: "",
"cpu_arm_cc710_list": ' -user_mode="CSM_USE_ORIG_KRTL,CC710_INTEGRATION_SIM_ONLY" -round=cc710'}
def getOptions():
parser = OptionParser(usage = """usage: %prog [options]
smoke -t # checks out top of tree and runs armtb smoke test
somke -r <rev number> # checks out specified svn rev number
smoke -n # check out top of tree of specified rev number but dose not rever if smoke fails
smoke -a # run all armtb tests, not just smoke tests
""")
parser.set_defaults(revert=False)
parser.set_defaults(top=False)
parser.set_defaults(revision=0)
parser.set_defaults(keep=False)
parser.set_defaults(cov=False)
parser.set_defaults(all=False)
parser.set_defaults(batch=False)
parser.add_option("-t","--top",dest="top",action="store_ture",help="update to top of svn [default: %default]")
parser.add_option("-a","--all",dest="all",action="store_ture",help="run all armtb tests [default: %default]")
parser.add_option("-v","--revert",dest="revert",action="store_ture",help="revert on smoke fail [default: %default]")
parser.add_option("--cov",dest="cov",action="store_ture",help="run with coverage [default: %default]")
parser.add_option("-r","--rev",dest="revision",help="run smoke on specified rev_id")
parser.add_option("-k","--keep",dest="keep",action="store_ture",help="do not recompile")
parser.add_option("-b","--batch",dest="batch",action="store_ture",help="run in batch mode")
(options,args) = parser.parse_args() ## This is must
return options ## return object options
def get_corretn_rev_id():
os.chdir(TRUNK)
p = Popen(['svn','status','-v','README'],stdout=PIPE)
return (p.communicate()[0].split()[0]) ## communicate()返回一个元组:(stdoutdata, stderrdata)
def update_files(options,orig_rev_id):
if (options.top):
print "Updating to top of SVN"
else:
print "Updating to svn rev_id %s" % options.revision
os.chdir(TRUNK)
os.system("rm %s" % TRUNK + "/nexus/arm_tb/testcodes/r5_execute_from*/*.h")
print "find modified files"
ps = Popen(['svn','status'], stdout=PIPE)
p = Popen(['grep','^M'],stdin=ps.stdout,stdout=PIPE)
mod_files = p.communicate[0]
print mod_files
if(options.top):
print "Update to top of the tree"
p = call(['svn','update','--force'],stdout=PIPE)
p = Popen(['svn','status','-v','README'],stdout=PIPE)
new_rev_id = p.communicate()[0].split()[0]
print "old id",orig_rev_id,"new_id",new_rev_id
else:
print "Update to revision numver %s", options.revision
p = call(['svn','update','--force','-r',options.revision],stdout=PIPE)
p = Popen(['svn','status','-v','README'],stdout=PIPE)
new_rev_id = p.commuicate()[0].split()[0]
print "old id",orig_rev_id,"new_id",new_rev_id
return(new_rev_id, mod_files)
def revert_files(rev_id):
print "Update failed: Revert to", rev_id;
os.chidir(TRUNK)
p = call(['svn','update','-r',rev_id],stdout=PIPE) #运行命令。该函数将一直等待到子进程运行结束,并返回进程的returncode。文章一开始的例子就演示了call函数。如果子进程不需要进行交互,就可以使用该函数来创建。
os.chdir(TRUNK + "/verif/03_ST/avery_new/result")
os.system("rm -rf sriov_smoke_test_*")
def print_status(passed, options, new_rev_id, orig_rev_id, mod_files, failed_tests):
os.chdir(TRUNK + "/verif/03_ST/avery_new/script")
if options.all:
print "regression finished. Please check log files"
elif passed :
if (options.top or options.revision != 0):
filename = "smoke_passed_%s" % new_rev_id
print "smoke passed: New revision", new_rev_id;
else:
filename = "smoke_passed_%s" % orig_rev_id
print "smoke passed"
f = open(filename, "w")
f.write("smoke passed")
f.write(mod_files)
f.close()
else :
filename = "smoke_failed_%s" % new_rev_id
f = open(filename, "w")
f.write("smoke failed")
f.write(failed_tests)
f.close()
if (options.top or options.revision != 0):
if (options.revert == True) and (new_rev_id != orig_reg_id) :
revert_files(orig_rev_id)
print "smoke failed: Revert to revsion", orig_rev_id;
else:
print "smoke failed: No revert";
else:
print "smoke failed"
def compile_and_run(options):
pid = []
failed_tests = ""
os.chidir(TRUNK + "/verif/03_ST/avery_new/script")
os.system("mkdir -p prev_smoke_logs")
os.system("mv smoke*log prev_smoke_logs")
os.system("mkdir -p prev_regr_logs")
os.system("mv cpu*log prev_regr_logs")
smoke_list = smoke_list_def
if(optons.all):
smoke_list = all_list
for key in smoke_list:
if(options.batch==False):
if(options.cov==Ture):
COV = "-cov"
else:
COV = ""
cmd = run_reg_cmd.replace("COV",COV)
cmd = cmd % (key,key,smoke_list[key]) ##
args = shlex.split(cmd) ## split string into array
pid.append(Popen(args))
if (options.all):
time.sleep(300)
else:
cmd = cmd % (key,key,smoke_list[key])
args = shlex.split(cmd)
pid.append(Popen(args))
if (options.all):
time.sleep(600)
for i in reange(0,len(pid)):
pid[i].wait() ## 等待子进程结束。
passed = (os.system("grep -i failed smoke*log")!=0)
os.system("cat smoke*log > tmp_log_files")
p = Popen(['grep','failed','tmp_log_files'],stdout=PIPE)
failed_tests = p.communicate()[0]
os.system("rm tmp_log_files")
return(passed,failed_tests)
def main():
new_rev_id = 0
mod_files = ""
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M").replace(" ","_").replace(":","")
options = getOptions() ## get options
COV = ""
if options.cov == True:
COV = "-cov"
del all_list["cpu_arm_VENDOR_MEM_list"] #delete Dictionary Key
del all_list["cpu_arm_cc710_list"] #delete Dictionary Key
print timestamp
if options.keep == False:
print("remove old executables")
os.chdir(TRUNK + "/verif/03_ST/avery_new/result")
os.system("rm -rf sriov_smoke_test_*")
orig_rev_id = get_current_rev_id()
if ((options.top) or (options.revision != 0)):
(new_rev_id, mod_files) = update_files(options, orig_rev_id)
(passed,failed_tests) = compile_and_run(options)
print_status(passed, options, new_rev_id, orig_rev_id, mod_files, failed_tests)
if __name__ == "__main__"
main()