Salome HOME
Correction for Python3
[modules/hydrosolver.git] / src / salome_hydro / run_study / launcher.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 in terminal of a steering file
19 """
20
21 import os
22 import subprocess
23 import tempfile
24
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)
29
30 def run_study(param_dict):
31   """
32   Run the Python Telemac launching script, eventually preceded and
33   followed by data conversion scripts.
34   """
35   interm_files = [] # Intermediate files that can eventually be deleted in the end
36
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):
41       os.makedirs(wrkdir)
42   else:
43     wrkdir = tempfile.mkdtemp(prefix = "tel-")
44     interm_files += [wrkdir]
45
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")
52
53   cmd = "set -x ; "
54
55   # Run the code itself
56   code = param_dict["CODE"]
57   cmd += "cd %s && runcode.py -w %s %s %s" % \
58          (steering_file_dir, wrkdir, code, steering_filepath)
59   cmd += " ; rc=$?"
60
61   # Cleanup intermediate files if the computation was successful
62   cmd += " ; if test $rc -eq 0; then rm -rf %s ; fi" % " ".join(interm_files)
63
64   cmd += ' ; echo "return code is $rc"'
65
66   # Launch the command
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)
73
74 def check_file_or_create_link(filepath, dirpath, interm_file_list = None):
75   """
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
80   *filepath* otherwise.
81   """
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]
90   return filename