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")
56 code = param_dict["CODE"]
57 cmd += "cd %s && runcode.py -w %s %s %s" % \
58 (steering_file_dir, wrkdir, code, steering_filepath)
61 # Cleanup intermediate files if the computation was successful
62 cmd += " ; if test $rc -eq 0; then rm -rf %s ; fi" % " ".join(interm_files)
64 cmd += ' ; echo "return code is $rc"'
67 logger.debug("Running the following command in xterm: %s" % cmd)
68 args = ["xterm", "-T", "Execution of Telemac", "-geo", "80x60", "-hold", "-l", "-e", cmd]
69 if 'batchExec' in param_dict:
70 if param_dict['batchExec'] == True:
71 args = ["xterm", "-T", "Execution of Telemac", "-geo", "80x60", "+hold", "-l", "-e", cmd]
72 subprocess.Popen(args, cwd = wrkdir)
74 def check_file_or_create_link(filepath, dirpath, interm_file_list = None):
76 This utility function checks if the file *filepath* is in directory *dirpath*.
77 If not, it creates a link with a unique name in *dirpath* to the file *filepath*
78 and it prepends the link path to the list *interm_file_list* if it is not None.
79 It returns the name of the link if a link was created, or the name of the file
82 filename = os.path.basename(filepath)
83 if not os.path.samefile(dirpath, os.path.dirname(filepath)):
84 name_wo_ext, ext = os.path.splitext(filename)
85 linkpath = tempfile.mktemp(dir = dirpath, prefix = name_wo_ext + "_", suffix = ext)
86 os.symlink(filepath, linkpath)
87 filename = os.path.basename(linkpath)
88 if interm_file_list is not None:
89 interm_file_list[:0] = [linkpath]