Salome HOME
spns #34816: restore backward compatibility of SAT master since SalomeContext does...
[tools/sat.git] / src / fileEnviron.py
index e74290a7f024bc51a2c33f0410ccd109d3a79080..20cdc1a53390016443f8a079360189e52ee3f65b 100644 (file)
@@ -489,6 +489,10 @@ class LauncherFileEnviron(FileEnviron):
         if not self.environ.is_defined("PATH"):
             self.environ.set("PATH","")
 
+        if self.init_path:
+            self.output.write('\n'+self.indent)
+            self.add_echo("Modifier cette variable pour ne pas rĂ©initialiser les PATHS")
+            self.output.write(self.indent+'reinitialise_paths=True\n\n')
 
     def add_echo(self, text):
         """Add a comment
@@ -518,30 +522,35 @@ class LauncherFileEnviron(FileEnviron):
         if separator in value:
             raise Exception(msg % (key, value, separator))
 
-        if (self.init_path and (not self.environ.is_defined(key))):
+        is_key_defined=self.environ.is_defined(key)
+        conditional_reinit=False
+        if (self.init_path and (not is_key_defined)):
             # reinitialisation mode set to true (the default)
             # for the first occurrence of key, we set it.
             # therefore key will not be inherited from environment
+            self.output.write(self.indent+'if reinitialise_paths:\n'+self.indent)
             self.set(key, value)
-            return
+            self.output.write(self.indent+'else:\n'+self.indent)
+            conditional_reinit=True # in this case do not register value in self.environ a second time
 
         # in all other cases we use append (except if value is already the key
         do_append=True
-        if self.environ.is_defined(key):
+        if is_key_defined:
             value_list = self.environ.get(key).split(sep)
             # rem : value cannot be expanded (unlike bash/bat case) - but it doesn't matter.
             if value in value_list:
                 do_append=False  # value is already in key path : we don't append it again
             
         if do_append:
-            self.environ.append_value(key, value,sep) # register value in self.environ
+            if not conditional_reinit:
+                self.environ.append_value(key, value,sep) # register value in self.environ
             if key in self.specialKeys.keys():
                 #for these special keys we use the specific salomeContext function
                 self.output.write(self.begin+'addTo%s(r"%s")\n' % 
                                   (self.specialKeys[key], self.value_filter(value)))
             else:
                 # else we use the general salomeContext addToVariable function
-                self.output.write(self.indent+'appendPath(r"%s", r"%s",separator="%s")\n' 
+                self.output.write(self.begin+'appendVariable(r"%s", r"%s",separator="%s")\n'
                                   % (key, self.value_filter(value), sep))
 
     def append(self, key, value, sep=":"):
@@ -571,22 +580,28 @@ class LauncherFileEnviron(FileEnviron):
         if separator in value:
             raise Exception(msg % (key, value, separator))
 
-        if (self.init_path and (not self.environ.is_defined(key))):
+        is_key_defined=self.environ.is_defined(key)
+        conditional_reinit=False
+        if (self.init_path and (not is_key_defined)):
             # reinitialisation mode set to true (the default)
             # for the first occurrence of key, we set it.
             # therefore key will not be inherited from environment
+            self.output.write(self.indent+'if reinitialise_paths:\n'+self.indent)
             self.set(key, value)
-            return
+            self.output.write(self.indent+'else:\n'+self.indent)
+            conditional_reinit=True # in this case do not register value in self.environ a second time
+
         # in all other cases we use append (except if value is already the key
         do_append=True
-        if self.environ.is_defined(key):
+        if is_key_defined:
             value_list = self.environ.get(key).split(sep)
             # rem : value cannot be expanded (unlike bash/bat case) - but it doesn't matter.
             if value in value_list:
                 do_append=False  # value is already in key path : we don't append it again
             
         if do_append:
-            self.environ.append_value(key, value,sep) # register value in self.environ
+            if not conditional_reinit:
+                self.environ.append_value(key, value,sep) # register value in self.environ
             if key in self.specialKeys.keys():
                 #for these special keys we use the specific salomeContext function
                 self.output.write(self.begin+'addTo%s(r"%s")\n' % 
@@ -761,7 +776,7 @@ launcher_header2="""\
 import os
 import sys
 import subprocess
-
+import os.path
 
 # Add the pwdPath to able to run the launcher after unpacking a package
 # Used only in case of a salomeTools package
@@ -812,8 +827,22 @@ def main(args):
   # Create a SalomeContext which parses configFileNames to initialize environment
   try:
     from salomeContext import SalomeContext, SalomeContextException
+    if 'appendVariable' not in dir(SalomeContext):
+      # check whether the appendVariable method is implemented
+      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
+      SalomeContext.appendVariable = appendVariable
+
     context = SalomeContext(None)
-    
+
     # Here set specific variables, if needed
     # context.addToPath('mypath')
     # context.addToLdLibraryPath('myldlibrarypath')
@@ -835,7 +864,7 @@ launcher_header3="""\
 import os
 import sys
 import subprocess
-
+import os.path
 
 # Add the pwdPath to able to run the launcher after unpacking a package
 # Used only in case of a salomeTools package
@@ -891,8 +920,21 @@ def main(args):
   # Create a SalomeContext which parses configFileNames to initialize environment
   try:
     from salomeContext import SalomeContext, SalomeContextException
+    if 'appendVariable' not in dir(SalomeContext):
+      # check whether the appendVariable method is implemented
+      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
+      SalomeContext.appendVariable = appendVariable
+
     context = SalomeContext(None)
-    
     # Here set specific variables, if needed
     # context.addToPath('mypath')
     # context.addToLdLibraryPath('myldlibrarypath')
@@ -908,15 +950,21 @@ launcher_tail_py2="""\
     
     # Load all files extra.env.d/*.py and call the module's init routine]
 
-    if os.path.exists("extra.env.d"):
-        sys.path.insert(0, os.path.join(os.getcwd(), "extra.env.d"))
-        for filename in os.listdir("extra.env.d"):
+    extradir=out_dir_Path + r"/extra.env.d"
+
+    if os.path.exists(extradir):
+        import imp
+        sys.path.insert(0, os.path.join(os.getcwd(), extradir))
+        for filename in sorted(
+            filter(lambda x: os.path.isfile(os.path.join(extradir, x)),
+                   os.listdir(extradir))):
+
             if filename.endswith(".py"):
-                f = os.path.join("extra.env.d", filename)
+                f = os.path.join(extradir, filename)
                 module_name = os.path.splitext(os.path.basename(f))[0]
                 fp, path, desc = imp.find_module(module_name)
                 module = imp.load_module(module_name, fp, path, desc)
-                module.init(context, out_dir_Path) 
+                module.init(context, out_dir_Path)
 
     #[manage salome doc command]
     if len(args) >1 and args[0]=='doc':
@@ -931,19 +979,6 @@ launcher_tail_py2="""\
     import logging
     logging.getLogger("salome").error(e)
     sys.exit(1)
-#
-# salomeContext only prepend variables, we use our own appendPath when required
-def appendPath(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
-
 
 if __name__ == "__main__":
   args = sys.argv[1:]
@@ -956,15 +991,21 @@ launcher_tail_py3="""\
     
     # Load all files extra.env.d/*.py and call the module's init routine]
 
-    if os.path.exists("extra.env.d"):
-        sys.path.insert(0, os.path.join(os.getcwd(), "extra.env.d"))
-        for filename in os.listdir("extra.env.d"):
+    extradir=out_dir_Path + r"/extra.env.d"
+
+    if os.path.exists(extradir):
+        import imp
+        sys.path.insert(0, os.path.join(os.getcwd(), extradir))
+        for filename in sorted(
+            filter(lambda x: os.path.isfile(os.path.join(extradir, x)),
+                   os.listdir(extradir))):
+
             if filename.endswith(".py"):
-                f = os.path.join("extra.env.d", filename)
+                f = os.path.join(extradir, filename)
                 module_name = os.path.splitext(os.path.basename(f))[0]
                 fp, path, desc = imp.find_module(module_name)
                 module = imp.load_module(module_name, fp, path, desc)
-                module.init(context, out_dir_Path) 
+                module.init(context, out_dir_Path)
 
     #[manage salome doc command]
     if len(args) >1 and args[0]=='doc':
@@ -980,18 +1021,6 @@ launcher_tail_py3="""\
     logging.getLogger("salome").error(e)
     sys.exit(1)
  
-# salomeContext only prepend variables, we use our own appendPath when required
-def appendPath(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
-
 
 if __name__ == "__main__":
   args = sys.argv[1:]