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