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 6292 2010-04-01T16:47:07.011733Z 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 
  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 
 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 grp.add_option("--foamVersion", 52 dest="foamVersion", 53 default=None, 54 help="Change the OpenFOAM-version that is to be used") 55 grp.add_option("--force-32bit", 56 dest="force32", 57 default=False, 58 action="store_true", 59 help="Forces the usage of a 32-bit-version if that version exists as 32 and 64 bit. Only used when --foamVersion is used") 60 grp.add_option("--force-64bit", 61 dest="force64", 62 default=False, 63 action="store_true", 64 help="Forces the usage of a 64-bit-version if that version exists as 32 and 64 bit. Only used when --foamVersion is used") 65 grp.add_option("--force-debug", 66 dest="compileOption", 67 const="Debug", 68 default=None, 69 action="store_const", 70 help="Forces the value Debug for the WM_COMPILE_OPTION. Only used when --foamVersion is used") 71 grp.add_option("--force-opt", 72 dest="compileOption", 73 const="Opt", 74 default=None, 75 action="store_const", 76 help="Forces the value Opt for the WM_COMPILE_OPTION. Only used when --foamVersion is used") 77 78 grp.add_option("--psyco-accelerated", 79 dest="psyco", 80 default=False, 81 action="store_true", 82 help="Accelerate the script using the psyco-library (EXPERIMENTAL and requires a separatly installed psyco)") 83 grp.add_option("--profile-python", 84 dest="profilePython", 85 default=False, 86 action="store_true", 87 help="Profile the python-script (not the OpenFOAM-program) - mostly of use for developers") 88 grp.add_option("--profile-cpython", 89 dest="profileCPython", 90 default=False, 91 action="store_true", 92 help="Profile the python-script (not the OpenFOAM-program) using the better cProfile library - mostly of use for developers") 93 grp.add_option("--profile-hotshot", 94 dest="profileHotshot", 95 default=False, 96 action="store_true", 97 help="Profile the python-script using the hotshot-library (not the OpenFOAM-program) - mostly of use for developers - EXPERIMENTAL") 98 99 self.parser.add_option_group(grp) 100 101 self.addOptions() 102 self.parser.parse(nr=nr,exactNr=exactNr) 103 self.opts=self.parser.getOptions() 104 105 if self.opts.psyco: 106 try: 107 import psyco 108 psyco.full() 109 except ImportError: 110 warning("No psyco installed. Continuing without acceleration") 111 112 if self.opts.profilePython or self.opts.profileCPython or self.opts.profileHotshot: 113 if sum([self.opts.profilePython,self.opts.profileCPython,self.opts.profileHotshot])>1: 114 self.error("Profiling with hotshot and regular profiling are mutual exclusive") 115 print "Running profiled" 116 if self.opts.profilePython: 117 import profile 118 elif self.opts.profileCPython: 119 import cProfile as profile 120 else: 121 import hotshot 122 profileData=path.basename(sys.argv[0])+".profile" 123 if self.opts.profilePython or self.opts.profileCPython: 124 profile.runctx('self.run()',None,{'self':self},profileData) 125 print "Reading python profile" 126 import pstats 127 stats=pstats.Stats(profileData) 128 else: 129 profileData+=".hotshot" 130 prof=hotshot.Profile(profileData) 131 prof.runctx('self.run()',{},{'self':self}) 132 print "Writing and reading hotshot profile" 133 prof.close() 134 import hotshot.stats 135 stats=hotshot.stats.load(profileData) 136 stats.strip_dirs() 137 stats.sort_stats('time','calls') 138 stats.print_stats(20) 139 else: 140 self.run()
141
142 - def ensureGeneralOptions(self):
143 if self.generalOpts==None: 144 self.generalOpts=OptionGroup(self.parser, 145 "General", 146 "General options for the control of OpenFOAM-runs") 147 self.parser.add_option_group(self.generalOpts)
148
149 - def addOptions(self):
150 """ 151 Add options to the parser 152 """ 153 pass
154
155 - def run(self):
156 """ 157 Run the real application 158 """ 159 error("Not a valid application")
160 161
162 - def error(self,*args):
163 """ 164 Prints an error message and exits 165 @param args: Arguments that are to be printed 166 """ 167 print format.error+"Error in",sys.argv[0],":", 168 for a in args: 169 print a, 170 print format.reset 171 sys.exit(-1)
172
173 - def warning(self,*args):
174 """ 175 Prints a warning message 176 @param args: Arguments that are to be printed 177 """ 178 print format.warn+"Warning in",sys.argv[0],":", 179 for a in args: 180 print a, 181 print format.reset
182
183 - def silent(self,*args):
184 """ 185 Don't print a warning message 186 @param args: Arguments that are to be printed 187 """ 188 pass
189
190 - def checkCase(self,name,fatal=True,verbose=True):
191 """ 192 Check whether this is a valid OpenFOAM-case 193 @param name: the directory-bame that is supposed to be the case 194 @param fatal: If this is not a case then the application ends 195 @param verbose: If this is not a case no warning is issued 196 """ 197 if fatal: 198 func=self.error 199 elif verbose: 200 func=self.warning 201 else: 202 func=self.silent 203 204 if not path.exists(name): 205 func("Case",name,"does not exist") 206 return False 207 if not path.isdir(name): 208 func("Case",name,"is not a directory") 209 return False 210 if not path.exists(path.join(name,"system")): 211 func("Case",name,"does not have a 'system' directory") 212 return False 213 if not path.exists(path.join(name,"constant")): 214 func("Case",name,"does not have a 'constant' directory") 215 return False 216 217 return True
218
219 - def addToCaseLog(self,name,*text):
220 """ 221 Add information about the application that was run to the case-log 222 """ 223 224 logline=[NoTouchSolutionDirectory(name)] 225 logline+=["Application:",path.basename(sys.argv[0])]+sys.argv[1:] 226 logline+=[" | with cwd",getcwd()," | "] 227 logline+=text 228 apply(NoTouchSolutionDirectory.addToHistory,logline)
229
230 - def addLocalConfig(self,directory=None):
231 """ 232 Adds a local directory (assuming it is found) 233 """ 234 if directory!=None: 235 configuration().addFile(path.join(directory,"LocalConfigPyFoam"),silent=True)
236