Salome HOME
edf5f31ca63dc7b0c0f91a62db9a1affe541e846
[modules/hydrosolver.git] / src / salome_hydro / pytel / 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 import os
19 import tempfile
20 from datetime import datetime
21 import glob
22
23 import salome
24
25 convertion_script_template = """
26 converter.py %(input_file)s -b %(log_file)s -o %(output_file)s
27 """
28
29 job_script_template = """#!/bin/sh
30
31 . %(env_file)s
32 runcode.py %(code)s %(cas)s --ncsize %(nbcore)d
33 """
34
35 def generate_job(pytel_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(pytel_params["FICHIER_CAS"])
42   job_script = job_script_template % {"env_file": telemac_env_file,
43                                       "code": pytel_params["CODE"],
44                                       "cas": basename,
45                                       "nbcore": nbcore}
46
47
48   (fd, job_script_file) = tempfile.mkstemp(prefix = "job_" + basename + "_", suffix = ".sh")
49   os.close(fd)
50   f = open(job_script_file, "w")
51   f.write(job_script)
52   f.close()
53
54   # Define job parameters
55   job_params = salome.JobParameters()
56   job_params.job_name = basename
57   job_params.job_type = "command"
58   job_params.job_file = job_script_file
59   input_files = glob.glob(os.path.join(input_data_dir, "*")) + [pytel_params["FICHIER_CAS"]]
60   input_files = list(set(input_files)) # Remove duplicates
61   job_params.in_files = input_files
62   job_params.out_files = []
63   job_params.result_directory = result_dir
64
65   # Define resource parameters
66   job_params.resource_required = salome.ResourceParameters()
67   job_params.resource_required.name = resource
68   job_params.resource_required.nb_proc = nbcore
69
70   # Generate name for the working directory
71   res_manager = salome.naming_service.Resolve("/ResourcesManager")
72   res_definition = res_manager.GetResourceDefinition(resource)
73   res_work_dir = res_definition.working_directory
74   if res_work_dir != "":
75       timestr = datetime.now().ctime()
76       timestr = timestr.replace('/', '_')
77       timestr = timestr.replace('-', '_')
78       timestr = timestr.replace(':', '_')
79       timestr = timestr.replace(' ', '_')
80       work_dir = res_work_dir + "/" + job_params.job_name + "_" + timestr
81       job_params.work_directory = work_dir
82   
83   # Create Launcher job
84   launcher = salome.naming_service.Resolve('/SalomeLauncher')
85   launcher.createJob(job_params)