Salome HOME
fix bug for overwriting products in the application pyconf
[tools/sat.git] / commands / config.py
index fbb4cb90cf5c0274b39551df7e56a4b3fedf3cd7..0efbfbd105de9491d6bcb87f4c38202fa29b6b09 100644 (file)
@@ -32,19 +32,23 @@ gettext.install('salomeTools', os.path.join(satdir, 'src', 'i18n'))
 # Define all possible option for config command :  sat config <options>
 parser = src.options.Options()
 parser.add_option('v', 'value', 'string', 'value',
-    _("print the value of CONFIG_VARIABLE."))
+    _("Optional: print the value of CONFIG_VARIABLE."))
 parser.add_option('e', 'edit', 'boolean', 'edit',
-    _("edit the product configuration file."))
+    _("Optional: edit the product configuration file."))
 parser.add_option('i', 'info', 'string', 'info',
-    _("get information on a product."))
+    _("Optional: get information on a product."))
 parser.add_option('l', 'list', 'boolean', 'list',
-    _("list all available applications."))
+    _("Optional: list all available applications."))
+parser.add_option('', 'show_patchs', 'boolean', 'show_patchs',
+    _("Optional: synthetic view of all patches used in the application"))
 parser.add_option('c', 'copy', 'boolean', 'copy',
-    _("""copy a config file to the personnal config files directory.
+    _("""Optional: copy a config file to the personal config files directory.
 \tWARNING the included files are not copied.
 \tIf a name is given the new config file takes the given name."""))
 parser.add_option('n', 'no_label', 'boolean', 'no_label',
-    _("do not print labels, Works only with --value and --list."))
+    _("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."))
 
@@ -55,7 +59,7 @@ class ConfigOpener:
     def __init__(self, pathList):
         '''Initialization
         
-        :param pathList list: The list of paths where to serach a pyconf.
+        :param pathList list: The list of paths where to search a pyconf.
         '''
         self.pathList = pathList
 
@@ -98,6 +102,7 @@ class ConfigManager:
         var['salometoolsway'] = os.path.dirname(
                                     os.path.dirname(os.path.abspath(__file__)))
         var['srcDir'] = os.path.join(var['salometoolsway'], 'src')
+        var['internal_dir'] = os.path.join(var['srcDir'], 'internal_config')
         var['sep']= os.path.sep
         
         # datadir has a default location
@@ -107,6 +112,27 @@ class ConfigManager:
 
         var['personalDir'] = os.path.join(os.path.expanduser('~'),
                                            '.salomeTools')
+        src.ensure_path_exists(var['personalDir'])
+
+        var['personal_applications_dir'] = os.path.join(var['personalDir'],
+                                                        "Applications")
+        src.ensure_path_exists(var['personal_applications_dir'])
+        
+        var['personal_products_dir'] = os.path.join(var['personalDir'],
+                                                    "products")
+        src.ensure_path_exists(var['personal_products_dir'])
+        
+        var['personal_archives_dir'] = os.path.join(var['personalDir'],
+                                                    "Archives")
+        src.ensure_path_exists(var['personal_archives_dir'])
+
+        var['personal_jobs_dir'] = os.path.join(var['personalDir'],
+                                                "Jobs")
+        src.ensure_path_exists(var['personal_jobs_dir'])
+
+        var['personal_machines_dir'] = os.path.join(var['personalDir'],
+                                                    "Machines")
+        src.ensure_path_exists(var['personal_machines_dir'])
 
         # read linux distributions dictionary
         distrib_cfg = src.pyconf.Config(os.path.join(var['srcDir'],
@@ -219,14 +245,15 @@ class ConfigManager:
         # apply overwrite from command line if needed
         for rule in self.get_command_line_overrides(options, ["INTERNAL"]):
             exec('cfg.' + rule) # this cannot be factorized because of the exec        
-        
+               
         # =====================================================================
         # Load SITE config file
         # search only in the data directory
         src.pyconf.streamOpener = ConfigOpener([cfg.VARS.datadir])
         try:
             site_cfg = src.pyconf.Config(open(os.path.join(cfg.VARS.datadir, 
-                                                           'site.pyconf')))
+                                                           'site.pyconf')),
+                                         PWD = ('SITE', cfg.VARS.datadir) )
         except src.pyconf.ConfigError as e:
             raise src.SatException(_("Error in configuration file: "
                                      "site.pyconf\n  %(error)s") % \
@@ -240,81 +267,178 @@ class ConfigManager:
                   + cfg.VARS.sep 
                   + "site.pyconf and edit the file")
             raise src.SatException( e );
-        
-        # add user local path for configPath
-        site_cfg.SITE.config.config_path.append(
-                        os.path.join(cfg.VARS.personalDir, 'Applications'), 
-                        "User applications path")
-        
         merger.merge(cfg, site_cfg)
 
         # apply overwrite from command line if needed
         for rule in self.get_command_line_overrides(options, ["SITE"]):
             exec('cfg.' + rule) # this cannot be factorized because of the exec
-  
         
+        # =====================================================================
+        # Load the PROJECTS
+        projects_cfg = src.pyconf.Config()
+        projects_cfg.addMapping("PROJECTS",
+                                src.pyconf.Mapping(projects_cfg),
+                                "The projects\n")
+        projects_cfg.PROJECTS.addMapping("projects",
+                                src.pyconf.Mapping(cfg.PROJECTS),
+                                "The projects definition\n")
+        
+        for project_pyconf_path in cfg.PROJECTS.project_file_paths:
+            if not os.path.exists(project_pyconf_path):
+                msg = _("WARNING: The project file %s cannot be found. "
+                        "It will be ignored\n" % project_pyconf_path)
+                sys.stdout.write(msg)
+                continue
+            project_name = os.path.basename(
+                                    project_pyconf_path)[:-len(".pyconf")]
+            try:
+                project_pyconf_dir = os.path.dirname(project_pyconf_path)
+                project_cfg = src.pyconf.Config(open(project_pyconf_path),
+                                                PWD=("", project_pyconf_dir))
+            except Exception as 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)
+            projects_cfg.PROJECTS.projects[project_name]=project_cfg
+            projects_cfg.PROJECTS.projects[project_name]["file_path"] = \
+                                                        project_pyconf_path
+                   
+        merger.merge(cfg, projects_cfg)
+
+        # apply overwrite from command line if needed
+        for rule in self.get_command_line_overrides(options, ["PROJECTS"]):
+            exec('cfg.' + rule) # this cannot be factorized because of the exec
+        
+        # =====================================================================
+        # Create the paths where to search the application configurations, 
+        # the product configurations, the products archives, 
+        # the jobs configurations and the machines configurations
+        cfg.addMapping("PATHS", src.pyconf.Mapping(cfg), "The paths\n")
+        cfg.PATHS["APPLICATIONPATH"] = src.pyconf.Sequence(cfg.PATHS)
+        cfg.PATHS.APPLICATIONPATH.append(cfg.VARS.personal_applications_dir, "")
+        
+        cfg.PATHS["PRODUCTPATH"] = src.pyconf.Sequence(cfg.PATHS)
+        cfg.PATHS.PRODUCTPATH.append(cfg.VARS.personal_products_dir, "")
+        cfg.PATHS["ARCHIVEPATH"] = src.pyconf.Sequence(cfg.PATHS)
+        cfg.PATHS.ARCHIVEPATH.append(cfg.VARS.personal_archives_dir, "")
+        cfg.PATHS["JOBPATH"] = src.pyconf.Sequence(cfg.PATHS)
+        cfg.PATHS.JOBPATH.append(cfg.VARS.personal_jobs_dir, "")
+        cfg.PATHS["MACHINEPATH"] = src.pyconf.Sequence(cfg.PATHS)
+        cfg.PATHS.MACHINEPATH.append(cfg.VARS.personal_machines_dir, "")
+        # Loop over the projects in order to complete the PATHS variables
+        for project in cfg.PROJECTS.projects:
+            for PATH in ["APPLICATIONPATH",
+                         "PRODUCTPATH",
+                         "ARCHIVEPATH",
+                         "JOBPATH",
+                         "MACHINEPATH"]:
+                if PATH not in cfg.PROJECTS.projects[project]:
+                    continue
+                cfg.PATHS[PATH].append(cfg.PROJECTS.projects[project][PATH], "")
+        
+        # apply overwrite from command line if needed
+        for rule in self.get_command_line_overrides(options, ["PATHS"]):
+            exec('cfg.' + rule) # this cannot be factorized because of the exec
+
         # =====================================================================
         # Load APPLICATION config file
         if application is not None:
             # search APPLICATION file in all directories in configPath
-            cp = cfg.SITE.config.config_path
+            cp = cfg.PATHS.APPLICATIONPATH
             src.pyconf.streamOpener = ConfigOpener(cp)
+            do_merge = True
             try:
                 application_cfg = src.pyconf.Config(application + '.pyconf')
             except IOError as e:
                 raise src.SatException(_("%s, use 'config --list' to get the"
                                          " list of available applications.") %e)
             except src.pyconf.ConfigError as e:
-                raise src.SatException(_("Error in configuration file:"
-                                " %(application)s.pyconf\n  %(error)s") % \
-                    { 'application': application, 'error': str(e) } )
-
-            merger.merge(cfg, application_cfg)
-
-            # 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'
+                if (not ('-e' in parser.parse_args()[1]) 
+                                         or ('--edit' in parser.parse_args()[1]) 
+                                         and command == 'config'):
+                    raise src.SatException(_("Error in configuration file: "
+                                             "%(application)s.pyconf\n "
+                                             " %(error)s") % \
+                        { 'application': application, 'error': str(e) } )
+                else:
+                    sys.stdout.write(src.printcolors.printcWarning(
+                                        "There is an error in the file"
+                                        " %s.pyconf.\n" % cfg.VARS.application))
+                    do_merge = False
+            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))
+                    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
-       
-        # The directory containing the softwares definition
-        products_dir = os.path.join(cfg.VARS.datadir, 'products')
-        
-        # Loop on all files that are in softsDir directory
-        # and read their config
-        for fName in os.listdir(products_dir):
-            if fName.endswith(".pyconf"):
-                src.pyconf.streamOpener = ConfigOpener([products_dir])
-                try:
-                    prod_cfg = src.pyconf.Config(open(
-                                                os.path.join(products_dir, fName)))
-                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) })
-                
-                merger.merge(cfg.PRODUCTS, prod_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
-
-        
+        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)
+            
         # =====================================================================
         # load USER config
         self.set_user_config_file(cfg)
@@ -387,14 +511,22 @@ class ConfigManager:
                                  'evince', 
                                  "This is the pdf_viewer used "
                                  "to read pdf documentation\n")
-        user_cfg.USER.addMapping("bases",
-                                 src.pyconf.Mapping(user_cfg.USER),
-                                 "The products installation base(s)\n")
+        user_cfg.USER.addMapping("base",
+                                 src.pyconf.Reference(
+                                            user_cfg,
+                                            src.pyconf.DOLLAR,
+                                            'workdir  + $VARS.sep + "BASE"'),
+                                 "The products installation base (could be "
+                                 "ignored if this key exists in the site.pyconf"
+                                 " file of salomTools).\n")
         
-        user_cfg.USER.bases.base = src.pyconf.Reference(
+        user_cfg.USER.addMapping("log_dir",
+                                 src.pyconf.Reference(
                                             user_cfg,
                                             src.pyconf.DOLLAR,
-                                            'workdir  + $VARS.sep + "BASE"')
+                                            'workdir  + $VARS.sep + "LOGS"'),
+                                 "The log repository\n")
+        
         # 
         src.ensure_path_exists(config.VARS.personalDir)
         src.ensure_path_exists(os.path.join(config.VARS.personalDir, 
@@ -403,8 +535,6 @@ class ConfigManager:
         f = open(cfg_name, 'w')
         user_cfg.__save__(f)
         f.close()
-        print(_("You can edit it to configure salomeTools "
-                "(use: sat config --edit).\n"))
 
         return user_cfg   
 
@@ -456,7 +586,7 @@ def show_product_info(config, name, logger):
     # Type of the product
     ptype = src.get_cfg_param(pinfo, "type", "")
     src.printcolors.print_value(logger, "type", ptype, 2)
-    if "opt_depend" in pinfo:
+    if "depend" in pinfo:
         src.printcolors.print_value(logger, 
                                     "depends on", 
                                     ', '.join(pinfo.depend), 2)
@@ -508,39 +638,44 @@ def show_product_info(config, name, logger):
         return
     
     # information on compilation
-    logger.write("\n", 2)
-    logger.write(src.printcolors.printcLabel("compile:") + "\n", 2)
-    src.printcolors.print_value(logger, 
-                                "compilation method", 
-                                pinfo.build_source, 
-                                2)
-    
-    if pinfo.build_source == "script" and "compil_script" in pinfo:
+    if src.product.product_compiles(pinfo):
+        logger.write("\n", 2)
+        logger.write(src.printcolors.printcLabel("compile:") + "\n", 2)
         src.printcolors.print_value(logger, 
-                                    "Compilation script", 
-                                    pinfo.compil_script
+                                    "compilation method", 
+                                    pinfo.build_source
                                     2)
+        
+        if pinfo.build_source == "script" and "compil_script" in pinfo:
+            src.printcolors.print_value(logger, 
+                                        "Compilation script", 
+                                        pinfo.compil_script, 
+                                        2)
+        
+        if 'nb_proc' in pinfo:
+            src.printcolors.print_value(logger, "make -j", pinfo.nb_proc, 2)
     
-    if 'nb_proc' in pinfo:
-        src.printcolors.print_value(logger, "make -j", pinfo.nb_proc, 2)
-
-    src.printcolors.print_value(logger, 
-                                "source dir", 
-                                check_path(pinfo.source_dir), 
-                                2)
-    if 'install_dir' in pinfo:
         src.printcolors.print_value(logger, 
-                                    "build dir", 
-                                    check_path(pinfo.build_dir), 
-                                    2)
-        src.printcolors.print_value(logger, 
-                                    "install dir", 
-                                    check_path(pinfo.install_dir), 
+                                    "source dir", 
+                                    check_path(pinfo.source_dir), 
                                     2)
+        if 'install_dir' in pinfo:
+            src.printcolors.print_value(logger, 
+                                        "build dir", 
+                                        check_path(pinfo.build_dir), 
+                                        2)
+            src.printcolors.print_value(logger, 
+                                        "install dir", 
+                                        check_path(pinfo.install_dir), 
+                                        2)
+        else:
+            logger.write("  " + 
+                         src.printcolors.printcWarning(_("no install dir")) + 
+                         "\n", 2)
     else:
-        logger.write("  " + 
-                     src.printcolors.printcWarning(_("no install dir")) + 
-                     "\n", 2)
+        logger.write("\n", 2)
+        msg = _("This product does not compile")
+        logger.write("%s\n" % msg, 2)
 
     # information on environment
     logger.write("\n", 2)
@@ -557,7 +692,27 @@ def show_product_info(config, name, logger):
     zz.set_python_libdirs()
     zz.set_a_product(name, logger)
         
+def show_patchs(config, logger):
+    '''Prints all the used patchs in the application.
     
+    :param config Config: the global configuration.
+    :param logger Logger: The logger instance to use for the display
+    '''
+    len_max = max([len(p) for p in config.APPLICATION.products]) + 2
+    for product in config.APPLICATION.products:
+        product_info = src.product.get_product_config(config, product)
+        if src.product.product_has_patches(product_info):
+            logger.write("%s: " % product, 1)
+            logger.write(src.printcolors.printcInfo(
+                                            " " * (len_max - len(product) -2) +
+                                            "%s\n" % product_info.patches[0]),
+                         1)
+            if len(product_info.patches) > 1:
+                for patch in product_info.patches[1:]:
+                    logger.write(src.printcolors.printcInfo(len_max*" " +
+                                                            "%s\n" % patch), 1)
+            logger.write("\n", 1)
+
 def print_value(config, path, show_label, logger, level=0, show_full_path=False):
     '''Prints a value from the configuration. Prints recursively the values 
        under the initial path.
@@ -659,7 +814,8 @@ def description():
     :rtype: str
     '''
     return _("The config command allows manipulation "
-             "and operation on config files.")
+             "and operation on config files.\n\nexample:\nsat config "
+             "SALOME-master --info ParaView")
     
 
 def run(args, runner, logger):
@@ -686,16 +842,19 @@ def run(args, runner, logger):
     # case : edit user pyconf file or application file
     elif options.edit:
         editor = runner.cfg.USER.editor
-        if 'APPLICATION' not in runner.cfg: # edit user pyconf
+        if ('APPLICATION' not in runner.cfg and
+                       'open_application' not in runner.cfg): # edit user pyconf
             usercfg = os.path.join(runner.cfg.VARS.personalDir, 
                                    'salomeTools.pyconf')
+            logger.write(_("Openning %s\n" % usercfg), 3)
             src.system.show_in_editor(editor, usercfg, logger)
         else:
             # search for file <application>.pyconf and open it
-            for path in runner.cfg.SITE.config.config_path:
+            for path in runner.cfg.PATHS.APPLICATIONPATH:
                 pyconf_path = os.path.join(path, 
                                     runner.cfg.VARS.application + ".pyconf")
                 if os.path.exists(pyconf_path):
+                    logger.write(_("Openning %s\n" % pyconf_path), 3)
                     src.system.show_in_editor(editor, pyconf_path, logger)
                     break
     
@@ -720,7 +879,7 @@ def run(args, runner, logger):
         # get application file path 
         source = runner.cfg.VARS.application + '.pyconf'
         source_full_path = ""
-        for path in runner.cfg.SITE.config.config_path:
+        for path in runner.cfg.PATHS.APPLICATIONPATH:
             # ignore personal directory
             if path == runner.cfg.VARS.personalDir:
                 continue
@@ -737,9 +896,9 @@ def run(args, runner, logger):
             if len(args) > 0:
                 # a name is given as parameter, use it
                 dest = args[0]
-            elif 'copy_prefix' in runner.cfg.SITE.config:
+            elif 'copy_prefix' in runner.cfg.INTERNAL.config:
                 # use prefix
-                dest = (runner.cfg.SITE.config.copy_prefix 
+                dest = (runner.cfg.INTERNAL.config.copy_prefix 
                         + runner.cfg.VARS.application)
             else:
                 # use same name as source
@@ -760,7 +919,7 @@ def run(args, runner, logger):
     elif options.list:
         lproduct = list()
         # search in all directories that can have pyconf applications
-        for path in runner.cfg.SITE.config.config_path:
+        for path in runner.cfg.PATHS.APPLICATIONPATH:
             # print a header
             if not options.no_label:
                 logger.write("------ %s\n" % src.printcolors.printcHeader(path))
@@ -784,5 +943,18 @@ def run(args, runner, logger):
                             logger.write("%s\n" % appliname)
                             
             logger.write("\n")
+    # case : give a synthetic view of all patches used in the application
+    elif options.show_patchs:
+        src.check_config_has_application(runner.cfg)
+        # Print some informations
+        logger.write(_('Show the patchs of application %s\n') % 
+                    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