Salome HOME
Correction for Python3
[modules/hydrosolver.git] / src / salome_hydro / run_study / genjob.py
1 #  Copyright (C) 2012-2013 EDF
2 #
3 #  This file is part of SALOME HYDRO module.
4 #
5 #  SALOME HYDRO module is free software: you can redistribute it and/or modify
6 #  it under the terms of the GNU General Public License as published by
7 #  the Free Software Foundation, either version 3 of the License, or
8 #  (at your option) any later version.
9 #
10 #  SALOME HYDRO module 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
13 #  GNU General Public License for more details.
14 #
15 #  You should have received a copy of the GNU General Public License
16 #  along with SALOME HYDRO module.  If not, see <http://www.gnu.org/licenses/>.
17 """
18 Script for launch a steering file via the jobmanager
19 """
20
21 import os
22 import tempfile
23 from datetime import datetime
24 import glob
25
26 import salome
27
28 JOB_SCRIPT_TEMPLATE = """#!/bin/sh
29
30 . %(env_file)s
31 runcode.py %(code)s %(cas)s --ncsize %(nbcore)d
32 cd -
33 """
34
35 def generate_job(study_params, resource, telemac_root_dir, telemac_env_file,
36                  nbcore, input_data_dir, result_dir):
37     """
38     Create a Launcher job using the parameters specified by the user.
39     """
40     # Generate job script
41     basename = os.path.basename(study_params["FICHIER_CAS"])
42     job_script = JOB_SCRIPT_TEMPLATE % {"env_file": telemac_env_file,
43                                         "code": study_params["CODE"],
44                                         "cas": basename,
45                                         "nbcore": nbcore}
46
47
48     (fd, job_script_file) = \
49             tempfile.mkstemp(prefix="job_" + basename + "_", suffix=".sh")
50     os.close(fd)
51     f = open(job_script_file, "w")
52     f.write(job_script)
53     f.close()
54
55     # Define job parameters
56     job_params = salome.JobParameters()
57     job_params.job_name = basename
58     job_params.job_type = "command"
59     job_params.job_file = job_script_file
60     input_files = glob.glob(os.path.join(input_data_dir, "*")) \
61                   + [study_params["FICHIER_CAS"]]
62     input_files = list(set(input_files)) # Remove duplicates
63     job_params.in_files = input_files
64     job_params.out_files = []
65     job_params.result_directory = result_dir
66
67     # Define resource parameters
68     job_params.resource_required = salome.ResourceParameters()
69     job_params.resource_required.name = resource
70     job_params.resource_required.nb_proc = nbcore
71
72     # Generate name for the working directory
73     res_manager = salome.naming_service.Resolve("/ResourcesManager")
74     res_definition = res_manager.GetResourceDefinition(resource)
75     res_work_dir = res_definition.working_directory
76     if res_work_dir != "":
77         timestr = datetime.now().ctime()
78         timestr = timestr.replace('/', '_')
79         timestr = timestr.replace('-', '_')
80         timestr = timestr.replace(':', '_')
81         timestr = timestr.replace(' ', '_')
82         work_dir = res_work_dir + "/" + job_params.job_name + "_" + timestr
83         job_params.work_directory = work_dir
84
85     # Create Launcher job
86     launcher = salome.naming_service.Resolve('/SalomeLauncher')
87     launcher.createJob(job_params)