def read(self,fh): """reads the next line fh - filehandle to read from Return value: False if the end of the file was reached. True otherwise""" if not self.goOn: return False try: self.line=fh.readline() if PY3: if type(self.line) is bytes: self.line=self.line.decode() self.bytes+=len(self.line) except KeyboardInterrupt: e=sys.exc_info()[1] foamLogger().warning("Keyboard Interrupt") print_(" Interrupted by the Keyboard") self.wasInterupted=True self.goOn=False self.line="" return False if len(self.line)>0: status=True else: status=False self.line=self.line.strip() return status # Should work with Python3 and Python2
def prepareHooks(self): """Prepare the hooks and output additional info if wanted""" self.hookmessage("Preparing hooks") if self.opts.disableAllHooks: self.hookmessage("Disabling all hooks") self.opts.runPreHook=False self.opts.runPostHook=False if self.opts.listHooks: print_("Hooks to execute before run") print_("---------------------------") self.dumpHooks(self.getHooksWithPrefix("preRunHook")) print_() print_("Hooks to execute after run") print_("--------------------------") self.dumpHooks(self.getHooksWithPrefix("postRunHook")) self.preHookInstances={} self.postHookInstances={} self.hookmessage("Creating pre-hooks") if self.opts.runPreHook: self.checkAndCreateHookInstances( self.preHookInstances, "preRunHook" ) self.hookmessage("Creating post-hooks") if self.opts.runPostHook: self.checkAndCreateHookInstances( self.postHookInstances, "postRunHook" )
def setStatus(self,text): print_(text) if not self.status: self.status=QtGui.QStatusBar(self) self.layout().addWidget(self.status) self.status.showMessage(text)
def compare(self, src, dst, depth, name): if type(src) != type(dst): print_( f.diff + ">><<", name, ": Types differ" + f.reset + "\n+" + f.src + ">>Source:" + f.reset + "\n", makeString(src), "\n" + f.dst + "<<Destination:" + f.reset + "\n", makeString(dst) + f.reset) self.pling = True elif type(src) in [tuple, list, TupleProxy]: self.compareIterable(src, dst, depth, name) elif isinstance( src, (str, float, bool, BoolProxy) + integer_types) or src == None: self.comparePrimitive(src, dst, depth, name) elif src.__class__ in [Dimension, Tensor, SymmTensor, Vector]: self.comparePrimitive(src, dst, depth, name) elif src.__class__ == Field: self.compareField(src, dst, depth, name) elif type(src) in [DictProxy, dict]: self.compareDict(src, dst, depth, name) else: warning("Type of", name, "=", type(src), "unknown") if self.opts.debug: try: print_("Class of", name, "=", src.__class__, "unknown") except: pass
def setCase(self,case): """Rewrite the state-file so that it uses another case than the one predefined in the state-file :param case: The path to the new case-file""" reader=self.getReader() caseDir=path.realpath(path.dirname(case)) origPath=path.dirname(reader.getProperty("FileName")) print_("Setting Foam-file",origPath,"to",caseDir) typ=reader.data.getAttribute("type") if typ=="PV3FoamReader": reader.setProperty("FileName",case) elif typ=="OpenFOAMReader": oldFile=reader.getProperty("FileName") fName=path.basename(oldFile) newFile=path.join(path.dirname(case),fName) if not path.exists(newFile): open(newFile,"w") reader.setProperty("FileName",newFile) else: error("Reader type",typ,"not implemented for state-file rewritting") for r in self.getProxy(".+Reader",regexp=True): if r.id()!=reader.id(): oldPath=r.getProperty("FileName") if oldPath is None: continue for p in [oldPath,path.realpath(oldPath)]: if p.find(origPath)==0: newPath=path.join(caseDir,p[len(origPath)+1:]) print_("Rewriting path",oldPath,"to",newPath) r.setProperty("FileName",newPath)
def processABoundaryFile(self,func,targetDir): """ :param func: the function that transforms the actual boundary file """ boundaryPath=path.join(targetDir,"polyMesh","boundary") try: boundary=ParsedParameterFile(boundaryPath, debug=False, boundaryDict=True, treatBinaryAsASCII=True) except IOError: self.warning("Problem opening boundary file",boundaryPath) return False bnd=boundary.content if type(bnd)!=list: self.warning("Problem with boundary file (not a list)") return False boundary.content=func(bnd,targetDir) if boundary.content: if self.opts.test: print_(boundary) else: boundary.writeFile() # self.addToCaseLog(boundaryPath) return True else: self.warning(boundaryPath,"unchanged") return False
def run(self): dest = self.parser.getArgs()[0] if path.exists(dest) and self.opts.create: self.error("database-file", dest, "exists already.") sources = self.parser.getArgs()[1:] db = RunDatabase(dest, create=self.opts.create, verbose=self.opts.verbose) for s in sources: if self.opts.verbose: print_("\nProcessing file", s) try: data = pickle.Unpickler(open(s, "rb")).load() except (IOError, pickle.UnpicklingError): e = sys.exc_info()[ 1] # Needed because python 2.5 does not support 'as e' if self.opts.skipMissing: self.warning("File", s, "missing") continue else: self.error("There was a problem reading file", s, ":", e) try: db.add(data, update_existing=self.opts.update) except KeyError as e: print("Data from file {} already in database: {}".format(s, e))
def readPickledData(self): if "inputData" in self: if self.opts.pickledFileRead: self.error( "--pickled-file specified, when input data was provided via the Python-API" ) data = self["inputData"] else: if not self.opts.pickledFileRead: if sys.stdin.isatty(): self.error("The option --pickled-file has to be set") else: self.opts.pickledFileRead = "stdin" if self.opts.pickledFileRead == "stdin": pick = pickle.Unpickler(sys.stdin) else: pick = pickle.Unpickler(open(self.opts.pickledFileRead)) data = pick.load() del pick if self.opts.printStdout: try: print_(data["stdout"]) except KeyError: print_("<No stdout in data>") if self.opts.printPickledData: import pprint printer = pprint.PrettyPrinter() printer.pprint(data) return data
def printPhase(self,*args): out=" ".join([str(a) for a in args]) print_() print_("="*len(out)) print_(out) print_("="*len(out)) print_()
def readPickledData(self): if "inputData" in self: if self.opts.pickledFileRead: self.error("--pickled-file specified, when input data was provided via the Python-API") data=self["inputData"] else: if not self.opts.pickledFileRead: if sys.stdin.isatty(): self.error("The option --pickled-file has to be set") else: self.opts.pickledFileRead="stdin" if self.opts.pickledFileRead=="stdin": pick=pickle.Unpickler(sys.stdin) else: pick=pickle.Unpickler(open(self.opts.pickledFileRead,"rb")) data=pick.load() del pick if self.opts.printStdout: try: print_(data["stdout"]) except KeyError: print_("<No stdout in data>") if self.opts.printPickledData: import pprint printer=pprint.PrettyPrinter() printer.pprint(data) return data
def run(self): fName=self.parser.getArgs()[0] bName=self.parser.getArgs()[1] tName=self.parser.getArgs()[2] boundaryPath=path.join(".",fName,self.opts.time,self.opts.region,"polyMesh","boundary") try: boundary=ParsedParameterFile(boundaryPath,debug=False,boundaryDict=True) except IOError: self.error("Problem opening boundary file",boundaryPath) bnd=boundary.content if type(bnd)!=list: self.error("Problem with boundary file (not a list)") found=False for val in bnd: if val==bName: found=True elif found: val["type"]=tName break if not found: self.error("Boundary",bName,"not found in",bnd[::2]) if self.opts.test: print_(boundary) else: boundary.writeFile() self.addToCaseLog(fName)
def execute(self, para, log): argv = [self.utility, ".", para['case']] + self.options.split() print_(" Executing and analyzing", " ".join(argv), end=" ") sys.stdout.flush() run = UtilityRunner(argv, silent=True, lam=Command.parallel, logname="_".join(argv)) run.add("data", self.regexp) run.start() data = run.analyzer.getData("data") result = None if data != None: result = [] for a in data: result.append(a) if result == None: print_("no data", end=" ") else: print_(result, end=" ") if run.runOK(): print_() else: print_("---> there was a problem") return run.runOK(), result
def execute(self, cmd, debug=False, workdir=None, echo=None, getReturnCode=False): """Execute the command cmd. If specified change the working directory Currently no error-handling is done :return: A list with all the output-lines of the execution""" if debug: print_(cmd) oldDir = None if workdir: oldDir = os.getcwd() os.chdir(workdir) if type(cmd) == list: fpath = cmd[0] else: fpath = cmd.split(" ")[0] # Check if the file is there. Then we assume that this is a script if os.path.exists(fpath): # Script seems to be unexecutable if not os.access(fpath, os.X_OK): error("The script file", fpath, "is not executable") if sys.version_info < (2, 6): raus, rein = popen4(cmd) else: p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True, universal_newlines=True) (rein, raus) = (p.stdin, p.stdout) if echo != None: tmp = [] while p.poll() == None: l = raus.readline() print_(echo, l, end="") tmp.append(l) else: tmp = raus.readlines() # line=raus.readline() # while line!="": # print line # line=raus.readline() if oldDir: os.chdir(oldDir) if getReturnCode: return p.returncode, tmp else: return tmp
def getLinuxMem(thrd): """Reads the Memory usage of a thread on a linux-System :param thrd: the thread object in question""" # print "Timer called" if not thrd.isLinux or thrd.threadPid<0: return mem=0 try: import psutil me=psutil.Process(thrd.threadPid) procs=[me]+me.children(True) try: for p in procs: try: mInfo=p.memory_full_info() mem+=mInfo.uss+mInfo.pss # Unique-memory and proportional # of shared memory. smaller than # RSS except AttributeError: mInfo=p.memory_info() mem+=mInfo.rss except psutil.NoSuchProcess: pass except ImportError: try: t=open('/proc/%d/status' % thrd.threadPid) v=t.read() t.close() # f=open('/tmp/test%dstatus' % thrd.threadPid,'w') # f.write(v) # f.close() i=v.index('VmRSS') tmp=v[i:].split() if len(tmp)>=3: mem=int(tmp[1]) if tmp[2].lower()=='kb': mem*=1024 elif tmp[2].lower()=='mb': mem*=1024*1024 else: mem=-1 except Exception: e = sys.exc_info()[1] # compatible with 2.x and 3.x print_("Getting LinuxMem:",e) mem=-1 if mem>thrd.linuxMaxMem: # print "Setting Memory to: ",mem thrd.linuxMaxMem=mem # print "Restarting Timer" thrd.timer=Timer(thrd.timerTime,getLinuxMem,args=[thrd]) thrd.timer.start()
def store_clicked(b): l=line.value v=varName.value name=self.__getObjectName(self) store=self.__getStorageName() if mode.value=="Time": t=time.value print_("Storing fields at t=",t,"on line",l,"from",directoryName,"in",self.path,"to variable",v) cmdBase="%s.sampleTime('%s','%s','%s')" % (name,directoryName,l,t) if store: sname,sval=store cmd="%s=%s('%s',lambda:%s)" % (v,sname,v,cmdBase) val=sval(v,lambda:self.sampleTime(directoryName,l,t)) else: cmd="%s=%s" % (v,cmdBase) val=self.sampleTime(directoryName,l,t) elif mode.value=="Field": f=field.value print_("Storing fields",f," at all times on line",l,"from",directoryName,"in",self.path,"to variable",v) cmdBase="%s.sampleField('%s','%s','%s')" % (name,directoryName,l,f) if store: sname,sval=store cmd="%s=%s('%s',lambda:%s)" % (v,sname,v,cmdBase) val=sval(v,lambda:self.sampleField(directoryName,l,f)) else: cmd="%s=%s" % (v,cmdBase) val=self.sampleField(directoryName,l,f) else: print "Unknown mode",mode return create_code_cell(cmd,"below") get_ipython().push({v:val}) varName.value=""
def run(self): source=self.parser.getArgs()[0] dest=self.parser.getArgs()[1] if self.opts.noWrite: dest=None db=RunDatabase(source, verbose=self.opts.verbose) selections=[] if self.opts.selection: selections=self.opts.selection dump=db.dumpToCSV(dest, selection=selections, disableRunData=self.opts.disableRunData, pandasFormat=self.opts.usePandasFormat, excel=self.opts.excel) if self.opts.pandas: if dump is None: print_("No data. Seems that pandas is not installed") else: print_("Pandas data:\n",dump) self.setData({ "database" : path.abspath(source), "dump" : dump })
def run(self): source=self.parser.getArgs()[0] dest=self.parser.getArgs()[1] if self.opts.noWrite: dest=None db=RunDatabase(source, verbose=self.opts.verbose) selections=[] if self.opts.selection: selections=self.opts.selection dump=db.dumpToCSV(dest, selection=selections, disableRunData=self.opts.disableRunData, pandasFormat=self.opts.usePandasFormat, excel=self.opts.excel) if self.opts.pandas: if not dump: print_("No data. Seems that pandas is not installed") else: print_("Pandas data:\n",dump) self.setData({ "database" : db , "dump" : dump })
def execute(self,para,log): if self.key in para: return True,para[self.key] else: print_("-----> ",self.key,"not in set of valid keys",list(para.keys())) print_(self.key,"not in set of valid keys of dictionary",para,file=log) return False,None
def execute(self,para,log): val=replaceValues(self.val,para) var=replaceValues(self.var,para) print_(" Setting variable",var,"to",val) environ[var]=val return True,None
def run(self): fName=self.parser.getArgs()[0] destPatches=self.parser.getArgs()[1:] if self.opts.patch==None and self.opts.value==None: self.error("Either a patch or a value must be specified") if self.opts.patch!=None and self.opts.value!=None: self.error("Only a patch or a value can be specified") try: fieldFile=ParsedParameterFile(fName,backup=False) except IOError: e = sys.exc_info()[1] # Needed because python 2.5 does not support 'as e' self.error("Problem with file",fName,":",e) value="" if self.opts.patch: value=fieldFile["boundaryField"][self.opts.patch][self.opts.srckey] else: value="uniform "+self.opts.value for destPatch in destPatches: fieldFile["boundaryField"][destPatch][self.opts.destkey]=value if self.opts.test: print_(str(fieldFile)) else: fieldFile.writeFile()
def __init__(self,port=None): """:param port: The port on which the server should listen""" if port==None: port=config().getint("Metaserver","port") foamLogger("server").info("Starting Server up") self.pid=os.getpid() try: self.webserver = ServerProxy(WEBSERVER_RPCURL) self.servers={} self.dataLock=Lock() self.startupLock=Lock() self.collect() self.checker=MetaChecker(self) self.checker.setDaemon(True) self.checker.start() self._server=ServerBase(('',port),logRequests=False) self._server.register_instance(self) self._server.register_introspection_functions() self._server.serve_forever() # occasional errors with "Broken pipe" except KeyboardInterrupt: foamLogger("server").warning("Keyboard interrupt") except socket.error as reason: foamLogger("server").error("Socket Error: "+str(reason)) print_("Can't start server, Problem with socket: ",reason[1]) except: foamLogger("server").error("Unknown exception "+str(sys.exc_info()[0])) foamLogger("server").error(str(sys.exc_info()[1])) foamLogger("server").error("Traceback: "+str(extract_tb(sys.exc_info()[2])))
def recursiveCompress(self, dirName): if self.verbose > 1: print_("Recursively checking", dirName) if path.isdir(dirName): s = SolutionDirectory(dirName, archive=None, paraviewLink=False, parallel=True) if s.isValid(): try: self.compressCase(dirName) except OSError: e = sys.exc_info()[ 1] # Needed because python 2.5 does not support 'as e' self.warning("Problem processing", dirName, ":", e) return for f in listdir(dirName): name = path.join(dirName, f) try: if path.isdir(name): self.recursiveCompress(name) except OSError: e = sys.exc_info()[ 1] # Needed because python 2.5 does not support 'as e' self.warning("Problem processing", name, ":", e)
def writePicture(self,fName): self.ren.ResetCamera() xMin,xMax,yMin,yMax,zMin,zMax=self.output.GetBounds() # self.axes.SetBounds(self.output.GetBounds()) boundRange=[(xMax-xMin,0), (yMax-yMin,1), (zMax-zMin,2)] boundRange.sort(lambda a,b:cmp(b[0],a[0])) focalPoint=[0.5*(xMax+xMin),0.5*(yMax+yMin),0.5*(zMax+zMin)] position=copy(focalPoint) if self.opts.autoCamera: ratio=max(0.2,boundRange[1][0]/max(boundRange[0][0],1e-10)) self.opts.height=int(self.opts.width*ratio)+70 camOffset=[0,0,0] camOffset[boundRange[2][1]]=boundRange[1][0]*3 up=[0,0,0] up[boundRange[1][1]]=1. else: if self.opts.height==None: self.opts.height=int(self.opts.width/sqrt(2)) offset=self.getVector(self.opts.focalOffset) for i in range(3): focalPoint[i]+=offset[i] camOffset=self.getVector(self.opts.cameraOffset) up=self.getVector(self.opts.upDirection) for i in range(3): position[i]+=camOffset[i] if self.opts.reportCamera: print_("Picture size:",self.opts.width,self.opts.height) print_("Data bounds:",xMin,xMax,yMin,yMax,zMin,zMax) print_("Focal point:",focalPoint) print_("Up-direction:",up) print_("Camera position:",position) self.renWin.SetSize(self.opts.width,self.opts.height) self.barActor.SetDisplayPosition(int(self.opts.width*0.124), 20) self.textActor.SetDisplayPosition(int(self.opts.width*0.124),self.opts.height-30) self.ren.GetActiveCamera().SetFocalPoint(focalPoint) self.ren.GetActiveCamera().SetViewUp(up) self.ren.GetActiveCamera().SetPosition(position) self.ren.GetActiveCamera().Dolly(self.opts.dollyFactor) self.ren.ResetCameraClippingRange() self.renWin.Render() self.renderLarge = vtk.vtkRenderLargeImage() self.renderLarge.SetInput(self.ren) self.renderLarge.SetMagnification(1) self.writer = vtk.vtkPNGWriter() self.writer.SetInputConnection(self.renderLarge.GetOutputPort()) self.writer.SetFileName(fName) self.writer.Write() if self.opts.wait: input("waiting for key")
def float_array(m): """Return the argument as a numpy array of type at least 'Float32'. Leave 'Float64' unchanged, but upcast all other types to 'Float32'. Allow also for the possibility that the argument is a python native type that can be converted to a numpy array using 'numpy.asarray()', but in that case don't worry about downcasting to single-precision float. """ try: # Try Float32 (this will refuse to downcast) return numpy.asarray(m, numpy.float32) except TypeError: # That failure might have been because the input array was # of a wider data type than float32; try to convert to the # largest floating-point type available: # NOTE TBD: I'm not sure float_ is the best data-type for this... try: return numpy.asarray(m, numpy.float_) except TypeError: # TBD: Need better handling of this error! print_("Fatal: array dimensions not equal!") return None
def createMixingPlanePatch(self, patch, patchName): description="""\ Create a default definition for a mixingPlane patch, and replace the current definition """ print_("Replacing definition of patch: ", patchName, ":", patch) newPatch={ 'type' : "mixingPlane", 'nFaces' : patch["nFaces"], 'startFace' : patch["startFace"], 'shadowPatch' : 'unknown', 'coordinateSystem' : { 'name' : 'mixingCS', 'type' : 'cylindrical', 'origin' : '(0 0 0)', 'e1' : '(1 0 0)', 'e3' : '(0 0 1)' }, 'ribbonPatch' : { 'sweepAxis' : 'Theta', 'stackAxis' : 'Z', 'discretisation' : 'bothPatches', } } return newPatch
def shell(self, *args): """Run a command in the case directory and let it directly write to the output @param workingDirectory: change to this directory""" workingDirectory = None if not workingDirectory: workingDirectory = self.caseDir cmd = " ".join([str(a) for a in args]) self.status("Executing", cmd, "in", workingDirectory) oldDir = os.getcwd() os.chdir(workingDirectory) p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) self.status("Output of the command") self.line() print_(p.communicate()[0]) self.line() sts = p.returncode if sts != 0: self.fail("Command", cmd, "ended with status", sts) else: self.status(cmd, "ran OK") os.chdir(oldDir)
def lineHandle(self, line): """Not to be called: calls the analyzer for the current line""" self.analyzer.analyzeLine(line) if self.automaticCustom: # if one of the readers added custom plots add them AFTER # processing the whole line if len(self.automaticCustom) > 0: print_("Adding automatic plots:", ", ".join([e.name for e in self.automaticCustom])) automaticPlots = self.addCustomExpressions( self.automaticCustom, persist=self.persist, start=self.start_, end=self.end, raiseit=self.raiseit, writeFiles=self.writeFiles, splitThres=self.splitThres, gnuplotTerminal=self.gnuplotTerminal, plottingImplementation=self.plottingImplementation) # plots.update(customPlots) self.reset() self.addPlots(automaticPlots) self.automaticCustom = None
def __getitem__(self, key): """Get application data""" try: return self.__appData[key] except KeyError: print_("available keys:", list(self.__appData.keys())) raise
def processABoundaryFile(self,func,targetDir): """ @param func: the function that transforms the actual boundary file """ boundaryPath=path.join(targetDir,self.opts.region,"polyMesh","boundary") try: boundary=ParsedParameterFile(boundaryPath, debug=False, boundaryDict=True, treatBinaryAsASCII=True) except IOError: self.warning("Problem opening boundary file",boundaryPath) return False bnd=boundary.content if type(bnd)!=list: self.warning("Problem with boundary file (not a list)") return False boundary.content=func(bnd,targetDir) if boundary.content: if self.opts.test: print_(boundary) else: boundary.writeFile() # self.addToCaseLog(boundaryPath) return True else: self.warning(boundaryPath,"unchanged") return False
def run(self): fName = self.parser.getArgs()[0] destPatches = self.parser.getArgs()[1:] if self.opts.patch == None and self.opts.value == None: self.error("Either a patch or a value must be specified") if self.opts.patch != None and self.opts.value != None: self.error("Only a patch or a value can be specified") try: fieldFile = ParsedParameterFile(fName, backup=False) except IOError: e = sys.exc_info()[ 1] # Needed because python 2.5 does not support 'as e' self.error("Problem with file", fName, ":", e) value = "" if self.opts.patch: value = fieldFile["boundaryField"][self.opts.patch][ self.opts.srckey] else: value = "uniform " + self.opts.value for destPatch in destPatches: fieldFile["boundaryField"][destPatch][self.opts.destkey] = value if self.opts.test: print_(str(fieldFile)) else: fieldFile.writeFile()
def createMixingPlanePatch(self, patch, patchName): description = """\ Create a default definition for a mixingPlane patch, and replace the current definition """ print_("Replacing definition of patch: ", patchName, ":", patch) newPatch = { 'type': "mixingPlane", 'nFaces': patch["nFaces"], 'startFace': patch["startFace"], 'shadowPatch': 'unknown', 'coordinateSystem': { 'name': 'mixingCS', 'type': 'cylindrical', 'origin': '(0 0 0)', 'e1': '(1 0 0)', 'e3': '(0 0 1)' }, 'ribbonPatch': { 'sweepAxis': 'Theta', 'stackAxis': 'Z', 'discretisation': 'bothPatches', } } return newPatch
def run(self): # print_(path.dirname(self.parser.getArgs()[0])) bmFile = self.parser.getArgs()[0] if not path.exists(bmFile): self.error(bmFile, "not found") outbmFile = self.opts.destination if self.opts.extrude: mesh = BlockMesh2D(bmFile, "EXTRUDE", -abs(self.opts.backvalue), abs(self.opts.frontvalue), abs(self.opts.division)) elif self.opts.rotatex: mesh = BlockMesh2D(bmFile, "ROTATEX", -abs(self.opts.backangle), abs(self.opts.frontangle), abs(self.opts.division)) elif self.opts.rotatey: mesh = BlockMesh2D(bmFile, "ROTATEY", -abs(self.opts.backangle), abs(self.opts.frontangle), abs(self.opts.division)) else: self.error( "No transformation specified: --extrude, --rotate-x or --rotate-y" ) if self.opts.printToStdout: print_(mesh.convert2DBlockMesh()) else: open(outbmFile, "w").write(mesh.convert2DBlockMesh())
def printPhase(self, *args): out = " ".join([str(a) for a in args]) print_() print_("=" * len(out)) print_(out) print_("=" * len(out)) print_()
def shell(self, *args): """Run a command in the case directory and let it directly write to the output @param workingDirectory: change to this directory""" workingDirectory=None if not workingDirectory: workingDirectory=self.caseDir cmd=" ".join([str(a) for a in args]) self.status("Executing",cmd,"in",workingDirectory) oldDir=os.getcwd() os.chdir(workingDirectory) p=subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) self.status("Output of the command") self.line() print_(p.communicate()[0]) self.line() sts=p.returncode if sts!=0: self.fail("Command",cmd,"ended with status",sts) else: self.status(cmd,"ran OK") os.chdir(oldDir)
def lineHandle(self,line): """Not to be called: calls the analyzer for the current line""" self.analyzer.analyzeLine(line) if self.automaticCustom: # if one of the readers added custom plots add them AFTER # processing the whole line if len(self.automaticCustom)>0: print_("Adding automatic plots:",", ".join( [e.name for e in self.automaticCustom])) automaticPlots=self.addCustomExpressions(self.automaticCustom, persist=self.persist, start=self.start_, end=self.end, raiseit=self.raiseit, writeFiles=self.writeFiles, splitThres=self.splitThres, gnuplotTerminal=self.gnuplotTerminal, plottingImplementation=self.plottingImplementation) # plots.update(customPlots) self.reset() self.addPlots(automaticPlots) self.automaticCustom=None
def setStatus(self, text): print_(text) if not self.status: self.status = QtGui.QStatusBar(self) self.layout().addWidget(self.status) self.status.showMessage(text)
def prepareHooks(self): """Prepare the hooks and output additional info if wanted""" self.hookmessage("Preparing hooks") if self.opts.disableAllHooks: self.hookmessage("Disabling all hooks") self.opts.runPreHook = False self.opts.runPostHook = False if self.opts.listHooks: print_("Hooks to execute before run") print_("---------------------------") self.dumpHooks(self.getHooksWithPrefix("preRunHook")) print_() print_("Hooks to execute after run") print_("--------------------------") self.dumpHooks(self.getHooksWithPrefix("postRunHook")) self.preHookInstances = {} self.postHookInstances = {} self.hookmessage("Creating pre-hooks") if self.opts.runPreHook: self.checkAndCreateHookInstances(self.preHookInstances, "preRunHook") self.hookmessage("Creating post-hooks") if self.opts.runPostHook: self.checkAndCreateHookInstances(self.postHookInstances, "postRunHook")
def __init__(self, filename, silent=False, tailLength=1000, sleep=0.1, follow=True): """:param filename: name of the logfile to watch :param silent: if True no output is sent to stdout :param tailLength: number of bytes at the end of the fail that should be output. :param follow: if the end of the file is reached wait for further input Because data is output on a per-line-basis :param sleep: interval to sleep if no line is returned""" self.filename = filename self.silent = silent self.tail = tailLength self.sleep = sleep self.follow = follow self.isTailing = False if not path.exists(self.filename): print_("Error: Logfile ", self.filename, "does not exist") self.reader = LineReader(config().getboolean("SolverOutput", "stripSpaces"))
def __init__(self,basename, parameterfile, variationfile, template=None, **kwargs): self.__parameterfile=parameterfile self.__variationfile=variationfile print_("Parameter file",self.__parameterfile) print_("Variation file",self.__variationfile) data=RunParameterVariation(args=[template, path.join(template,self.__variationfile), "--parameter="+path.join(template,self.__parameterfile), "--list-variations"]).getData() taskID=int(os.environ["SGE_TASK_ID"])-1 if "solver" in data["variations"][taskID]: solver=data["variations"][taskID]["solver"] else: solver=data["fixed"]["solver"] SolverJob.__init__(self,basename,solver, arrayJob=True, template=template, **kwargs)
def run(self): if not self.opts.keepPostprocessing: self.opts.additional.append("postProcessing") for cName in self.parser.getArgs(): if self.checkCase(cName, fatal=self.opts.fatal, verbose=not self.opts.silent): self.addLocalConfig(cName) if self.opts.verbose: print_("Clearing", cName) sol = SolutionDirectory(cName, archive=None, paraviewLink=False) sol.clear( after=self.parser.getOptions().after, processor=self.parser.getOptions().processor, pyfoam=self.parser.getOptions().pyfoam, vtk=self.parser.getOptions().vtk, removeAnalyzed=self.parser.getOptions().removeAnalyzed, keepRegular=self.parser.getOptions().keepRegular, keepParallel=self.parser.getOptions().keepParallel, keepLast=self.parser.getOptions().latest, keepInterval=self.parser.getOptions().keepInterval, clearHistory=self.parser.getOptions().clearHistory, clearParameters=self.parser.getOptions().clearParameters, additional=self.parser.getOptions().additional, functionObjectData=self.parser.getOptions( ).functionObjectData) self.addToCaseLog(cName)
def run(self): if not self.opts.keepPostprocessing: self.opts.additional.append("postProcessing") for cName in self.parser.getArgs(): if self.checkCase(cName,fatal=self.opts.fatal,verbose=not self.opts.silent): self.addLocalConfig(cName) if self.opts.verbose: print_("Clearing",cName) sol=SolutionDirectory(cName,archive=None,paraviewLink=False) sol.clear(after=self.parser.getOptions().after, processor=self.parser.getOptions().processor, pyfoam=self.parser.getOptions().pyfoam, vtk=self.parser.getOptions().vtk, removeAnalyzed=self.parser.getOptions().removeAnalyzed, keepRegular=self.parser.getOptions().keepRegular, keepParallel=self.parser.getOptions().keepParallel, keepLast=self.parser.getOptions().latest, keepInterval=self.parser.getOptions().keepInterval, clearHistory=self.parser.getOptions().clearHistory, clearParameters=self.parser.getOptions().clearParameters, additional=self.parser.getOptions().additional, functionObjectData=self.parser.getOptions().functionObjectData) self.addToCaseLog(cName)
def __call__(self): if self.useSSL: meth = httplib.HTTPSConnection else: meth = httplib.HTTPConnection conn = meth(self.host) parameters = {} for n, val in iteritems(self.parameters): if n in self.templates: template = TemplateFile(content=val, expressionDelimiter="|-", encoding="ascii") try: parameters[n] = str( template.getString(self.runner.getData())) except TemplateRenderError: e = sys.exc_info()[ 1] # Needed because python 2.5 does not support 'as e' error("Template error", e, "while rendering", val) else: parameters[n] = val encoded = urllib.urlencode(parameters) try: conn.request(self.method, self.url, encoded, self.headers) except socket.error: e = sys.exc_info()[ 1] # Needed because python 2.5 does not support 'as e' error("Could not connect to", self.host, ":", e) result = conn.getresponse() print_("\n", self.name, "Result of request:", result.status, result.reason, result.read())
def __getitem__(self,key): """Get application data""" try: return self.__appData[key] except KeyError: print_("available keys:",list(self.__appData.keys())) raise
def run(self): """start the command""" # print "Starting ",self.cmdline self.resStart=getrusage(self.who) self.timeStart=time() if sys.version_info<(2,4): run=Popen4(self.cmdline) self.output=run.fromchild else: run=subprocess.Popen(shellExecutionPrefix()+self.cmdline, shell=True, bufsize=0, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, close_fds=True) self.output=run.stdout self.run=run self.threadPid=run.pid foamLogger().info("Started with PID %d" % self.threadPid) if self.isLinux: # print "Starting Timer" self.timer=Timer(0.1*self.timerTime,getLinuxMem,args=[self]) self.timer.start() # print "Starting Timer" self.timer2=Timer(0.5*self.timerTime,checkForStopFile,args=[self]) self.timer2.start() self.hasSomethingToSay=True self.stateLock.release() try: # print "Waiting",time() self.status=run.wait() # Python 2.3 on Mac OS X never seems to reach this point # print "After wait",time() # print "Status:",self.status # to give a chance to read the remaining output if self.hasSomethingToSay: sleep(2.) while self.reader.read(self.output): print_("Unused output:",self.reader.line) except OSError: e = sys.exc_info()[1] # compatible with 2.x and 3.x print_("Exeption caught:",e) self.stopTimer() self.threadPid=-1 self.resEnd=getrusage(self.who) self.timeEnd=time() # print "End:",self.timeEnd # print "Returned",self.status self.getReturnCode()
def processTimestepOptions(self, sol): """Process the options :param sol: the solution-directory that is to be worked with""" if self.opts.parallelTimes: sol.setToParallel() if self.opts.latest: self.opts.time.append(float(sol.getLast())) if self.singleTime: if len(self.opts.time)>1: self.error("Only a single time allow. We got",len(self.opts.time)," : ", ", ".join(self.opts.time)) else: if self.opts.all: for t in sol.getTimes(): self.opts.time.append(float(t)) if self.opts.beforeTime or self.opts.afterTime: start=float(sol.getFirst()) end=float(sol.getLast()) if self.opts.beforeTime: end=self.opts.beforeTime if self.opts.afterTime: start=self.opts.afterTime for t in sol.getTimes(): tVal=float(t) if tVal>=start and tVal<=end: self.opts.time.append(tVal) self.opts.time.sort() times=[] for s in self.opts.time: times.append(sol.timeName(s,minTime=True)) if self.opts.unique: tmp=[] last=None cnt=0 for s in times: if last!=s: tmp.append(s) else: cnt+=1 last=s if cnt>0: self.warning("Removed",cnt,"duplicate times") times=tmp if len(times)==0: self.warning("No valid times specified") if self.opts.showTimes: print_("Times in case:",sol.getTimes()) print_("Used times:",times) return times
def __flattenDict(self,oData,prefix=""): data=[(prefix+k,v) for k,v in iteritems(oData) if type(v)!=dict] subtables=dict([(k,v) for k,v in iteritems(oData) if type(v)==dict]) for name,val in iteritems(subtables): data+=list(self.__flattenDict(val,prefix+name+self.separator).items()) if self.verbose: print_("Flattened",oData,"to",data) return dict(data)
def run(self): cName=self.parser.getArgs()[0] sol=SolutionDirectory(cName,archive=None) # self.addLocalConfig(cName) initial=sol[0] if "U" not in initial or "p" not in initial: error("Either 'p' or 'U' missing from the initial directory",initial.baseName()) if self.opts.writep: initial["p.prepotential"]=initial["p"] initial["U.prepotential"]=initial["U"] lam=self.getParallel(sol) if self.opts.writep: writep=["-writep"] else: writep=[] argv=["potentialFoam"] if oldApp(): argv+=[".",cName] else: argv+=["-case",cName] self.setLogname(default="Potential",useApplication=False) self.checkAndCommit(sol) run=BasicRunner(argv=argv+writep, server=self.opts.server, logname=self.opts.logname, compressLog=self.opts.compress, silent=self.opts.progress or self.opts.silent, lam=lam, logTail=self.opts.logTail, echoCommandLine=self.opts.echoCommandPrefix, noLog=self.opts.noLog) print_("Setting system-directory for potentialFoam") trig=PotentialTrigger(sol, self.opts.noCorr, self.opts.tolerance, self.opts.relTol, pRefCell=self.opts.pRefCell, pRefValue=self.opts.pRefValue, removeLibs=self.opts.removeLibs, removeFunctions=self.opts.removeFunctions) run.addEndTrigger(trig.resetIt) self.addToCaseLog(cName,"Starting") run.start() self.setData(run.data) self.addToCaseLog(cName,"Ending")
def run(self): files=self.parser.getArgs()[0:] if len(files)==1 and path.isdir(files[0]): sol=SolutionDirectory( self.parser.getArgs()[0], archive=None, parallel=self.opts.parallelTimes, paraviewLink=False) self.processTimestepOptions(sol) if len(self.opts.time)<1: self.error("No time specified") globStr=self.parser.getArgs()[0] if self.opts.parallelTimes: globStr=path.join(globStr,"processor*") usedTime=sol.timeName(self.opts.time[0]) globStr=path.join(globStr, usedTime, "uniform","profiling*") files=glob(globStr) print_("Profiling info from time",usedTime) if len(files)<1: self.error("No profiling data found") elif len(files)>1: lst=[] for f in files: lst.append(self.readProfilingInfo(f)) dataAll,children0,root0=lst[0] for i in dataAll: d=dataAll[i] d["totalTimeMin"]=d["totalTime"] d["totalTimeMax"]=d["totalTime"] d["callsMin"]=d["calls"] d["callsMax"]=d["calls"] for data,children,root in lst[1:]: if root0!=root or children!=children0 or data.keys()!=dataAll.keys(): self.error("Inconsistent profiling data. Probably not from same run/timestep") for i in data: d=data[i] s=dataAll[i] s["totalTime"]+=d["totalTime"] s["totalTimeMin"]=min(s["totalTimeMin"],d["totalTime"]) s["totalTimeMax"]=max(s["totalTimeMax"],d["totalTime"]) s["calls"]+=d["calls"] s["callsMin"]=min(s["callsMin"],d["calls"]) s["callsMax"]=max(s["callsMax"],d["calls"]) s["childTime"]+=d["childTime"] for i in dataAll: d=dataAll[i] d["totalTime"]=d["totalTime"]/len(lst) d["childTime"]=d["childTime"]/len(lst) d["calls"]=d["calls"]/len(lst) self.printProfilingInfo(dataAll,children,root,True) else: data,children,root=self.readProfilingInfo(files[0]) self.printProfilingInfo(data,children,root)
def execute(self,para,log): tmp=replaceValues(self.expression,para) try: val=eval(tmp) except SyntaxError: error("Syntax error in",tmp) print_(" Setting",self.name,"to",val) para[self.name]=str(val) return True,None
def run(self): if self.opts.allInstallations: installed=FI.foamInstalledVersions() total=0 for k in sorted(installed.keys()): instPath=installed[k] total+=self.reportInstallation(instPath) print_("\nTotal disk space used by binaries:"+humanReadableSize(total)) else: self.reportInstallation(FI.installationPath())
def wrapCallbacks(self): """Wrap the callback methods with a Python exception handler. This is not done here so that methoids that the child classes overwrote will be wrapped to""" # not yet working for m in callbackMethods: print_("Wrapping method",m) # setattr(self,m,original) self.wrapACallback(m)