Salome HOME
0c27934995e8db288261243f8c6e49e338c266fc
[modules/hydrosolver.git] / src / salome_hydro / pytel / 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 import os
19 import subprocess
20 import tempfile
21
22 from salome.kernel.logger import Logger
23 from salome.kernel import termcolor
24 logger = Logger("salome.hydro.pytel.launcher", color = termcolor.BLUE)
25 #logger.setLevel(logging.ERROR)
26
27 def run_pytel(param_dict):
28   """
29   Run the Python Telemac launching script (pytel), eventually preceded and
30   followed by data conversion scripts.
31   """
32   interm_files = [] # Intermediate files that can eventually be deleted in the end
33
34   # Get and eventually create working directory
35   if "REPERTOIRE_TRAVAIL" in param_dict:
36     wrkdir = param_dict["REPERTOIRE_TRAVAIL"]
37     if not os.path.exists(wrkdir):
38       os.makedirs(wrkdir)
39   else:
40     wrkdir = tempfile.mkdtemp(prefix = "tel-")
41     interm_files += [wrkdir]
42
43   # Read original steering file
44   steering_filepath = param_dict["FICHIER_CAS"]
45   steering_file_dir = os.path.dirname(steering_filepath)
46   with open(steering_filepath) as f:
47     orig_steering = f.read()
48   orig_steering_lines = orig_steering.split("\n")
49
50   cmd = "set -x ; "
51
52   # Run the code itself
53   code = param_dict["CODE"]
54   cmd += "runcode.py -w %s %s %s" % \
55          (wrkdir, code, steering_filepath)
56   cmd += " ; rc=$?"
57
58   # Cleanup intermediate files if the computation was successful
59   cmd += " ; if test $rc -eq 0; then rm -rf %s ; fi" % " ".join(interm_files)
60
61   cmd += ' ; echo "return code is $rc"'
62
63   # Launch the command
64   logger.debug("Running the following command in xterm: %s" % cmd)
65   args = ["xterm", "-T", "Execution of Telemac", "-geo", "80x60", "-hold", "-l", "-e", cmd]
66   if param_dict.has_key('batchExec'):
67     if param_dict['batchExec'] == True:
68       args = ["xterm", "-T", "Execution of Telemac", "-geo", "80x60", "+hold", "-l", "-e", cmd]
69   subprocess.Popen(args, cwd = wrkdir)
70
71 def check_file_or_create_link(filepath, dirpath, interm_file_list = None):
72   """
73   This utility function checks if the file *filepath* is in directory *dirpath*.
74   If not, it creates a link with a unique name in *dirpath* to the file *filepath*
75   and it prepends the link path to the list *interm_file_list* if it is not None.
76   It returns the name of the link if a link was created, or the name of the file
77   *filepath* otherwise.
78   """
79   filename = os.path.basename(filepath)
80   if not os.path.samefile(dirpath, os.path.dirname(filepath)):
81     name_wo_ext, ext = os.path.splitext(filename)
82     linkpath = tempfile.mktemp(dir = dirpath, prefix = name_wo_ext + "_", suffix = ext)
83     os.symlink(filepath, linkpath)
84     filename = os.path.basename(linkpath)
85     if interm_file_list is not None:
86       interm_file_list[:0] = [linkpath]
87   return filename