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