From ee6f63c9b44329161cf74836a43a7cd410719914 Mon Sep 17 00:00:00 2001 From: =?utf8?q?C=C3=A9dric=20Aguerre?= Date: Thu, 26 Feb 2015 17:12:32 +0100 Subject: [PATCH] add 'context' command --- bin/appliskel/.salome-completion.sh | 2 +- bin/salomeContext.py | 66 ++++++++++++++++++++--------- 2 files changed, 46 insertions(+), 22 deletions(-) diff --git a/bin/appliskel/.salome-completion.sh b/bin/appliskel/.salome-completion.sh index f60157ac4..34e0f4f23 100644 --- a/bin/appliskel/.salome-completion.sh +++ b/bin/appliskel/.salome-completion.sh @@ -51,7 +51,7 @@ _salome() local cur prev command options COMPREPLY=( ) _get_comp_words_by_ref -n = cur prev - commands='start shell connect kill killall test info help coffee' + commands='start context shell connect kill killall test info help coffee' # Algorithm: # If cursor is at index 1 diff --git a/bin/salomeContext.py b/bin/salomeContext.py index aaa0e6b99..57540f36a 100644 --- a/bin/salomeContext.py +++ b/bin/salomeContext.py @@ -40,17 +40,18 @@ Usage: salome [command] [options] [--config=] Commands: ========= - start Starts a SALOME session (through virtual application) - shell Initializes SALOME environment, and executes scripts passed - as command arguments - connect Connects a Python console to the active SALOME session + start Starts a SALOME session (through virtual application) + context Initializes SALOME context. + shell Initializes SALOME context, and executes scripts passed + as command arguments + connect Connects a Python console to the active SALOME session kill Terminate SALOME session running on given ports for current user Port numbers must be separated by blank characters - killall Kill *all* SALOME running sessions for current user + killall Kill all SALOME running sessions for current user test Run SALOME tests. - info Display some information about SALOME - help Show this message - coffee Yes! SALOME can also make coffee!! + info Display some information about SALOME + help Show this message + coffee Yes! SALOME can also make coffee!! If no command is given, default to start. @@ -61,7 +62,7 @@ Command options: --config= ========================== - Initialize SALOME environment from a list of context files and/or a list + Initialize SALOME context from a list of context files and/or a list of folders containing context files. The list is comma-separated, whithout any blank characters. ''' @@ -70,18 +71,18 @@ Command options: # """ -The SalomeContext class in an API to configure SALOME environment then +The SalomeContext class in an API to configure SALOME context then start SALOME using a single python command. """ class SalomeContext: """ - Initialize environment from a list of configuration files + Initialize context 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 SalomeContext class will try to automatically convert them - to .cfg format before setting the environment. + to .cfg format before setting the context. """ def __init__(self, configFileNames=0): #it could be None explicitely (if user use multiples setVariable...for standalone) @@ -95,14 +96,14 @@ class SalomeContext: for filename in configFileNames: basename, extension = os.path.splitext(filename) if extension == ".cfg": - self.__setEnvironmentFromConfigFile(filename, reserved) + self.__setContextFromConfigFile(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) + self.__setContextFromConfigFile(temp.name, reserved) temp.close() except (ConfigParser.ParsingError, ValueError) as e: self.getLogger().error("Invalid token found when parsing file: %s\n"%(filename)) @@ -126,7 +127,7 @@ class SalomeContext: def runSalome(self, args): import os # Run this module as a script, in order to use appropriate Python interpreter - # according to current path (initialized from environment files). + # according to current path (initialized from context files). mpi_module_option = "--with-mpi-module=" mpi_module = [x for x in args if x.startswith(mpi_module_option)] if mpi_module: @@ -212,8 +213,9 @@ class SalomeContext: options = args[1:] availableCommands = { - 'start' : '_runAppli', - 'shell' : '_runSession', + 'start' : '_runAppli', + 'context' : '_setContext', + 'shell' : '_runSession', 'connect' : '_runConsole', 'kill' : '_kill', 'killall' : '_killAll', @@ -273,7 +275,7 @@ class SalomeContext: sys.exit(1) # - def __setEnvironmentFromConfigFile(self, filename, reserved=None): + def __setContextFromConfigFile(self, filename, reserved=None): if reserved is None: reserved = [] try: @@ -289,7 +291,7 @@ class SalomeContext: temp = tempfile.NamedTemporaryFile(suffix='.cfg') try: convertEnvFileToConfigFile(sh_file, temp.name, reserved) - self.__setEnvironmentFromConfigFile(temp.name, reserved) + self.__setContextFromConfigFile(temp.name, reserved) msg += "OK\n" self.getLogger().warning(msg) temp.close() @@ -307,7 +309,7 @@ class SalomeContext: for var in unsetVars: self.unsetVariable(var) - # set environment + # set context for reserved in reservedDict: a = filter(None, reservedDict[reserved]) # remove empty elements a = [ os.path.realpath(x) for x in a ] @@ -334,7 +336,29 @@ class SalomeContext: runSalome.runSalome() # - def _runSession(self, args=[]): + def _setContext(self, args=None): + salome_context_set = os.getenv("SALOME_CONTEXT_SET") + if salome_context_set: + print "***" + print "*** SALOME context has already been set." + print "*** Enter 'exit' (only once!) to leave SALOME context." + print "***" + return + + os.environ["SALOME_CONTEXT_SET"] = "yes" + print "***" + print "*** SALOME context is now set." + print "*** Enter 'exit' (only once!) to leave SALOME context." + print "***" + + cmd = ["/bin/bash"] + proc = subprocess.Popen(cmd, shell=False, close_fds=True) + return proc.communicate() + # + + def _runSession(self, args=None): + if args is None: + args = [] sys.argv = ['runSession'] + args import runSession params, args = runSession.configureSession(args, exe="salome shell") -- 2.39.2