Salome HOME
Merge branch 'V9_2_2_BR'
[modules/kernel.git] / bin / setenv.py
index 46bc337bed9a9ddb795f5c9a42a2a77713eee993..21ad0a2a77ecd23d857f8e668e715c6de056a520 100755 (executable)
@@ -1,6 +1,6 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3
 #  -*- coding: iso-8859-1 -*-
-# Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
+# Copyright (C) 2007-2019  CEA/DEN, EDF R&D, OPEN CASCADE
 #
 # Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
@@ -8,7 +8,7 @@
 # This library is free software; you can redistribute it and/or
 # modify it under the terms of the GNU Lesser General Public
 # License as published by the Free Software Foundation; either
-# version 2.1 of the License.
+# version 2.1 of the License, or (at your option) any later version.
 #
 # This library is distributed in the hope that it will be useful,
 # but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -43,7 +43,7 @@ def add_path(directory, variable_name):
       splitsym = ";"
     else:
       splitsym = ":"
-    if not os.environ.has_key(variable_name):
+    if variable_name not in os.environ:
         os.environ[variable_name] = ""
         pass
     if os.path.exists(directory):
@@ -59,12 +59,11 @@ def add_path(directory, variable_name):
                 if os.path.abspath(_dir) != os.path.abspath(directory):
                   newpath.append(_dir)
             pass
-        import string
         newpath[:0] = [ directory ]
-        newpath = string.join(newpath, splitsym)
+        newpath = splitsym.join(newpath)
         os.environ[variable_name] = newpath
         if variable_name == "PYTHONPATH":
-            sys.path[:0] = [directory]
+            sys.path[:0] = [os.path.realpath(directory)]
 
 # -----------------------------------------------------------------------------
 
@@ -78,35 +77,28 @@ def get_lib_dir():
 
 # -----------------------------------------------------------------------------
 
-def get_config(silent=False):
+def get_config(silent=False, exeName=None):
     """
     Get list of modules, paths.
-    
+
     Read args from launch configure xml file and command line options.
     Check variables <module>_ROOT_DIR and set list of used modules.
-    Return args, modules_list, modules_root_dir    
+    Return args, modules_list, modules_root_dir
     """
-    
+
     # read args from launch configure xml file and command line options
 
-    #*** Test additional option
-    #*** import optparse
-    #*** help_str = "Test options addition."
-    #*** o_j = optparse.Option("-j", "--join", action="store_true", dest="join", help=help_str)
 
     import launchConfigureParser
-    args = launchConfigureParser.get_env()
+    args = launchConfigureParser.get_env(exeName=exeName)
 
-    #*** Test additional option
-    #*** args = launchConfigureParser.get_env([o_j])
-    #*** if args.has_key("join"): print args["join"]
 
     # Check variables <module>_ROOT_DIR
     # and set list of used modules (without KERNEL)
 
     modules_list = []
-    if args.has_key("modules"):
-        modules_list += args["modules"]
+    if "modules" in args:
+        modules_list += [a for a in args["modules"] if a.strip()]
     # KERNEL must be last in the list to locate it at the first place in PATH
     if args["gui"] :
         modules_list[:0] = ["GUI"]
@@ -118,14 +110,14 @@ def get_config(silent=False):
     to_remove_list=[]
     for module in modules_list :
         module_variable=module+"_ROOT_DIR"
-        if not os.environ.has_key(module_variable):
+        if module_variable not in os.environ:
             if not silent:
-                print "*******************************************************"
-                print "*"
-                print "* Environment variable",module_variable,"must be set"
-                print "* Module", module, "will be not available"
-                print "*"
-                print "********************************************************"
+                print("*******************************************************")
+                print("*")
+                print("* Environment variable",module_variable,"must be set")
+                print("* Module", module, "will be not available")
+                print("*")
+                print("********************************************************")
                 pass
             to_remove_list.append(module)
             continue
@@ -150,14 +142,14 @@ def get_config(silent=False):
 
 def set_env(args, modules_list, modules_root_dir, silent=False):
     """Add to the PATH-variables modules specific paths"""
-    
+
     import os
-    from salome_utils import getTmpDir, generateFileName, makeTmpDir, getPortNumber
+    from salome_utils import getLogDir, generateFileName, makeTmpDir, getPortNumber
 
     # create temporary directory for environment files needed by modules from the list
     port = getPortNumber(False)
     if port:
-        tmp_dir = getTmpDir()
+        tmp_dir = getLogDir()
         env_dir = generateFileName(tmp_dir, prefix="env", with_port=True)
         makeTmpDir(env_dir)
         pass
@@ -167,22 +159,25 @@ def set_env(args, modules_list, modules_root_dir, silent=False):
     if os.getenv('SALOME_BATCH') == None:
       os.putenv('SALOME_BATCH','0')
     if args["gui"] :
-        modules_list = modules_list[:] + ["GUI"] 
-    modules_list = modules_list[:] + ["KERNEL"] 
+        modules_list = modules_list[:] + ["GUI"]
+    modules_list = modules_list[:] + ["KERNEL"]
     for module in modules_list :
-        if modules_root_dir.has_key(module):
+        if module in modules_root_dir:
             module_root_dir = modules_root_dir[module]
             if module_root_dir not in modules_root_dir_list:
               modules_root_dir_list[:0] = [module_root_dir]
             if sys.platform == "win32":
               add_path(os.path.join(module_root_dir,get_lib_dir(),salome_subdir),
                      "PATH")
+            elif sys.platform == "darwin":
+              add_path(os.path.join(module_root_dir,get_lib_dir(),salome_subdir),
+                     "DYLD_LIBRARY_PATH")
             else:
               add_path(os.path.join(module_root_dir,get_lib_dir(),salome_subdir),
                      "LD_LIBRARY_PATH")
             add_path(os.path.join(module_root_dir,"bin",salome_subdir),
                      "PATH")
-            if os.path.exists(module_root_dir + "/examples") :
+            if os.path.exists(os.path.join(module_root_dir, "examples")):
                 add_path(os.path.join(module_root_dir,"examples"),
                          "PYTHONPATH")
                 pass
@@ -224,10 +219,10 @@ def set_env(args, modules_list, modules_root_dir, silent=False):
         os.environ["SALOMEPATH"]=";".join(modules_root_dir_list)
     else:
         os.environ["SALOMEPATH"]=":".join(modules_root_dir_list)
-    
+
     # set trace environment variable
-    
-    if not os.environ.has_key("SALOME_trace"):
+
+    if "SALOME_trace" not in os.environ:
         os.environ["SALOME_trace"]="local"
     if args['file']:
         os.environ["SALOME_trace"]="file:"+args['file'][0]
@@ -242,8 +237,8 @@ def set_env(args, modules_list, modules_root_dir, silent=False):
 
 # -----------------------------------------------------------------------------
 
-def main(silent=False):
-    args, modules_list, modules_root_dir = get_config(silent=silent)
+def main(silent=False, exeName=None):
+    args, modules_list, modules_root_dir = get_config(silent=silent, exeName=exeName)
     set_env(args, modules_list, modules_root_dir, silent=silent)
     return args