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