Salome HOME
salomeContext.py: New routine removeFromVariable.
[modules/kernel.git] / bin / salomeContext.py
index 1d24a808e84862f7a7c52c690cf70a64129097cc..93a74d457da59cc5328834f428205b6269d2c4e0 100755 (executable)
@@ -1,5 +1,5 @@
 #! /usr/bin/env python3
-# Copyright (C) 2013-2022  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
@@ -35,7 +35,7 @@ from salomeContextUtils import SalomeContextException
 
 def usage():
   msg = '''\
-Usage: salome [command] [options] [--config=<file,folder,...>]
+Usage: salome [command] [options] [--config=<file,folder,...>] [--with-env-modules=<env_module1,env_module2,...>]
 
 Commands:
 =========
@@ -71,6 +71,11 @@ 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=<env_module1,env_module2,...>
+================================================
+    Initialize SALOME context with the provided additional environment modules.
+    The list is comma-separated, without any blank characters.
 '''
 
   print(msg)
@@ -111,7 +116,7 @@ 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 Exception:
       raise SalomeContextException("Failed to load env modules: %s ..." % ' '.join(env_modules))
@@ -195,11 +200,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
@@ -213,6 +218,37 @@ 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)
+      env = env.removesuffix(separator + value)
+      env = env.replace(separator + value + separator, ':')
+
+    os.environ[name] = env
+  #
+
   ###################################
   # This begins the private section #
   ###################################
@@ -414,7 +450,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)