1 #! /usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 # Copyright (C) 2017-2020 CEA/DEN, EDF R&D
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2.1 of the License, or (at your option) any later version.
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # Lesser General Public License for more details.
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 """ Easier access to SalomeLauncher"""
27 JOB_FILE_NAME = "jobDump.xml"
31 This class makes an easier access to SalomeLauncher.
32 It adds an automatic save of the job's parameters after the launch. The save
33 is done into the result directory of the job. It is then possible to reload
34 the job from that file. It also provides a waiting loop for the job to finish.
35 This proxy also hides the calls to the naming service in order to get the
36 instance of SalomeLauncher.
40 def launch(job_params):
41 """ Create, submit and dump a new job to result_dir."""
43 launcher = salome.naming_service.Resolve('/SalomeLauncher')
44 myjob.launcher = launcher
45 result_dir = job_params.result_directory # local path where to copy out_files
47 myjob.job_id = launcher.createJob(job_params) #SALOME id of the job
48 launcher.launchJob(myjob.job_id) # copy files, run pre_command, submit job
49 myjob.saveJob(result_dir)
53 def reloadJob(result_dir):
54 """ Create a new job from a job dumped in result_dir."""
56 launcher = salome.naming_service.Resolve('/SalomeLauncher')
57 myjob.launcher = launcher
60 job_file_path = os.path.join(result_dir, JOB_FILE_NAME)
62 with open(job_file_path, "r") as f:
64 myjob.job_id = launcher.restoreJob(job_string)
69 def saveJob(self, result_dir):
70 job_string = self.launcher.dumpJob(self.job_id)
71 job_file_path = os.path.join(result_dir, JOB_FILE_NAME)
72 if not os.path.exists(result_dir):
73 os.makedirs(result_dir)
74 with open(job_file_path, "w") as f:
77 def wait(self, sleep_delay=10):
78 """ Wait for the end of the job """
79 launcher = self.launcher
81 jobState = launcher.getJobState(job_id)
82 while jobState != "FINISHED" and jobState != "FAILED" :
83 time.sleep(sleep_delay)
84 jobState = launcher.getJobState(job_id)
85 print ("Job %d state: %s" % (job_id,jobState))
88 """ Get the return code of the solver if the job is finished.
89 If the job is not finished (submission error or another state),
90 return an empty string.
92 launcher = self.launcher
94 jobState = launcher.getJobState(job_id)
96 if jobState != "FINISHED" :
97 print ("Job has not finished correctly.")
98 print ("Job %d state: %s" % (job_id,jobState))
100 job_params = launcher.getJobParameters(job_id)
101 temp_dir = job_params.result_directory
102 temp_log_dir = os.path.join(temp_dir, "logs")
103 if(launcher.getJobWorkFile(job_id, "logs/exit_code.log", temp_log_dir)):
104 exit_code_file = os.path.join(temp_log_dir, "exit_code.log")
105 if os.path.isfile(exit_code_file):
106 with open(exit_code_file) as myfile:
107 exit_code = myfile.read()
108 return exit_code.strip()
110 def getResults(self):
111 """ Copy the result files from remote working_directory
112 to the local result_directory."""
113 self.launcher.getJobResults(self.job_id, "")
115 def relaunch(self, script=""):
116 job_params = self.launcher.getJobParameters(self.job_id)
118 job_params.job_file = script
119 job_params.pre_command = ""
120 self.job_id = self.launcher.createJob(job_params)
121 self.launcher.launchJob(self.job_id)
122 self.saveJob(job_params.result_directory)