Salome HOME
improve the load of environment for the APPLICATION.environ section
[tools/sat.git] / src / environment.py
index 5ad12b0675ec764bd4ba2b622cabbec468e835df..29c8edb1e3f1987cd4979baf54a81246a39e42d1 100644 (file)
@@ -167,7 +167,12 @@ class SalomeEnviron:
     """Class to manage the environment of SALOME.
     """
 
-    def __init__(self, cfg, environ, forBuild=False, for_package=None):
+    def __init__(self,
+                 cfg,
+                 environ,
+                 forBuild=False,
+                 for_package=None,
+                 enable_simple_env_script = True):
         '''Initialization.
 
         :param cfg Config: the global config
@@ -182,6 +187,7 @@ class SalomeEnviron:
         self.cfg = cfg
         self.forBuild = forBuild
         self.for_package = for_package
+        self.enable_simple_env_script = enable_simple_env_script
         self.silent = False
 
     def __repr__(self):
@@ -302,13 +308,16 @@ class SalomeEnviron:
         """Get the products name to add in SALOME_MODULES environment variable
            It is the name of the product, except in the case where the is a 
            component name. And it has to be in SALOME_MODULES variable only 
-           if has_gui = "yes"
+           if the product has the property has_salome_hui = "yes"
         
         :param lProducts list: List of products to potentially add
         """
-        lProdHasGui = [p for p in lProducts if 'type ' in 
-                    src.product.get_product_config(self.cfg, p) and 
-                    src.product.get_product_config(self.cfg, p).type=='salome']
+        lProdHasGui = [p for p in lProducts if 'properties' in 
+            src.product.get_product_config(self.cfg, p) and
+            'has_salome_gui' in 
+            src.product.get_product_config(self.cfg, p).properties and
+            src.product.get_product_config(self.cfg,
+                                           p).properties.has_salome_gui=='yes']
         lProdName = []
         for ProdName in lProdHasGui:
             pi = src.product.get_product_config(self.cfg, ProdName)
@@ -327,8 +336,11 @@ class SalomeEnviron:
         # Set the variables defined in the "environ" section
         if 'environ' in self.cfg.APPLICATION:
             self.add_comment("APPLICATION environment")
-            for p in self.cfg.APPLICATION.environ:
-                self.set(p, self.cfg.APPLICATION.environ[p])
+            self.load_cfg_environment(self.cfg.APPLICATION.environ)
+            if self.forBuild and "build" in self.cfg.APPLICATION.environ:
+                self.load_cfg_environment(self.cfg.APPLICATION.environ.build)
+            if not self.forBuild and "launch" in pi.environ:
+                self.load_cfg_environment(self.cfg.APPLICATION.environ.launch)
             self.add_line(1)
 
         # If there is an "environ_script" section, load the scripts
@@ -339,27 +351,7 @@ class SalomeEnviron:
                 self.run_env_script("APPLICATION_%s" % sname,
                                 self.cfg.APPLICATION.environ_script[pscript],
                                 logger)
-                self.add_line(1)
-        
-        # If there is profile (SALOME), then define additional variables
-        if ('profile' in self.cfg.APPLICATION and 
-                                    "product" in self.cfg.APPLICATION.profile):
-            profile_product = self.cfg.APPLICATION.profile.product
-            product_info_profile = src.product.get_product_config(self.cfg,
-                                                            profile_product)
-            profile_share_salome = os.path.join(product_info_profile.install_dir,
-                                                "share",
-                                                "salome" )
-            self.set( "SUITRoot", profile_share_salome )
-            self.set( "SalomeAppConfig",
-                      os.path.join(profile_share_salome,
-                                   "resources",
-                                   profile_product.lower() ) )
-        
-        # The list of products to launch
-        lProductsName = self.get_names(self.cfg.APPLICATION.products.keys())
-        
-        self.set( "SALOME_MODULES",    ','.join(lProductsName))
+                self.add_line(1)       
 
     def set_salome_minimal_product_env(self, product_info, logger):
         """Sets the minimal environment for a SALOME product.
@@ -377,20 +369,25 @@ class SalomeEnviron:
                 logger.write("  " + _("No install_dir for product %s\n") %
                               product_info.name, 5)
         
-        if not self.for_package:
+        source_in_package = src.get_property_in_product_cfg(product_info,
+                                                           "sources_in_package")
+        if not self.for_package or source_in_package == "yes":
             # set source dir, unless no source dir
             if not src.product.product_is_fixed(product_info):
                 src_dir = product_info.name + "_SRC_DIR"
                 if not self.is_defined(src_dir):
-                    self.set(src_dir, product_info.source_dir)
-
-    def set_salome_generic_product_env(self, product):
+                    if not self.for_package:
+                        self.set(src_dir, product_info.source_dir)
+                    else:
+                        self.set(src_dir, os.path.join("out_dir_Path",
+                                                       "SOURCES",
+                                                       product_info.name))
+
+    def set_salome_generic_product_env(self, pi):
         """Sets the generic environment for a SALOME product.
         
-        :param product str: The product name    
+        :param pi Config: The product description
         """
-        # get the product descritption
-        pi = src.product.get_product_config(self.cfg, product)
         # Construct XXX_ROOT_DIR
         env_root_dir = self.get(pi.name + "_ROOT_DIR")
         l_binpath_libpath = []
@@ -398,7 +395,7 @@ class SalomeEnviron:
         # create additional ROOT_DIR for CPP components
         if 'component_name' in pi:
             compo_name = pi.component_name
-            if compo_name + "CPP" == product:
+            if compo_name + "CPP" == pi.name:
                 compo_root_dir = compo_name + "_ROOT_DIR"
                 envcompo_root_dir = os.path.join(
                             self.cfg.TOOLS.common.install_root, compo_name )
@@ -432,6 +429,35 @@ class SalomeEnviron:
                 ]
             self.prepend('PYTHONPATH', l)
 
+    def set_cpp_env(self, product_info):
+        """Sets the generic environment for a SALOME cpp product.
+        
+        :param product_info Config: The product description
+        """
+        # Construct XXX_ROOT_DIR
+        env_root_dir = self.get(product_info.name + "_ROOT_DIR")
+        l_binpath_libpath = []
+
+        # Construct the paths to prepend to PATH and LD_LIBRARY_PATH and 
+        # PYTHONPATH
+        bin_path = os.path.join(env_root_dir, 'bin')
+        lib_path = os.path.join(env_root_dir, 'lib')
+        l_binpath_libpath.append( (bin_path, lib_path) )
+
+        for bin_path, lib_path in l_binpath_libpath:
+            if not self.forBuild:
+                self.prepend('PATH', bin_path)
+                if src.architecture.is_windows():
+                    self.prepend('PATH', lib_path)
+                else :
+                    self.prepend('LD_LIBRARY_PATH', lib_path)
+
+            l = [ bin_path, lib_path,
+                  os.path.join(env_root_dir, self.python_lib0),
+                  os.path.join(env_root_dir, self.python_lib1)
+                ]
+            self.prepend('PYTHONPATH', l)
+
     def load_cfg_environment(self, cfg_env):
         """Loads environment defined in cfg_env 
         
@@ -487,23 +513,56 @@ class SalomeEnviron:
             pi.install_dir = os.path.join("out_dir_Path",
                                           self.for_package,
                                           pi.name)
-                    
-        # Do not define environment if the product is native
-        if src.product.product_is_native(pi):
-            return
-        
+
         if not self.silent:
             logger.write(_("Setting environment for %s\n") % product, 4)
 
         self.add_line(1)
         self.add_comment('setting environ for ' + product)
-        
+            
+        # Do not define environment if the product is native
+        if src.product.product_is_native(pi):
+            if src.product.product_has_env_script(pi):
+                self.run_env_script(pi, native=True)
+            return
+               
         # Set an additional environment for SALOME products
         if src.product.product_is_salome(pi):
             # set environment using definition of the product
             self.set_salome_minimal_product_env(pi, logger)
-            self.set_salome_generic_product_env(product)
-
+            self.set_salome_generic_product_env(pi)
+        
+        if src.product.product_is_cpp(pi):
+            # set a specific environment for cpp modules
+            self.set_salome_minimal_product_env(pi, logger)
+            self.set_cpp_env(pi)
+            
+            if src.product.product_is_generated(pi):
+                if "component_name" in pi:
+                    # hack the source and install directories in order to point  
+                    # on the generated product source install directories
+                    install_dir_save = pi.install_dir
+                    source_dir_save = pi.source_dir
+                    name_save = pi.name
+                    pi.install_dir = os.path.join(self.cfg.APPLICATION.workdir,
+                                                  "INSTALL",
+                                                  pi.component_name)
+                    if self.for_package:
+                        pi.install_dir = os.path.join("out_dir_Path",
+                                                      self.for_package,
+                                                      pi.component_name)
+                    pi.source_dir = os.path.join(self.cfg.APPLICATION.workdir,
+                                                  "GENERATED",
+                                                  pi.component_name)
+                    pi.name = pi.component_name
+                    self.set_salome_minimal_product_env(pi, logger)
+                    self.set_salome_generic_product_env(pi)
+                    
+                    # Put original values
+                    pi.install_dir = install_dir_save
+                    pi.source_dir = source_dir_save
+                    pi.name = name_save
+        
         # Put the environment define in the configuration of the product
         if "environ" in pi:
             self.load_cfg_environment(pi.environ)
@@ -518,11 +577,12 @@ class SalomeEnviron:
         
             
 
-    def run_env_script(self, product_info, logger=None):
+    def run_env_script(self, product_info, logger=None, native=False):
         """Runs an environment script. 
         
         :param product_info Config: The product description
         :param logger Logger: The logger instance to display messages
+        :param native Boolean: if True load set_native_env instead of set_env
         """
         env_script = product_info.environ.env_script
         # Check that the script exists
@@ -538,8 +598,13 @@ class SalomeEnviron:
             import imp
             pyproduct = imp.load_source(product_info.name + "_env_script",
                                         env_script)
-            pyproduct.set_env(self, product_info.install_dir,
-                              product_info.version)
+            if not native:
+                pyproduct.set_env(self,
+                                  product_info.install_dir,
+                                  product_info.version)
+            else:
+                if "set_nativ_env" in dir(pyproduct):
+                    pyproduct.set_nativ_env(self)
         except:
             __, exceptionValue, exceptionTraceback = sys.exc_info()
             print(exceptionValue)
@@ -554,6 +619,8 @@ class SalomeEnviron:
         :param script_path str: a path to an environment script
         :param logger Logger: The logger instance to display messages
         """
+        if not self.enable_simple_env_script:
+            return
         # Check that the script exists
         if not os.path.exists(script_path):
             raise src.SatException(_("Environment script not found: %s") % 
@@ -588,22 +655,15 @@ class SalomeEnviron:
         self.add_line(1)
         self.add_comment('setting environ for all products')
 
-        self.set_python_libdirs()
-
-        # Set the application working directory
-        if src_root is None:
-            src_root = self.cfg.APPLICATION.workdir
-        self.set('SRC_ROOT', src_root)
-
-        # SALOME variables
-        appli_name = "APPLI"
-        if "APPLI" in self.cfg and "application_name" in self.cfg.APPLI:
-            appli_name = self.cfg.APPLI.application_name
-        self.set("SALOME_APPLI_ROOT",
-                 os.path.join(self.cfg.APPLICATION.workdir, appli_name))
+        # Make sure that the python lib dirs are set after python
+        if "Python" in self.cfg.APPLICATION.products:
+            self.set_a_product("Python", logger)
+            self.set_python_libdirs()
 
         # The loop on the products
         for product in self.cfg.APPLICATION.products.keys():
+            if product == "Python":
+                continue
             self.set_a_product(product, logger)
             self.finish(False)
  
@@ -619,7 +679,7 @@ class SalomeEnviron:
 
         self.set_python_libdirs()
 
-        # set products        
+        # set products
         for product in env_info:
             self.set_a_product(product, logger)
 
@@ -642,7 +702,7 @@ class FileEnvWriter:
         self.silent = True
         self.env_info = env_info
 
-    def write_env_file(self, filename, forBuild, shell):
+    def write_env_file(self, filename, forBuild, shell, for_package = None):
         """Create an environment file.
         
         :param filename str: the file path
@@ -660,7 +720,7 @@ class FileEnvWriter:
         tmp = src.fileEnviron.get_file_environ(env_file,
                                                shell,
                                                {})
-        env = SalomeEnviron(self.config, tmp, forBuild)
+        env = SalomeEnviron(self.config, tmp, forBuild, for_package=for_package)
         env.silent = self.silent
 
         # Set the environment
@@ -669,6 +729,11 @@ class FileEnvWriter:
         else:
             # set env from the APPLICATION
             env.set_application_env(self.logger)
+            
+            # The list of products to launch
+            lProductsName = env.get_names(self.config.APPLICATION.products.keys())
+            env.set( "SALOME_MODULES",    ','.join(lProductsName))
+            
             # set the products
             env.set_products(self.logger,
                             src_root=self.src_root)
@@ -682,7 +747,8 @@ class FileEnvWriter:
     def write_cfgForPy_file(self,
                             filename,
                             additional_env = {},
-                            for_package = None):
+                            for_package = None,
+                            with_commercial = True):
         """Append to current opened aFile a cfgForPy 
            environment (SALOME python launcher).
            
@@ -704,7 +770,8 @@ class FileEnvWriter:
         env = SalomeEnviron(self.config,
                             tmp,
                             forBuild=False,
-                            for_package=for_package)
+                            for_package=for_package,
+                            enable_simple_env_script = with_commercial)
         env.silent = self.silent
 
         if self.env_info is not None:
@@ -713,6 +780,10 @@ class FileEnvWriter:
             # set env from PRODUCT
             env.set_application_env(self.logger)
 
+            # The list of products to launch
+            lProductsName = env.get_names(self.config.APPLICATION.products.keys())
+            env.set( "SALOME_MODULES",    ','.join(lProductsName))
+
             # set the products
             env.set_products(self.logger,
                             src_root=self.src_root)
@@ -747,4 +818,4 @@ def load_environment(config, build, logger):
     environ = SalomeEnviron(config, Environ(os.environ), build)
     environ.set_application_env(logger)
     environ.set_products(logger)
-    environ.finish(True)
\ No newline at end of file
+    environ.finish(True)