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 changeVersion=False,
35 nr=1,
36 interspersed=1)
37
39 self.parser.add_option("--procnr",
40 type="int",
41 dest="procnr",
42 default=None,
43 help="The number of processors the script should be started on")
44 self.parser.add_option("--machinefile",
45 dest="machinefile",
46 default=None,
47 help="The machinefile that specifies the parallel machine")
48 self.parser.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 self.parser.add_option("--restart",
54 action="store_true",
55 default=False,
56 dest="restart",
57 help="Do not clear the Environment from OpenFOAM-specific variables")
58 self.parser.add_option("--taskid",
59 type="int",
60 dest="taskid",
61 default=None,
62 help="The task-ID of a multitask job")
63 self.parser.add_option("--job-id",
64 type="int",
65 dest="jobid",
66 default=666,
67 help="The job-ID")
68 self.parser.add_option("--jobname",
69 dest="jobname",
70 default=None,
71 help="The job-Name")
72
74 scriptName=self.parser.getArgs()[0]
75
76 if self.opts.clear:
77 print "Clearing out old the environment ...."
78 for k in os.environ.keys():
79 if k.find("FOAM")==0 or k.find("WM_")==0:
80 del os.environ[k]
81 continue
82
83 if k=="PATH" or k=="LD_LIBRARY_PATH":
84 tmp=os.environ[k].split(":")
85 vals=[item for item in tmp if item.find("OpenFOAM")<0]
86 os.environ[k]=string.join(vals,":")
87
88 tmpdir=path.join("/tmp","pyClusterTest.%d" % self.opts.jobid)
89 os.environ["TMP"]=tmpdir
90
91 if not path.exists(tmpdir):
92 mkdir(tmpdir)
93
94 if self.opts.procnr!=None:
95 os.environ["NSLOTS"]=str(self.opts.procnr)
96 if self.opts.machinefile!=None:
97 os.environ["PE_HOSTFILE"]=self.opts.machinefile
98
99 machinefile=path.join(tmpdir,"machines")
100 if self.opts.machinefile!=None:
101 open(machinefile,"w").write(open(self.opts.machinefile).read())
102 elif self.opts.procnr!=None:
103 open(machinefile,"w").write("localhost\n"*self.opts.procnr)
104 os.environ["PE_HOSTFILE"]=machinefile
105
106 if self.opts.restart:
107 os.environ["RESTARTED"]="1"
108 else:
109 os.environ["RESTARTED"]="0"
110
111 if self.opts.taskid!=None:
112 os.environ["SGE_TASK_ID"]=str(self.opts.taskid)
113
114 os.environ["JOB_ID"]=str(self.opts.jobid)
115
116 if self.opts.jobname==None:
117 self.opts.jobname=scriptName
118
119 os.environ["JOB_NAME"]=self.opts.jobname
120
121 os.environ["SHELL"]=config().get("Paths","python")
122
123 print "Executing",scriptName
124 if sys.version_info<(2,4):
125 ret=system(config().get("Paths","python")+" "+scriptName)
126 else:
127 ret=subprocess.call([config().get("Paths","python"),scriptName])
128 print "Result=",ret
129