X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=bin%2FrunSession.py;h=f698e323d252914a436b0cbc298a2c3cbb9d49b8;hb=3091a8beac430b5b36c7606fc672dcf869a8683d;hp=0e2fd7ce1b5e3b48204e0d0903036b349f663ef8;hpb=6d9c102060aa7ad2e25f3e252ab2c1d57de4d38f;p=modules%2Fkernel.git diff --git a/bin/runSession.py b/bin/runSession.py index 0e2fd7ce1..f698e323d 100644 --- a/bin/runSession.py +++ b/bin/runSession.py @@ -1,5 +1,5 @@ # -*- coding: iso-8859-1 -*- -# Copyright (C) 2007-2012 CEA/DEN, EDF R&D, OPEN CASCADE +# Copyright (C) 2007-2014 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 @@ -7,7 +7,7 @@ # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public # License as published by the Free Software Foundation; either -# version 2.1 of the License. +# version 2.1 of the License, or (at your option) any later version. # # This library is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of @@ -25,6 +25,8 @@ import os import sys from optparse import OptionParser from NSparam import getNSparams +import socket +import subprocess # Use to display newlines (\n) in epilog class MyParser(OptionParser): @@ -32,20 +34,26 @@ class MyParser(OptionParser): return self.epilog # -def configureSession(args=[]): - usage = "Usage: %prog [options]" - epilog = """\nIf the command is not given a shell is opened. +def configureSession(args=None): + if args is None: + args = [] + usage = "Usage: %prog [options] [command]" + epilog = """\n +If the command is not given a shell is opened; else execute the given command. +Command may be a series of Python scripts with arguments: [PYTHON_FILE [args] [PYTHON_FILE [args]...]] +Python file arguments, if any, must be comma-separated (without blank characters) and prefixed by "args:" (without quotes), e.g. myscript.py args:arg1,arg2=val,... +\n If PORT and MACHINE are not given, try to connect to the last active session on the local machine. If PORT and MACHINE are given, try to connect to the remote session associated with PORT on MACHINE. If MACHINE is not given, try to connect to the session associated to PORT on the local machine. If PORT is not given, try to connect to the remote session associated to port 2810 on MACHINE.\n\n""" parser = MyParser(usage=usage, epilog=epilog) parser.add_option("-p", "--port", metavar="", default=0, - action="store", type="int", dest="port", + action="store", type="string", dest="port", help="The port to connect to." ) parser.add_option("-m", "--machine", metavar="", default=0, - action="store", type="int", dest="machine", + action="store", type="string", dest="host", help="The machine to connect to." ) try: @@ -55,48 +63,43 @@ If PORT is not given, try to connect to the remote session associated to port 28 return port = options.port - machine = options.machine + host = options.host # :GLITCH: this code defines specific environment variables (OMNIORB_CONFIG, NSPORT, # NSHOST) which are later used by other modules. Working, but not really "safe"... if not port: - if not machine: + if not host: # neither MACHINE nor PORT are given # --- set omniORB configuration to current session if any - absoluteAppliPath = os.environ['ABSOLUTE_APPLI_PATH'] - fileOmniConfig = absoluteAppliPath + '/USERS/.omniORB_' + os.environ['USER'] + '_last.cfg' + omniorbUserPath = os.environ['OMNIORB_USER_PATH'] + fileOmniConfig = omniorbUserPath + '/.omniORB_' + os.environ['USER'] + '_last.cfg' if os.path.isfile(fileOmniConfig): os.environ['OMNIORB_CONFIG'] = fileOmniConfig # --- set environment variables for port and hostname of NamingService host, port = getNSparams() - os.environ['NSHOST'] = host - os.environ['NSPORT'] = port else: # No running session - os.environ['NSHOST'] = "no_host" - os.environ['NSPORT'] = "no_port" - pass + host = "no_host" + port = "no_port" else: # only MACHINE is given - _writeConfigFile(2810, os.environ['NSHOST']) + port = '2810' + _writeConfigFile(port, host) # else: - if not machine: + if not host: # only PORT is given - os.environ['NSHOST'] = `hostname` - pass + host = socket.gethostname() # both MACHINE and PORT are given - _writeConfigFile(os.environ['NSPORT'], os.environ['NSHOST']) + _writeConfigFile(port, host) # + os.environ['NSPORT'] = port + os.environ['NSHOST'] = host # # --- set the OMNIORB_CONFIG file and environment relative to this run of SALOME def _writeConfigFile(port, host): - os.environ['NSPORT'] = port - os.environ['NSHOST'] = host - - absoluteAppliPath = os.environ['ABSOLUTE_APPLI_PATH'] - path = absoluteAppliPath + '/USERS' + path = os.environ['OMNIORB_USER_PATH'] kwargs = {'with_username' : os.environ['USER']} from ORBConfigFile import writeORBConfigFile @@ -104,3 +107,40 @@ def _writeConfigFile(port, host): os.environ['OMNIORB_CONFIG'] = filename # + +# command looks like a Bash command-line: +# script1.py [args] ; script2.py [args] ; ... +def runSession(command): + if command: + sep = ";" + if sys.platform == "win32": + sep= "&" + command = command.split(sep) + outmsg = [] + errmsg = [] + for cmd in command: + save_cmd = cmd + cmd = cmd.strip().split(' ') + #proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + proc = subprocess.Popen(cmd) + (stdoutdata, stderrdata) = proc.communicate() + if stdoutdata: + outmsg.append(stdoutdata) + if stderrdata: + errmsg.append(stderrdata) + + if proc.returncode != 0: + errmsg.append("Error raised when executing command: %s\n"%save_cmd) + if outmsg: + sys.stdout.write("".join(outmsg)) + if errmsg: + sys.stderr.write("".join(errmsg)) + sys.exit(proc.returncode) + + return ("".join(outmsg), "".join(errmsg)) + else: + absoluteAppliPath = os.getenv('ABSOLUTE_APPLI_PATH','') + cmd = ["/bin/bash", "--rcfile", absoluteAppliPath + "/.bashrc" ] + proc = subprocess.Popen(cmd, shell=False, close_fds=True) + return proc.communicate() +#