1
2 """
3 Application class that implements pyFoamClusterTester
4 """
5 import sys
6
7 if sys.version_info<(2,4):
8 from os import system
9 else:
10 import subprocess
11
12 import os,string
13 from os import mkdir,path
14 from optparse import OptionGroup
15
16 from PyFoamApplication import PyFoamApplication
17 from PyFoam.FoamInformation import changeFoamVersion
18 from PyFoam import configuration as config
19
20 from CommonParallel import CommonParallel
21
25 description="""
26 Is used to test Cluster-Scripts before they are submitted to the
27 cluster. It tries to resemble the environment the script will find. Cluster in
28 this context means the Sun Grid Engine
29 """
30
31 PyFoamApplication.__init__(self,
32 args=args,
33 description=description,
34 usage="%prog [options] <cluster-script> <script options>",
35 changeVersion=False,
36 nr=1,
37 exactNr=False,
38 interspersed=1)
39
41 general=OptionGroup(self.parser,
42 "Cluster General",
43 "Stuff that is similar for all queueing implementations")
44 general.add_option("--no-clear",
45 action="store_false",
46 default=True,
47 dest="clear",
48 help="Do not clear the Environment from OpenFOAM-specific variables")
49 general.add_option("--restart",
50 action="store_true",
51 default=False,
52 dest="restart",
53 help="Treat the case as being restarted")
54 self.parser.add_option_group(general)
55
56 sge=OptionGroup(self.parser,
57 "SGE",
58 "Stuff that is specific to a SunGridEngine-environment")
59 sge.add_option("--taskid",
60 type="int",
61 dest="taskid",
62 default=None,
63 help="The task-ID of a multitask job")
64 sge.add_option("--job-id",
65 type="int",
66 dest="jobid",
67 default=666,
68 help="The job-ID")
69 sge.add_option("--jobname",
70 dest="jobname",
71 default=None,
72 help="The job-Name")
73 self.parser.add_option_group(sge)
74
75 CommonParallel.addOptions(self)
76
78 scriptName=self.parser.getArgs()[0]
79
80 if self.opts.clear:
81 print "Clearing out old the environment ...."
82 for k in os.environ.keys():
83 if k.find("FOAM")==0 or k.find("WM_")==0:
84 del os.environ[k]
85 continue
86
87 if k=="PATH" or k=="LD_LIBRARY_PATH":
88 tmp=os.environ[k].split(":")
89 vals=[item for item in tmp if item.find("OpenFOAM")<0]
90 os.environ[k]=string.join(vals,":")
91
92 tmpdir=path.join("/tmp","pyClusterTest.%d" % self.opts.jobid)
93 os.environ["TMP"]=tmpdir
94
95 if not path.exists(tmpdir):
96 mkdir(tmpdir)
97
98 if self.opts.procnr!=None:
99 os.environ["NSLOTS"]=str(self.opts.procnr)
100 if self.opts.machinefile!=None:
101 os.environ["PE_HOSTFILE"]=self.opts.machinefile
102
103 machinefile=path.join(tmpdir,"machines")
104 if self.opts.machinefile!=None:
105 open(machinefile,"w").write(open(self.opts.machinefile).read())
106 elif self.opts.procnr!=None:
107 open(machinefile,"w").write("localhost\n"*self.opts.procnr)
108 os.environ["PE_HOSTFILE"]=machinefile
109
110 if self.opts.restart:
111 os.environ["RESTARTED"]="1"
112 else:
113 os.environ["RESTARTED"]="0"
114
115 if self.opts.taskid!=None:
116 os.environ["SGE_TASK_ID"]=str(self.opts.taskid)
117
118 os.environ["JOB_ID"]=str(self.opts.jobid)
119
120 if self.opts.jobname==None:
121 self.opts.jobname=scriptName
122
123 os.environ["JOB_NAME"]=self.opts.jobname
124
125 os.environ["SHELL"]=config().get("Paths","python")
126
127 callString=scriptName
128 if len(self.parser.getArgs())>1:
129 for a in self.parser.getArgs()[1:]:
130 callString+=" "+a
131
132 print "Executing",callString
133 if sys.version_info<(2,4):
134 ret=system(config().get("Paths","python")+" "+callString)
135 else:
136 ret=subprocess.call([config().get("Paths","python")]+self.parser.getArgs())
137 print "Result=",ret
138