Salome HOME
Padder execution error management (the errors are raised toward the gui)
[modules/smesh.git] / src / Tools / padder / unittests / usecase_meshJobManager.py
1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2011-2012  EDF R&D
3 #
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License.
8 #
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 # Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 #
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #
20 # Author(s): Guillaume Boulant (23/03/2011)
21 #
22
23 # This script illustrates the standard use case of the component
24 # MeshJobManager from within a SALOME script. It could be used as a
25 # unit test of the component. The typical procedure is:
26 # $ <appli>/runAppli -t
27 # $ <appli>/runSession </path/to>/usecase_meshJobManager.py
28
29 #
30 # =======================================================================
31 # Preparing the configuration parameters
32 # =======================================================================
33 #
34 import sys
35 import os
36 import time
37 from salome.smesh.spadder.configreader import ConfigReader, printConfig, getPadderTestDir
38
39 configReader = ConfigReader()
40 defaultConfig = configReader.getDefaultConfig()
41 printConfig(defaultConfig)
42
43 from salome.smesh import spadder
44
45 import salome
46 import MESHJOB
47
48 #
49 # Setup the configuration in the component. We first have to load the
50 # catalog of SPADDER components, then load the component
51 # MeshJobManager, and finally configure this component.
52 #
53 spadder.loadSpadderCatalog()
54
55 salome.salome_init()
56 component = salome.lcc.FindOrLoadComponent("FactoryServer","MeshJobManager")
57 config = MESHJOB.ConfigParameter(resname=defaultConfig.resname,
58                                  binpath=defaultConfig.binpath,
59                                  envpath=defaultConfig.envpath)
60
61 configId = "localhost"
62 component.configure(configId,config)
63
64
65 #
66 # =======================================================================
67 # Define several datasets for the different use cases
68 # =======================================================================
69 #
70
71 # We define several functions that create each a dataset of med files
72 # for testing the component. The test function number corresponds to
73 # the number of the test defined in the SpherePadder installation
74 # directory.
75 PADDERTESTDIR = getPadderTestDir(defaultConfig)
76 #PADDERTESTDIR = spadder.getTestPadderDataDir()
77 #
78 # WARN: the above instruction (spadder.getTestPadderDataDir())
79 # localizes the PADDERTEST DIR using the PADDERDIR shell variable,
80 # while the previous one (getPadderTestDir) localizes this directory
81 # from data of the config (read from the configuration file
82 # padder.cfg).
83 #
84 def test00_parameters():
85     """Test using a concrete mesh and a single steelbar mesh""" 
86     file_concrete=os.path.join(spadder.getTestDataDir(),"concrete.med")
87     file_steelbar=os.path.join(spadder.getTestDataDir(),"ferraill.med")
88
89     meshJobParameterList = []
90     param = MESHJOB.MeshJobParameter(file_name=file_concrete,
91                                      file_type=MESHJOB.MED_CONCRETE,
92                                      group_name="concrete")
93     meshJobParameterList.append(param)
94
95     param = MESHJOB.MeshJobParameter(file_name=file_steelbar,
96                                      file_type=MESHJOB.MED_STEELBAR,
97                                      group_name="steelbar")
98     meshJobParameterList.append(param)
99     return meshJobParameterList
100
101 def test01_parameters():
102     """One concrete mesh and two steelbar meshes"""
103     datadir = os.path.join(PADDERTESTDIR,"test01")
104     meshJobParameterList = []
105
106     medfile = os.path.join(datadir,"concrete.med")
107     param = MESHJOB.MeshJobParameter(file_name=medfile,
108                                      file_type=MESHJOB.MED_CONCRETE,
109                                      group_name="concrete")
110     meshJobParameterList.append(param)
111     
112     medfile = os.path.join(datadir,"ferraill.med")
113     param = MESHJOB.MeshJobParameter(file_name=medfile,
114                                      file_type=MESHJOB.MED_STEELBAR,
115                                      group_name="ferraill")
116     meshJobParameterList.append(param)
117
118     medfile = os.path.join(datadir,"ferrtran.med")
119     param = MESHJOB.MeshJobParameter(file_name=medfile,
120                                      file_type=MESHJOB.MED_STEELBAR,
121                                      group_name="ferrtran")
122     meshJobParameterList.append(param)
123     
124     return meshJobParameterList
125
126 def test02_parameters():
127     """One steelbar mesh only, without a concrete mesh"""
128     datadir = os.path.join(PADDERTESTDIR,"test02")
129     meshJobParameterList = []
130
131     medfile = os.path.join(datadir,"cadreef.med")
132     param = MESHJOB.MeshJobParameter(file_name=medfile,
133                                      file_type=MESHJOB.MED_STEELBAR,
134                                      group_name="cadre")
135     meshJobParameterList.append(param)
136     return meshJobParameterList
137
138 def test03_parameters():
139     """One concrete mesh only, without a steelbar mesh"""
140     datadir = os.path.join(PADDERTESTDIR,"test03")
141     meshJobParameterList = []
142
143     medfile = os.path.join(datadir,"concrete.med")
144     param = MESHJOB.MeshJobParameter(file_name=medfile,
145                                      file_type=MESHJOB.MED_CONCRETE,
146                                      group_name="concrete")
147     meshJobParameterList.append(param)
148     return meshJobParameterList
149
150 #
151 # =======================================================================
152 # Prepare the job parameters and initialize the job
153 # =======================================================================
154 #
155
156 # Choose here the use case
157 #meshJobParameterList = test00_parameters()
158 #meshJobParameterList = test01_parameters()
159 #meshJobParameterList = test02_parameters()
160 meshJobParameterList = test03_parameters()
161
162 #
163 # Prepare, start and follow-up the job
164 #
165 jobid = component.initialize(meshJobParameterList, configId)
166 if jobid<0:
167     msg = component.getLastErrorMessage()
168     print "ERR: %s"%msg
169     sys.exit(1)
170     
171 created = False
172 nbiter  = 0
173 while not created:
174     state = component.getState(jobid)
175     print "MeshJobManager ["+str(nbiter)+"] : state = "+str(state)
176     if state == "CREATED":
177         created = True
178     time.sleep(0.5)
179     nbiter+=1
180
181
182 #
183 # =======================================================================
184 # Submit the job and start the supervision
185 # =======================================================================
186 #
187 # Start the execution of the job identified by its job id.
188 #
189 ok=component.start(jobid)
190 if not ok:
191     msg = component.getLastErrorMessage()
192     print "ERR: %s"%msg
193     sys.exit(1)
194
195 print "job started: %s"%ok
196
197 #
198 # This part illustrates how you can follow the execution of the job.
199 #
200 run_states = ["CREATED", "IN_PROCESS", "QUEUED", "RUNNING", "PAUSED"];
201 end_states = ["FINISHED", "ERROR"]
202 all_states = run_states+end_states;
203
204 ended  = False
205 nbiter = 0
206 while not ended:
207     state = component.getState(jobid)
208     print "MeshJobManager ["+str(nbiter)+"] : state = "+str(state)
209     if state not in run_states:
210         ended=True
211     time.sleep(0.5)
212     nbiter+=1
213         
214 if state not in end_states:
215     print "ERR: jobid = "+str(jobid)+" ended abnormally with state="+str(state)
216     msg = component.getLastErrorMessage()
217     print "ERR: %s"%msg    
218 else:
219     print "OK:  jobid = "+str(jobid)+" ended with state="+str(state)
220     meshJobResults = component.finalize(jobid)
221     print meshJobResults
222     if meshJobResults.status is not True:
223         print "ERR: the results are not OK: %s"%component.getLastErrorMessage()
224         print "ERR: see log files in %s"%meshJobResults.results_dirname