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