Salome HOME
salomeContext.py: New routine removeFromVariable.
authorPascal Obry <pascal.obry@edf.fr>
Mon, 29 Apr 2024 11:45:38 +0000 (13:45 +0200)
committerPascal Obry <pascal.obry@edf.fr>
Tue, 30 Apr 2024 07:32:38 +0000 (09:32 +0200)
This removes a value from a string like PATH or LD_LIBRARY_PATH.

bin/salomeContext.py

index d9b42b06dbfde5922256f75be4b26a21a48b055e..93a74d457da59cc5328834f428205b6269d2c4e0 100755 (executable)
@@ -231,6 +231,24 @@ class SalomeContext:
       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 #
   ###################################