From 0644c1b5bfcd0db8bb67f59f8fb75af895a2504f Mon Sep 17 00:00:00 2001 From: Mohamed Fawzi Date: Fri, 22 Nov 2002 11:30:05 +0000 Subject: [PATCH] updated prettiviaction and checkin preparation scripts. svn-origin-rev: 994 --- tools/addSynopsis.py | 39 ++--- tools/diffEpsilon.py | 3 +- tools/instantiateTemplates.py | 6 +- tools/normalizeUse.py | 13 +- tools/prepare_ci.py | 259 +++++++++++++++++++++++----------- tools/prettify.py | 136 ++++++++++++++++++ tools/replacer.py | 5 +- 7 files changed, 347 insertions(+), 114 deletions(-) create mode 100755 tools/prettify.py diff --git a/tools/addSynopsis.py b/tools/addSynopsis.py index bdf757d25f..b49afdc8f5 100755 --- a/tools/addSynopsis.py +++ b/tools/addSynopsis.py @@ -64,10 +64,11 @@ def parseInterface(iFile,logFile=sys.stdout): if not result.has_key(stack[0]["kind"]): result[stack[0]["kind"]]={} kind_table=result[stack[0]["kind"]] if kind_table.has_key(stack[0]["name"]): - e=SyntaxWarning("double definition of '"+stack[0]["name"]+"' ignored") - del stack[0] - raise e - kind_table[stack[0]["name"]]=stack[0] # put only the expansion?? + logFile.write("\nWARNING double definition of '"+ + stack[0]["name"]+"' at line "+ + str(lineNr)+", ignoring\n") + else: + kind_table[stack[0]["name"]]=stack[0] # put only the expansion?? else: logFile.write("ignoring group "+str(stack[0])+"\n") del stack[0] @@ -209,6 +210,23 @@ def insertSynopsis(defs,infile,outfile,logFile=sys.stdout): (lineNr,infile.name)) logFile.write(str(w)+"\n") +def addSynopsisToFile(ifile,sfile,outfile,logFile=sys.stdout): + """Adds the synopsis to the file sfile, reading the interface from + ifile, and writing the result to outfile""" + rawDefs=parseInterface(ifile,logFile) + defs={} + #print "rawDefs=",rawDefs + for kind in ["function","subroutine"]: + if rawDefs.has_key(kind): + for key in rawDefs[kind].keys(): + if (defs.has_key(key)): + raise Exception("duplicate name") + else: + defs[key]=rawDefs[kind][key] + #print "def=",defs + insertSynopsis(defs,sfile,outfile,logFile) + + def addSynopsisInDir(interfaceDir, outDir,filePaths,logFile=sys.stdout): """Add the synopsis to the files in filePaths, reading the interfaces from interfaceDir, and writing them to outDir. @@ -228,18 +246,7 @@ def addSynopsisInDir(interfaceDir, outDir,filePaths,logFile=sys.stdout): ifile = open(interfaceFilePath,'r') sfile = open(sourceFilePath,'r') outfile= open(outFilePath,'w') - rawDefs=parseInterface(ifile,logFile) - defs={} - #print "rawDefs=",rawDefs - for kind in ["function","subroutine"]: - if rawDefs.has_key(kind): - for key in rawDefs[kind].keys(): - if (defs.has_key(key)): - raise Exception("duplicate name") - else: - defs[key]=rawDefs[kind][key] - #print "def=",defs - insertSynopsis(defs,sfile,outfile,logFile) + addSynopsisToFile(ifile,sfile,outfile,logFile=logFile) fileMapping[sfile]=outfile except: import sys, traceback diff --git a/tools/diffEpsilon.py b/tools/diffEpsilon.py index 6af56a068b..9f808eaece 100644 --- a/tools/diffEpsilon.py +++ b/tools/diffEpsilon.py @@ -28,11 +28,12 @@ def diffEpsilon(str1, str2,incomparable_val=1): def getCoreLine(file,lineNr): import re - bannerRe=re.compile("([ *]+PROGRAM | CP2K\\| Input file name| CP2K\\| Program compiled)") + bannerRe=re.compile(r"([ *]+PROGRAM | CP2K\| | IO\| | +[a-zA-Z_\.0-9@]* +has created process number| *$)") timingRe=re.compile(" *- +T I M I N G +- *$") scfLineRe=re.compile(" *([0-9]+) +([a-zA-Z]+) +([-+]?[0-9]*\\.?[0-9]+[EedD]?[-+]?[0-9]*) +([-+]?[0-9]*\\.?[0-9]+[EedD]?[-+]?[0-9]*) +([-+]?[0-9]*\\.?[0-9]+[EedD]?[-+]?[0-9]*) +([-+]?[0-9]*\\.?[0-9]+[EedD]?[+-]?[0-9]*) *$") line=file.readline() while bannerRe.match(line): + if not line: return (line,lineNr) lineNr=lineNr+1 line=file.readline() if scfLineRe.match(line): diff --git a/tools/instantiateTemplates.py b/tools/instantiateTemplates.py index 3925b3530d..f01b21a850 100755 --- a/tools/instantiateTemplates.py +++ b/tools/instantiateTemplates.py @@ -33,8 +33,10 @@ def instantiateTemplate(infile,outfile,subs,logFile=sys.stdout): if not subs.has_key(arg): logFile.write("ERROR: missing required argument:"+arg+"\n") outfile.write("! ERROR argument '"+arg+"' missing\n") - for arg in subs.keys(): - sost=subs[arg].split("\n") + kList=subs.keys() + kList.sort() + for arg in kList: + sost=subs[arg].split("\n") if (len(sost)>1): outfile.write('! '+arg+' = \n') outfile.write('! "'+sost[0]) diff --git a/tools/normalizeUse.py b/tools/normalizeUse.py index d53b586081..70409ddeb0 100755 --- a/tools/normalizeUse.py +++ b/tools/normalizeUse.py @@ -104,13 +104,13 @@ def writeUseLong(modules,outFile): outFile.write(",&\n"+string.ljust("",45)+m['renames'][i]) if m.has_key('comments') and m['comments']: comments=m['comments'].splitlines() - outfile.write("&") + outFile.write("&") for i in range(0,len(comments)-1): - outfile.write("\n&"+comments[i]) - outfile.write("\n"+comments[-1]) - outfile.write("\n") + outFile.write("\n&"+comments[i]) + outFile.write("\n"+comments[-1]) + outFile.write("\n") -def rewriteUse(inFile,outFile): +def rewriteUse(inFile,outFile,logFile=sys.stdout): """rewrites the use statements of in file to outFile. It sorts them and removes the repetitions.""" import os.path @@ -134,8 +134,7 @@ def rewriteUse(inFile,outFile): writeUseLong(modules,outFile) outFile.write(line) except: - import sys, traceback - logFile=sys.stdout + import traceback logFile.write('-'*60+"\n") traceback.print_exc(file=logFile) logFile.write('-'*60+"\n") diff --git a/tools/prepare_ci.py b/tools/prepare_ci.py index 24bb7741b6..4637bdb411 100755 --- a/tools/prepare_ci.py +++ b/tools/prepare_ci.py @@ -1,14 +1,11 @@ #! /usr/bin/env python # prepares cp2k for checkin -import sys +import sys, re, os, os.path, commands, time from sys import argv -import os from os.path import join -import commands -import addSynopsis # .addSynopsisInDir -import instantiateTemplates # .evaluateInstantiationFile -import time +import prettify +import instantiateTemplates import diffEpsilon def buildCp2k(cp2kRoot,buildType="sopt",logFilePath=None,clean=None): @@ -22,18 +19,43 @@ def buildCp2k(cp2kRoot,buildType="sopt",logFilePath=None,clean=None): os.chdir(join(cp2kRoot,"makefiles")) if os.access("/usr/bin/gnumake",os.X_OK): makeCmd="/usr/bin/gnumake" else: makeCmd="gmake" - pipe=os.popen("{ { "+makeCmd+" "+buildType+"; } 2>&1 ; } >>"+logFilePath) - logFile=open(logFilePath,'a') + pipe=os.popen("{ { "+makeCmd+" VERSION="+buildType+"; } 2>&1 ; } >>"+logFilePath) if (pipe.close()): + logFile=open(logFilePath,'a') logFile.write("\n+++ ERROR, build "+buildType+" FAILED! +++\n") logFile.close() return None else: + logFile=open(logFilePath,'a') logFile.write("\n+++ build "+buildType+" SUCESSFULLY! +++\n") logFile.close() return 1 +# read directives +directives={"normalize-use":1,"upcase-keywords":1,"clean":1, + "replace":0,"synopsis":1,"prettify-cvs":0,"popt":0} +directiveRe=re.compile(r"--(no-)?(normalize-use|upcase-keywords|"+ + r"replace|synopsis|prettify-cvs|clean|popt)$") +descStr=("usage:"+sys.argv[0]+""" + [--[no-]normalize-use] [--[no-]upcase-keywords] [--[no-]replace] + [--help] [--[no-]synopsis] [--[no-]prettify-cvs] [--[no-]clean] + + Prepares for checkin the source. + defaults="""+str(directives) + ) +if "--help" in sys.argv[1:]: + print descStr + sys.exit(0) +for directive in sys.argv[1:]: + m=directiveRe.match(directive) + if m: + directives[m.groups()[1]]=not m.groups()[0] + else: + print " ** ERROR **\nUnknown argument",directive + print descStr + sys.exit(-1) + cp2kRoot=os.path.abspath(os.path.join(os.path.dirname(argv[0]),"..")) logDirPath=join(cp2kRoot,"test-"+ commands.getoutput(join(cp2kRoot,"tools","get_arch_code"))+ @@ -45,13 +67,6 @@ outDir= join(cp2kRoot,"src","outDir") print "logDirectory: "+logDirPath print "main log: "+mainLog.name -mainLog.write("===== cleaning forpar =====\n") -os.chdir(join(cp2kRoot,"tools")) -if os.access("/usr/bin/gnumake",os.X_OK): makeCmd="/usr/bin/gnumake" -else: makeCmd="gmake" -os.popen("{ { "+makeCmd+" clean ; } 2>&1 ; } >/dev/null").close() -os.popen("{ { "+makeCmd+" ; } 2>&1 ; } >/dev/null").close() - mainLog.write("===== instantiating templates =====\n") mainLog.flush() import glob @@ -77,110 +92,182 @@ if not buildCp2k(cp2kRoot,"sdbg",join(logDirPath,"cp2kBuildSdbg.log")): else: mainLog.write("+++ build SUCESSFULL! +++\n") -# add synopsis -mainLog.write("====== updating synopsis ======\n") +# prettifyes files +mainLog.write("====== prettifying files ======\n") mainLog.flush() -logFile=open(os.path.join(logDirPath,"addSynopsis.log"),'w') +logFile=open(os.path.join(logDirPath,"prettify.log"),'w') buildDir= join(cp2kRoot,"obj", commands.getoutput(join(cp2kRoot,"tools","get_arch_code")), "sdbg") -outDir=join(logDirPath,"synopsisDir") +outDir=join(logDirPath,"prettifyDir") if os.access(outDir,os.W_OK): commands.getoutput('rm -rf "'+outDir+'"') os.mkdir(outDir) -filesToSyn2=(glob.glob(os.path.join(cp2kRoot,"src","cp_*.F"))+ +filesToPret2=(glob.glob(os.path.join(cp2kRoot,"src","cp_*.F"))+ glob.glob(os.path.join(cp2kRoot,"src","pao_*.F"))+ glob.glob(os.path.join(templateDir,"*.F"))) -filesToSyn=templateInstances -baseNames=map(os.path.basename,filesToSyn) -for fileToSyn in filesToSyn2: - if not os.path.basename(fileToSyn) in baseNames: - filesToSyn.append(fileToSyn) -addSynopsis.addSynopsisInDir(buildDir,outDir,filesToSyn,logFile) +filesToPret=templateInstances +baseNames=map(os.path.basename,filesToPret) +for fileToPret in filesToPret2: + if not os.path.basename(fileToPret) in baseNames: + filesToPret.append(fileToPret) +# if requested adds cvs modified files to files to prettify +if "--prettify-cvs" in sys.argv[1:]: + mainLog.write("+ adding cvs modified files to the files to prettify\n") + mainLog.flush() + os.chdir(os.path.join(cp2kRoot,"src")) + filesC=commands.getoutput("cvs -n update") + shouldUpdate=0 + filesToPret2=[] + conflicts=0 + conflictsDir=os.path.join(outDir,"conflicts") + fileCRe=re.compile(r"([ACRMUP]?) ([a-zA-Z_\.\-]+)$") + + for line in filesC.splitlines(): + m=fileCRe.match(line) + if m: + if m.groups()[0] in ["A","M","C"]: + filesToPret2.append(os.path.join(cp2kRoot,"src",m.groups()[1])) + if m.groups()[0]=="C": + conflicts=conflicts+1 + if not os.path.isdir(conflictsDir): os.mkdir(conflictsDir) + shutil.copyfile(m.groups()[1], + os.path.join(conflictsDir,m.groups()[1])) + logFile.write(" copied "+m.groups()[1]+" to "+ + conflictsDir+"\n") + if m.groups()[0] in ["U","P","C"]: + shouldUpdate=1 -# check synopsis diffs + baseNames=map(os.path.basename,filesToPret) + for fileToPret in filesToPret2: + if not os.path.basename(fileToPret) in baseNames: + filesToPret.append(fileToPret) + + logFile.write("cvs modified file to prettify:\n"+str(filesToPret2)+"\n") + if shouldUpdate: + mainLog.write("++ WARNING cvs update is needed\n") + mainLog.write("++ there were "+str(conflicts)+" conflicts\n") + if conflicts: + mainLog.write("++ consider restoring the original files from\n"+ + "++ "+conflictsDir+"\n") + mainLog.flush() + +# start prettyfication +logFile.write("Files to prettify:\n"+str(filesToPret)+"\n") +logFile.write("\nStarting prettification\n") +logFile.flush() +mainLog.write("+ starting prettify process\n") +mainLog.flush() + +if not directives["synopsis"]: buildDir=None +errors=0 +for fileP in filesToPret: + try: + logFile.write("\n+ processing file '"+os.path.basename(fileP)+"'\n") + infile=open(fileP,"r") + outfile=open(os.path.join(outDir,os.path.basename(fileP)),"w") + prettify.prettifyFile(infile,outfile, + normalize_use=directives["normalize-use"], + upcase_keywords=directives["upcase-keywords"], + interfaces_dir=buildDir, + replace=directives["replace"], + logFile=logFile) + infile.close() + outfile.close() + except: + logFile.write("\n** ERROR prettifying the file "+fileP+"\n") + import traceback + logFile.write('-'*60+"\n") + traceback.print_exc(file=logFile) + logFile.write('-'*60+"\n") + logFile.flush() + errors=errors+1 +os.mkdir(os.path.join(outDir,"orig")) os.chdir(outDir) -filesSyn=glob.glob("*.F") -for fileSyn in filesSyn: - if not os.path.exists(join(cp2kRoot,"src",fileSyn)): - os.rename(fileSyn,join(cp2kRoot,"src",fileSyn)) - diffs='' - else: - origFilePath=join(cp2kRoot,"src",fileSyn) - diffs=commands.getoutput("diff "+fileSyn+" "+ - origFilePath) - if diffs!='' and fileSyn in baseNames: - origFilePath=templateInstances[baseNames.index(fileSyn)] - diffs=commands.getoutput("diff "+fileSyn+" "+ - origFilePath) - if diffs=='': - diffOk=None - else: - diffOk=1 - import re - commentOrHeader = re.compile("([0-9]+,*[0-9]*[adc][0-9]+,*[0-9]*$|[><] *|[><] *!| *$|---$)") - for line in diffs.splitlines(): - if not commentOrHeader.match(line): - mainLog.write("+ WARNING checking synopsis for "+fileSyn+ - " found the unaccetable line.\n line='"+line+ - "' addSynopsis not performed\n") - diffOk=None - break - if diffOk: - os.rename(fileSyn,join(cp2kRoot,"src",os.path.basename(fileSyn))) -mainLog.write(" addSynopsis logFile in '%s'\n"%os.path.basename(logFile.name)) -logFile.close() +for fileP in os.listdir(outDir): + try: + if os.path.isfile(fileP) and fileP[-4:]!=".err": + logFile.write("moving of file "+fileP) + origF=os.path.join(cp2kRoot,"src",os.path.basename(fileP)) + if os.path.isfile(origF): + if commands.getoutput('diff "'+origF+'" "'+fileP+'"'): + os.rename(origF,os.path.join(outDir,"orig", + os.path.basename(fileP))) + os.rename(fileP,origF) + else: + logFile.write(" NOT") + else: + os.rename(fileP,origF) + logFile.write(" done\n") + except: + logFile.write("\n** ERROR moving the file "+ + os.path.basename(fileP)+"\n") + import traceback + logFile.write('-'*60+"\n") + traceback.print_exc(file=logFile) + logFile.write('-'*60+"\n") + logFile.flush() + errors=errors+1 -# clean? compile -shouldClean=not "-noclean" in sys.argv[1:] -mainLog.write("====== clean="+`shouldClean`+" compile cp2k sopt ======\n") +logFile.write(" ***** prettification finished *****\n") +logFile.write("+ there were "+str(errors)+" errors\n") +logFile.close() +mainLog.write("+ there were "+str(errors)+" errors while prettifying\n") +mainLog.write(" prettification logFile in '%s'\n"%os.path.basename(logFile.name)) +mainLog.flush() +# os.rename returns before the operation is complete, so wait a little +# (use command.getoutput intread of os.rename?) +time.sleep(3.0) + +# clean? compile sopt +mainLog.write("====== clean="+str(directives["clean"])+ + " compile cp2k sopt ======\n") mainLog.flush() mainLog.write(" compilation logFile in 'cp2kBuildSopt.log'\n") if not buildCp2k(cp2kRoot,"sopt",join(logDirPath,"cp2kBuildSopt.log"), - clean=shouldClean): + clean=directives["clean"]): mainLog.write("+++ ERROR, build FAILED! +++\n") else: mainLog.write("+++ build SUCESSFULL! +++\n") +mainLog.flush() + +# clean? compile popt +if directives["popt"]: + mainLog.write("====== clean="+directives["clean"]+ + " compile cp2k popt ======\n") + mainLog.flush() + mainLog.write(" compilation logFile in 'cp2kBuildPopt.log'\n") + if not buildCp2k(cp2kRoot,"popt",join(logDirPath,"cp2kBuildPopt.log"), + clean=directives["clean"]): + mainLog.write("+++ ERROR, build FAILED! +++\n") + else: + mainLog.write("+++ build SUCESSFULL! +++\n") # do tests +mainLog.write("====== H2O test ======\n") +mainLog.flush() exePath= join(cp2kRoot,"exe", commands.getoutput(join(cp2kRoot,"tools","get_arch_code")), "cp2k.sopt") -log1Path=join(logDirPath,"Ar.out") +log1Path=join(logDirPath,"H2O.out") os.chdir(join(cp2kRoot,"tests","QS")) -commands.getoutput("{ { "+exePath+" "+join(cp2kRoot,"tests","QS","Ar.inp")+ +commands.getoutput("{ { "+exePath+" "+join(cp2kRoot,"tests","QS","H2O.inp")+ " ; } 2>&1 ; } > "+log1Path) -logFile=open(os.path.join(logDirPath,"Ar.diffs"),'w') -file1=open(join(cp2kRoot,"tests","QS","Ar.out"),'r') +logFile=open(os.path.join(logDirPath,"H2O.diffs"),'w') +file1=open(join(cp2kRoot,"tests","QS","H2O.out"),'r') file2=open(log1Path) diffVal=diffEpsilon.compareCp2kOutput(file1,file2, 0.0001,logFile) file1.close(); file2.close() logFile.write("totalDiff="+`diffVal`+"\n") if diffVal>0.0001: - mainLog.write("+++ ERROR, Argon test failed +++\n diff="+`diffVal`+ - " more info in Argon.diffs\n") + mainLog.write("+++ ERROR, H2O test failed +++\n diff="+`diffVal`+ + " more info in H2O.diffs\n") else: - mainLog.write("+++ Argon test SUCESSFULL (diff="+`diffVal`+")! +++\n") + mainLog.write("+++ H2O test SUCESSFULL (diff="+`diffVal`+")! +++\n") logFile.close() - -# cvs -if "-cvs" in sys.argv[1:]: - os.chdir(cp2kRoot) - # cvs -n update - mainLog.write("====== cvs -n update ======\n") - mainLog.flush() - os.popen('{ { cvs -n update; } 2>&1 ; } >"%s"'% - os.path.join(logDirPath,"cvsUpdate.log")) - mainLog.write(" cvs update log in 'cvsUpdate.log'\n") - - # cvs diff - mainLog.write("====== cvs diff ======\n") - mainLog.flush() - os.popen('{ { cvs diff; } 2>&1 ; } >"%s"'% - os.path.join(logDirPath,"cvsDiff.log")) - mainLog.write(" cvs diff log in 'cvsDiff.log'\n") - mainLog.close() + + diff --git a/tools/prettify.py b/tools/prettify.py new file mode 100755 index 0000000000..1779d1c51a --- /dev/null +++ b/tools/prettify.py @@ -0,0 +1,136 @@ +#!/usr/bin/env python + +import sys +import re +import os, os.path +import normalizeUse +import replacer +import addSynopsis +from sys import argv + +operatorsStr=r"\.(?:and|eqv?|false|g[et]|l[et]|n(?:e(?:|qv)|ot)|or|true)\." + +keywordsStr="(?:a(?:llocat(?:able|e)|ssign(?:|ment))|b(?:ackspace|lock)|c(?:a(?:ll|se)|haracter|lose|o(?:m(?:mon|plex)|nt(?:ains|inue))|ycle)|d(?:ata|eallocate|imension|o(?:|uble))|e(?:lse(?:|if|where)|n(?:d(?:|do|file|if)|try)|quivalence|x(?:it|ternal))|f(?:or(?:all|mat)|unction)|goto|i(?:f|mplicit|n(?:clude|quire|t(?:e(?:ger|nt|rface)|rinsic)))|logical|module|n(?:amelist|one|ullify)|o(?:nly|p(?:en|erator|tional))|p(?:a(?:rameter|use)|ointer|r(?:ecision|i(?:nt|vate)|o(?:cedure|gram))|ublic)|re(?:a[dl]|cursive|sult|turn|wind)|s(?:ave|e(?:lect|quence)|top|ubroutine)|t(?:arget|hen|ype)|use|w(?:h(?:ere|ile)|rite))" + +intrinsic_procStr=r"(?:a(?:bs|c(?:har|os)|djust[lr]|i(?:mag|nt)|ll(?:|ocated)|n(?:int|y)|s(?:in|sociated)|tan2?)|b(?:it_size|test)|c(?:eiling|har|mplx|o(?:njg|sh?|unt)|shift)|d(?:ate_and_time|ble|i(?:gits|m)|ot_product|prod)|e(?:oshift|psilon|xp(?:|onent))|f(?:loor|raction)|huge|i(?:a(?:char|nd)|b(?:clr|its|set)|char|eor|n(?:dex|t)|or|shftc?)|kind|l(?:bound|en(?:|_trim)|g[et]|l[et]|og(?:|10|ical))|m(?:a(?:tmul|x(?:|exponent|loc|val))|erge|in(?:|exponent|loc|val)|od(?:|ulo)|vbits)|n(?:earest|int|ot)|p(?:ack|r(?:e(?:cision|sent)|oduct))|r(?:a(?:dix|n(?:dom_(?:number|seed)|ge))|e(?:peat|shape)|rspacing)|s(?:ca(?:le|n)|e(?:lected_(?:int_kind|real_kind)|t_exponent)|hape|i(?:gn|nh?|ze)|p(?:acing|read)|qrt|um|ystem_clock)|t(?:anh?|iny|r(?:ans(?:fer|pose)|im))|u(?:bound|npack)|verify)(?= *\()" + +toUpcaseRe=re.compile("(?"+operatorsStr+ + "|"+ keywordsStr +"|"+ intrinsic_procStr + + ")(?![A-Za-z0-9_%])",flags=re.IGNORECASE) + +def upcaseStringKeywords(line): + """Upcases the fortran keywords, operators and intrinsic routines + in line""" + endComment=line.find("!") + if endComment<0: endComment=len(line) + return toUpcaseRe.sub(lambda match: match.group("toUpcase").upper(), + line[:endComment])+line[endComment:] + +def upcaseKeywords(infile,outfile,logFile=sys.stdout): + """Writes infile to outfile with all the fortran keywords upcased""" + while 1: + line=infile.readline() + if not line: break + outfile.write(upcaseStringKeywords(line)) + +def prettifyFile(infile,outfile,normalize_use=1, upcase_keywords=1, + interfaces_dir=None,replace=None,logFile=sys.stdout): + """prettifyes the fortran source in infile into outfile + if normalize_use normalizes the use statements (defaults to true) + if upcase_keywords upcases the keywords (defaults to true) + if interfaces_dir is defined (and contains the directory with the + interfaces) updates the synopsis + if replace does the replacements contained in replacer.py (defaults + to false)""" + ifile=infile + try: + if normalize_use: + tmpfile=os.tmpfile() + normalizeUse.rewriteUse(ifile,tmpfile,logFile) + tmpfile.seek(0) + ifile=tmpfile + if replace: + tmpfile=os.tmpfile() + replacer.replaceWords(ifile,tmpfile,logFile) + tmpfile.seek(0) + ifile=tmpfile + if upcase_keywords: + tmpfile=os.tmpfile() + upcaseKeywords(ifile,tmpfile,logFile) + tmpfile.seek(0) + ifile=tmpfile + if interfaces_dir: + fileName=os.path.basename(infile.name) + fileName=fileName[:fileName.rfind(".")] + try: + interfaceFile=open(os.path.join(interfaces_dir, + fileName+".int"),"r") + except: + logFile.write("error opening file "+ + os.path.join(interfaces_dir, + fileName+".int")+"\n") + logFile.write("skipping addSynopsis step for "+fileName+"\n") + interfaceFile=None + if interfaceFile: + tmpfile=os.tmpfile() + addSynopsis.addSynopsisToFile(interfaceFile,ifile, + tmpfile,logFile=logFile) + tmpfile.seek(0) + ifile=tmpfile + while 1: + line=ifile.readline() + if not line: break + outfile.write(line) + except: + logFile.write("error processing file '"+infile.name+"'\n") + outfile.close() + os.rename(outfile.name,outfile.name+".err") + raise + +if __name__ == '__main__': + usageDesc=("usage:\n"+sys.argv[0]+ """ + [--[no-]upcase] [--[no-]normalize-use] [--[no-]replace] + [--interface-dir=~/cp2k/obj/platform/target] + out_dir file1 [file2 ...] + + Writes file1,... to outdir after performing on them upcase of the + fortran keywords, and normalizion the use statements. + If the interface direcory is given updates also the synopsis. + If requested the replacements performed by the replacer.py script + are also preformed""") + argStart=1 + normalize_use=1 + upcase_keywords=1, + interfaces_dir=None + replace=None + for i in range(1,len(sys.argv)): + if (sys.argv[i][:2]!="--"): break + argStart=i+1 + m=re.match(r"--(no-)?(normalize-use|upcase)",sys.argv[i]) + if m: + if (m.groups()[1]=="upcase"): + upcase_keywords=not m.groups()[0] + else: + normalize_use=not m.groups()[0] + else: + m=re.match(r"--interface-dir=(.*)",sys.argv[i]) + if m: + interfaces_dir=os.path.expanduser(m.groups()[0]) + else: + print "ignoring unknown directive",sys.argv[i] + if len(sys.argv)-argStart<2: + print usageDesc + else: + outDir=sys.argv[argStart] + if not os.path.isdir(outDir): + print "out_dir must be a directory" + print usageDesc + else: + for fileName in sys.argv[argStart+1:]: + infile=open(fileName,'r') + outfile=open(os.path.join(outDir, + os.path.basename(fileName)),'w') + prettifyFile(infile,outfile,normalize_use=normalize_use, + upcase_keywords=upcase_keywords, + interfaces_dir=interfaces_dir, + replace=replace) diff --git a/tools/replacer.py b/tools/replacer.py index 6368858a2a..b9c5345050 100755 --- a/tools/replacer.py +++ b/tools/replacer.py @@ -14,10 +14,11 @@ repl={ 'cp_full_matrix':'cp_fm_types' } -specialRepl={} +specialRepl=None # {re.compile(r"%blacs_matrix(\W)",flags=re.IGNORECASE):r"%matrix\1"} -def replaceWords(infile,outfile,replacements,specialReplacements=None, +def replaceWords(infile,outfile,replacements=repl, + specialReplacements=specialRepl, logFile=sys.stdout): """Replaces the words in infile writing the output to outfile.