Salome HOME
fix bug for overwriting products in the application pyconf
[tools/sat.git] / commands / config.py
index 9023b0758acbccee193e76690e9109328a442b8a..0efbfbd105de9491d6bcb87f4c38202fa29b6b09 100644 (file)
@@ -47,6 +47,8 @@ parser.add_option('c', 'copy', 'boolean', 'copy',
 \tIf a name is given the new config file takes the given name."""))
 parser.add_option('n', 'no_label', 'boolean', 'no_label',
     _("Internal use: do not print labels, Works only with --value and --list."))
+parser.add_option('', 'completion', 'boolean', 'completion',
+    _("Internal use: print only keys, works only with --value."))
 parser.add_option('s', 'schema', 'boolean', 'schema',
     _("Internal use."))
 
@@ -294,9 +296,11 @@ class ConfigManager:
                 project_cfg = src.pyconf.Config(open(project_pyconf_path),
                                                 PWD=("", project_pyconf_dir))
             except Exception as e:
-                raise src.SatException(_("Error in configuration file: "
-                                 "%(file_path)s\n  %(error)s") % \
-                            {'file_path' : project_pyconf_path, 'error': str(e) })
+                msg = _("ERROR: Error in configuration file: "
+                                 "%(file_path)s\n  %(error)s\n") % \
+                            {'file_path' : project_pyconf_path, 'error': str(e) }
+                sys.stdout.write(msg)
+                continue
             projects_cfg.PROJECTS.projects.addMapping(project_name,
                              src.pyconf.Mapping(projects_cfg.PROJECTS.projects),
                              "The %s project\n" % project_name)
@@ -341,46 +345,6 @@ class ConfigManager:
         for rule in self.get_command_line_overrides(options, ["PATHS"]):
             exec('cfg.' + rule) # this cannot be factorized because of the exec
 
-        # =====================================================================
-        # Load product config files in PRODUCTS section
-        products_cfg = src.pyconf.Config()
-        products_cfg.addMapping("PRODUCTS",
-                                src.pyconf.Mapping(products_cfg),
-                                "The products\n")
-        src.pyconf.streamOpener = ConfigOpener(cfg.PATHS.PRODUCTPATH)
-        for products_dir in cfg.PATHS.PRODUCTPATH:
-            # Loop on all files that are in softsDir directory
-            # and read their config
-            for fName in os.listdir(products_dir):
-                if fName.endswith(".pyconf"):
-                    pName = fName[:-len(".pyconf")]
-                    if pName in products_cfg.PRODUCTS:
-                        continue
-                    try:
-                        prod_cfg = src.pyconf.Config(open(
-                                                    os.path.join(products_dir,
-                                                                 fName)),
-                                                     PWD=("", products_dir))
-                    except src.pyconf.ConfigError as e:
-                        raise src.SatException(_(
-                            "Error in configuration file: %(prod)s\n  %(error)s") % \
-                            {'prod' :  fName, 'error': str(e) })
-                    except IOError as error:
-                        e = str(error)
-                        raise src.SatException( e );
-                    except Exception as e:
-                        raise src.SatException(_(
-                            "Error in configuration file: %(prod)s\n  %(error)s") % \
-                            {'prod' :  fName, 'error': str(e) })
-                    
-                    products_cfg.PRODUCTS[pName] = prod_cfg
-        
-        merger.merge(cfg, products_cfg)
-        
-        # apply overwrite from command line if needed
-        for rule in self.get_command_line_overrides(options, ["PRODUCTS"]):
-            exec('cfg.' + rule) # this cannot be factorized because of the exec
-
         # =====================================================================
         # Load APPLICATION config file
         if application is not None:
@@ -406,37 +370,74 @@ class ConfigManager:
                                         "There is an error in the file"
                                         " %s.pyconf.\n" % cfg.VARS.application))
                     do_merge = False
-            except:
+            except Exception as e:
                 if (not ('-e' in parser.parse_args()[1]) 
                                         or ('--edit' in parser.parse_args()[1]) 
                                         and command == 'config'):
+                    sys.stdout.write(src.printcolors.printcWarning("%s\n" % str(e)))
                     raise src.SatException(_("Error in configuration file:"
                                              " %(application)s.pyconf\n") % \
                         { 'application': application} )
                 else:
                     sys.stdout.write(src.printcolors.printcWarning(
-                                    "There is an error in the file"
-                                    " %s.pyconf. Opening the file with the"
-                                    " default viewer\n" % cfg.VARS.application))
+                                "There is an error in the file"
+                                " %s.pyconf. Opening the file with the"
+                                " default viewer\n" % cfg.VARS.application))
+                    sys.stdout.write("The error:"
+                                 " %s\n" % src.printcolors.printcWarning(
+                                                                      str(e)))
                     do_merge = False
+        
+            else:
+                cfg['open_application'] = 'yes'
 
+        # =====================================================================
+        # Load product config files in PRODUCTS section
+        products_cfg = src.pyconf.Config()
+        products_cfg.addMapping("PRODUCTS",
+                                src.pyconf.Mapping(products_cfg),
+                                "The products\n")
+        if application is not None:
+            src.pyconf.streamOpener = ConfigOpener(cfg.PATHS.PRODUCTPATH)
+            for product_name in application_cfg.APPLICATION.products.keys():
+                # Loop on all files that are in softsDir directory
+                # and read their config
+                product_file_name = product_name + ".pyconf"
+                product_file_path = src.find_file_in_lpath(product_file_name, cfg.PATHS.PRODUCTPATH)
+                if product_file_path:
+                    products_dir = os.path.dirname(product_file_path)
+                    try:
+                        prod_cfg = src.pyconf.Config(open(
+                                                    os.path.join(products_dir,
+                                                                 product_file_name)),
+                                                     PWD=("", products_dir))
+                        products_cfg.PRODUCTS[product_name] = prod_cfg
+                    except Exception as e:
+                        msg = _(
+                            "WARNING: Error in configuration file"
+                            ": %(prod)s\n  %(error)s" % \
+                            {'prod' :  product_name, 'error': str(e) })
+                        sys.stdout.write(msg)
+            
+            merger.merge(cfg, products_cfg)
+            
+            # apply overwrite from command line if needed
+            for rule in self.get_command_line_overrides(options, ["PRODUCTS"]):
+                exec('cfg.' + rule) # this cannot be factorized because of the exec
+            
             if do_merge:
                 merger.merge(cfg, application_cfg)
-            
+
+                # default launcher name ('salome')
+                if ('profile' in cfg.APPLICATION and 
+                    'launcher_name' not in cfg.APPLICATION.profile):
+                    cfg.APPLICATION.profile.launcher_name = 'salome'
+
                 # apply overwrite from command line if needed
                 for rule in self.get_command_line_overrides(options,
                                                              ["APPLICATION"]):
                     # this cannot be factorized because of the exec
                     exec('cfg.' + rule)
-                    
-                # default launcher name ('salome')
-                if ('profile' in cfg.APPLICATION and 
-                    'launcher_name' not in cfg.APPLICATION.profile):
-                    cfg.APPLICATION.profile.launcher_name = 'salome'
-        
-            else:
-                cfg['open_application'] = 'yes'
-                
             
         # =====================================================================
         # load USER config
@@ -950,5 +951,10 @@ def run(args, runner, logger):
                     src.printcolors.printcLabel(runner.cfg.VARS.application), 3)
         logger.write("\n", 2, False)
         show_patchs(runner.cfg, logger)
+    
+    # case: print all the products name of the application (internal use for completion)
+    elif options.completion:
+        for product_name in runner.cfg.APPLICATION.products.keys():
+            logger.write("%s\n" % product_name)
         
-    
\ No newline at end of file
+