-import os
-import sys
-import logging
-import ConfigParser
-
-from parseConfigFile import parseConfigFile
-from parseConfigFile import convertEnvFileToConfigFile
-
-import tempfile
-import pickle
-import subprocess
-import platform
-
-from salomeLauncherUtils import SalomeRunnerException
-from salomeLauncherUtils import getScriptsAndArgs, formatScriptsAndArgs
-
-def usage():
- #exeName = os.path.splitext(os.path.basename(__file__))[0]
-
- msg = '''\
-Usage: salome [command] [options] [--config=file1,...,filen]
-
-Commands:
- start Launches SALOME virtual application [DEFAULT]
- shell Executes a script under SALOME application environment
- connect Connects a Python console to the active SALOME session
- killall Kill all SALOME running sessions
- info Display some information about SALOME
- help Show this message
- coffee Yes! SALOME can also make coffee!!"
-
-Use salome start --help or salome shell --help
-to show help on start and shell commands.
-'''
-
- print msg
-#
-
-"""
-The SalomeRunner class in an API to configure SALOME environment then
-start SALOME using a single python command.
-
-"""
-class SalomeRunner:
- """
- Initialize environment from a list of configuration files
- identified by their names.
- These files should be in appropriate (new .cfg) format.
- However you can give old .sh environment files; in this case,
- the SalomeRunner class will try to automatically convert them
- to .cfg format before setting the environment.
- """
- def __init__(self, configFileNames=[]):
- #it could be None explicitely (if user use multiples setEnviron...for standalone)
- if configFileNames==None:
- return
-
- if len(configFileNames) == 0:
- raise SalomeRunnerException("No configuration files given")
-
- reserved=['PATH', 'LD_LIBRARY_PATH', 'PYTHONPATH', 'MANPATH', 'PV_PLUGIN_PATH']
- for filename in configFileNames:
- basename, extension = os.path.splitext(filename)
- if extension == ".cfg":
- self.__setEnvironmentFromConfigFile(filename, reserved)
- elif extension == ".sh":
- #new convert procedures, temporary could be use not to be automatically deleted
- #temp = tempfile.NamedTemporaryFile(suffix='.cfg', delete=False)
- temp = tempfile.NamedTemporaryFile(suffix='.cfg')
- try:
- convertEnvFileToConfigFile(filename, temp.name, reserved)
- self.__setEnvironmentFromConfigFile(temp.name, reserved)
- except ConfigParser.ParsingError, e:
- self.getLogger().warning("Invalid token found when parsing file: %s\n"%(filename))
- print e
- print '\n'
- finally:
- # Automatically cleans up the file
- temp.close()
- else:
- self.getLogger().warning("Unrecognized extension for configuration file: %s", filename)
- #
-
- def go(self, args):
- # Run this module as a script, in order to use appropriate Python interpreter
- # according to current path (initialized from environment files).
- absoluteAppliPath = os.getenv('ABSOLUTE_APPLI_PATH','')
- proc = subprocess.Popen(['python', absoluteAppliPath+'/bin/salome/salomeRunner.py', pickle.dumps(self), pickle.dumps(args)], shell=False, close_fds=True)
- proc.wait()
- #
-
- """Append value to PATH environment variable"""
- def addToPath(self, value):
- self.addToEnviron('PATH', value)
- #
-
- """Append value to LD_LIBRARY_PATH environment variable"""
- def addToLdLibraryPath(self, value):
- self.addToEnviron('LD_LIBRARY_PATH', value)
- #
-
- """Append value to PYTHONPATH environment variable"""
- def addToPythonPath(self, value):
- self.addToEnviron('PYTHONPATH', value)
- #
-
- """Set environment variable to value"""
- def setEnviron(self, name, value, overwrite=False):
- env = os.getenv(name, '')
- if env and not overwrite:
- self.getLogger().warning("Environment variable already existing (and not overwritten): %s=%s", name, value)
- return
-
- if env:
- self.getLogger().warning("Overwriting environment variable: %s=%s", name, value)
-
- value = os.path.expandvars(value) # expand environment variables
- self.getLogger().debug("Set environment variable: %s=%s", name, value)
- os.environ[name] = value
- #
-
- """Unset environment variable"""
- def unsetEnviron(self, name):
- if os.environ.has_key(name):
- del os.environ[name]
- #
-
- """Append value to environment variable"""
- def addToEnviron(self, name, value, separator=os.pathsep):
- if value == '':
- return
-
- value = os.path.expandvars(value) # expand environment variables
- self.getLogger().debug("Add to %s: %s", name, value)
- env = os.getenv(name, None)
- if env is None:
- os.environ[name] = value
- else:
- os.environ[name] = value + separator + env
- #
-
- ###################################
- # This begins the private section #
- ###################################
-
- def __parseArguments(self, args):
- if len(args) == 0 or args[0].startswith("-"):
- return None, args
-
- command = args[0]
- options = args[1:]
-
- availableCommands = {
- 'start' : '_runAppli',
- 'shell' : '_runSession',
- 'connect' : '_runConsole',
- 'killall': '_killAll',
- 'info': '_showInfo',
- 'help': '_usage',
- 'coffee' : '_makeCoffee'
- }
-
- if not command in availableCommands.keys():
- command = "start"
- options = args
-
- return availableCommands[command], options
- #
-
- """
- Run SALOME!
- Args consist in a mandatory command followed by optionnal parameters.
- See usage for details on commands.
- """
- def _getStarted(self, args):
- command, options = self.__parseArguments(args)
- sys.argv = options
-
- if command is None:
- if args and args[0] in ["-h","--help","help"]:
- usage()
- sys.exit(0)
- # try to default to "start" command
- command = "_runAppli"
-
- try:
- res = getattr(self, command)(options) # run appropriate method
- return res or (None, None)
- except SystemExit, exc:
- if exc==0:
- sys.exit(0) #catch sys.exit(0) happy end no warning
- if exc==1:
- self.getLogger().warning("SystemExit 1 in method %s.", command)
- sys.exit(1)
- except StandardError:
- self.getLogger().error("Unexpected error:")
- import traceback
- traceback.print_exc()
- sys.exit(1)
- except SalomeRunnerException, e:
- self.getLogger().error(e)
- sys.exit(1)
- #
-
- def __setEnvironmentFromConfigFile(self, filename, reserved=[]):
- unsetVars, configVars, reservedDict = parseConfigFile(filename, reserved)
-
- # unset variables
- for var in unsetVars:
- self.unsetEnviron(var)
-
- # set environment
- for reserved in reservedDict:
- a = filter(None, reservedDict[reserved]) # remove empty elements
- reformattedVals = ':'.join(a)
- self.addToEnviron(reserved, reformattedVals)
- pass
-
- for key,val in configVars:
- self.setEnviron(key, val, overwrite=True)
- pass
-
- sys.path[:0] = os.getenv('PYTHONPATH','').split(':')
- #
-
- def _runAppli(self, args=[]):
- # Initialize SALOME environment
- sys.argv = ['runSalome'] + args
- import setenv
- setenv.main(True)
-
- import runSalome
- runSalome.runSalome()
- #
-
- def _runSession(self, args=[]):
- sys.argv = ['runSession'] + args
- import runSession
- runSession.configureSession(args)
-
- import setenv
- setenv.main(True)
-
- scriptArgs = getScriptsAndArgs(args)
- command = formatScriptsAndArgs(scriptArgs)
- if command:
- proc = subprocess.Popen(command, shell=True, close_fds=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
- return proc.communicate()
- else:
- absoluteAppliPath = os.getenv('ABSOLUTE_APPLI_PATH','')
- cmd = ["/bin/bash", "--rcfile", absoluteAppliPath + "/.bashrc" ]
- proc = subprocess.Popen(cmd, shell=False, close_fds=True)
- proc.wait()
- #
-
- def _runConsole(self, args=[]):
- # Initialize SALOME environment
- sys.argv = ['runConsole'] + args
- import setenv
- setenv.main(True)
-
- import runConsole
- runConsole.connect()
- #
-
- def _killAll(self, args=[]):
- from killSalome import killAllPorts
- killAllPorts()
- #
-
- def _showInfo(self, args=[]):
- print "Running with python", platform.python_version()
- self._runAppli(["--version"])
- #
-
- def _usage(self, unused=[]):
- usage()
- #
-
- def _makeCoffee(self, args=[]):
- print " ("
- print " ) ("
- print " ___...(-------)-....___"
- print " .-\"\" ) ( \"\"-."
- print " .-\'``\'|-._ ) _.-|"
- print " / .--.| `\"\"---...........---\"\"` |"
- print " / / | |"
- print " | | | |"
- print " \\ \\ | |"
- print " `\\ `\\ | |"
- print " `\\ `| |"
- print " _/ /\\ /"
- print " (__/ \\ /"
- print " _..---\"\"` \\ /`\"\"---.._"
- print " .-\' \\ / \'-."
- print " : `-.__ __.-\' :"
- print " : ) \"\"---...---\"\" ( :"
- print " \'._ `\"--...___...--\"` _.\'"
- print " \\\"\"--..__ __..--\"\"/"
- print " \'._ \"\"\"----.....______.....----\"\"\" _.\'"
- print " `\"\"--..,,_____ _____,,..--\"\"`"
- print " `\"\"\"----\"\"\"`"
- sys.exit(0)
- #
-
- # Add the following two methods since logger is not pickable
- # Ref: http://stackoverflow.com/questions/2999638/how-to-stop-attributes-from-being-pickled-in-python
- def __getstate__(self):
- d = dict(self.__dict__)
- if hasattr(self, '_logger'):
- del d['_logger']
- return d
- #
- def __setstate__(self, d):
- self.__dict__.update(d) # I *think* this is a safe way to do it
- #
- # Excluding self._logger from pickle operation imply using the following method to access logger
- def getLogger(self):
- if not hasattr(self, '_logger'):
- self._logger = logging.getLogger(__name__)
- #self._logger.setLevel(logging.DEBUG)
- self._logger.setLevel(logging.ERROR)
- return self._logger;
- #
-
-###
-import pickle
-if __name__ == "__main__":
- if len(sys.argv) == 3:
- runner = pickle.loads(sys.argv[1])
- args = pickle.loads(sys.argv[2])
- (out, err) = runner._getStarted(args)
- if out:
- sys.stdout.write(out)
- if err:
- sys.stderr.write(err)
- else:
- usage()
-#
+import os\r
+import sys\r
+import logging\r
+import ConfigParser\r
+\r
+from parseConfigFile import parseConfigFile\r
+from parseConfigFile import convertEnvFileToConfigFile\r
+\r
+import tempfile\r
+import pickle\r
+import subprocess\r
+import platform\r
+\r
+from salomeLauncherUtils import SalomeRunnerException\r
+from salomeLauncherUtils import getScriptsAndArgs, formatScriptsAndArgs\r
+\r
+def usage():\r
+ #exeName = os.path.splitext(os.path.basename(__file__))[0]\r
+\r
+ msg = '''\\r
+Usage: salome [command] [options] [--config=file1,...,filen]\r
+\r
+Commands:\r
+ start Launches SALOME virtual application [DEFAULT]\r
+ shell Executes a script under SALOME application environment\r
+ connect Connects a Python console to the active SALOME session\r
+ killall Kill all SALOME running sessions\r
+ info Display some information about SALOME\r
+ help Show this message\r
+ coffee Yes! SALOME can also make coffee!!"\r
+\r
+Use salome start --help or salome shell --help\r
+to show help on start and shell commands.\r
+'''\r
+\r
+ print msg\r
+#\r
+\r
+"""\r
+The SalomeRunner class in an API to configure SALOME environment then\r
+start SALOME using a single python command.\r
+\r
+"""\r
+class SalomeRunner:\r
+ """\r
+ Initialize environment from a list of configuration files\r
+ identified by their names.\r
+ These files should be in appropriate (new .cfg) format.\r
+ However you can give old .sh environment files; in this case,\r
+ the SalomeRunner class will try to automatically convert them\r
+ to .cfg format before setting the environment.\r
+ """\r
+ def __init__(self, configFileNames=[]):\r
+ #it could be None explicitely (if user use multiples setEnviron...for standalone)\r
+ if configFileNames==None:\r
+ return\r
+\r
+ if len(configFileNames) == 0:\r
+ raise SalomeRunnerException("No configuration files given")\r
+\r
+ reserved=['PATH', 'LD_LIBRARY_PATH', 'PYTHONPATH', 'MANPATH', 'PV_PLUGIN_PATH']\r
+ for filename in configFileNames:\r
+ basename, extension = os.path.splitext(filename)\r
+ if extension == ".cfg":\r
+ self.__setEnvironmentFromConfigFile(filename, reserved)\r
+ elif extension == ".sh":\r
+ #new convert procedures, temporary could be use not to be automatically deleted\r
+ #temp = tempfile.NamedTemporaryFile(suffix='.cfg', delete=False)\r
+ temp = tempfile.NamedTemporaryFile(suffix='.cfg')\r
+ try:\r
+ convertEnvFileToConfigFile(filename, temp.name, reserved)\r
+ self.__setEnvironmentFromConfigFile(temp.name, reserved)\r
+ except ConfigParser.ParsingError, e:\r
+ self.getLogger().warning("Invalid token found when parsing file: %s\n"%(filename))\r
+ print e\r
+ print '\n'\r
+ finally:\r
+ # Automatically cleans up the file\r
+ temp.close()\r
+ else:\r
+ self.getLogger().warning("Unrecognized extension for configuration file: %s", filename)\r
+ #\r
+\r
+ def go(self, args):\r
+ # Run this module as a script, in order to use appropriate Python interpreter\r
+ # according to current path (initialized from environment files).\r
+ absoluteAppliPath = os.getenv('ABSOLUTE_APPLI_PATH','')\r
+ proc = subprocess.Popen(['python', os.path.join(absoluteAppliPath,"bin","salome","salomeRunner.py"), pickle.dumps(self), pickle.dumps(args)], shell=False, close_fds=True)\r
+ proc.wait()\r
+ #\r
+\r
+ """Append value to PATH environment variable"""\r
+ def addToPath(self, value):\r
+ self.addToEnviron('PATH', value)\r
+ #\r
+\r
+ """Append value to LD_LIBRARY_PATH environment variable"""\r
+ def addToLdLibraryPath(self, value):\r
+ self.addToEnviron('LD_LIBRARY_PATH', value)\r
+ #\r
+\r
+ """Append value to PYTHONPATH environment variable"""\r
+ def addToPythonPath(self, value):\r
+ self.addToEnviron('PYTHONPATH', value)\r
+ #\r
+\r
+ """Set environment variable to value"""\r
+ def setEnviron(self, name, value, overwrite=False):\r
+ env = os.getenv(name, '')\r
+ if env and not overwrite:\r
+ self.getLogger().warning("Environment variable already existing (and not overwritten): %s=%s", name, value)\r
+ return\r
+\r
+ if env:\r
+ self.getLogger().warning("Overwriting environment variable: %s=%s", name, value)\r
+\r
+ value = os.path.expandvars(value) # expand environment variables\r
+ self.getLogger().debug("Set environment variable: %s=%s", name, value)\r
+ os.environ[name] = value\r
+ #\r
+\r
+ """Unset environment variable"""\r
+ def unsetEnviron(self, name):\r
+ if os.environ.has_key(name):\r
+ del os.environ[name]\r
+ #\r
+\r
+ """Append value to environment variable"""\r
+ def addToEnviron(self, name, value, separator=os.pathsep):\r
+ if value == '':\r
+ return\r
+\r
+ value = os.path.expandvars(value) # expand environment variables\r
+ self.getLogger().debug("Add to %s: %s", name, value)\r
+ env = os.getenv(name, None)\r
+ if env is None:\r
+ os.environ[name] = value\r
+ else:\r
+ os.environ[name] = value + separator + env\r
+ #\r
+\r
+ ###################################\r
+ # This begins the private section #\r
+ ###################################\r
+\r
+ def __parseArguments(self, args):\r
+ if len(args) == 0 or args[0].startswith("-"):\r
+ return None, args\r
+\r
+ command = args[0]\r
+ options = args[1:]\r
+\r
+ availableCommands = {\r
+ 'start' : '_runAppli',\r
+ 'shell' : '_runSession',\r
+ 'connect' : '_runConsole',\r
+ 'killall': '_killAll',\r
+ 'info': '_showInfo',\r
+ 'help': '_usage',\r
+ 'coffee' : '_makeCoffee'\r
+ }\r
+\r
+ if not command in availableCommands.keys():\r
+ command = "start"\r
+ options = args\r
+\r
+ return availableCommands[command], options\r
+ #\r
+\r
+ """\r
+ Run SALOME!\r
+ Args consist in a mandatory command followed by optionnal parameters.\r
+ See usage for details on commands.\r
+ """\r
+ def _getStarted(self, args):\r
+ command, options = self.__parseArguments(args)\r
+ sys.argv = options\r
+\r
+ if command is None:\r
+ if args and args[0] in ["-h","--help","help"]:\r
+ usage()\r
+ sys.exit(0)\r
+ # try to default to "start" command\r
+ command = "_runAppli"\r
+\r
+ try:\r
+ res = getattr(self, command)(options) # run appropriate method\r
+ return res or (None, None)\r
+ except SystemExit, exc:\r
+ if exc==0:\r
+ sys.exit(0) #catch sys.exit(0) happy end no warning\r
+ if exc==1:\r
+ self.getLogger().warning("SystemExit 1 in method %s.", command)\r
+ sys.exit(1)\r
+ except StandardError:\r
+ self.getLogger().error("Unexpected error:")\r
+ import traceback\r
+ traceback.print_exc()\r
+ sys.exit(1)\r
+ except SalomeRunnerException, e:\r
+ self.getLogger().error(e)\r
+ sys.exit(1)\r
+ #\r
+\r
+ def __setEnvironmentFromConfigFile(self, filename, reserved=[]):\r
+ unsetVars, configVars, reservedDict = parseConfigFile(filename, reserved)\r
+\r
+ # unset variables\r
+ for var in unsetVars:\r
+ self.unsetEnviron(var)\r
+\r
+ # set environment\r
+ for reserved in reservedDict:\r
+ a = filter(None, reservedDict[reserved]) # remove empty elements\r
+ reformattedVals = ':'.join(a)\r
+ self.addToEnviron(reserved, reformattedVals)\r
+ pass\r
+\r
+ for key,val in configVars:\r
+ self.setEnviron(key, val, overwrite=True)\r
+ pass\r
+\r
+ sys.path[:0] = os.getenv('PYTHONPATH','').split(':')\r
+ #\r
+\r
+ def _runAppli(self, args=[]):\r
+ # Initialize SALOME environment\r
+ sys.argv = ['runSalome'] + args\r
+ import setenv\r
+ setenv.main(True)\r
+\r
+ import runSalome\r
+ runSalome.runSalome()\r
+ #\r
+\r
+ def _runSession(self, args=[]):\r
+ sys.argv = ['runSession'] + args\r
+ import runSession\r
+ runSession.configureSession(args)\r
+\r
+ import setenv\r
+ setenv.main(True)\r
+\r
+ scriptArgs = getScriptsAndArgs(args)\r
+ command = formatScriptsAndArgs(scriptArgs)\r
+ if command:\r
+ proc = subprocess.Popen(command, shell=True, close_fds=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)\r
+ return proc.communicate()\r
+ else:\r
+ absoluteAppliPath = os.getenv('ABSOLUTE_APPLI_PATH','')\r
+ cmd = ["/bin/bash", "--rcfile", absoluteAppliPath + "/.bashrc" ]\r
+ proc = subprocess.Popen(cmd, shell=False, close_fds=True)\r
+ proc.wait()\r
+ #\r
+\r
+ def _runConsole(self, args=[]):\r
+ # Initialize SALOME environment\r
+ sys.argv = ['runConsole'] + args\r
+ import setenv\r
+ setenv.main(True)\r
+\r
+ import runConsole\r
+ runConsole.connect()\r
+ #\r
+\r
+ def _killAll(self, args=[]):\r
+ from killSalome import killAllPorts\r
+ killAllPorts()\r
+ #\r
+\r
+ def _showInfo(self, args=[]):\r
+ print "Running with python", platform.python_version()\r
+ self._runAppli(["--version"])\r
+ #\r
+\r
+ def _usage(self, unused=[]):\r
+ usage()\r
+ #\r
+\r
+ def _makeCoffee(self, args=[]):\r
+ print " ("\r
+ print " ) ("\r
+ print " ___...(-------)-....___"\r
+ print " .-\"\" ) ( \"\"-."\r
+ print " .-\'``\'|-._ ) _.-|"\r
+ print " / .--.| `\"\"---...........---\"\"` |"\r
+ print " / / | |"\r
+ print " | | | |"\r
+ print " \\ \\ | |"\r
+ print " `\\ `\\ | |"\r
+ print " `\\ `| |"\r
+ print " _/ /\\ /"\r
+ print " (__/ \\ /"\r
+ print " _..---\"\"` \\ /`\"\"---.._"\r
+ print " .-\' \\ / \'-."\r
+ print " : `-.__ __.-\' :"\r
+ print " : ) \"\"---...---\"\" ( :"\r
+ print " \'._ `\"--...___...--\"` _.\'"\r
+ print " \\\"\"--..__ __..--\"\"/"\r
+ print " \'._ \"\"\"----.....______.....----\"\"\" _.\'"\r
+ print " `\"\"--..,,_____ _____,,..--\"\"`"\r
+ print " `\"\"\"----\"\"\"`"\r
+ sys.exit(0)\r
+ #\r
+\r
+ # Add the following two methods since logger is not pickable\r
+ # Ref: http://stackoverflow.com/questions/2999638/how-to-stop-attributes-from-being-pickled-in-python\r
+ def __getstate__(self):\r
+ d = dict(self.__dict__)\r
+ if hasattr(self, '_logger'):\r
+ del d['_logger']\r
+ return d\r
+ #\r
+ def __setstate__(self, d):\r
+ self.__dict__.update(d) # I *think* this is a safe way to do it\r
+ #\r
+ # Excluding self._logger from pickle operation imply using the following method to access logger\r
+ def getLogger(self):\r
+ if not hasattr(self, '_logger'):\r
+ self._logger = logging.getLogger(__name__)\r
+ #self._logger.setLevel(logging.DEBUG)\r
+ self._logger.setLevel(logging.ERROR)\r
+ return self._logger;\r
+ #\r
+\r
+###\r
+import pickle\r
+if __name__ == "__main__":\r
+ if len(sys.argv) == 3:\r
+ runner = pickle.loads(sys.argv[1])\r
+ args = pickle.loads(sys.argv[2])\r
+ (out, err) = runner._getStarted(args)\r
+ if out:\r
+ sys.stdout.write(out)\r
+ if err:\r
+ sys.stderr.write(err)\r
+ else:\r
+ usage()\r
+#\r