Package PyFoam :: Package Applications :: Module PyFoamApplication
[hide private]
[frames] | no frames]

Source Code for Module PyFoam.Applications.PyFoamApplication

  1  #  ICE Revision: $Id: /local/openfoam/Python/PyFoam/PyFoam/Applications/PyFoamApplication.py 7393 2011-03-29T14:55:03.425417Z bgschaid  $  
  2  """Base class for pyFoam-applications 
  3   
  4  Classes can also be called with a command-line string""" 
  5   
  6  from optparse import OptionGroup 
  7  from PyFoam.Basics.FoamOptionParser import FoamOptionParser 
  8  from PyFoam.Error import error,warning,FatalErrorPyFoamException 
  9  from PyFoam.FoamInformation import oldAppConvention as oldApp 
 10  from PyFoam.RunDictionary.SolutionDirectory import NoTouchSolutionDirectory 
 11   
 12  from PyFoam.Basics.TerminalFormatter import TerminalFormatter 
 13  from PyFoam import configuration 
 14   
 15  format=TerminalFormatter() 
 16  format.getConfigFormat("error") 
 17  format.getConfigFormat("warn") 
 18   
 19  import sys 
 20  from os import path,getcwd,environ 
 21   
22 -class PyFoamApplication(object):
23 - def __init__(self, 24 args=None, 25 description=None, 26 usage=None, 27 interspersed=False, 28 nr=None, 29 changeVersion=True, 30 exactNr=True):
31 """ 32 @param description: description of the command 33 @param usage: Usage 34 @param interspersed: Is the command line allowed to be interspersed (options after the arguments) 35 @param args: Command line arguments when using the Application as a 'class' from a script 36 @param nr: Number of required arguments 37 @param changeVersion: May this application change the version of OF used? 38 @param exactNr: Must not have more than the required number of arguments 39 """ 40 self.parser=FoamOptionParser(args=args, 41 description=description, 42 usage=usage, 43 interspersed=interspersed) 44 self.generalOpts=None 45 46 grp=OptionGroup(self.parser, 47 "Default", 48 "Options common to all PyFoam-applications") 49 50 if changeVersion: 51 # the options are evaluated in Basics.FoamOptionParser 52 grp.add_option("--foamVersion", 53 dest="foamVersion", 54 default=None, 55 help="Change the OpenFOAM-version that is to be used") 56 if "WM_PROJECT_VERSION" in environ: 57 grp.add_option("--currentFoamVersion", 58 dest="foamVersion", 59 const=environ["WM_PROJECT_VERSION"], 60 default=None, 61 action="store_const", 62 help="Use the current OpenFOAM-version "+environ["WM_PROJECT_VERSION"]) 63 64 grp.add_option("--force-32bit", 65 dest="force32", 66 default=False, 67 action="store_true", 68 help="Forces the usage of a 32-bit-version if that version exists as 32 and 64 bit. Only used when --foamVersion is used") 69 grp.add_option("--force-64bit", 70 dest="force64", 71 default=False, 72 action="store_true", 73 help="Forces the usage of a 64-bit-version if that version exists as 32 and 64 bit. Only used when --foamVersion is used") 74 grp.add_option("--force-debug", 75 dest="compileOption", 76 const="Debug", 77 default=None, 78 action="store_const", 79 help="Forces the value Debug for the WM_COMPILE_OPTION. Only used when --foamVersion is used") 80 grp.add_option("--force-opt", 81 dest="compileOption", 82 const="Opt", 83 default=None, 84 action="store_const", 85 help="Forces the value Opt for the WM_COMPILE_OPTION. Only used when --foamVersion is used") 86 87 grp.add_option("--psyco-accelerated", 88 dest="psyco", 89 default=False, 90 action="store_true", 91 help="Accelerate the script using the psyco-library (EXPERIMENTAL and requires a separatly installed psyco)") 92 grp.add_option("--profile-python", 93 dest="profilePython", 94 default=False, 95 action="store_true", 96 help="Profile the python-script (not the OpenFOAM-program) - mostly of use for developers") 97 grp.add_option("--profile-cpython", 98 dest="profileCPython", 99 default=False, 100 action="store_true", 101 help="Profile the python-script (not the OpenFOAM-program) using the better cProfile library - mostly of use for developers") 102 grp.add_option("--profile-hotshot", 103 dest="profileHotshot", 104 default=False, 105 action="store_true", 106 help="Profile the python-script using the hotshot-library (not the OpenFOAM-program) - mostly of use for developers - EXPERIMENTAL") 107 grp.add_option("--traceback-on-error", 108 dest="traceback", 109 default=False, 110 action="store_true", 111 help="Prints a traceback when an error is encountered (for debugging)") 112 113 self.parser.add_option_group(grp) 114 115 self.addOptions() 116 self.parser.parse(nr=nr,exactNr=exactNr) 117 self.opts=self.parser.getOptions() 118 119 if self.opts.psyco: 120 try: 121 import psyco 122 psyco.full() 123 except ImportError: 124 warning("No psyco installed. Continuing without acceleration") 125 126 if self.opts.profilePython or self.opts.profileCPython or self.opts.profileHotshot: 127 if sum([self.opts.profilePython,self.opts.profileCPython,self.opts.profileHotshot])>1: 128 self.error("Profiling with hotshot and regular profiling are mutual exclusive") 129 print "Running profiled" 130 if self.opts.profilePython: 131 import profile 132 elif self.opts.profileCPython: 133 import cProfile as profile 134 else: 135 import hotshot 136 profileData=path.basename(sys.argv[0])+".profile" 137 if self.opts.profilePython or self.opts.profileCPython: 138 profile.runctx('self.run()',None,{'self':self},profileData) 139 print "Reading python profile" 140 import pstats 141 stats=pstats.Stats(profileData) 142 else: 143 profileData+=".hotshot" 144 prof=hotshot.Profile(profileData) 145 prof.runctx('self.run()',{},{'self':self}) 146 print "Writing and reading hotshot profile" 147 prof.close() 148 import hotshot.stats 149 stats=hotshot.stats.load(profileData) 150 stats.strip_dirs() 151 stats.sort_stats('time','calls') 152 stats.print_stats(20) 153 else: 154 try: 155 result=self.run() 156 return result 157 except FatalErrorPyFoamException,e: 158 if self.opts.traceback: 159 raise 160 else: 161 self.error(e.descr)
162
163 - def ensureGeneralOptions(self):
164 if self.generalOpts==None: 165 self.generalOpts=OptionGroup(self.parser, 166 "General", 167 "General options for the control of OpenFOAM-runs") 168 self.parser.add_option_group(self.generalOpts)
169
170 - def addOptions(self):
171 """ 172 Add options to the parser 173 """ 174 pass
175
176 - def run(self):
177 """ 178 Run the real application 179 """ 180 error("Not a valid application")
181 182
183 - def error(self,*args):
184 """ 185 Prints an error message and exits 186 @param args: Arguments that are to be printed 187 """ 188 print format.error+"Error in",sys.argv[0],":", 189 for a in args: 190 print a, 191 print format.reset 192 sys.exit(-1)
193
194 - def warning(self,*args):
195 """ 196 Prints a warning message 197 @param args: Arguments that are to be printed 198 """ 199 print format.warn+"Warning in",sys.argv[0],":", 200 for a in args: 201 print a, 202 print format.reset
203
204 - def silent(self,*args):
205 """ 206 Don't print a warning message 207 @param args: Arguments that are to be printed 208 """ 209 pass
210
211 - def checkCase(self,name,fatal=True,verbose=True):
212 """ 213 Check whether this is a valid OpenFOAM-case 214 @param name: the directory-bame that is supposed to be the case 215 @param fatal: If this is not a case then the application ends 216 @param verbose: If this is not a case no warning is issued 217 """ 218 if fatal: 219 func=self.error 220 elif verbose: 221 func=self.warning 222 else: 223 func=self.silent 224 225 if not path.exists(name): 226 func("Case",name,"does not exist") 227 return False 228 if not path.isdir(name): 229 func("Case",name,"is not a directory") 230 return False 231 if not path.exists(path.join(name,"system")): 232 func("Case",name,"does not have a 'system' directory") 233 return False 234 if not path.exists(path.join(name,"constant")): 235 func("Case",name,"does not have a 'constant' directory") 236 return False 237 238 return True
239
240 - def addToCaseLog(self,name,*text):
241 """ 242 Add information about the application that was run to the case-log 243 """ 244 245 logline=[NoTouchSolutionDirectory(name)] 246 logline+=["Application:",path.basename(sys.argv[0])]+sys.argv[1:] 247 logline+=[" | with cwd",getcwd()," | "] 248 logline+=text 249 apply(NoTouchSolutionDirectory.addToHistory,logline)
250
251 - def addLocalConfig(self,directory=None):
252 """ 253 Adds a local directory (assuming it is found) 254 """ 255 if directory!=None: 256 configuration().addFile(path.join(directory,"LocalConfigPyFoam"),silent=True)
257