X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=bin%2FsalomeContext.py;h=04f960e31b69b98e77b3a6febf68a954989a8584;hb=17f89acccd51b2d2c86d44e70a1c053c10da0b3d;hp=d4d44afdd8ed3cf77c908e2eeea9f98302144f9c;hpb=7b6895b48ccd982f69db4fe3ecd30d75be0514dc;p=modules%2Fkernel.git diff --git a/bin/salomeContext.py b/bin/salomeContext.py index d4d44afdd..04f960e31 100755 --- a/bin/salomeContext.py +++ b/bin/salomeContext.py @@ -1,5 +1,5 @@ #! /usr/bin/env python3 -# Copyright (C) 2013-2021 CEA/DEN, EDF R&D, OPEN CASCADE +# Copyright (C) 2013-2024 CEA, EDF, OPEN CASCADE # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public @@ -33,30 +33,33 @@ import platform from salomeContextUtils import SalomeContextException -def usage(): +def usage(appended_cmd_doc = "", appended_opt_doc = ""): + add_in_help = {"appended_cmd_doc":appended_cmd_doc,"appended_opt_doc":appended_opt_doc} msg = '''\ -Usage: salome [command] [options] [--config=] +Usage: salome [command] [options] [--config=] [--with-env-modules=] Commands: ========= - start Start a new SALOME instance. + start Start a new SALOME instance. Start a single SALOME_Session_Server_No_Server + process with environment relevant to the application and hosting all servants in it. context Initialize SALOME context. Current environment is extended. shell Initialize SALOME context, attached to the last created SALOME instance if any, and executes scripts passed as command arguments. User works in a Shell terminal. SALOME environment is set but application is not started. - connect Connect a Python console to the active SALOME instance. - remote run command in SALOME environment from remote call, ssh or rsh. - kill Terminate SALOME instances running on given ports for current user. - Port numbers must be separated by blank characters. - killall Terminate *all* SALOME running instances for current user. - Do not start a new one. test Run SALOME tests. info Display some information about SALOME. doc Show online module documentation (if available). Module names must be separated by blank characters. help Show this message. - + remote run command in SALOME environment from remote call, ssh or rsh. + withsession Start a new SWS SALOME instance with multiple servers hosting all servants. + connect In SWS context, Connect a Python console to the active SALOME instance. + kill In SWS context, Terminate SALOME instances running on given ports for current user. + Port numbers must be separated by blank characters. + killall Terminate *all* SALOME running SWS instances for current user. + Do not start a new one. +%(appended_cmd_doc)s If no command is given, default is start. Command options: @@ -69,9 +72,15 @@ Command options: Initialize SALOME context from a list of context files and/or a list of folders containing context files. The list is comma-separated, without any blank characters. + +--with-env-modules= +================================================ + Initialize SALOME context with the provided additional environment modules. + The list is comma-separated, without any blank characters. +%(appended_opt_doc)s ''' - print(msg) + print(msg%add_in_help) # """ @@ -109,9 +118,9 @@ class SalomeContext: raise SalomeContextException("Module environment not present") return try: - out, err = subprocess.Popen([modulecmd, "python", "load"] + env_modules, stdout=subprocess.PIPE).communicate() + out, err = subprocess.Popen([modulecmd, "python", "try-load"] + env_modules, stdout=subprocess.PIPE).communicate() exec(out) # define specific environment variables - except: + except Exception: raise SalomeContextException("Failed to load env modules: %s ..." % ' '.join(env_modules)) pass # @@ -193,11 +202,11 @@ class SalomeContext: """Unset environment variable""" def unsetVariable(self, name): - if os.environ.has_key(name): + if name in os.environ: del os.environ[name] # - """Append value to environment variable""" + """Prepend value to environment variable""" def addToVariable(self, name, value, separator=os.pathsep): if value == '': return @@ -211,6 +220,43 @@ class SalomeContext: os.environ[name] = value + separator + env # + """Append a variable""" + def appendVariable(self, name, value, separator=os.pathsep): + if value == '': + return + + value = os.path.expandvars(value) # expand environment variables + env = os.getenv(name, None) + if env is None: + os.environ[name] = value + else: + os.environ[name] = env + separator + value + return + + """Remove value from environment variable""" + def removeFromVariable(self, name, value, separator=os.pathsep): + if value == '': + return + + value = os.path.expandvars(value) # expand environment variables + self.getLogger().debug("Remove from %s: %s", name, value) + env = os.getenv(name, None) + if env == value: + env = '' + else: + # env = env.removeprefix(value + separator) (Python >= 3.9) + str = value + separator + if env.startswith(str): + env = env[len(str):] + # env = env.removesuffix(separator + value) (Python >= 3.9) + str = separator + value + if env.endswith(str): + env = env[:-len(str)] + env = env.replace(separator + value + separator, ':') + + os.environ[name] = env + # + ################################### # This begins the private section # ################################### @@ -223,7 +269,8 @@ class SalomeContext: options = args[1:] availableCommands = { - 'start' : '_runAppli', + 'start' : '_sessionless', + 'withsession' : '_runAppli', 'context' : '_setContext', 'shell' : '_runSession', 'remote' : '_runRemote', @@ -261,7 +308,7 @@ class SalomeContext: path = os.path.realpath(os.path.join(absoluteAppliPath, "bin", "salome", "appliskel")) add_path(path, "PYTHONPATH") - except: + except Exception: pass command, options = self.__parseArguments(args) @@ -272,7 +319,7 @@ class SalomeContext: usage() return 0 # try to default to "start" command - command = "_runAppli" + command = "_sessionless" try: res = getattr(self, command)(options) # run appropriate method @@ -349,9 +396,21 @@ class SalomeContext: if args is None: args = [] # Initialize SALOME environment + sys.argv = ['runSalomeOld'] + args + import setenv + setenv.main(True, exeName="salome withsession") + + import runSalomeOld + runSalomeOld.runSalome() + return 0 + # + + def _sessionless(self, args=None): + if args is None: + args = [] sys.argv = ['runSalome'] + args import setenv - setenv.main(True, exeName="salome start") + setenv.main(True, exeName="salome withsession") import runSalome runSalome.runSalome() @@ -399,7 +458,7 @@ class SalomeContext: def _runRemote(self, args=None): if args is None: args = [] -# complete salome environment +# complete salome environment sys.argv = ['runRemote'] import setenv setenv.main(True) @@ -440,7 +499,6 @@ class SalomeContext: else: proc = subprocess.Popen(["killSalomeWithPort.py", str(port)]) proc.communicate() - return 0 # @@ -467,6 +525,8 @@ class SalomeContext: from killSalome import killAllPorts killAllPorts() pass + from addToKillList import killList + killList() return 0 # @@ -494,7 +554,7 @@ class SalomeContext: versions[software.upper()] = version if len(software) > max_len: max_len = len(software) - except: + except Exception: pass pass pass @@ -561,7 +621,7 @@ Available options are: if "-v" in args or "--version" in args: print("Running with python", platform.python_version()) - return self._runAppli(["--version"]) + return self._sessionless(["--version"]) return 0 #