X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=bin%2FrunSession.py;h=c83cf502f87d182ccc7f42ad8bdf7cae8d41c550;hb=28ccecc39947373ea231ff46a3d0c9a5c2448bc3;hp=c83ef01a1d4c7e3e3b18dd984d45e5338b90cb62;hpb=718e0abe0126e5e53b3ba41fff1028efcf5bc887;p=modules%2Fkernel.git diff --git a/bin/runSession.py b/bin/runSession.py index c83ef01a1..c83cf502f 100644 --- a/bin/runSession.py +++ b/bin/runSession.py @@ -1,5 +1,5 @@ # -*- coding: iso-8859-1 -*- -# Copyright (C) 2007-2016 CEA/DEN, EDF R&D, OPEN CASCADE +# Copyright (C) 2007-2021 CEA/DEN, EDF R&D, OPEN CASCADE # # Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS @@ -29,7 +29,7 @@ import socket import subprocess import re from salomeContextUtils import getScriptsAndArgs, formatScriptsAndArgs, getShortAndExtraArgs -from salome_utils import getUserName +from salome_utils import getUserName, getShortHostName # Use to display newlines (\n) in epilog class MyParser(OptionParser): @@ -118,15 +118,25 @@ User "myself" connects to remotemachine to run the script concatenate.py in help="[Remote mode] The user on the computer to connect to." ) + parser.add_option('-l', '--launcher', dest="launcher", default=None, + help="[Remote mode] The machine and the port to connect to." + ) + short_args, extra_args = getShortAndExtraArgs(args) try: (options, args) = parser.parse_args(short_args) - except Exception, e: - print e - return + except Exception as e: + print(e) + return None, [] port = options.port host = options.host + launcher = options.launcher + if launcher is not None: + pos = launcher.find(":") + if pos != -1: + host = launcher[0:pos] + port = launcher[pos+1:] # :GLITCH: this code defines specific environment variables (OMNIORB_CONFIG, NSPORT, # NSHOST) which are later used by other modules. Working, but not really "safe"... @@ -140,6 +150,11 @@ User "myself" connects to remotemachine to run the script concatenate.py in os.environ['OMNIORB_CONFIG'] = fileOmniConfig # --- set environment variables for port and hostname of NamingService host, port = getNSparams() + try: + # keep short name for host, for a correct comparison with getShortHostName() later + host=host.split('.')[0] + except: + pass else: # No running session host = "no_host" @@ -152,17 +167,17 @@ User "myself" connects to remotemachine to run the script concatenate.py in else: if not host: # only PORT is given - host = socket.gethostname() + host = getShortHostName() # both MACHINE and PORT are given _writeConfigFile(port, host) # os.environ['NSPORT'] = port os.environ['NSHOST'] = host - # determine running mode, taht is either 'local' or 'remote' - here = socket.gethostname() + # determine running mode, that is either 'local' or 'remote' + here = getShortHostName() mode = "local" - if host != here and host != "localhost" and host != "no_host": + if host != here and host != "localhost" and host != "no_host" and launcher is None: mode="remote" pass params = SessionParameters(mode, port, host, options.user, options.directory) @@ -193,6 +208,7 @@ def __runLocalSession(command): for cmd in command: single_cmd = cmd.strip().split(' ') any_error = False + error_code = 1 try: proc = subprocess.Popen(single_cmd) (stdoutdata, stderrdata) = proc.communicate() # Wait for process to terminate @@ -203,6 +219,7 @@ def __runLocalSession(command): if proc.returncode != 0: any_error = True + error_code = proc.returncode except: any_error = True pass @@ -213,9 +230,9 @@ def __runLocalSession(command): sys.stdout.write("".join(outmsg)) if errmsg: sys.stderr.write("".join(errmsg)) - sys.exit(1) + sys.exit(error_code) - return ("".join(outmsg), "".join(errmsg)) + return 0 else: absoluteAppliPath = os.getenv('ABSOLUTE_APPLI_PATH','') if sys.platform == "win32": @@ -223,7 +240,8 @@ def __runLocalSession(command): else: cmd = ["/bin/bash", "--rcfile", absoluteAppliPath + "/.bashrc" ] proc = subprocess.Popen(cmd, shell=False, close_fds=True) - return proc.communicate() + proc.communicate() + return proc.returncode # def __copyFiles(user, machine, script, infiles, outfiles): @@ -249,7 +267,7 @@ def __copyFiles(user, machine, script, infiles, outfiles): # copy the infile to the remote server cmd = "scp %s %s@%s:%s" % (infile, user, machine, tmp_file) - print "[ SCP ]", cmd + print("[ SCP ]", cmd) os.system(cmd) list_infiles.append(tmp_file) @@ -273,7 +291,7 @@ def __copyFiles(user, machine, script, infiles, outfiles): # copy the salome script on the remote server cmd = "scp %s %s@%s:%s" % (tmp_script, user, machine, tmp_script) - print "[ SCP ]", cmd + print("[ SCP ]", cmd) os.system(cmd) return list_infiles, list_outfiles, tmp_script @@ -282,11 +300,11 @@ def __copyFiles(user, machine, script, infiles, outfiles): # sa_obj is a ScriptAndArgs object (from salomeContextUtils) def __runRemoteSession(sa_obj, params): if not params.user: - print "ERROR: The user login on remote machine MUST be given." - return + print("ERROR: The user login on remote machine MUST be given.") + return 1 if not params.directory: - print "ERROR: The remote directory MUST be given." - return + print("ERROR: The remote directory MUST be given.") + return 1 # sa_obj.script may be 'python script.py' --> only process .py file header = " ".join(sa_obj.script.split()[:-1]) @@ -295,11 +313,13 @@ def __runRemoteSession(sa_obj, params): tmp_in, tmp_out, tmp_script = __copyFiles(params.user, params.machine, script, sa_obj.args or [], sa_obj.out or []) # execute command on the remote SALOME application - command = "ssh %s@%s %s/salome shell " % (params.user, params.machine, params.directory) + command = "%s/salome shell" % (params.directory) if params.port: - command = command + "-p %s "%params.port - command = command + " %s %s args:%s"%(header, tmp_script, ",".join(tmp_in)) - print '[ SSH ] ' + command + command += " -p %s "%params.port + command += " %s %s args:%s"%(header, tmp_script, ",".join(tmp_in)) + # salome shell command must run in a login shell because of module function + command = "ssh %s@%s -t 'bash -l -c \"%s\"'" % (params.user, params.machine, command) + print('[ SSH ] ' + command) os.system(command) # Get remote files and clean @@ -309,15 +329,16 @@ def __runRemoteSession(sa_obj, params): for outfile in (sa_obj.out or []): remote_outfile = tmp_out.pop(0) command = "scp %s@%s:%s %s" %(params.user, params.machine, remote_outfile, outfile) - print "[ SCP ] " + command + print("[ SCP ] " + command) os.system(command) # clean temporary files command = "ssh %s@%s \\rm -f %s" % (params.user, params.machine, " ".join(temp_files)) - print '[ SSH ] ' + command + print('[ SSH ] ' + command) os.system(command) os.remove(tmp_script) + return 0 # def runSession(params, args): @@ -328,6 +349,10 @@ def runSession(params, args): return __runLocalSession(command) elif params.mode == "remote": + any_error = 0 for sa_obj in scriptArgs: - __runRemoteSession(sa_obj, params) + ok = __runRemoteSession(sa_obj, params) + if not ok: + any_error = 1 + return any_error #