1 # Copyright (C) 2012-2013 EDF
3 # This file is part of SALOME HYDRO module.
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.
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.
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/>.
18 Script for launch in terminal of a steering file
25 from salome.kernel.logger import Logger
26 from salome.kernel import termcolor
27 logger = Logger("salome.hydro.run_study.launcher", color = termcolor.BLUE)
28 #logger.setLevel(logging.ERROR)
30 def run_study(param_dict):
32 Run the Python Telemac launching script, eventually preceded and
33 followed by data conversion scripts.
35 interm_files = [] # Intermediate files that can eventually be deleted in the end
37 # Get and eventually create working directory
38 if "REPERTOIRE_TRAVAIL" in param_dict:
39 wrkdir = param_dict["REPERTOIRE_TRAVAIL"]
40 if not os.path.exists(wrkdir):
43 wrkdir = tempfile.mkdtemp(prefix = "tel-")
44 interm_files += [wrkdir]
46 # Read original steering file
47 steering_filepath = param_dict["FICHIER_CAS"]
48 steering_file_dir = os.path.dirname(steering_filepath)
49 with open(steering_filepath) as f:
50 orig_steering = f.read()
51 orig_steering_lines = orig_steering.split("\n")
53 #TODO: replace by call to Telemac Python (import runcode.py) ? for windows compatibility
57 code = param_dict["CODE"]
58 listing_name = "run.log"
59 listing = os.path.join(steering_file_dir, '..', listing_name)
60 cmd = "runcode.py {code} {steering_filepath}" \
61 .format(steering_file_dir=steering_file_dir,
63 steering_filepath=os.path.basename(steering_filepath),
67 logger.debug("Running the following command in salome shell in %s: %s", wrkdir, cmd)
68 log = subprocess.check_output(cmd, cwd=wrkdir, shell=True)
70 return log.decode('utf-8')
73 def check_file_or_create_link(filepath, dirpath, interm_file_list = None):
75 This utility function checks if the file *filepath* is in directory *dirpath*.
76 If not, it creates a link with a unique name in *dirpath* to the file *filepath*
77 and it prepends the link path to the list *interm_file_list* if it is not None.
78 It returns the name of the link if a link was created, or the name of the file
81 filename = os.path.basename(filepath)
82 if not os.path.samefile(dirpath, os.path.dirname(filepath)):
83 name_wo_ext, ext = os.path.splitext(filename)
84 linkpath = tempfile.mktemp(dir = dirpath, prefix = name_wo_ext + "_", suffix = ext)
85 os.symlink(filepath, linkpath)
86 filename = os.path.basename(linkpath)
87 if interm_file_list is not None:
88 interm_file_list[:0] = [linkpath]