def addOptions(self): behaviour=OptionGroup(self.parser, "Behaviour", "The behaviour of the parser") self.parser.add_option_group(behaviour) switchedOn=" - Already switched on by the configuration" tolerant=config().getboolean("Template","tolerantRender") behaviour.add_option("--tolerant-expression-evaluation", action="store_true", default=False, dest="tolerantRender", help="Instead of failing when encountering a problem during an evaluation a string with the error message is inserted into the output"+ (switchedOn if tolerant else "")) allow=config().getboolean("Template","allowExecution") behaviour.add_option("--allow-exec-instead-of-assignment", action="store_true", default=allow, dest="allowExec", help="Allows exectution of non-assignments in $$-lines. This is potentially unsafe as it allows 'import' and calling of external programs"+ (switchedOn if allow else "")) assignDebug=config().getboolean("Template","assignmentDebug") behaviour.add_option("--add-assignment-debug", action="store_true", default=assignDebug, dest="addAssignmentDebug", help="Adds a commented out line for each assignment with the name of the variable and the used value"+ (switchedOn if assignDebug else ""))
def register(self): """Tries to register with the Meta-Server""" foamLogger().info( "Trying to register as IP:%s PID:%d Port:%d" % (self._answerer.ip(), self._answerer.pid(), self._port)) try: try: meta = ServerProxy("http://%s:%d" % (config().get("Metaserver", "ip"), config().getint("Metaserver", "port"))) response = meta.registerServer(self._answerer.ip(), self._answerer.pid(), self._port) self.isRegistered = True foamLogger().info("Registered with server. Response " + str(response)) except socket.error: reason = sys.exc_info()[1] # compatible with 2.x and 3.x foamLogger().warning( "Can't connect to meta-server - SocketError: " + str(reason)) except: foamLogger().error( "Can't connect to meta-server - Unknown Error: " + str(sys.exc_info()[0])) foamLogger().error(str(sys.exc_info()[1])) foamLogger().error("Traceback: " + str(extract_tb(sys.exc_info()[2]))) except: # print "Error during registering (no socket module?)" pass
def register(self): """Tries to register with the Meta-Server""" foamLogger().info("Trying to register as IP:%s PID:%d Port:%d" % (self._answerer.ip(), self._answerer.pid(),self._port)) try: try: meta=ServerProxy( "http://%s:%d" % (config().get( "Metaserver","ip"),config().getint("Metaserver","port"))) response=meta.registerServer(self._answerer.ip(), self._answerer.pid(),self._port) self.isRegistered=True foamLogger().info("Registered with server. Response " + str(response)) except socket.error: reason = sys.exc_info()[1] # compatible with 2.x and 3.x foamLogger().warning("Can't connect to meta-server - SocketError: "+str(reason)) except: foamLogger().error("Can't connect to meta-server - Unknown Error: "+str(sys.exc_info()[0])) foamLogger().error(str(sys.exc_info()[1])) foamLogger().error("Traceback: "+str(extract_tb(sys.exc_info()[2]))) except: # print "Error during registering (no socket module?)" pass
def foamInstalledVersions(): """:return: A list with the installed versions of OpenFOAM""" global __foamInstallations if __foamInstallations: return __foamInstallations __foamInstallations={} forks=config().getList("OpenFOAM","Forks") for fork in forks: currentFork=foamFork() if "WM_PROJECT_INST_DIR" in environ and currentFork==fork: basedir=environ["WM_PROJECT_INST_DIR"] else: basedir=path.expanduser(config().get("OpenFOAM","Installation-"+fork)) if not path.exists(basedir) or not path.isdir(basedir): warning("Basedir",basedir,"for fork",fork,"does not exist or is not a directory") # continue for bdir in [basedir]+config().getList("OpenFOAM","AdditionalInstallation-"+fork): for val in [re.compile(s) for s in config().getList("OpenFOAM","DirPatterns-"+fork)]: __foamInstallations.update(findInstalledVersions(bdir,val,fork)) return __foamInstallations
def addOptions(self): behaviour = OptionGroup(self.parser, "Behaviour", "The behaviour of the parser") self.parser.add_option_group(behaviour) switchedOn = " - Already switched on by the configuration" tolerant = config().getboolean("Template", "tolerantRender") behaviour.add_option( "--tolerant-expression-evaluation", action="store_true", default=False, dest="tolerantRender", help= "Instead of failing when encountering a problem during an evaluation a string with the error message is inserted into the output" + (switchedOn if tolerant else "")) allow = config().getboolean("Template", "allowExecution") behaviour.add_option( "--allow-exec-instead-of-assignment", action="store_true", default=allow, dest="allowExec", help= "Allows exectution of non-assignments in $$-lines. This is potentially unsafe as it allows 'import' and calling of external programs" + (switchedOn if allow else "")) assignDebug = config().getboolean("Template", "assignmentDebug") behaviour.add_option( "--add-assignment-debug", action="store_true", default=assignDebug, dest="addAssignmentDebug", help= "Adds a commented out line for each assignment with the name of the variable and the used value" + (switchedOn if assignDebug else ""))
def deregister(self): """Tries to deregister with the Meta-Server""" if self.isRegistered: try: meta = ServerProxy("http://%s:%d" % (config().get("Metaserver", "ip"), config().getint("Metaserver", "port"))) meta.deregisterServer(self._answerer.ip(), self._answerer.pid(), self._port) except socket.error: reason = sys.exc_info()[1] # compatible with 2.x and 3.x foamLogger().warning( "Can't connect to meta-server - SocketError: " + str(reason)) except: foamLogger().error( "Can't connect to meta-server - Unknown Error: " + str(sys.exc_info()[0])) foamLogger().error(str(sys.exc_info()[1])) foamLogger().error("Traceback: " + str(extract_tb(sys.exc_info()[2]))) else: foamLogger().warning( "Not deregistering, because it seems we were not registered in the first place " ) self._server.server_close()
def __init__(self): description="""\ Lists all the processes known to a meta-server """ self.defaultHost=config().get("Metaserver","ip") self.defaultPort=config().getint("Metaserver","port") PyFoamApplication.__init__(self,description=description,usage="%prog [options]",interspersed=True,nr=0)
def __init__(self): description = """\ Lists all the processes known to a meta-server """ self.defaultHost = config().get("Metaserver", "ip") self.defaultPort = config().getint("Metaserver", "port") PyFoamApplication.__init__(self, description=description, usage="%prog [options]", interspersed=True, nr=0)
def findFreePort(): """Finds a free server port on this machine and returns it Valid server ports are in the range 18000 upward (the function tries to find the lowest possible port number ATTENTION: this part may introduce race conditions""" return freeServerPort(config().getint("Network", "startServerPort"), length=config().getint("Network", "nrServerPorts"))
def findFreePort(): """Finds a free server port on this machine and returns it Valid server ports are in the range 18000 upward (the function tries to find the lowest possible port number ATTENTION: this part may introduce race conditions""" return freeServerPort(config().getint("Network","startServerPort"), length=config().getint("Network","nrServerPorts"))
def deregister(self): """Tries to deregister with the Meta-Server""" if self.isRegistered: try: meta=ServerProxy("http://%s:%d" % (config().get("Metaserver","ip"),config().getint("Metaserver","port"))) meta.deregisterServer(self._answerer.ip(),self._answerer.pid(),self._port) except socket.error, reason: foamLogger().warning("Can't connect to meta-server - SocketError: "+str(reason)) except:
def findBaseDir(newDir): if environ.has_key("WM_PROJECT_INST_DIR"): basedir=environ["WM_PROJECT_INST_DIR"] else: basedir=path.expanduser(config().get("OpenFOAM","Installation")) for bdir in [basedir]+config().getList("OpenFOAM","AdditionalInstallation"): if path.exists(path.join(bdir,"OpenFOAM-"+newDir)): return (bdir,"OpenFOAM-") elif path.exists(path.join(bdir,"openfoam"+newDir)): return (bdir,"openfoam") error("Can't find basedir for OpenFOAM-version",newDir)
def _getLoggingLevel(name): """Gets the logging level value from its name""" level = config().get("Logging", "default") try: level = config().get("Logging", name) except: pass value = logging.INFO try: value = getattr(logging, level) except AttributeError, reason: print "WARNING: Wrong specification of debug level " + level + " for log " + name
def run(self): section=self.parser.getArgs()[0] option=self.parser.getArgs()[1] print "Foam-Version: ",foamVersionString() print "Section: ",section print "Option: ",option print "Value: ", try: print config().get(section,option) except ConfigParser.NoSectionError: print "<section not found>" except ConfigParser.NoOptionError: print "<option not found>"
def addOptions(self): tformat=OptionGroup(self.parser, "Format", "Specifying details about the format of the pyratemp-templates (new format)") self.parser.add_option_group(tformat) tformat.add_option("--expression-delimiter", action="store", default=config().get("Template","expressionDelimiter"), dest="expressionDelimiter", help="String that delimits an expression. At the end of the expression the reverse string is being used. Default: %default") tformat.add_option("--assignment-line-start", action="store", default=config().get("Template","assignmentLineStart"), dest="assignmentLineStart", help="String at the start of a line that signifies that this is an assignment. Default: %default")
def pathInfo(self): if self.opts.descPath: print_() print_("Directories that are searched for pfcb-files:") print_() for i, d in enumerate(config().get("CaseBuilder", "descriptionpath")): status = "<not existing>" if path.isdir(d): status = " " * len(status) print_("%2d: %s %s" % (i + 1, status, d)) return True if self.opts.listDescr: dl = CaseBuilderDescriptionList() print_() print_("Available description files:") print_() for i, d in enumerate(dl): print_("%4d: %s" % (i + 1, d[1])) print_(" %s - %s" % (d[2], d[3])) return True return False
def __init__(self): CaseBuilderTUI.__init__(self) self.descriptions=CaseBuilderDescriptionList() if len(self.descriptions)==0: error("No description-files (.pfcb) found in path",config().get("CaseBuilder","descriptionpath")) self.items=[] mxlen=apply(max,map(lambda x:len(x[2]),self.descriptions)+[0]) for i,d in enumerate(self.descriptions): txt=urwid.Text([('button',d[2]+" "*(mxlen-len(d[2])))," ",d[1],"\n",d[3]]) if i%2==0: attr='bgl' else: attr='bg' self.items.append(urwid.AttrWrap(txt,attr,'bgbutton')) self.walk=urwid.SimpleListWalker(self.items) self.listbox = urwid.ListBox(self.walk) self.statusText=urwid.Text(" Press <ESC> to exit <ENTER> to select description") footer = urwid.AttrWrap( self.statusText, 'header' ) top = urwid.Frame(self.listbox, footer=footer) if len(self.descriptions)%2==0: self.top=urwid.AttrWrap(top,'bgl') else: self.top=urwid.AttrWrap(top,'bg') self.ui.run_wrapper(self.runTUI)
def __init__(self, progress=False): """ :param progress: Print time progress on console? """ self.analyzers = {} self.time = "" self.oDir = "" self.line = LineReader(config().getboolean("SolverOutput", "stripSpaces")) self.timeListeners = [] self.timeTriggers = [] self.resetFileTriggers = [] self.customExpr = re.compile("Custom([0-9]+)_(.+)") if progress: self.progressOut = ProgressOutput(stdout) else: self.progressOut = ProgressOutput() # tm=CountLineAnalyzer() tm = TimeLineAnalyzer() self.addAnalyzer("Time", tm) tm.addListener(self.setTime) self.analyzedLines = 0
def freeServerPort(start,length=1): """ Finds a port that is free for serving @param start: the port to start with @param length: the number of ports to scan @return: number of the first free port, -1 if none is found """ port=-1 for p in range(start,start+length): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) sock.bind(('',p)) except socket.error: e = sys.exc_info()[1] # compatible with 2.x and 3.x if e[0]!=errno.EADDRINUSE: # sock.shutdown(2) sock.close() raise else: # sock.shutdown(2) sock.close() time.sleep(config().getfloat("Network","portWait")) # to avoid that the port is not available. Introduces possible race-conditons port=p break return port
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 __init__(self): CaseBuilderQt.__init__(self) self.descriptions = CaseBuilderDescriptionList() if len(self.descriptions) == 0: error("No description-files (.pfcb) found in path", config().get("CaseBuilder", "descriptionpath")) mainLayout = QtGui.QVBoxLayout() self.setLayout(mainLayout) self.descriptsList = QtGui.QListWidget() self.descriptsList.setSelectionMode(QtGui.QAbstractItemView.SingleSelection) mainLayout.addWidget(self.descriptsList) self.itemlist = [] for d in self.descriptions: item = QtGui.QListWidgetItem(d[2]) item.setToolTip(d[3]) self.descriptsList.addItem(item) self.itemlist.append((item, d)) buttons = QtGui.QDialogButtonBox(QtGui.QDialogButtonBox.Cancel) mainLayout.addWidget(buttons) selectButton = QtGui.QPushButton("Select") selectButton.setToolTip("Select the case description that we want to work with") buttons.addButton(selectButton, QtGui.QDialogButtonBox.AcceptRole) try: buttons.accepted.connect(self.selectPressed) buttons.rejected.connect(self.reject) except AttributeError: warning("Old QT-version where QDialogButtonBox doesn't have the accepted/rejected-attributes") self.connect(buttons, QtCore.SIGNAL("accepted()"), self.selectPressed) self.connect(buttons, QtCore.SIGNAL("rejected()"), self.reject)
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 buildMPIrun(self, argv, expandApplication=True): """Builds a list with a working mpirun command (for that MPI-Implementation) @param argv: the original arguments that are to be wrapped @param expandApplication: Expand the @return: list with the correct mpirun-command""" mpirun = ["mpprun"] progname = argv[0] if expandApplication: stat, progname = commands.getstatusoutput('which '+progname) if stat: progname = argv[0] warning( "which can not find a match for", progname, ". Hoping for the best" ) mpirun += [progname] + argv[1:3] + ["-parallel"] + argv[3:] if config().getdebug("ParallelExecution"): debug("MPI:", foamMPI()) debug("Arguments:", mpirun) system("which mpirun") system("which rsh") debug("Environment", environ) for a in mpirun: if a in environ: debug("Transfering variable", a, "with value", environ[a]) return mpirun
def addOptions(self): paraview = OptionGroup(self.parser, "Paraview specifications", "Options concerning paraview") paraview.add_option( "--state-file", dest="state", default=None, help= "The pvsm-file that should be used. If none is specified the file 'default.pvsm' in the case-directory is used" ) paraview.add_option( "--paraview-command", dest="paraview", default=config().get("Paths", "paraview"), help= "The paraview-version that should be called. Default: %default (set in the configuration 'Paths'/'paraview')" ) decoChoices = ["keep", "decomposed", "reconstructed", "auto"] paraview.add_option( "--decompose-mode", dest="decomposeMode", type="choice", default="auto", choices=decoChoices, help= "Whether the decomposed or the reconstructed data should be used. Possible values are" + ", ".join(decoChoices) + " with default value %default. 'auto' means that the data set where more time-steps are present is used. 'keep' doesn't change the mode in the state-file. Default: %default" ) self.parser.add_option_group(paraview)
def pathInfo(self): if self.opts.descPath: print print "Directories that are searched for pfcb-files:" print for i,d in enumerate(config().get("CaseBuilder","descriptionpath")): status="<not existing>" if path.isdir(d): status=" "*len(status) print "%2d: %s %s" %(i+1,status,d) return True if self.opts.listDescr: dl=CaseBuilderDescriptionList() print print "Available description files:" print for i,d in enumerate(dl): print "%4d: %s" % (i+1,d[1]) print " %s - %s" % (d[2],d[3]) return True return False
def __init__(self): dirList = config().get("CaseBuilder", "descriptionpath") self.list = [] for d in dirList: for f in glob.glob(path.join(d, "*.pfcb")): nm = path.basename(f) cb = CaseBuilderFile(f) self.list.append((nm, f, cb.name(), cb.description()))
def __init__(self): dirList=config().get("CaseBuilder","descriptionpath") self.list=[] for d in dirList: for f in glob.glob(path.join(d,"*.pfcb")): nm=path.basename(f) cb=CaseBuilderFile(f) self.list.append((nm,f,cb.name(),cb.description()))
def _getLoggingLevel(name): """Gets the logging level value from its name""" level = config().get("Logging", "default") try: level = config().get("Logging", name) except: pass value = logging.INFO try: value = getattr(logging, level) except AttributeError: print_("WARNING: Wrong specification of debug level " + level + " for log " + name) return value
def deregister(self): """Tries to deregister with the Meta-Server""" if self.isRegistered: try: meta=ServerProxy("http://%s:%d" % (config().get("Metaserver","ip"),config().getint("Metaserver","port"))) meta.deregisterServer(self._answerer.ip(),self._answerer.pid(),self._port) except socket.error: reason = sys.exc_info()[1] # compatible with 2.x and 3.x foamLogger().warning("Can't connect to meta-server - SocketError: "+str(reason)) except: foamLogger().error("Can't connect to meta-server - Unknown Error: "+str(sys.exc_info()[0])) foamLogger().error(str(sys.exc_info()[1])) foamLogger().error("Traceback: "+str(extract_tb(sys.exc_info()[2]))) else: foamLogger().warning("Not deregistering, because it seems we were not registered in the first place ") self._server.server_close()
def findFreePort(useSSL=None): """Finds a free server port on this machine and returns it Valid server ports are in the range 18000 upward (the function tries to find the lowest possible port number ATTENTION: this part may introduce race conditions""" if useSSL is None: useSSL=config().getboolean("Network","SSLServerDefault") if useSSL: startPort=config().getint("Network","startServerPortSSL") else: startPort=config().getint("Network","startServerPort") return useSSL,freeServerPort(startPort, length=config().getint("Network","nrServerPorts"))
def __init__(self,run=None,master=None,lines=100): """ :param run: The thread that controls the run :param master: The Runner-Object that controls everything :param lines: the number of lines the server should remember """ Thread.__init__(self) self.isRegistered=False tries=0 maxTries=config().getint("Network","socketRetries") ok=False self._zConf=ZeroConfFoamServer() while not ok and tries<maxTries: ok=True tries+=1 self.__ssl,self._port=findFreePort() self._running=False if self._port<0: foamLogger().warning("Could not get a free port. Server not started") return try: foamLogger().info("Serving on port %d" % self._port) self._server=ServerBase(('',self._port),useSSL=self.__ssl,logRequests=False) self.__ssl=self._server.useSSL self._server.register_introspection_functions() self._answerer=FoamAnswerer(run=run,master=master,lines=lines,foamserver=self) self._server.register_instance(self._answerer) self._server.register_function(self.killServer) self._server.register_function(self.kill) if run: self._server.register_function(run.cpuTime) self._server.register_function(run.cpuUserTime) self._server.register_function(run.cpuSystemTime) self._server.register_function(run.wallTime) self._server.register_function(run.usedMemory) except socket.error: reason = sys.exc_info()[1] # compatible with 2.x and 3.x ok=False warning("Could not start on port",self._port,"althoug it was promised. Try:",tries,"of",maxTries) foamLogger().warning("Could not get port %d - SocketError: %s. Try %d of %d" % (self._port,str(reason),tries,maxTries)) sleep(2+20*random()) if not ok: foamLogger().warning("Exceeded maximum number of tries for getting a port: %d" % maxTries) warning("Did not get a port after %d tries" % tries) else: if tries>1: warning("Got a port after %d tries" % tries)
def findFreePort(useSSL=None): """Finds a free server port on this machine and returns it Valid server ports are in the range 18000 upward (the function tries to find the lowest possible port number ATTENTION: this part may introduce race conditions""" if useSSL is None: useSSL = config().getboolean("Network", "SSLServerDefault") if useSSL: startPort = config().getint("Network", "startServerPortSSL") else: startPort = config().getint("Network", "startServerPort") return useSSL, freeServerPort(startPort, length=config().getint( "Network", "nrServerPorts"))
def __init__(self,run=None,master=None,lines=100): """ @param run: The thread that controls the run @param master: The Runner-Object that controls everything @param lines: the number of lines the server should remember """ Thread.__init__(self) self.isRegistered=False tries=0 maxTries=config().getint("Network","socketRetries") ok=False while not ok and tries<maxTries: ok=True tries+=1 self._port=findFreePort() self._running=False if self._port<0: foamLogger().warning("Could not get a free port. Server not started") return try: foamLogger().info("Serving on port %d" % self._port) self._server=ServerBase(('',self._port),logRequests=False) self._server.register_introspection_functions() self._answerer=FoamAnswerer(run=run,master=master,lines=lines,foamserver=self) self._server.register_instance(self._answerer) self._server.register_function(self.killServer) self._server.register_function(self.kill) if run: self._server.register_function(run.cpuTime) self._server.register_function(run.cpuUserTime) self._server.register_function(run.cpuSystemTime) self._server.register_function(run.wallTime) self._server.register_function(run.usedMemory) except socket.error: reason = sys.exc_info()[1] # compatible with 2.x and 3.x ok=False warning("Could not start on port",self._port,"althoug it was promised. Try:",tries,"of",maxTries) foamLogger().warning("Could not get port %d - SocketError: %s. Try %d of %d" % (self._port,str(reason),tries,maxTries)) sleep(2+20*random()) if not ok: foamLogger().warning("Exceeded maximum number of tries for getting a port: %d" % maxTries) warning("Did not get a port after %d tries" % tries) else: if tries>1: warning("Got a port after %d tries" % tries)
def searchDescriptionFile(self,name): if self.opts.search: fName=None for d in config().get("CaseBuilder","descriptionpath"): if path.exists(path.join(d,name)): fName=path.join(d,name) break if path.exists(path.join(d,name+".pfcb")): fName=path.join(d,name+".pfcb") break if not fName: error("Description",name,"does not exist in search path",config().get("CaseBuilder","descriptionpath")) else: print "Found",fName else: fName=name if not path.exists(fName): error("The description file",fName,"does not exist") return fName
def searchDescriptionFile(self, name): if self.opts.search: fName = None for d in config().get("CaseBuilder", "descriptionpath"): if path.exists(path.join(d, name)): fName = path.join(d, name) break if path.exists(path.join(d, name + ".pfcb")): fName = path.join(d, name + ".pfcb") break if not fName: error("Description", name, "does not exist in search path", config().get("CaseBuilder", "descriptionpath")) else: print_("Found", fName) else: fName = name if not path.exists(fName): error("The description file", fName, "does not exist") return fName
def buildMPIrun(self,argv,expandApplication=True): """Builds a list with a working mpirun command (for that MPI-Implementation) @param argv: the original arguments that are to be wrapped @param expandApplication: Expand the @return: list with the correct mpirun-command""" nr=str(self.cpuNr()) mpirun=[config().get("MPI","run_"+foamMPI(),default="mpirun")] mpiRunPath=self.which(" ".join(mpirun)) if not mpiRunPath: error("Could not find a path for",mpirun, "Check configuration variable","run_"+foamMPI(), "in section 'MPI'") mpirun+=eval(config().get("MPI","options_"+foamMPI()+"_pre",default="[]")) if(foamMPI()=="LAM"): mpirun+=["-np",nr] elif foamMPI().find("OPENMPI")>=0: nr=[] if "MPI_ARCH_PATH" in environ and config().getboolean("MPI","OpenMPI_add_prefix"): nr+=["--prefix",environ["MPI_ARCH_PATH"]] if self.procNr!=None: nr+=["--n",str(self.procNr)] machine=[] if self.mFile!=None: machine=["--machinefile",self.mFile] if config().getdebug("ParallelExecution"): debug("Start of",self.mFile) debug("\n"+open(self.mFile).read()) debug("End of",self.mFile) mpirun+=machine+nr else: error(" Unknown or missing MPI-Implementation for mpirun: "+foamMPI()) mpirun+=eval(config().get("MPI","options_"+foamMPI()+"_post",default="[]")) progname=argv[0] if expandApplication: # old implementation: stat,progname=commands.getstatusoutput('which '+progname) progname=self.which(progname) if progname: progname=argv[0] warning("which can not find a match for",progname,". Hoping for the best") if oldAppConvention(): mpirun+=[progname]+argv[1:3]+["-parallel"]+argv[3:] else: mpirun+=[progname]+argv[1:]+["-parallel"] if config().getdebug("ParallelExecution"): debug("MPI:",foamMPI()) debug("Arguments:",mpirun) system("which mpirun") system("which rsh") debug("Environment",environ) for a in mpirun: if a in environ: debug("Transfering variable",a,"with value",environ[a]) return mpirun
def foamInstalledVersions(): """@return: A list with the installed versions of OpenFOAM""" versions=set() valid=re.compile("^OpenFOAM-([0-9]\.[0-9].*)$") valid2=re.compile("^openfoam(1[0-9]+)$") if environ.has_key("WM_PROJECT_INST_DIR"): basedir=environ["WM_PROJECT_INST_DIR"] else: basedir=path.expanduser(config().get("OpenFOAM","Installation")) if not path.exists(basedir) or not path.isdir(basedir): warning("Basedir",basedir,"does not exist or is not a directory") return [] for bdir in [basedir]+config().getList("OpenFOAM","AdditionalInstallation"): for val in [valid,valid2]: versions |= findInstalledVersions(bdir,val) return list(versions)
def __init__(self,run=None,master=None,lines=100,foamserver=None): """ @param run: The thread that controls the run @param master: The Runner-Object that controls everything @param lines: the number of lines the server should remember """ self._run=run self._master=master self._foamserver=foamserver self._lines=RingBuffer(nr=lines) self._lastTime=time() self._linesLock=Lock() self._maxOutputTime=config().getfloat("IsAlive","maxTimeStart")
def __init__(self, run=None, master=None, lines=100, foamserver=None): """ :param run: The thread that controls the run :param master: The Runner-Object that controls everything :param lines: the number of lines the server should remember """ self._run = run self._master = master self._foamserver = foamserver self._lines = RingBuffer(nr=lines) self._lastTime = time() self._linesLock = Lock() self._maxOutputTime = config().getfloat("IsAlive", "maxTimeStart")
def checkFoamServers(host, start, length=1): """ Finds the port on a remote host on which Foam-Servers are running :param host: the IP of the host that should be checked :param start: the port to start with :param length: the number of ports to scan :return: a list with the found ports, None if the machine is unreachable """ ports = [] ## try: ## name,alias,rest =socket.gethostbyaddr(host) ## except socket.herror,reason: ## # no name for the host ## return None for p in range(start, start + length): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) socket.setdefaulttimeout(config().getfloat("Network", "socketTimeout")) ok = False try: sock.connect((host, p)) sock.close() except socket.error: reason = sys.exc_info()[1] # compatible with 2.x and 3.x code = reason.errno if code in [ errno.EHOSTUNREACH, errno.ENETUNREACH, errno.ETIMEDOUT ] or code == "timed out" or code < 0: # Host unreachable: no more scanning return None elif code == errno.ECONNREFUSED: # port does not exist continue else: print_(errno.errorcode[code]) raise reason try: server = xmlrpclib.ServerProxy("http://%s:%d" % (host, p)) ok = server.isFoamServer() except xmlrpclib.ProtocolError: pass except xml.parsers.expat.ExpatError: pass if ok: ports.append(p) return ports
def run(self): section = self.parser.getArgs()[0] option = self.parser.getArgs()[1] print_("Foam-Version: ", foamVersionString()) print_("Section: ", section) print_("Option: ", option) print_("Value: ", end="") try: print_(config().get(section, option)) except ConfigParser.NoSectionError: print_("<section not found>") except ConfigParser.NoOptionError: print_("<option not found>")
def run(self): section=self.parser.getArgs()[0] option=self.parser.getArgs()[1] print_("Foam-Version: ",foamVersionString()) print_("Section: ",section) print_("Option: ",option) print_("Value: ",end="") try: print_(config().get(section,option)) except ConfigParser.NoSectionError: print_("<section not found>") except ConfigParser.NoOptionError: print_("<option not found>")
def addOptions(self): paraview=OptionGroup(self.parser, "Paraview specifications", "Options concerning paraview") paraview.add_option("--state-file", dest="state", default=None, help="The pvsm-file that should be used. If none is specified the file 'default.pvsm' in the case-directory is used") paraview.add_option("--paraview-command", dest="paraview", default=config().get("Paths","paraview"), help="The paraview-version that should be called. Default: %default (set in the configuration 'Paths'/'paraview')") self.parser.add_option_group(paraview)
def checkFoamServers(host,start,length=1): """ Finds the port on a remote host on which Foam-Servers are running @param host: the IP of the host that should be checked @param start: the port to start with @param length: the number of ports to scan @return: a list with the found ports, None if the machine is unreachable """ ports=[] ## try: ## name,alias,rest =socket.gethostbyaddr(host) ## except socket.herror,reason: ## # no name for the host ## return None for p in range(start,start+length): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) socket.setdefaulttimeout(config().getfloat("Network","socketTimeout")) ok=False try: sock.connect((host,p)) sock.close() except socket.error: reason = sys.exc_info()[1] # compatible with 2.x and 3.x code=reason[0] if code in [errno.EHOSTUNREACH,errno.ENETUNREACH,errno.ETIMEDOUT] or code=="timed out" or code<0: # Host unreachable: no more scanning return None elif code==errno.ECONNREFUSED: # port does not exist continue else: print_(errno.errorcode[code]) raise reason try: server=xmlrpclib.ServerProxy("http://%s:%d" % (host,p)) ok=server.isFoamServer() except xmlrpclib.ProtocolError: pass except xml.parsers.expat.ExpatError: pass if ok: ports.append(p) return ports
def __init__(self, filenames, silent=False, tailLength=1000, sleep=0.1, endTime=None, 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""" if type(filenames) is list: meshTimes = [] createMesh = "Create mesh for time = " for fName in filenames: meshTime = None with open(fName) as f: for l in f.readlines()[:100]: if l.find(createMesh) == 0: meshTime = float(l[len(createMesh):]) break meshTimes.append((fName, meshTime)) meshTimes.sort(key=lambda x: 1e50 if x[1] is None else x[1]) filenames = [m[0] for m in meshTimes] self.filename = filenames[0] self.nextFiles = filenames[1:] self.changeTimes = [m[1] for m in meshTimes[1:]] else: self.filename = filenames self.nextFiles = [] self.changeTimes = [] self._changeFileHooks = [] self.silent = silent self.tail = tailLength self.sleep = sleep self.follow = follow self.endTime = endTime 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 getServerProxy(host, port, useSSL=None): """Get a proxy to a server. If the useSSL-parameter is unset then it tries to automatically get the most secure connection""" if useSSL is None: useSSL = [True, False] else: useSSL = [useSSL] for s in useSSL: if s: try: import ssl import socket import uuid context = None if config().getboolean("Network", "allowSelfSignedSSL"): try: context = ssl._create_unverified_context() except AttributeError: pass try: user = getUserName() # user="******" challenge = auth.createChallengeString(str(uuid.uuid4())) try: server = ServerProxy("https://%s:%s@%s:%d" % (user, challenge, host, port), context=context) except TypeError: server = ServerProxy("https://%s:%s@%s:%d" % (user, challenge, host, port)) server.system.listMethods() # provoke a socket-error return server except socket.error as reason: if hasattr(reason, "reason"): if reason.reason == "CERTIFICATE_VERIFY_FAILED" and context is None: warning( "Can't verify certificate. Set setting 'network'/'allowSelfSignedSSL' to True. This may be insecure" ) raise reason except ImportError: warning("Problem with the imports. Dropping SSL-support") else: return ServerProxy("http://%s:%d" % (host, port)) warning("Could not connect to", host, "at", port) return None
def foamVersionString(useConfigurationIfNoInstallation=False): """@return: string for the Foam-version as found in $WM_PROJECT_VERSION""" if "WM_PROJECT_VERSION" not in environ and not useConfigurationIfNoInstallation: return "" else: if "WM_PROJECT_VERSION" in environ: vStr = environ["WM_PROJECT_VERSION"] else: vStr = "" if vStr == "" and useConfigurationIfNoInstallation: vStr = config().get("OpenFOAM", "Version") return vStr
def foamVersionString(useConfigurationIfNoInstallation=False): """:return: string for the Foam-version as found in $WM_PROJECT_VERSION""" if "WM_PROJECT_VERSION" not in environ and not useConfigurationIfNoInstallation: return "" else: if "WM_PROJECT_VERSION" in environ: vStr=environ["WM_PROJECT_VERSION"] else: vStr="" if vStr=="" and useConfigurationIfNoInstallation: vStr=config().get("OpenFOAM","Version") return vStr
def getServerProxy(host,port,useSSL=None): """Get a proxy to a server. If the useSSL-parameter is unset then it tries to automatically get the most secure connection""" if useSSL is None: useSSL=[True,False] else: useSSL=[useSSL] for s in useSSL: if s: try: import ssl import socket import uuid context=None if config().getboolean("Network","allowSelfSignedSSL"): try: context=ssl._create_unverified_context() except AttributeError: pass try: user=getUserName() # user="******" challenge=auth.createChallengeString(str(uuid.uuid4())) try: server=ServerProxy("https://%s:%s@%s:%d" % (user,challenge, host,port), context=context) except TypeError: server=ServerProxy("https://%s:%s@%s:%d" % (user,challenge, host,port)) server.system.listMethods() # provoke a socket-error return server except socket.error as reason: if hasattr(reason,"reason"): if reason.reason=="CERTIFICATE_VERIFY_FAILED" and context is None: warning("Can't verify certificate. Set setting 'network'/'allowSelfSignedSSL' to True. This may be insecure") raise reason except ImportError: warning("Problem with the imports. Dropping SSL-support") else: return ServerProxy("http://%s:%d" % (host,port)) warning("Could not connect to",host,"at",port) return None
def __init__(self,run=None,master=None,lines=100): """ @param run: The thread that controls the run @param master: The Runner-Object that controls everything @param lines: the number of lines the server should remember """ Thread.__init__(self) self.isRegistered=False tries=0 maxTries=length=config().getint("Network","socketRetries") ok=False while not ok and tries<maxTries: ok=True tries+=1 self._port=findFreePort() self._running=False if self._port<0: foamLogger().warning("Could not get a free port. Server not started") return try: foamLogger().info("Serving on port %d" % self._port) self._server=ServerBase(('',self._port),logRequests=False) self._server.register_introspection_functions() self._answerer=FoamAnswerer(run=run,master=master,lines=lines,foamserver=self) self._server.register_instance(self._answerer) self._server.register_function(self.killServer) self._server.register_function(self.kill) if run: self._server.register_function(run.cpuTime) self._server.register_function(run.cpuUserTime) self._server.register_function(run.cpuSystemTime) self._server.register_function(run.wallTime) self._server.register_function(run.usedMemory) except socket.error,reason: ok=False warning("Could not start on port",self._port,"althoug it was promised. Try:",tries,"of",maxTries) foamLogger().warning("Could not get port %d - SocketError: %s. Try %d of %d" % (self._port,str(reason),tries,maxTries)) sleep(2+20*random())
def __init__(self,filenames, 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""" if type(filenames) is list: meshTimes=[] createMesh="Create mesh for time = " for fName in filenames: meshTime=None with open(fName) as f: for l in f.readlines()[:100]: if l.find(createMesh)==0: meshTime=float(l[len(createMesh):]) break meshTimes.append((fName,meshTime)) meshTimes.sort(key=lambda x:1e50 if x[1] is None else x[1]) filenames=[m[0] for m in meshTimes] self.filename=filenames[0] self.nextFiles=filenames[1:] self.changeTimes=[m[1] for m in meshTimes[1:]] else: self.filename=filenames self.nextFiles=[] self.changeTimes=[] self._changeFileHooks=[] 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, cmdline, runner): """:param cmdline:cmdline - Command line of the OpenFOAM command :param runner: the Runner-object that started this thread""" Thread.__init__(self) self.cmdline = cmdline self.runner = runner self.output = None self.reader = LineReader(config().getboolean("SolverOutput", "stripSpaces")) self.keyboardInterupted = False self.isLinux = False self.isDarwin = False self.isWindows = False self.threadPid = -1 self.who = RUSAGE_CHILDREN if uname()[0] == "Linux": self.isLinux = True self.linuxMaxMem = 0 elif uname()[0] == "Darwin": self.isDarwin = True elif uname()[0] == "Windows": self.isWindows = True self.resStart = None self.resEnd = None self.timeStart = None self.timeEnd = None self.timerTime = 5. self.stateLock = Lock() self.setState(False) self.status = None self.returncode = None self.lineLock = Lock() self.line = "" self.stateLock.acquire()
def addOptions(self): paraview = OptionGroup(self.parser, "Paraview specifications", "Options concerning paraview") paraview.add_option( "--state-file", dest="state", default=None, help= "The pvsm-file that should be used. If none is specified the file 'default.pvsm' in the case-directory is used" ) paraview.add_option( "--paraview-command", dest="paraview", default=config().get("Paths", "paraview"), help= "The paraview-version that should be called. Default: %default (set in the configuration 'Paths'/'paraview')" ) self.parser.add_option_group(paraview)
def __init__(self, wind, app, bufflen=100, powerline=False): self.app = app self.wind = wind self.powerline = powerline and ("UTF-8" in sys.stdout.encoding) and six.PY3 validColors = [ "red", "white", "blue", "black", "green", "cyan", "magenta", "yellow" ] def colorCode(name): return eval("curses.COLOR_%s" % name.upper()) fgHeader = colorCode(config().getchoice("Curses", "headerTextColor", validColors)) bgHeader = colorCode(config().getchoice("Curses", "headerBackgroundColor", validColors)) bgText = colorCode(config().getchoice("Curses", "textBackgroundColor", validColors)) fgText = colorCode(config().getchoice("Curses", "textColor", validColors)) fgMatch = colorCode(config().getchoice("Curses", "textMatchColor", validColors)) fgGroup = colorCode(config().getchoice("Curses", "textGroupColor", validColors)) curses.init_pair(self.HEADER_COLOR, fgHeader, bgHeader) curses.init_pair(self.REGULAR_COLOR, fgText, bgText) curses.init_pair(self.REGEX_COLOR, fgMatch, bgText) curses.init_pair(self.GROUP_COLOR, fgGroup, bgText) self.wind.nodelay(True) self.lineBuffer = deque(maxlen=bufflen) self.footerText = [] self.headerText = [] self.oldTOutHeight = 0 self._update() self.oldStdout = sys.stdout self.encoding = sys.stdout.encoding self.errors = sys.stdout.errors self.oldStderr = sys.stderr sys.stdout = self sys.stderr = self self.skipNext = False self.lineCount = 0 self.stopOutput = False
def __init__(self): CaseBuilderQt.__init__(self) self.descriptions = CaseBuilderDescriptionList() if len(self.descriptions) == 0: error("No description-files (.pfcb) found in path", config().get("CaseBuilder", "descriptionpath")) mainLayout = QtGui.QVBoxLayout() self.setLayout(mainLayout) self.descriptsList = QtGui.QListWidget() self.descriptsList.setSelectionMode( QtGui.QAbstractItemView.SingleSelection) mainLayout.addWidget(self.descriptsList) self.itemlist = [] for d in self.descriptions: item = QtGui.QListWidgetItem(d[2]) item.setToolTip(d[3]) self.descriptsList.addItem(item) self.itemlist.append((item, d)) buttons = QtGui.QDialogButtonBox(QtGui.QDialogButtonBox.Cancel) mainLayout.addWidget(buttons) selectButton = QtGui.QPushButton("Select") selectButton.setToolTip( "Select the case description that we want to work with") buttons.addButton(selectButton, QtGui.QDialogButtonBox.AcceptRole) try: buttons.accepted.connect(self.selectPressed) buttons.rejected.connect(self.reject) except AttributeError: warning( "Old QT-version where QDialogButtonBox doesn't have the accepted/rejected-attributes" ) self.connect(buttons, QtCore.SIGNAL("accepted()"), self.selectPressed) self.connect(buttons, QtCore.SIGNAL("rejected()"), self.reject)