class BaseTest(object): def __init__(self,matrix,macro,srcfile,rstfile): self._log=LogUtil().logger('Data') self._matrix=matrix self._file1=srcfile self._file2=rstfile self._macro=macro def genSrcFile(self): pass def genCriterion(self): return ((0,1),(0,1),(0,1)) def genCombination(self): criterion=self.genCriterion() iidxlist=tuple(criterion[-1]) for c in itertools.product(*criterion[:-1]): yield iidxlist,c def checkRule(self,iidxlist,comb): rule=[i for i in range(len(self._matrix.ruleValues()))] for idx,iidx in enumerate(iidxlist): rule=self._checkRule(iidx,comb[idx],rule) if len(rule)<1: break if len(rule)!=1: self._log.error('Rule Error:Cnt[%d] Values[%s]'%(len(rule),','.join(rule))) return -1 else: return rule[0] def _checkRule(self,idx,value,ruleidxlist): return tuple(ruleidxlist)
class DriverParser(object): def __init__(self, tokenmanager=tm): self._log = LogUtil().logger("DriverParser") self._tm = tokenmanager self._dfile = "" self._tfile = "" self._funcdriver = "" self._functarget = "" self._funcdmy = () def parse(self, driverfile): tfile = driverfile + self._tm.D_EXT_TOK if self._tm.genTokenFile(driverfile, tfile): fblist = self._tm.getFunctionList(tfile) fdlist = self._tm.getFunctionDeclarationList(tfile) fbset = set() for func in fblist: parts = func.split(":") funcname = parts[5] # fullfuncname=parts[6] fbset.add(funcname) fdset = set() for func in fdlist: parts = func.split(":") funcname = parts[5] # fullfuncname=parts[6] fdset.add(funcname) driverfunc = tuple(fbset - fdset) testfunc = tuple(fdset - fbset) if len(driverfunc) != 1 or len(testfunc) != 1: self._log.error("Driver file format error") return False else: self._dfile = driverfile self._tfile = tfile self._funcdriver = driverfunc[0] self._functarget = testfunc[0] self._funcdmy = tuple(fdset & fbset) return True return False @property def driver_func(self): return self._funcdriver @property def target_func(self): return self._functarget @property def dummy_func(self): return tuple(self._funcdmy)
def _initdata(self): self._log=LogUtil().logger('Data') self._rules=() self._ivars=() self._ovars=() self._icnt=-1 self._iopairs=()
def __init__(self, tokenmanager=tm): self._log = LogUtil().logger("DriverParser") self._tm = tokenmanager self._dfile = "" self._tfile = "" self._funcdriver = "" self._functarget = "" self._funcdmy = ()
#!/usr/bin/python # -*- coding: UTF-8 -*- import getconn_util from logutil import LogUtil # get_con = getconn_util.get_conn('hive') # print 'jdbc : '+get_con.jdbc_link() # print 'cxoracle : '+get_con.cxoracle_link() # print 'orauser: '******'E:/work_files/logutil_test.log' log = LogUtil(logfile, level='warn') log.info('***** test info ******') log.info('***** test sads ******') log.info('***** test fdfdf ******') log.warn('***** test sdad ******') log.error('***** test error ******')
class DmyRenameBase(object): ''' Usage: 1. dr=DmyRenameBase() 2. dr.setSourcePath(srcpath,tokpath) 3. dr.setFuncName(funcname) 4. dr.setDmyFunc(dmyfuncs) 5. dr.doRename() 6. dr.doRestore() ''' def __init__(self): self._log=LogUtil().logger('dummy') self._srcpath='' self._tokpath='' self._funcname='' def setSourcePath(self,srcpath,tokpath): if os.path.exists(srcpath) and os.path.isdir(srcpath): self._srcpath=srcpath if not os.path.exists(tokpath) or not os.path.isdir(tokpath): os.makedirs(tokpath) self._tokpath=tokpath return True else: self._log.error('Invalid source file dir[%s]'%(srcpath,)) return False def setFuncName(self,funcname): if not funcname: self._funcname='' return False else: self._funcname=str(funcname) return True def setDmyFunc(self,dmyfuncs): if dmyfuncs: self._dmyfunc=tuple(dmyfuncs) return True else: return False def doRename(self): funcfiles=self.filterFiles(self.findFunc()) if not funcfiles: return False for finfo in funcfiles: tokfile=finfo['tokfile'] self.rename(tokfile,self._funcname,self._dmyfunc) def doRestore(self): if self._srcpath: cparser.unDummyFolder(self._srcpath) def filterFiles(self,funcfiles): return funcfiles def findFunc(self): if not self._funcname: return () if not self._srcpath or not self._tokpath: return () tm=cparser.tokenmanager ret=tm.findFunctionBySrc(self._srcpath,self._tokpath,self._funcname) if len(ret[self._funcname])<1: return () else: return tuple(ret[self._funcname]) def rename(self,tokfile,funcname,dmylist): info={'FB':funcname, 'DMY':tuple(dmylist), } cparser.dummyFile(tokfile,info)
def __init__(self): self._log=LogUtil().logger('dummy') self._srcpath='' self._tokpath='' self._funcname=''
def __init__(self): self._log=LogUtil().logger('core') self._qr=qrcode.QRCode() self._progfunc=None self.clearLastData()
class File2QRCode(object): D_CONST_MAXSIZE=2331 D_CONST_PREFERSIZE=560 def __init__(self): self._log=LogUtil().logger('core') self._qr=qrcode.QRCode() self._progfunc=None self.clearLastData() def clearLastData(self): self._lastwidth=0 self._lasttotalpack=0 self._lastprefix='' self._lastextname='' self._lastoutpath='' def regProgressFunc(self,func): self._log.info('callback(%s) register'%(func.__name__,)) if callable(func): self._progfunc=func else: self._log.error('callback function error') def quickSplit(self,infile,splitsize=D_CONST_PREFERSIZE,outpath='',prefix='quick',method='png'): if self._fileCheck(infile): if outpath=='': outpath=dirname(infile) self.splitFile(infile, splitsize, outpath, prefix, method) return True return False def splitFile(self,infile,splitsize,outpath,outprefix,outmethod): self._log.info('InFile(%s) SplitSize(%d) OutPath(%s) Prefix(%s) Format(%s)'%(infile,splitsize,outpath,outprefix,outmethod)) if self._fileCheck(infile): self.clearLastData() if outpath=='': self._log.debug('use current dir as output dir') outpath='.' if exists(outpath) and isdir(outpath): pass else: self._log.debug('make output dir') makedirs(outpath) self._lastoutpath=outpath self._lastprefix=outprefix if splitsize<=0 or splitsize>File2QRCode.D_CONST_MAXSIZE: splitsize=File2QRCode.D_CONST_MAXSIZE self._log.warning('SplitSize out of range, use %d'%(splitsize,)) om=str(outmethod).upper() if om=='PNG': image_factory=qrcode.image.pil.PilImage self._lastextname='png' else: image_factory=qrcode.image.svg.SvgImage self._lastextname='svg' totalsize=getsize(infile) self._log.debug('InFile Size:%d'%(totalsize,)) currentcnt=1 self._lasttotalpack=(totalsize+splitsize-1)/splitsize self._log.debug('Package Count:%d'%(self._lasttotalpack,)) self._lastwidth=len(str(self._lasttotalpack)) with open(infile,'rb') as fh: while currentcnt<=self._lasttotalpack: data=fh.read(splitsize) self._qr.add_data(data) img = self._qr.make_image(image_factory=image_factory) outfile=self.getLastFilename(currentcnt) img.save(outfile) self._log.debug('%d/%d InSize:%d OutFile:%s QRVersion:%d'%(currentcnt,self._lasttotalpack,len(data),outfile,self._qr.version)) self._qr.clear() self._qr.version=0 self._fireCallback(currentcnt,self._lasttotalpack) currentcnt+=1 return True return False def getLastFilename(self,idx): self._log.info('Index:%d Width:%d Prefix:%s'%(idx,self._lastwidth,self._lastprefix)) filename=self._genOutfile(self._lastprefix, self._lastextname, idx, self._lastwidth) return join(self._lastoutpath,filename) def _genOutfile(self,prefix,extname,curcnt,width): outstr=prefix strfmt="%0"+str(width)+"d" outstr+=strfmt%(curcnt) outstr+='.'+extname return outstr def _fileCheck(self,infile): if exists(infile) and isfile(infile): return True return False def _fireCallback(self,cur,total): self._log.info('Current:%d Total:%d'%(cur,total)) if self._progfunc: self._progfunc(cur,total)
def __init__(self, tokenmanager=cparser.tokenmanager): self._log = LogUtil().logger("TestManager") self._tm = tokenmanager self._dp = driverparser.DriverParser() self._vc = VcProj()
class BaseMatrix(object): def __init__(self,ivars,ovars,irules,orules): self._initdata() self._checkdata(ivars,ovars,irules,orules) #def __init__(self,ivars,ovars,rules): #self._initdata() #self._checkdata(ivars,ovars,rules) #def _checkdata(self,ivars,ovars,rules): #if len(rules)<1: #self._log.error('No Rules') #return #if len(ivars)+len(ovars)!=len(rules[0]): #self._log.error('The Number Of I/O Variables Is Wrong') #return #self._setdata(ivars,ovars,rules) def getIVars(self): return self._getVars(self._ivars) def getOVars(self): return self._getVars(self._ovars) def iterRules(self): for rule in self._rules: yield tuple(rule) def ruleValues(self): outlist=[] for rule in self._rules: outlist.append(tuple([item['value'] for item in rule])) return tuple(outlist) def getPairs(self): return tuple(self._iopairs) def _getVars(self,varlist): outlist=[] for v in varlist: outlist.append(v['value']) return tuple(outlist) def _initdata(self): self._log=LogUtil().logger('Data') self._rules=() self._ivars=() self._ovars=() self._icnt=-1 self._iopairs=() def _checkdata(self,ivars,ovars,irules,orules): if len(irules)!=len(orules): self._log.error('Rule Count for Input[%d] And Output[%d] Are Not Equal'%(len(irules),len(orules))) return if len(irules)<1: self._log.error('No Rules') return if len(ivars)!=len(irules[0]) or len(ovars)!=len(orules[0]): self._log.error('The Number Of I/O Variables Is Wrong') return rules=[] for i in range(len(irules)): rules.append([]) rules[-1].extend(irules[i]) rules[-1].extend(orules[i]) self._setdata(ivars,ovars,rules) def _setdata(self,ivars,ovars,rules): def rulecmp(objx,objy): maxidx=len(ivars) def _itemcmp(objx,objy,idx): ret=cmp(objx[idx]['value'],objy[idx]['value']) if ret!=0: if objx[idx]['value']=='__ELSE__' or objy[idx]['value']=='__NOTCARE__': ret=1 elif objy[idx]['value']=='__ELSE__' or objx[idx]['value']=='__NOTCARE__': ret=-1 return ret def _rulecmp(objx,objy): i=0 ret=0 while i<maxidx and ret==0: ret=_itemcmp(objx,objy,i) i+=1 return ret return _rulecmp(objx,objy) tmprules=list(rules) tmprules.sort(cmp=rulecmp) self._rules=list(tmprules) self._ivars=list(ivars) self._ovars=list(ovars) self._icnt=len(self._ivars) i=0 j=0 maxj=len(self._ovars) pairs=[] while i<self._icnt: ivar=self._ivars[i]['value'] if ivar.endswith(')'): self._log.debug('Finding Pair For Input[%d]:%s'%(i,ivar)) while j<maxj: ovar=self._ovars[j]['value'] self._log.debug('Checking Output[%d]:%s'%(j+self._icnt,ovar)) if ovar==ivar: self._log.debug('Found Pair Input[%d]:Output[%d]'%(i,j+self._icnt)) pairs.append((i,j+self._icnt)) j+=1 break j+=1 else: self._log.error('Functions In Input And Output Donot Match') return i+=1 self._iopairs=tuple(pairs)
def __init__(self): self._log = LogUtil().logger("XmlReader")
def __init__(self,parent,id=wx.ID_ANY,pos=wx.DefaultPosition,size=wx.DefaultSize,style=wx.TAB_TRAVERSAL,name=wx.PanelNameStr): super(F2QToolPanel,self).__init__(parent,id,pos,size,style,name) self._log=LogUtil().logger('ui') self._tool=f2q.File2QRCode() self._tool.regProgressFunc(self.OnProgress) self.SetupUi()
class XmlReader(object): def __init__(self): self._log = LogUtil().logger("XmlReader") def loadXml(self, filename): self._log.info("Reading XML File:%s" % (filename,)) self._initdata() tagstack = [] for action, elem in etree.iterparse(filename, events=("start", "end")): if action == "start": tag = elem.tag tagstack.append(tag) self.starttag(":".join(tagstack), elem) else: self.endtag(":".join(tagstack), elem) tagstack.pop(-1) def getInfo(self, infoobj): if not isinstance(infoobj, ManageC): return False for func in self._funcs: infoobj.addFunction(func) for var in self._vars: infoobj.addVariable(var) infoobj.setMatrix(self._invars, self._outvars, self._incond, self._outcond) return True def _initdata(self): self._funcs = [] self._funcs.append([]) self._vars = [] self._invars = [] self._outvars = [] self._incond = [] self._outcond = [] def starttag(self, path, elem): if path.startswith("root:function:param"): self._log.debug(path) self._funcs[0].append(elem.attrib) elif path == "root:ex-function": self._log.debug(path) self._funcs.append([]) elif path.startswith("root:ex-function:param"): self._log.debug(path) self._funcs[-1].append(elem.attrib) elif path.startswith("root:ex-var:var"): self._log.debug(path) self._vars.append(elem.attrib) elif path.startswith("root:auto-var:var"): self._log.debug(path) self._vars.append(elem.attrib) elif path.startswith("root:matrix:in-var:var"): self._log.debug(path) self._invars.append(elem.attrib) elif path.startswith("root:matrix:out-var:var"): self._log.debug(path) self._outvars.append(elem.attrib) elif path.startswith("root:matrix:rule"): if path.endswith("in-rule"): self._incond.append([]) elif path.endswith("out-rule"): self._outcond.append([]) if ":in-rule:value" in path: self._log.debug(path) self._incond[-1].append(elem.attrib) elif ":out-rule:value" in path: self._log.debug(path) self._outcond[-1].append(elem.attrib) def endtag(self, path, elem): if path == "root:function": self._log.debug(path) elif path == "root:ex-function": self._log.debug(path) elif path == "root:ex-var": self._log.debug(path) elif path == "root:auto-var": self._log.debug(path) elif path == "root:matrix:in-var": self._log.debug(path) elif path == "root:matrix:out-var": self._log.debug(path) elif path.startswith("root:matrix:rule"): if ":in-rule:value" in path: self._log.debug(path) elif ":out-rule:value" in path: self._log.debug(path)
class F2QToolPanel(wx.Panel): def __init__(self,parent,id=wx.ID_ANY,pos=wx.DefaultPosition,size=wx.DefaultSize,style=wx.TAB_TRAVERSAL,name=wx.PanelNameStr): super(F2QToolPanel,self).__init__(parent,id,pos,size,style,name) self._log=LogUtil().logger('ui') self._tool=f2q.File2QRCode() self._tool.regProgressFunc(self.OnProgress) self.SetupUi() def SetupUi(self): sizer=wx.GridBagSizer() label1=wx.StaticText(self,label='Input File') self._txt1=wx.TextCtrl(self,value='') btn1=wx.Button(self,label='...') label2=wx.StaticText(self,label='Output Path') self._txt2=wx.TextCtrl(self,value='') btn2=wx.Button(self,label='...') label31=wx.StaticText(self,label='Split Size') label32=wx.StaticText(self,label='Format') self._combo31=wx.ComboBox(self,choices=('14','26','42','62', '84','106','122','152', '180','213','251','287', '331','362','412','450', '504','560','624','666', '711','779','857','911', '997','1059','1125','1190', '1264','1370','1452','1538', '1628','1722','1809','1911', '1989','2099','2213','2331') ,value='560',style=wx.CB_READONLY) self._combo32=wx.ComboBox(self,choices=('png','svg'),value='png',style=wx.CB_READONLY) label4=wx.StaticText(self,label='Prefix') self._txt4=wx.TextCtrl(self,value='quick') btn4=wx.Button(self,label='Split') sizer.Add(label1,(0,0),flag=wx.EXPAND) sizer.Add(self._txt1,(0,1),(1,3),flag=wx.EXPAND) sizer.Add(btn1,(0,4),flag=wx.EXPAND) sizer.Add(label2,(1,0),flag=wx.EXPAND) sizer.Add(self._txt2,(1,1),(1,3),flag=wx.EXPAND) sizer.Add(btn2,(1,4),flag=wx.EXPAND) sizer.Add(label31,(2,0),flag=wx.EXPAND) sizer.Add(self._combo31,(2,1),flag=wx.EXPAND) sizer.Add(label32,(2,3),flag=wx.EXPAND) sizer.Add(self._combo32,(2,4),flag=wx.EXPAND) sizer.Add(label4,(3,0),flag=wx.EXPAND) sizer.Add(self._txt4,(3,1),(1,3),flag=wx.EXPAND) sizer.Add(btn4,(3,4),flag=wx.EXPAND) sizer.SetFlexibleDirection(wx.HORIZONTAL) sizer.AddGrowableCol(2) self.SetSizer(sizer) self.Bind(wx.EVT_BUTTON,self.OnInBtn,btn1) self.Bind(wx.EVT_BUTTON,self.OnOutBtn,btn2) self.Bind(wx.EVT_BUTTON,self.OnActBtn,btn4) def OnProgress(self,curcnt,totalcnt): self._log.info('Callback(%d/%d)'%(curcnt,totalcnt)) self.GetParent().SetStatusText("%d/%d"%(curcnt,totalcnt),2) if curcnt>=totalcnt: msgbox=wx.MessageDialog(self,message='Split Done!',style=wx.OK|wx.CENTER) msgbox.ShowModal() def OnInBtn(self,event): self._log.info('ButtonPressed') infile=self._txt1.GetValue() if infile: pathname,filename=os.path.split(infile) fd=wx.FileDialog(self,message='Select file to split...',defaultDir=pathname,defaultFile=filename) else: fd=wx.FileDialog(self,message='Select file to split...') if fd.ShowModal()==wx.ID_OK: fullname=fd.GetPath() self._log.debug('FileDialog:%s'%(fullname,)) pathname,filename=os.path.split(fullname) filesize=os.path.getsize(fullname) self._txt1.SetValue(fullname) self._txt2.SetValue(pathname) self.GetParent().SetStatusText(filename,0) self.GetParent().SetStatusText("%d Bytes"%(filesize,),1) self.GetParent().SetStatusText("",2) def OnOutBtn(self,event): self._log.info('ButtonPressed') outpath=self._txt2.GetValue() or '.' fd=wx.DirDialog(self,message='Select output path...',defaultPath=outpath) if fd.ShowModal()==wx.ID_OK: pathname=fd.GetPath() self._log.debug('DirDialog:%s'%(pathname,)) self._txt2.SetValue(pathname) def OnActBtn(self,event): self._log.info('ButtonPressed') infile=self._txt1.GetValue() outpath=self._txt2.GetValue() prefix=self._txt4.GetValue() splitsize=int(self._combo31.GetValue()) method=self._combo32.GetValue() self._log.debug('InFile:%s SplitSize:%d OutPath:%s Prefix:%s Format:%s'%(infile,splitsize,outpath,prefix,method)) self._tool.quickSplit(infile,splitsize,outpath,prefix,method)
def __init__(self): self._log = LogUtil().logger("TestManager")
class VcProj(object): def __init__(self): self._log = LogUtil().logger("TestManager") def autoVcproj(self, searchpath, tplfile, outfile, filelist): srcvcproj = self._findVcproj(searchpath) if srcvcproj: self.generateVcproj(srcvcproj, tplfile, outfile, filelist) def generateVcproj(self, infile, tplfile, outfile, filelist): if exists(infile): info = self._getVcprojInfo(infile) self.manualVcproj(info[0], info[1], tplfile, outfile, filelist) def manualVcproj(self, incpath, compileoption, tplfile, outfile, filelist): self._genVcproj(outfile, incpath, compileoption, filelist, tplfile) def _genVcproj(self, outfile, incpath, compileoption, filelist, tplfile): self._log.debug("Generate .vcproj file:%s" % (outfile,)) incpath = incpath.strip(" ;") incpath = re.sub(r";+", r";", incpath) compileoption = compileoption.strip(" ;") compileoption = re.sub(r";+", r";", compileoption) fin = open(tplfile, "rU") fout = open(outfile, "w") for line in fin.readlines(): if 'AdditionalIncludeDirectories=""' in line: self._log.debug("Set IncludePath:%s" % (incpath,)) fout.write('\t\t\t\tAdditionalIncludeDirectories="%s"\n' % (incpath,)) elif 'PreprocessorDefinitions=""' in line: self._log.debug("Set CompileOption:%s" % (compileoption,)) fout.write('\t\t\t\tPreprocessorDefinitions="%s"\n' % (compileoption,)) elif "<Files>" in line: self._log.debug("Set File:%s" % (str(filelist),)) fout.write(line) for filename in filelist: line = '\t\t<File\n\t\t\tRelativePath="%s"\n\t\t\t>\n\t\t</File>\n' % (filename,) fout.write(line) else: fout.write(line) self._incpath = incpath self._compileoption = compileoption fout.close() fin.close() def _getVcprojInfo(self, vcprojfile, part="Debug|Win32"): self._log.debug("Read .vcproj file:%s, part:%s" % (vcprojfile, part)) fin = open(vcprojfile, "rU") incpath = "" compileoption = "" flag = False for line in fin.readlines(): if flag: if not incpath and "AdditionalIncludeDirectories" in line: startidx = line.find('"') stopidx = line.rfind('"') incpath = line[startidx + 1 : stopidx] self._log.debug("IncludePath:%s" % (incpath,)) elif not compileoption and "PreprocessorDefinitions" in line: startidx = line.find('"') stopidx = line.rfind('"') compileoption = line[startidx + 1 : stopidx] self._log.debug("CompileOption:%s" % (compileoption,)) else: if part in line: flag = True fin.close() return incpath, compileoption def _findVcproj(self, startfolder): if exists(startfolder) and isdir(startfolder): sdir = abspath(startfolder) while True: self._log.debug("Find .vcproj file in folder:%s" % (sdir,)) for curfile in listdir(sdir): if curfile.upper().endswith(".VCPROJ"): self._log.debug("Found .vcproj file:%s" % (join(sdir, curfile))) return join(sdir, curfile) # move to parent dir while True: parts = split(sdir) if parts[0] == sdir: # sdir is root dir(e.g d:/) self._log.error("Cannot find .vcproj file until root folder") return "" sdir = parts[0] if parts[1] == "": # sdir endswith '/' pass else: break else: return ""
def formatExcelText(funcname, finfo): """ @param funcname: @param finfo: cparser.getTestFuncInfo()[funcname][idx] @ret text for Excel """ log = LogUtil().logger("DriverParser") outtxt = "" # target function fdecl = finfo["decl"] funcstr = " %s " % (funcname,) didx = fdecl.find(funcstr) functype = fdecl[0:didx] didx += len(funcstr) startidx = fdecl.find("(", didx) stopidx = fdecl.rfind(")") parts = fdecl[startidx + 1 : stopidx].strip().split(" ") params = [] while "," in parts: didx = parts.index(",") params.append(parts[0:didx]) parts = parts[didx + 1 :] outtxt += "%s\n" % (fdecl,) outtxt += "ret = %s(%s);\n\n" % (funcname, ",".join([x[-1] for x in params if len(x) > 1])) for x in params: if len(x) > 1: outtxt += "%s\t%s\tlocal\tmemset((char*)&%s,0x7c,sizeof(%s));\n" % ( x[-1], " ".join(x[0:-1]), x[-1], " ".join(x[0:-1]), ) outtxt += "\nret\t%s\tlocal\tmemset((char*)&ret,0x7c,sizeof(%s));\n\n" % (functype, functype) # dummy function fcalls = finfo["calls"] fothers = finfo["calldecl"] flog = {} for fcall in fcalls: parts = fcall.split(":") funcstr = " " + parts[0] + " " funccnt = int(parts[1]) flog[fcall] = [] for fother in fothers: if funcstr in fother: if len(flog[fcall]) < 1: didx = fother.find(funcstr) if funccnt > 1: for i in range(funccnt): outname = fother[0 : didx + 1] + "dummy" + str(i + 1) + "_" + fother[didx + 1 :] outtxt += "%s\t%s\tfunction\n" % (outname, fother[0:didx]) else: outname = fother outtxt += "%s\t%s\tfunction\n" % (outname, fother[0:didx]) else: # multiple function body for one dummy function pass flog[fcall].append(fother) for key, value in sorted(flog.items()): if len(value) == 0: log.debug("No Function Body[%s] Found" % (key,)) outtxt += "%s\t\tfunction\n" % (key,) elif len(value) > 1: tmpset = set(value) if len(tmpset) > 1: log.debug("Muliple Function Bodies[%s] Found:%s" % (key, str(tmpset))) return outtxt
def __init__(self): self._log=LogUtil().logger('Txt2Xml')
def __init__(self,matrix,macro,srcfile,rstfile): self._log=LogUtil().logger('Data') self._matrix=matrix self._file1=srcfile self._file2=rstfile self._macro=macro
class Txt2Xml(object): D_FIELDSEP='\t' D_CASTCHAR='(U1*)' D_FLAGSTR1=u'■関数宣言'.encode('utf-8') D_FLAGSTR21=u'■自動変数'.encode('utf-8') D_FLAGSTR22=u'■完了'.encode('utf-8') D_FLAGSTR31=u'■入出力表'.encode('utf-8') D_FLAGSTR32=u'■完了'.encode('utf-8') D_FLAGSTR33=u'入力'.encode('utf-8') D_FLAGSTR34=u'出力'.encode('utf-8') D_FLAGSTR41=u'■外部関数'.encode('utf-8') D_FLAGSTR42=u'■完了'.encode('utf-8') D_FLAGSTR51=u'■外部変数'.encode('utf-8') D_FLAGSTR52=u'■完了'.encode('utf-8') D_MARK1=u'戻り値'.encode('utf-8') D_MARK2=u'○'.encode('utf-8') D_MARK3=u'×'.encode('utf-8') D_MARK4=u'△'.encode('utf-8') D_MARK5=u'-'.encode('utf-8') D_MARK6=u'上記以外'.encode('utf-8') def __init__(self): self._log=LogUtil().logger('Txt2Xml') def loadTxt(self,filename,fileencode): self._log.info('File:%s Encode:%s'%(filename,fileencode)) fh=open(filename,'rU') data=fh.read() fh.close() data8=data.decode(fileencode).encode('utf-8') self.initData() self.splitTxt(data8) self.parseTxt() def toXml(self,filename): root=etree.Element('root') func=etree.SubElement(root,'function') self._xmlFunc(func,self._funcvars[0]) for i in range(len(self._funcvars)-1): func=etree.SubElement(root,'ex-function') self._xmlFunc(func,self._funcvars[i+1]) if len(self._exvardecl)>0: exvar=etree.SubElement(root,'ex-var') for data in self._exvardecl: self._xmlVar(exvar,data) if len(self._vardecl)>0: autovar=etree.SubElement(root,'auto-var') for data in self._vardecl: self._xmlVar(autovar,data) matrix=etree.SubElement(root,'matrix') self._xmlMatrix(matrix) with open(filename,'w') as fh: fh.write(etree.tostring(root, method='xml', xml_declaration=True, pretty_print=True, encoding='utf-8')) def _xmlFunc(self,element,funcdata): for idx,data in enumerate(funcdata): sub=etree.SubElement(element,'param%d'%(idx,)) sub.set('type',data[0]) sub.set('name',data[1]) def _xmlVar(self,element,vardata): subvar=etree.SubElement(element,'var') subvar.set('type',vardata[0]) subvar.set('name',vardata[1]) if len(vardata)==3: subvar.set('value',vardata[2]) elif len(vardata)==5: subvar.set('method',vardata[2]) subvar.set('value',vardata[3]) subvar.set('size',vardata[4]) def _xmlMatrix(self,element): if len(self._invars)>0: invar=etree.SubElement(element,'in-var') for idx,v in enumerate(self._invars): inv=etree.SubElement(invar,'var%d'%(idx,)) inv.set('value',v) if len(self._outvars)>0: outvar=etree.SubElement(element,'out-var') for idx,v in enumerate(self._outvars): outv=etree.SubElement(outvar,'var%d'%(idx,)) outv.set('value',v) for i in range(len(self._incond)): item=etree.SubElement(element,'rule%d'%(i,)) if len(self._incond[i])>0: irule=etree.SubElement(item,'in-rule') for idx,value in enumerate(self._incond[i]): ivalue=etree.SubElement(irule,'value%d'%(idx,)) ivalue.set('value',value) if len(self._outcond[i])>0: orule=etree.SubElement(item,'out-rule') for idx,value in enumerate(self._outcond[i]): ovalue=etree.SubElement(orule,'value%d'%(idx,)) ovalue.set('value',value) def initData(self): # output for splitTxt self._funcs=() self._extvars=() self._autovars=() self._iomatrix=() # output for parseTxt self._funcvars=[] self._exvardecl=[] self._vardecl=[] self._invars=[] self._outvars=[] self._incond=[] self._outcond=[] def splitTxt(self,data8): flag=0 #0:init step=0 flagtable=((self.D_FLAGSTR1,1), (self.D_FLAGSTR21,2), (self.D_FLAGSTR31,3), (self.D_FLAGSTR41,4), (self.D_FLAGSTR51,5)) donetable=[] funcdeclare='' autovars=[] iomatrix=[] extfunc=[] extvars=[] for lineno,line in enumerate(data8.splitlines()): if flag==0: for item in flagtable: if line.startswith(item[0]): self._log.debug('Found Start-tag:%d at line:%d'%(item[1],lineno)) if item[1] not in donetable: flag=item[1] donetable.append(flag) step=0 break else: self._log.warning('Found Duplicated Start-tag:%d'%(item[1],)) if flag==1: # function declare if step==0: step=1 elif step>=1: funcdeclare=line.strip() self._log.debug('Found End-tag:%d at line:%d'%(flag,lineno)) flag=0 elif flag==2: # auto vars if step<2: step+=1 elif step>=2: if line.startswith(self.D_FLAGSTR22): self._log.debug('Found End-tag:%d at line:%d'%(flag,lineno)) flag=0 else: autovars.append(line.strip()) elif flag==3: # i/o matrix if step==0: step+=1 else: iomatrix.append(line) if self.D_FLAGSTR32 in line: self._log.debug('Found End-tag:%d at line:%d'%(flag,lineno)) flag=0 elif flag==4: # extern function if step==0: step+=1 else: if line.startswith(self.D_FLAGSTR42): self._log.debug('Found End-tag:%d at line:%d'%(flag,lineno)) flag=0 else: extfunc.append(line.strip()) elif flag==5: # extern vars if step<2: step+=1 else: if line.startswith(self.D_FLAGSTR52): self._log.debug('Found End-tag:%d at line:%d'%(flag,lineno)) flag=0 else: extvars.append(line.strip()) self._autovars=tuple(autovars) self._iomatrix=tuple(iomatrix) funcs=[] funcs.append(funcdeclare) funcs.extend(extfunc) self._funcs=tuple(funcs) self._extvars=tuple(extvars) def parseTxt(self): for func in self._funcs: self._funcvars.append(self.parseFuncDecl(func)) self.parseExtVars() self.parseAutoVars() self.parseIOMatrix() def parseFuncDecl(self,fdec): self._log.info('Parse Function Declaration:%s'%(fdec,)) pos_l=fdec.find('(') pos_r=fdec.rfind(')') outdata=[] if pos_l<0 or pos_r!=len(fdec)-1: self._log.warning('cannot find proper "(" and ")"') else: outdata.append(self._parseFuncParam(fdec[:pos_l])) for param in fdec[pos_l+1:pos_r].split(','): outdata.append(self._parseFuncParam(param)) return tuple(outdata) def _parseFuncParam(self,data): self._log.debug('Parse Function Parameter:%s'%(data,)) ptype='' pname='' sdata=data.strip() rpat=re.compile(r'(?P<type>.+[^a-zA-Z0-9_])(?P<name>[a-zA-Z0-9_]+)') rret=rpat.search(sdata) if rret: ptype=rret.group('type').strip() pname=rret.group('name').strip() else: ptype=sdata.strip() pname=ptype return tuple((ptype,pname)) def parseExtVars(self): for line in self._extvars: self._log.debug('Parse External Parameter:%s'%(line,)) parts=line.split(self.D_FIELDSEP) if len(parts)<2: self._log.warning('Input Check Error, Skip') else: self._exvardecl.append((parts[0],parts[1])) def parseAutoVars(self): for line in self._autovars: self._log.debug('Parse Internal Parameter:%s'%(line,)) parts=line.split(self.D_FIELDSEP) if len(parts)<2: self._log.warning('Input Check Error, Skip') else: if len(parts)==2: parts.append('0') if parts[2]=='memset': if len(parts)<4: parts.append('') if parts[3]=='': parts[3]='0' if len(parts)<5: parts.append('') if parts[4]=='': parts[4]='sizeof(%s)'%(parts[0],) self._vardecl.append(tuple(parts[0:5])) elif parts[2]=='memcpy': if len(parts)<4: parts.append('') if parts[3]=='': self._log.warning('No Source Buffer Found, Change To memset') parts[2]='memset' parts[3]='0' if len(parts)<5: parts.append('') if parts[4]=='': parts[4]='sizeof(%s)'%(parts[0],) self._vardecl.append(tuple(parts[0:5])) else: self._vardecl.append(tuple(parts[0:3])) def parseIOMatrix(self): inidx,outidx=0,0 parts=self._iomatrix[0].split(self.D_FIELDSEP) if self.D_FLAGSTR34 in parts: inidx=parts.index(self.D_FLAGSTR34) else: self._log.error('No Output Found') return parts=self._iomatrix[-1].split(self.D_FIELDSEP) if self.D_FLAGSTR32 in parts: outidx=parts.index(self.D_FLAGSTR32) else: self._log.error('Cannot Figure Out Count Of In/Out Vars') return parts=self._iomatrix[1].split(self.D_FIELDSEP) if len(parts)<=outidx: self._log.error('Not Enough In/Out Vars') return else: if self.D_MARK1 in parts: parts[parts.index(self.D_MARK1)]='__RET__' for i in range(inidx): self._invars.append(parts[i].strip()) for i in range(outidx-inidx+1): self._outvars.append(parts[i+inidx].strip()) for line in self._iomatrix[2:-1]: self._log.debug('Parse I/O Matrix:%s'%(line.decode('utf-8'),)) parts=line.split(self.D_FIELDSEP) for markfrom,markto in ((self.D_MARK2,'__CALL__'), (self.D_MARK3,'__NOCALL__'), (self.D_MARK4,'__NOTCARE__'), (self.D_MARK5,'__NOACT__'), (self.D_MARK6,'__ELSE__')): while markfrom in parts: parts[parts.index(markfrom)]=markto self._incond.append([]) self._outcond.append([]) for i in range(inidx): self._incond[-1].append(parts[i].strip()) for i in range(outidx-inidx+1): self._outcond[-1].append(parts[i+inidx].strip())
# -*- coding: utf-8 -*- from logging import getLogger, config, StreamHandler, DEBUG import os from logutil import LogUtil PYTHON_APP_HOME = os.getenv('PYTHON_APP_HOME') LOG_CONFIG_FILE = ['config', 'log_config.json'] logger = getLogger(__name__) log_conf = LogUtil.get_log_conf(os.path.join(PYTHON_APP_HOME, *LOG_CONFIG_FILE)) config.dictConfig(log_conf) handler = StreamHandler() handler.setLevel(DEBUG) logger.setLevel(DEBUG) logger.addHandler(handler) logger.propagate = False class Util: @staticmethod def print(): logger.info('Hello Util.') return 'This is Util'
class TestManager(object): def __init__(self, tokenmanager=cparser.tokenmanager): self._log = LogUtil().logger("TestManager") self._tm = tokenmanager self._dp = driverparser.DriverParser() self._vc = VcProj() def generateProj(self, info): """ @param info:{'driverfile':"xxx", 'srcpath':"xxx", 'tokpath':"xxx", 'autovc':True/False, 'vcproj':"xxx", 'mcdc':True/False, 'tplpath':"xxx", # folder for template files 'vcpath':"xxx", 'testrtpath':"xxx', # used when mcdc==True } """ if self._checkParam(info): self._dp.parse(info["driverfile"]) # only one source file contains target_func? fdict = cparser.getFuncInfo(info["srcpath"], info["tokpath"], self._dp.target_func) fileset = set() for finfo in fdict[self._dp.target_func]: fileset.add(finfo["srcfile"]) if len(fileset) > 1: self._log.error("Found function[%s] in multiple files:%s" % (self._dp.target_func, str(fileset))) return False tokfile = fdict[self._dp.target_func][0]["tokfile"] cinfo = {"FB": (self._dp.target_func,), "DMY": []} # copy driver file copieddir = normpath(info["driverfile"][0 : info["driverfile"].rfind(".")]) if not exists(copieddir) or not isdir(copieddir): makedirs(copieddir) driverfile = split(info["driverfile"])[1] copyfile(info["driverfile"], join(copieddir, driverfile)) # new source filename for the test copiedfile = join(copieddir, self._dp.target_func + ".c") cinfo["OF"] = copiedfile # function rename ? dmyinfo = {} dmyptn = re.compile(r"^dummy(?P<index>\d*)_(?P<name>.+)") for dfunc in self._dp.dummy_func: rret = dmyptn.search(dfunc) if rret: fname = rret.group("name") fidx = rret.group("index") if fidx: cnt = int(fidx) else: cnt = -1 if fname not in dmyinfo: dmyinfo[fname] = [] dmyinfo[fname].append(cnt) for dfunc, cnts in dmyinfo.items(): tmplist = [dfunc] for cnt in sorted(cnts): if cnt >= 0: tmplist.append("dummy%d_%s" % (cnt, dfunc)) if -1 in cnts: tmplist.append("dummy_%s" % (dfunc,)) cinfo["DMY"].append(tuple(tmplist)) # copy source file cparser.copyFile(tokfile, cinfo) # copy other template files tplfile = "common_func_c" outfile = "common_func.c" copyfile(join(info["tplpath"], tplfile), join(copieddir, outfile)) # generate files based on template tplfile = "test_main_c" outfile = "test_main.c" self._genMain(self._dp.driver_func, join(info["tplpath"], tplfile), join(copieddir, outfile)) incstr = "".join(cparser.copyFile(tokfile, {})) tplfile = "drvinputfileread_h" outfile = "drvinputfileread.h" self._genHeader(incstr, join(info["tplpath"], tplfile), join(copieddir, outfile)) # set files to be linked filelist = [ ".\\common_func.c", ".\\test_main.c", ".\\%s.c" % (self._dp.target_func,), ".\\%s" % (driverfile,), ] flag_mcdc = info.get("mcdc", False) if flag_mcdc and "testrtpath" not in info: flag_mcdc = False if flag_mcdc: outfile = "TP.obj" copyfile(join(info["tplpath"], outfile), join(copieddir, outfile)) filelist.append(".\\TP.obj") # generate *.vcproj tplfile = "xxx_vcproj" outfile = "xxx.vcproj" if info.get("autovc", False): searchpath = split(fileset.pop())[0] self._vc.autoVcproj(searchpath, join(info["tplpath"], tplfile), join(copieddir, outfile), filelist) else: self._vc.generateVcproj( info["vcproj"], join(info["tplpath"], tplfile), join(copieddir, outfile), filelist ) if flag_mcdc: tplfile = "xxx_rsp" outfile = "xxx.rsp" self._genRsp( self._vc._incpath, self._vc._compileoption, self._dp.target_func, join(info["tplpath"], tplfile), join(copieddir, outfile), ) self._switchTestRT(True, info["tplpath"], info["vcpath"], info["testrtpath"]) self._compileTestRT(copieddir, info["vcpath"]) self._switchTestRT(False, info["tplpath"], info["vcpath"], info["testrtpath"]) self._buildProj(copieddir, info["vcpath"]) self._runProj(copieddir) return True else: return False def _checkParam(self, info): mustfield = ("driverfile", "srcpath", "tokpath", "tplpath", "vcpath") for mf in mustfield: if mf not in info: return False if not info.get("autovc", False): if "vcproj" not in info: return False return True def _genMain(self, testfunc, tplfile, outfile): fin = open(tplfile, "rU") fout = open(outfile, "w") for line in fin.readlines(): if "#Declaration#" in line: line = "extern void %s(void);\n" % (testfunc,) elif "#Call#" in line: line = "\t%s();\n" % (testfunc,) fout.write(line) fout.close() fin.close() def _genHeader(self, incstr, tplfile, outfile): fin = open(tplfile, "rU") fout = open(outfile, "w") for line in fin.readlines(): if "#include#" in line: line = "%s\n" % (incstr,) fout.write(line) fout.close() fin.close() def _genRsp(self, incstr, costr, funcname, tplfile, outfile): fin = open(tplfile, "rU") lines = fin.read() fin.close() outinc = incstr.replace(";", '" /I "') outinc = '/I "' + outinc + '"' outco = costr.replace(";", '" /D "') outco = '/D "' + outco + '"' lines = lines.decode("utf-16") lines = lines.replace("${inc_path}", outinc) lines = lines.replace("${compile_option}", outco) lines = lines.replace("${target_file}", funcname + ".c") fout = open(outfile, "w") fout.write(lines.encode("utf-16")) fout.close() outdir = split(outfile)[0] outfile2 = "TestRtInstrList.txt" fout = open(join(outdir, outfile2), "w") fout.write("%s\.(c|obj)\n" % (funcname,)) fout.close() outfile2 = "%s.xtp" % (funcname,) fout = open(join(outdir, outfile2), "w") fout.write("%s.fdc\natlout.spt\n" % (funcname,)) fout.close() def _switchTestRT(self, flag, tplpath, vcdir, testrtdir): if flag: folder = "TestRT_ON" else: folder = "TestRT_OFF" folder = join(tplpath, folder) for outfile in ("cl.exe", "link.exe"): copyfile(join(folder, outfile), join(vcdir, outfile)) for outfile in ("tdpGen.log", "tp.ini", "tpcpp.ini"): copyfile(join(folder, outfile), join(testrtdir, outfile)) def _compileTestRT(self, projdir, vcdir): cwd = getcwdu() cmd = [join(vcdir, "cl.exe"), "@xxx.rsp", "/nologo", "/errorReport:prompt", ">log_compile.txt"] chdir(projdir) debugdir = "Debug" if not exists(debugdir) or not isdir(debugdir): makedirs(debugdir) try: subprocess.call(cmd) except subprocess.CalledProcessError as e: self._log.error("Unknown Error During Build With TestRealTime") finally: chdir(cwd) def _buildProj(self, projdir, vcdir): cwd = getcwdu() folderdir = normpath(vcdir) parts = split(folderdir) while not parts[1]: folderdir = parts[0] parts = split(folderdir) folderdir = parts[0] cmd = [join(folderdir, "vcpackages", "vcbuild.exe"), "xxx.vcproj", "debug"] chdir(projdir) try: subprocess.call(cmd) except subprocess.CalledProcessError as e: self._log.error("Unknown Error During Build Project") finally: chdir(cwd) def _runProj(self, projdir): cwd = getcwdu() chdir(projdir) exefile = join("Debug", "xxx.exe") if exists(exefile): cmd = [exefile] try: subprocess.call(cmd) except subprocess.CalledProcessError as e: self._log.error("Unknown Error During Run Exe") else: self._log.error("Compile/Link Error") chdir(cwd)