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

Source Code for Module PyFoam.Applications.PyFoamApplication

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