Learning Perl: 5.7. Opening a Filehandle

本文介绍了Perl中文件操作的基础知识,包括如何使用open函数打开文件、处理错误情况、关闭文件句柄等。通过示例展示了不同场景下的文件读写操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

5.7. Opening a Filehandle You've seen that Perl provides three filehandlesSTDIN, STDOUT, and STDERRwhich are automatically open to files or devices established by the program's parent process (probably the shell). When you need other filehandles, use the open operator to tell Perl to ask the operating system to open the connection between your program and the outside world. Here are some examples: open CONFIG, "dino"; open CONFIG, "
class genTestcaselist(): def __init__(self,pjPath): self.testCaselist = [] self.testCfglist = [] self.msgCtrlObj = setEnvMsg("Analysis testcase and testlist infomation") self.projPath = pjPath self.cfgObj = getTestConfig() self.testCount = 0 self.waveCtrl = False self.seedWorkCtrl = False self.compVarList = [] self.compvarCount = 0 def genTestcase(self): if options.testCfgName!=None: for cfgName in options.testCfgName: if cfgName not in self.testCfglist: self.testCfglist.append(cfgName) if options.countCtrl: self.waveCtrl = False self.seedWorkCtrl = True if len(self.testCfglist)==0: self.testCfglist.append(options.caseCfgName) for testName in options.testNameCtrl: if os.path.exists(self.projPath+"/"+testName)==False and options.testCheckCtrl==1: self.msgCtrlObj.fatalMsgPrint("No exist " + testName + " in " + self.projPath) else: for cfgName in self.testCfglist: if options.testCheckCtrl==0: self.cfgObj.getCfgInfo(self.projPath+"/",cfgName) else: self.cfgObj.getCfgInfo(self.projPath+"/"+testName,cfgName) curCompVar = self.cfgObj.getCompOpts() if self.compVarList.count(curCompVar)==0: self.compVarList.append(curCompVar) self.compvarCount = self.compVarList.index(curCompVar) curTestCfgName = self.cfgObj.getCfgFileName() simVarList = self.cfgObj.getSimOpts() preVarList = self.cfgObj.getPreSimProc() postVarList = self.cfgObj.getPostSimProc() falseErrVarList = self.cfgObj.getFalseErr() expectErrVarList = self.cfgObj.getExpectErr() falseWarnVarList = self.cfgObj.getFalseWarn() expectWarnVarList = self.cfgObj.getExpectWarn() for curCountVal in range(0,options.countCtrl): if options.seedCtrl: curSeedData = options.seedCtrl.pop(0) else: randSeedObj = genRandSeed() curSeedData = randSeedObj.genRandData() curTestcaseInfo = [testName,curSeedData,curTestCfgName,self.compvarCount,self.compVarList[self.compvarCount],simVarList,preVarList,postVarList,falseErrVarList,expectErrVarList,falseWarnVarList,expectWarnVarList] self.testCaselist.append(curTestcaseInfo) self.testCount += 1 else: testNameList = options.testNameCtrl[:] testcaseCount = len(testNameList) if options.seedCtrl!=None: seedCount = len(options.seedCtrl) else: seedCount = 1 if len(self.testCfglist)==0: self.testCfglist.append(options.caseCfgName) testCfgCount = len(self.testCfglist) if testcaseCount == 1: self.waveCtrl = options.waveCtrl curTestcase = testNameList.pop(0) if os.path.exists(self.projPath+"/"+curTestcase)==False and options.testCheckCtrl==1: self.msgCtrlObj.fatalMsgPrint("No exist " + curTestcase + " in " + self.projPath) else: seedCountCtrl = int(math.ceil(float(seedCount)/float(testCfgCount))) for cfgName in self.testCfglist: if options.testCheckCtrl==0: self.cfgObj.getCfgInfo(self.projPath+"/",cfgName) else: self.cfgObj.getCfgInfo(self.projPath+"/"+curTestcase,cfgName) curCompVar = self.cfgObj.getCompOpts() if self.compVarList.count(curCompVar)==0: self.compVarList.append(curCompVar) self.compvarCount = self.compVarList.index(curCompVar) curTestCfgName = self.cfgObj.getCfgFileName() simVarList = self.cfgObj.getSimOpts() preVarList = self.cfgObj.getPreSimProc() postVarList = self.cfgObj.getPostSimProc() falseErrVarList = self.cfgObj.getFalseErr() expectErrVarList = self.cfgObj.getExpectErr() falseWarnVarList = self.cfgObj.getFalseWarn() expectWarnVarList = self.cfgObj.getExpectWarn() for seedIdx in range(0,seedCountCtrl): if options.seedCtrl: curSeedData = options.seedCtrl.pop(0) else: randSeedObj = genRandSeed() curSeedData = randSeedObj.genRandData() curTestcaseInfo = [curTestcase,curSeedData,curTestCfgName,self.compvarCount,self.compVarList[self.compvarCount],simVarList,preVarList,postVarList,falseErrVarList,expectErrVarList,falseWarnVarList,expectWarnVarList] self.testCaselist.append(curTestcaseInfo) self.testCount += 1 if self.testCount>1: self.seedWorkCtrl = True else: self.seedWorkCtrl = options.seedWorkCtrl else: self.waveCtrl = options.waveCtrl for testName in options.testNameCtrl: if os.path.exists(self.projPath+"/"+testName)==False and options.testCheckCtrl==1: self.msgCtrlObj.fatalMsgPrint("No exist " + testName + " in " + self.projPath) else: if len(self.testCfglist): cfgName = self.testCfglist.pop(0) else: cfgName = options.caseCfgName if options.testCheckCtrl==0: self.cfgObj.getCfgInfo(self.projPath+"/",cfgName) else: self.cfgObj.getCfgInfo(self.projPath+"/"+testName,cfgName) curCompVar = self.cfgObj.getCompOpts() if self.compVarList.count(curCompVar)==0: self.compVarList.append(curCompVar) self.compvarCount = self.compVarList.index(curCompVar) curTestCfgName = self.cfgObj.getCfgFileName() simVarList = self.cfgObj.getSimOpts() preVarList = self.cfgObj.getPreSimProc() postVarList = self.cfgObj.getPostSimProc() falseErrVarList = self.cfgObj.getFalseErr() expectErrVarList = self.cfgObj.getExpectErr() falseWarnVarList = self.cfgObj.getFalseWarn() expectWarnVarList = self.cfgObj.getExpectWarn() if options.seedCtrl: curSeedData = options.seedCtrl.pop(0) else: randSeedObj = genRandSeed() curSeedData = randSeedObj.genRandData() curTestcaseInfo = [testName,curSeedData,curTestCfgName,self.compvarCount,self.compVarList[self.compvarCount],simVarList,preVarList,postVarList,falseErrVarList,expectErrVarList,falseWarnVarList,expectWarnVarList] self.testCaselist.append(curTestcaseInfo) self.testCount += 1 if self.testCount>1: self.seedWorkCtrl = True else: self.seedWorkCtrl = options.seedWorkCtrl def genTestlist(self): curTestname = "" curCount = "" curTestCfg = "" curCompVar = "" simVar = "" seedVar = "" lineInfo = "" fileHandle = open(options.testListFile,'r') self.waveCtrl = False self.seedWorkCtrl = True while True: lineContext = fileHandle.readline() if not lineContext: break lineInfo = lineContext.strip() if len(lineInfo)==0: continue reInfo = "^\s*#.*" lineObj= re.match(reInfo,lineContext) if lineObj: continue else: testInfoQueue = lineContext.split(";") if len(testInfoQueue)<=2: self.msgCtrlObj.fatalMsgPrint("Regression list include testcase and count, current line: %0s"%(lineContext)) curTestname = testInfoQueue[0].strip() curCount = testInfoQueue[1].strip() if len(testInfoQueue)>2: curTestCfg = testInfoQueue[2].strip() if len(testInfoQueue)>3: curCompVar = testInfoQueue[3].strip() if len(testInfoQueue)>4: simVar = testInfoQueue[4].strip() #support multi seed if len(testInfoQueue)>5: seedVar = testInfoQueue[5].strip() seedTempVar = re.sub(' +',' ',seedVar) seedVarList = seedTempVar.split(' ') if os.path.exists(self.projPath+"/"+curTestname)==False and options.testCheckCtrl==1: self.msgCtrlObj.fatalMsgPrint("No exist " + curTestname + " in " + self.projPath) else: testMacroVarList = [] noMacroVarList = [] compMacroVar = "" compNoMacroVar = "" if curTestCfg: testCfgFile = curTestCfg elif options.testCfgName!=None: testCfgFile = options.testCfgName.pop(0) else: testCfgFile = options.caseCfgName if options.testCheckCtrl==0: self.cfgObj.getCfgInfo(self.projPath+"/",testCfgFile) else: self.cfgObj.getCfgInfo(self.projPath+"/"+curTestname,testCfgFile) #test.cfg compile parameter testCompVar = self.cfgObj.getCompOpts() compOptsVar = re.sub(' +',' ',testCompVar) CompVarList = compOptsVar.split(' ') CompVarList.sort() for macroVar in CompVarList: if macroVar: macroObj = re.match('\+define\+',macroVar) if macroObj: macroTmpVar = re.sub('\+define\+','',macroVar) macroVarList = macroTmpVar.split('+') macroVarList.sort() for compVar in macroVarList: if compVar not in testMacroVarList: testMacroVarList.append(compVar) else: compVar = re.sub(':',' ',macroVar) noMacroVarList.append(compVar) #List compile parameter testCompVar = curCompVar compOptsVar = re.sub(' +',' ',testCompVar) CompVarList = compOptsVar.split(' ') CompVarList.sort() for macroVar in CompVarList: if macroVar: macroObj = re.match('\+define\+',macroVar) if macroObj: macroTmpVar = re.sub('\+define\+','',macroVar) macroVarList = macroTmpVar.split('+') macroVarList.sort() for compVar in macroVarList: if compVar not in testMacroVarList: testMacroVarList.append(compVar) else: compVar = re.sub(':',' ',macroVar) noMacroVarList.append(compVar) #Merge all compile parameter if len(testMacroVarList): compMacroVar = "+define+" + "+".join(testMacroVarList) if len(noMacroVarList): compNoMacroVar = " ".join(noMacroVarList) if compMacroVar or compNoMacroVar: compMacroVar = compMacroVar + " " + compNoMacroVar if self.compVarList.count(compMacroVar)==0: self.compVarList.append(compMacroVar) self.compvarCount = self.compVarList.index(compMacroVar) #Sim paramter simVarList = self.cfgObj.getSimOpts() if simVar: simVarList = simVarList.strip() + " " + simVar curTestCfgName = self.cfgObj.getCfgFileName() preVarList = self.cfgObj.getPreSimProc() postVarList = self.cfgObj.getPostSimProc() falseErrVarList = self.cfgObj.getFalseErr() expectErrVarList = self.cfgObj.getExpectErr() falseWarnVarList = self.cfgObj.getFalseWarn() expectWarnVarList = self.cfgObj.getExpectWarn() for curCountVal in range(0,int(curCount)): if curCountVal>=len(seedVarList): randSeedObj = genRandSeed() curSeedData = randSeedObj.genRandData() elif seedVarList[curCountVal]: curSeedData = int(seedVarList[curCountVal]) else: randSeedObj = genRandSeed() curSeedData = randSeedObj.genRandData() curTestcaseInfo = [curTestname,curSeedData,curTestCfgName,self.compvarCount,self.compVarList[self.compvarCount],simVarList,preVarList,postVarList,falseErrVarList,expectErrVarList,falseWarnVarList,expectWarnVarList] self.testCaselist.append(curTestcaseInfo) self.testCount += 1 fileHandle.close() def getTestList(self): return self.testCaselist def getWaveCtrl(self): return self.waveCtrl def getSeedPathCtrl(self): return self.seedWorkCtrl def getTestcaseCount(self): return self.testCount def getCompVar(self): return self.compVarList
最新发布
06-25
import os import re import random import getpass import socket import time import sys import stat import signal import threading import shutil import resource import math import subprocess from optparse import OptionParser # TODO set some global control parameter glbUserName = "" glbWorkPath = "" glbBlockPath = "" glbBlockName = "" compileStatus = 0 reapedCount = 0 curLiveCount = 0 reapedFlag = False bsubBusy = 0 # TODO Set enviroment color frontGroundColor = { "ANSI_BLACK" : 30, "ANSI_RED" : 31, "ANSI_GREED" : 32, "ANSI_YELLOW": 33, "ANSI_BLUE" : 34, "ANSI_PURPLE": 35, "ANSI_CYAN" : 36, "ANSI_WHITE" : 37 } # TODO define every print message by every color class setEnvMsg(): def __init__(self,message): self.msgContext = message self.fileError = "err.log" def writeMessage(self,message): self.msgContext = message def blackPrint(self,message): print('\033[1;30m' + message + '\033[0m') def redPrint(self,message): print('\033[1;31m' + message + '\033[0m') def greedPrint(self,message): print('\033[1;32m' + message + '\033[0m') def yellowPrint(self,message): print('\033[1;33m' + message + '\033[0m') def bluePrint(self,message): print('\033[1;34m' + message + '\033[0m') def purplePrint(self,message): print('\033[1;35m' + message + '\033[0m') def cyanPrint(self,message): print('\033[1;36m' + message + '\033[0m') def msgPrint(self,message,fg=frontGroundColor["ANSI_BLACK"]): if fg == frontGroundColor["ANSI_BLACK"]: self.blackPrint(message) elif fg == frontGroundColor["ANSI_RED"]: self.redPrint(message) elif fg == frontGroundColor["ANSI_GREED"]: self.greedPrint(message) elif fg == frontGroundColor["ANSI_YELLOW"]: self.yellowPrint(message) elif fg == frontGroundColor["ANSI_BLUE"]: self.bluePrint(message) elif fg == frontGroundColor["ANSI_PURPLE"]: self.purplePrint(message) elif fg == frontGroundColor["ANSI_CYAN"]: self.cyanPrint(message) def putBannerPrint(self,fgh=frontGroundColor["ANSI_RED"],fgb=frontGroundColor["ANSI_RED"]): self.msgPrint("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-",fgh) self.msgPrint("->\t" + self.msgContext,fgb) self.msgPrint("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-",fgh) def fatalMsgPrint(self,message): self.msgPrint("(FATAL:) " + message, frontGroundColor["ANSI_RED"]) sys.exit(1) def errorMsgPrint(self,message): self.msgPrint("(ERROR:) " + message, frontGroundColor["ANSI_YELLOW"]) def warnMsgPrint(self,message): self.msgPrint("(WARNING:) " + message, frontGroundColor["ANSI_BLUE"]) def infoMsgPrint(self,message): self.msgPrint("(INFO:) " + message, frontGroundColor["ANSI_BLACK"]) def debugMsgPrint(self,message): self.msgPrint("(DEBUG:) " + message, frontGroundColor["ANSI_BLACK"]) def errorMsgWrite(self,fileHandle,message): if fileHandle: fileHandle.write(message) def closeErrorFile(self,fileHandle): if fileHandle: fileHandle.close() # TODO Scripts to submit task to openlava class bsubTaskProcess(): def __init__(self): self.writeBusy = 0 self.readBusy = 0 self.msgCtrlObj = setEnvMsg("Bsub to manage task submit and post process") def excuteBsubCmd(self,command,bsubJobList,processPath): global bsubBusy while True: if bsubBusy==0: break; time.sleep(1) bsubBusy = 1 jobInfo = "Job <(\d+)> is submitted" if os.path.exists(processPath)==True: os.chdir(processPath) else: self.msgCtrlObj.fatalMsgPrint("No found the path: %0s"%(processPath)) p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE,stderr=subprocess.STDOUT,universal_newlines=True) time.sleep(1) while True: p.poll() line=p.stdout.readline() jobObj = re.search(jobInfo,line) if jobObj: jobId = jobObj.group(1) while self.readBusy==1: time.sleep(1) while self.writeBusy==1: time.sleep(1) if self.writeBusy==0: break self.writeBusy = 1 time.sleep(1) bsubJobList.append(jobId) self.writeBusy = 0 break else: jobId = None break p.wait() if p.returncode!=0: self.msgCtrlObj.fatalMsgPrint("Error occurred(%s) from "%(p.returncode) + command) bsubBusy = 0 return jobId def checkBsubJob(self,jobId): bsubJobStatus = 0 jobIdQueue = [] jobInfoList = [] if jobId==None: return p = subprocess.Popen(options.bJobNameCtrl+" -noheader", shell=True, stdout=subprocess.PIPE,stderr=subprocess.STDOUT,universal_newlines=True) context=p.stdout.read() if "No unfinished job" not in context: for line in context.splitlines(): jobInfoList = line.split() jobIdQueue.append(jobInfoList[0]) if jobId in jobIdQueue: bsubJobStatus = 1 return bsubJobStatus def waitBsubCmdDone(self,jobId,bsubJobList): jobDoneCount = 3 if jobId==None: return time.sleep(10) while True: jobDoneStatus = 0 jobDoneIdx = 0 jobIdQueue = [] jobInfoList = [] time.sleep(5) p = subprocess.Popen(options.bJobNameCtrl+" -noheader", shell=True, stdout=subprocess.PIPE,stderr=subprocess.STDOUT,universal_newlines=True) context=p.stdout.read() if "No unfinished job" not in context: for line in context.splitlines(): jobInfoList = line.split() jobIdQueue.append(jobInfoList[0]) else: return if jobId not in jobIdQueue: while True: time.sleep(1) jobDoneStatus = self.checkBsubJob(jobId) jobDoneIdx += 1 if jobDoneIdx==jobDoneCount: break if jobDoneStatus: continue while self.writeBusy==1: time.sleep(1) while self.readBusy==1: time.sleep(1) if self.readBusy==0: break self.readBusy = 1 time.sleep(1) if jobId in bsubJobList: bsubJobList.remove(jobId) self.readBusy = 0 break
06-25
class getEnvVariableWork(): def __init__(self): self.msgCtrlObj = setEnvMsg("Start to check work directory valid or not") def getWorkDir(self): self.curWorkPath = os.getcwd() reInfo = "(.*)\/" + options.vrfDirName + "\/(\w+)\/(.*)$" curWorkObj = re.match(reInfo,self.curWorkPath) if curWorkObj: self.curProjPath = curWorkObj.group(1) self.curBlockPath= curWorkObj.group(2) self.curWrokPath = curWorkObj.group(3) if self.curWrokPath != "work" and options.localSimCtrl==False: self.msgCtrlObj.warnMsgPrint("the normal project, simulation start excute from work directory") os.environ[options.projTopName] = self.curProjPath elif options.selfDefineWork: self.msgCtrlObj.infoMsgPrint("Simulation scripts will current directory excute") elif options.pathCheckCtrl: self.curProjPath = os.getenv(options.projTopName) else: self.msgCtrlObj.errorMsgPrint("no surport excute the scripts in current directory, if you want, please enable --self=Ture") def getEnvInfo(self): return self.curProjPath+"/"+options.vrfDirName+"/"+self.curBlockPath, self.curBlockPath, self.curWorkPath def setSynEnv(self): self.envPath = os.getenv("PATH") self.vcsHome = "/edatools/synopsys/VCS" self.verdiHome = "/edatools/synopsys/Verdi" self.setPathStr = self.vcsHome + "/bin:" + self.envPath self.setVerdiPathStr = self.verdiHome + "/bin:" + self.envPath os.environ["VCS_HOME"] = self.vcsHome os.environ["NOVAS_HOME"] = self.verdiHome os.environ["VERDI_HOME"] = self.verdiHome os.environ["PATH"] = self.setPathStr os.environ["PATH"] = self.setVerdiPathStr def setCdnEnv(self): self.envPath = os.getenv("PATH") self.ldPath = os.getenv("LD_LIBRARY_PATH") self.cdsLicFile = "/edatools/tools_setup/lic/cadence_20240819.dat" self.xlmHome = "/edatools/cadence/XCELIUM" self.verdiHome = "/edatools/synopsys/Verdi" self.setXlmStr = self.xlmHome + "/bin:" + self.xlmHome + "/tools/bin:" + self.xlmHome + "/tools/dfII/bin:" self.setPathStr = self.setXlmStr + self.envPath self.setLdPathStr= self.xlmHome + "%0s/tools/lib:%0s/share/PLI/IUS/linux64:%0s"%(self.xlmHome,self.verdiHome,self.ldPath) os.environ["CDS_LIC_FILE"] = self.cdsLicFile os.environ["PATH"] = self.setPathStr os.environ["LD_LIBRARY_PATH"] = self.setLdPathStr def setResourceLimit(self): resource.setrlimit(resource.RLIMIT_STACK,(resource.RLIM_INFINITY,resource.RLIM_INFINITY)) resource.setrlimit(resource.RLIMIT_AS,(resource.RLIM_INFINITY,resource.RLIM_INFINITY)) resource.setrlimit(resource.RLIMIT_DATA,(resource.RLIM_INFINITY,resource.RLIM_INFINITY)) # TODO generate random seed class genRandSeed(): def __init__(self): self.randDataVal = random.randint(1,1000000000) self.randSymbol = random.randint(0,1) def genRandData(self): if self.randSymbol: self.randDataVal = -self.randDataVal; return self.randDataVal # TODO Testcase or Testlist anaysis class genTestcaselist(): def __init__(self,pjPath): self.testCaselist = [] self.testCfglist = [] self.msgCtrlObj = setEnvMsg("Analysis testcase and testlist infomation") self.projPath = pjPath self.cfgObj = getTestConfig() self.testCount = 0 self.waveCtrl = False self.seedWorkCtrl = False self.compVarList = [] self.compvarCount = 0 def genTestcase(self): if options.testCfgName!=None: for cfgName in options.testCfgName: if cfgName not in self.testCfglist: self.testCfglist.append(cfgName) if options.countCtrl: self.waveCtrl = False self.seedWorkCtrl = True if len(self.testCfglist)==0: self.testCfglist.append(options.caseCfgName) for testName in options.testNameCtrl: if os.path.exists(self.projPath+"/"+testName)==False and options.testCheckCtrl==1: self.msgCtrlObj.fatalMsgPrint("No exist " + testName + " in " + self.projPath) else: for cfgName in self.testCfglist: if options.testCheckCtrl==0: self.cfgObj.getCfgInfo(self.projPath+"/",cfgName) else: self.cfgObj.getCfgInfo(self.projPath+"/"+testName,cfgName) curCompVar = self.cfgObj.getCompOpts() if self.compVarList.count(curCompVar)==0: self.compVarList.append(curCompVar) self.compvarCount = self.compVarList.index(curCompVar) curTestCfgName = self.cfgObj.getCfgFileName() simVarList = self.cfgObj.getSimOpts() preVarList = self.cfgObj.getPreSimProc() postVarList = self.cfgObj.getPostSimProc() falseErrVarList = self.cfgObj.getFalseErr() expectErrVarList = self.cfgObj.getExpectErr() falseWarnVarList = self.cfgObj.getFalseWarn() expectWarnVarList = self.cfgObj.getExpectWarn() for curCountVal in range(0,options.countCtrl): if options.seedCtrl: curSeedData = options.seedCtrl.pop(0) else: randSeedObj = genRandSeed() curSeedData = randSeedObj.genRandData() curTestcaseInfo = [testName,curSeedData,curTestCfgName,self.compvarCount,self.compVarList[self.compvarCount],simVarList,preVarList,postVarList,falseErrVarList,expectErrVarList,falseWarnVarList,expectWarnVarList] self.testCaselist.append(curTestcaseInfo) self.testCount += 1 else: testNameList = options.testNameCtrl[:] testcaseCount = len(testNameList) if options.seedCtrl!=None: seedCount = len(options.seedCtrl) else: seedCount = 1 if len(self.testCfglist)==0: self.testCfglist.append(options.caseCfgName) testCfgCount = len(self.testCfglist) if testcaseCount == 1: self.waveCtrl = options.waveCtrl curTestcase = testNameList.pop(0) if os.path.exists(self.projPath+"/"+curTestcase)==False and options.testCheckCtrl==1: self.msgCtrlObj.fatalMsgPrint("No exist " + curTestcase + " in " + self.projPath) else: seedCountCtrl = int(math.ceil(float(seedCount)/float(testCfgCount))) for cfgName in self.testCfglist: if options.testCheckCtrl==0: self.cfgObj.getCfgInfo(self.projPath+"/",cfgName) else: self.cfgObj.getCfgInfo(self.projPath+"/"+curTestcase,cfgName) curCompVar = self.cfgObj.getCompOpts() if self.compVarList.count(curCompVar)==0: self.compVarList.append(curCompVar) self.compvarCount = self.compVarList.index(curCompVar) curTestCfgName = self.cfgObj.getCfgFileName() simVarList = self.cfgObj.getSimOpts() preVarList = self.cfgObj.getPreSimProc() postVarList = self.cfgObj.getPostSimProc() falseErrVarList = self.cfgObj.getFalseErr() expectErrVarList = self.cfgObj.getExpectErr() falseWarnVarList = self.cfgObj.getFalseWarn() expectWarnVarList = self.cfgObj.getExpectWarn() for seedIdx in range(0,seedCountCtrl): if options.seedCtrl: curSeedData = options.seedCtrl.pop(0) else: randSeedObj = genRandSeed() curSeedData = randSeedObj.genRandData() curTestcaseInfo = [curTestcase,curSeedData,curTestCfgName,self.compvarCount,self.compVarList[self.compvarCount],simVarList,preVarList,postVarList,falseErrVarList,expectErrVarList,falseWarnVarList,expectWarnVarList] self.testCaselist.append(curTestcaseInfo) self.testCount += 1 if self.testCount>1: self.seedWorkCtrl = True else: self.seedWorkCtrl = options.seedWorkCtrl else: self.waveCtrl = options.waveCtrl for testName in options.testNameCtrl: if os.path.exists(self.projPath+"/"+testName)==False and options.testCheckCtrl==1: self.msgCtrlObj.fatalMsgPrint("No exist " + testName + " in " + self.projPath) else: if len(self.testCfglist): cfgName = self.testCfglist.pop(0) else: cfgName = options.caseCfgName if options.testCheckCtrl==0: self.cfgObj.getCfgInfo(self.projPath+"/",cfgName) else: self.cfgObj.getCfgInfo(self.projPath+"/"+testName,cfgName) curCompVar = self.cfgObj.getCompOpts() if self.compVarList.count(curCompVar)==0: self.compVarList.append(curCompVar) self.compvarCount = self.compVarList.index(curCompVar) curTestCfgName = self.cfgObj.getCfgFileName() simVarList = self.cfgObj.getSimOpts() preVarList = self.cfgObj.getPreSimProc() postVarList = self.cfgObj.getPostSimProc() falseErrVarList = self.cfgObj.getFalseErr() expectErrVarList = self.cfgObj.getExpectErr() falseWarnVarList = self.cfgObj.getFalseWarn() expectWarnVarList = self.cfgObj.getExpectWarn() if options.seedCtrl: curSeedData = options.seedCtrl.pop(0) else: randSeedObj = genRandSeed() curSeedData = randSeedObj.genRandData() curTestcaseInfo = [testName,curSeedData,curTestCfgName,self.compvarCount,self.compVarList[self.compvarCount],simVarList,preVarList,postVarList,falseErrVarList,expectErrVarList,falseWarnVarList,expectWarnVarList] self.testCaselist.append(curTestcaseInfo) self.testCount += 1 if self.testCount>1: self.seedWorkCtrl = True else: self.seedWorkCtrl = options.seedWorkCtrl def genTestlist(self): curTestname = "" curCount = "" curTestCfg = "" curCompVar = "" simVar = "" seedVar = "" lineInfo = "" fileHandle = open(options.testListFile,'r') self.waveCtrl = False self.seedWorkCtrl = True while True: lineContext = fileHandle.readline() if not lineContext: break lineInfo = lineContext.strip() if len(lineInfo)==0: continue reInfo = "^\s*#.*" lineObj= re.match(reInfo,lineContext) if lineObj: continue else: testInfoQueue = lineContext.split(";") if len(testInfoQueue)<=2: self.msgCtrlObj.fatalMsgPrint("Regression list include testcase and count, current line: %0s"%(lineContext)) curTestname = testInfoQueue[0].strip() curCount = testInfoQueue[1].strip() if len(testInfoQueue)>2: curTestCfg = testInfoQueue[2].strip() if len(testInfoQueue)>3: curCompVar = testInfoQueue[3].strip() if len(testInfoQueue)>4: simVar = testInfoQueue[4].strip() #support multi seed if len(testInfoQueue)>5: seedVar = testInfoQueue[5].strip() seedTempVar = re.sub(' +',' ',seedVar) seedVarList = seedTempVar.split(' ') if os.path.exists(self.projPath+"/"+curTestname)==False and options.testCheckCtrl==1: self.msgCtrlObj.fatalMsgPrint("No exist " + curTestname + " in " + self.projPath) else: testMacroVarList = [] noMacroVarList = [] compMacroVar = "" compNoMacroVar = "" if curTestCfg: testCfgFile = curTestCfg elif options.testCfgName!=None: testCfgFile = options.testCfgName.pop(0) else: testCfgFile = options.caseCfgName if options.testCheckCtrl==0: self.cfgObj.getCfgInfo(self.projPath+"/",testCfgFile) else: self.cfgObj.getCfgInfo(self.projPath+"/"+curTestname,testCfgFile) #test.cfg compile parameter testCompVar = self.cfgObj.getCompOpts() compOptsVar = re.sub(' +',' ',testCompVar) CompVarList = compOptsVar.split(' ') CompVarList.sort() for macroVar in CompVarList: if macroVar: macroObj = re.match('\+define\+',macroVar) if macroObj: macroTmpVar = re.sub('\+define\+','',macroVar) macroVarList = macroTmpVar.split('+') macroVarList.sort() for compVar in macroVarList: if compVar not in testMacroVarList: testMacroVarList.append(compVar) else: compVar = re.sub(':',' ',macroVar) noMacroVarList.append(compVar) #List compile parameter testCompVar = curCompVar compOptsVar = re.sub(' +',' ',testCompVar) CompVarList = compOptsVar.split(' ') CompVarList.sort() for macroVar in CompVarList: if macroVar: macroObj = re.match('\+define\+',macroVar) if macroObj: macroTmpVar = re.sub('\+define\+','',macroVar) macroVarList = macroTmpVar.split('+') macroVarList.sort() for compVar in macroVarList: if compVar not in testMacroVarList: testMacroVarList.append(compVar) else: compVar = re.sub(':',' ',macroVar) noMacroVarList.append(compVar) #Merge all compile parameter if len(testMacroVarList): compMacroVar = "+define+" + "+".join(testMacroVarList) if len(noMacroVarList): compNoMacroVar = " ".join(noMacroVarList) if compMacroVar or compNoMacroVar: compMacroVar = compMacroVar + " " + compNoMacroVar if self.compVarList.count(compMacroVar)==0: self.compVarList.append(compMacroVar) self.compvarCount = self.compVarList.index(compMacroVar) #Sim paramter simVarList = self.cfgObj.getSimOpts() if simVar: simVarList = simVarList.strip() + " " + simVar curTestCfgName = self.cfgObj.getCfgFileName() preVarList = self.cfgObj.getPreSimProc() postVarList = self.cfgObj.getPostSimProc() falseErrVarList = self.cfgObj.getFalseErr() expectErrVarList = self.cfgObj.getExpectErr() falseWarnVarList = self.cfgObj.getFalseWarn() expectWarnVarList = self.cfgObj.getExpectWarn() for curCountVal in range(0,int(curCount)): if curCountVal>=len(seedVarList): randSeedObj = genRandSeed() curSeedData = randSeedObj.genRandData() elif seedVarList[curCountVal]: curSeedData = int(seedVarList[curCountVal]) else: randSeedObj = genRandSeed() curSeedData = randSeedObj.genRandData() curTestcaseInfo = [curTestname,curSeedData,curTestCfgName,self.compvarCount,self.compVarList[self.compvarCount],simVarList,preVarList,postVarList,falseErrVarList,expectErrVarList,falseWarnVarList,expectWarnVarList] self.testCaselist.append(curTestcaseInfo) self.testCount += 1 fileHandle.close() def getTestList(self): return self.testCaselist def getWaveCtrl(self): return self.waveCtrl def getSeedPathCtrl(self): return self.seedWorkCtrl def getTestcaseCount(self): return self.testCount def getCompVar(self): return self.compVarList
06-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值