Salome HOME
Copyright update 2021
[tools/libbatch.git] / src / Python / Test / Test_Python_Local_SH.py
1 # Copyright (C) 2007-2021  CEA/DEN, EDF R&D, OPEN CASCADE
2 #
3 # Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 #
6 # This library is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU Lesser General Public
8 # License as published by the Free Software Foundation; either
9 # version 2.1 of the License, or (at your option) any later version.
10 #
11 # This library is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 # Lesser General Public License for more details.
15 #
16 # You should have received a copy of the GNU Lesser General Public
17 # License along with this library; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 #
20 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 #
22
23 import config
24 import os
25 import sys
26 import time
27
28 # Import libbatch library
29 from libbatch import *
30
31 def work():
32     print("*******************************************************************************************")
33     print("This script tests the local batch submission based on SH. No specific configuration is")
34     print("needed for this test.")
35     print("*******************************************************************************************")
36
37     # eventually remove any previous result
38     if (os.path.exists("resultdir/seconddirname/result.txt")):
39         os.remove("resultdir/seconddirname/result.txt")
40
41     # Define the job...
42     job = Job()
43     # ... and its parameters ...
44     p = {}
45     p[EXECUTABLE] = config.TEST_SOURCE_DIR + "/test_script.py";
46     p[ARGUMENTS]  = ["copied_seta.py", "copied_setb.py", "orig_result.txt"];
47     p[NAME] = 'Test_Python_Local_SH'
48     p[WORKDIR] = config.TEST_LOCAL_SH_WORKDIR
49     p[INFILE] = [(config.TEST_SOURCE_DIR + '/seta.py', 'copied_seta.py'),
50                  (config.TEST_SOURCE_DIR + '/setb.py', 'copied_setb.py')]
51     p[OUTFILE] = [('result.txt', 'orig_result.txt')]
52     job.setParametre(p)
53     # ... and its environment
54     e = {}
55     e["MYENVVAR"] = "MYVALUE";
56     job.setEnvironnement(e)
57     print(job)
58
59     # Get the catalog
60     c = BatchManagerCatalog.getInstance()
61
62     # Create a BatchManager of type Local_SH on localhost
63     bm = c('LOCAL')('localhost', '', SH)
64
65     # Submit the job to the BatchManager
66     jobid = bm.submitJob(job)
67     print(jobid)
68
69     # Query the job
70     jobid.queryJob()
71
72     # Wait for the end of the job
73     state = bm.waitForJobEnd(jobid, config.TEST_LOCAL_SH_TIMEOUT);
74
75
76     if state == FINISHED:
77         print("Job", jobid, "is done")
78         bm.importOutputFiles(job, "resultdir/seconddirname")
79     elif state == FAILED:
80         print("Job", jobid, " finished in error")
81         bm.importOutputFiles(job, "resultdir/seconddirname")
82         return 1
83     else:
84         print("Timeout while executing job")
85         return 1
86
87     if state != FINISHED and state != FAILED:
88         print("Error: Job not finished after timeout")
89         return 1;
90
91     # test the result file
92     res = {}
93     exec(compile(open('resultdir/seconddirname/result.txt').read(), 'resultdir/seconddirname/result.txt', 'exec'), res)
94     if (res["c"] == 12 and res["MYENVVAR"] == "MYVALUE"):
95       print("OK, Expected result found.")
96       return 0
97     else:
98       print("result found : %s, expected : %s" % (res, 'res["c"] == 12 and res["MYENVVAR"] == "MYVALUE"'))
99       return 1
100
101 if __name__ == "__main__":
102     retcode = work()
103     sys.exit(retcode)