From 9940d302a0bd085217ce6c3c6b722d9da0450b3c Mon Sep 17 00:00:00 2001 From: Christian Van Wambeke Date: Tue, 3 Apr 2018 16:41:10 +0200 Subject: [PATCH] fix commands/config.py sat config -l --- commands/application.py | 9 ++++----- commands/config.py | 26 ++++++++++++++------------ commands/package.py | 5 +++-- sat | 3 ++- src/configManager.py | 22 ++++++++++------------ src/options.py | 40 ++++++++++++++++++++++++++++++---------- src/product.py | 13 +++++++------ 7 files changed, 70 insertions(+), 48 deletions(-) diff --git a/commands/application.py b/commands/application.py index 6c891ac..f14ff0d 100644 --- a/commands/application.py +++ b/commands/application.py @@ -94,15 +94,14 @@ NOTICE: this command will ssh to retrieve information to each machine in the l # check for product src.check_config_has_application( runner.cfg ) - application = src.printcolors.printcLabel(runner.cfg.VARS.application) - logger.write(_("Building application for %s\n") % application, 1) + application = runner.cfg.VARS.application + logger.info(_("Building application for
%s\n") % application) # if section APPLICATION.virtual_app does not exists create one if "virtual_app" not in runner.cfg.APPLICATION: msg = _("The section APPLICATION.virtual_app is not defined in the product.") - logger.write(src.printcolors.printcError(msg), 1) - logger.write("\n", 1) - return 1 + logger.info("red>" + msg + "" ) + return RCO.ReturnCode("KO", msg) # get application dir target_dir = runner.cfg.APPLICATION.workdir diff --git a/commands/config.py b/commands/config.py index 489375c..6db06c7 100644 --- a/commands/config.py +++ b/commands/config.py @@ -17,10 +17,12 @@ # License along with this library; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +import os import src.debug as DBG import src.returnCode as RCO from src.salomeTools import _BaseCommand +import src.configManager as CFGMGR ######################################################################## @@ -58,9 +60,10 @@ class Command(_BaseCommand): parser.add_option('p', 'show_patchs', 'boolean', 'show_patchs', _("Optional: synthetic view of all patches used in the application")) parser.add_option('c', 'copy', 'boolean', 'copy', - _("""Optional: copy a config file (.pyconf) to the personal config files directory. - Warning: the included files are not copied. - If a name is given the new config file takes the given name.""")) + _("""\ +Optional: copy a config file (.pyconf) to the personal config files directory. +Warning: the included files are not copied. +If 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('o', 'completion', 'boolean', 'completion', @@ -103,9 +106,9 @@ class Command(_BaseCommand): if options.value == ".": # if argument is ".", print all the config for val in sorted(config.keys()): - print_value(config, val, not options.no_label, logger) + CFGMGR.print_value(config, val, logger, not options.no_label) else: - print_value(config, options.value, not options.no_label, logger, + CFGMGR.print_value(config, options.value, logger, not options.no_label, level=0, show_full_path=False) if options.debug: @@ -191,11 +194,10 @@ class Command(_BaseCommand): for path in config.PATHS.APPLICATIONPATH: # print a header if not options.no_label: - logger.write("------ %s\n" % src.printcolors.printcHeader(path)) - + logger.info("
------ %s" % path) + msg = "" # only one multiline info if not os.path.exists(path): - logger.write(src.printcolors.printcError( - _("Directory not found")) + "\n" ) + msg += ("" + _("Directory not found") + "\n" ) else: for f in sorted(os.listdir(path)): # ignore file that does not ends with .pyconf @@ -207,11 +209,11 @@ class Command(_BaseCommand): lproduct.append(appliname) if path.startswith(config.VARS.personalDir) \ and not options.no_label: - logger.write("%s*\n" % appliname) + msg += "%s*\n" % appliname else: - logger.write("%s\n" % appliname) + msg += "%s\n" % appliname - logger.write("\n") + logger.info(msg) # case : give a synthetic view of all patches used in the application elif options.show_patchs: diff --git a/commands/package.py b/commands/package.py index 868fa48..6d4d536 100644 --- a/commands/package.py +++ b/commands/package.py @@ -31,6 +31,7 @@ import src.debug as DBG import src.returnCode as RCO from src.salomeTools import _BaseCommand import src.pyconf as PYCONF +import src.utilsSat as UTS BINARY = "binary" SOURCE = "Source" @@ -1142,7 +1143,7 @@ def find_product_scripts_and_pyconf(p_name, ''' # read the pyconf of the product - product_pyconf_path = src.find_file_in_lpath(p_name + ".pyconf", + product_pyconf_path = UTS.find_file_in_lpath(p_name + ".pyconf", config.PATHS.PRODUCTPATH) product_pyconf_cfg = PYCONF.Config(product_pyconf_path) @@ -1204,7 +1205,7 @@ def find_application_pyconf(config, application_tmp_dir): ''' # read the pyconf of the application application_name = config.VARS.application - application_pyconf_path = src.find_file_in_lpath( + application_pyconf_path = UTS.find_file_in_lpath( application_name + ".pyconf", config.PATHS.APPLICATIONPATH) application_pyconf_cfg = PYCONF.Config(application_pyconf_path) diff --git a/sat b/sat index 08eda1c..6769e8a 100755 --- a/sat +++ b/sat @@ -59,13 +59,14 @@ if __name__ == "__main__": sys.exit(returnCode.toSys()) except Exception as e: - msg = "Exception raised for execute_cli('%s'):\n\n%s" if (_debug) or (DBG._user in DBG._developpers): # verbose debug message with traceback + msg = "Exception raised for execute_cli(%s):\n\n%s" import traceback logger.critical( msg % (args, traceback.format_exc()) ) else: # short production message + msg = "Exception raised for execute_cli(%s):\n\n%s\n" logger.critical( msg % (args, e) ) sys.exit(KOSYS) diff --git a/src/configManager.py b/src/configManager.py index 2730434..91d5e13 100644 --- a/src/configManager.py +++ b/src/configManager.py @@ -351,8 +351,8 @@ class ConfigManager: try: application_cfg = PYCONF.Config(application + '.pyconf') except IOError as e: - raise Exception(_("%s, use 'config --list' to get the" - " list of available applications.") % e) + raise Exception(_("%s\n(use 'config --list' to get the" + " list of available applications)") % e) except PYCONF.ConfigError as e: if (not ('-e' in parser.parse_args()[1]) or ('--edit' in parser.parse_args()[1]) @@ -394,7 +394,7 @@ class ConfigManager: # 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) + product_file_path = UTS.find_file_in_lpath(product_file_name, cfg.PATHS.PRODUCTPATH) if product_file_path: products_dir = os.path.dirname(product_file_path) try: @@ -732,28 +732,26 @@ def getConfigColored(config, path, stream, show_label=False, level=0, show_full_ try: val = config.getByPath(path) except Exception as e: - stream.write(tab_level) - stream.write("
%s: ERROR %s\n" % (vname, str(e))) + stream.write(tab_level + "
%s: ERROR %s\n" % (vname, str(e))) return # in this case, display only the value if show_label: - stream.write(tab_level) - stream.write("" % vname) + stream.write(tab_level + "
%s: " % vname) # The case where the value has under values, # do a recursive call to the function if dir(val).__contains__('keys'): - if show_label: strean.write("\n") + if show_label: stream.write("\n") for v in sorted(val.keys()): - print_value(config, path + '.' + v, stream, show_label, level + 1) + getConfigColored(config, path + '.' + v, stream, show_label, level + 1) elif val.__class__ == PYCONF.Sequence or isinstance(val, list): # in this case, value is a list (or a Sequence) if show_label: stream.write("\n") index = 0 for v in val: p = path + "[" + str(index) + "]" - print_value(config, p, stream, show_label, level + 1) + getConfigColored(config, p, stream, show_label, level + 1) index += 1 else: # case where val is just a str stream.write("%s\n" % val) @@ -764,10 +762,10 @@ def print_value(config, path, logger, show_label=False, level=0, show_full_path= used recursively from the initial path. :param see getConfigColored - """ + """ outStream = DBG.OutStream() getConfigColored(config, path, outStream, show_label, level, show_full_path) - res = outStream.value + res = outStream.getvalue() # stream not closed logger.info(res) return diff --git a/src/options.py b/src/options.py index 714fcc4..57395bb 100644 --- a/src/options.py +++ b/src/options.py @@ -49,7 +49,7 @@ class OptResult(object): :param name str: The attribute to get the value. :return: the value corresponding to the attribute. - :rtype: str,int,list,boolean + :rtype: str,int,list,boolean,level ''' if name in self.__dict__: return self.__dict__[name] @@ -82,7 +82,7 @@ class Options(object): # in a list that contains dicts self.options = [] # The list of available option type - self.availableOptions = "boolean string int float long list list2".split() + self.availableOptions = "boolean string int float long list list2, level".split() self.default = None self.results = {} @@ -123,6 +123,17 @@ class Options(object): option['result'] = default self.options.append(option) + + def getDetailOption(self, option): + """for convenience + returns (shortName, longName, optionType, helpString') + """ + oos = option['shortName'] + ool = option['longName'] + oot = option['optionType'] + ooh = option['helpString'] + return (oos, ool, oot, ooh) + def get_help(self): '''Method that returns all options stored in self.options @@ -137,17 +148,23 @@ class Options(object): return _("No available options.") # for all options, gets its values. - # "shortname" is an optional field of the options - msg += "
" + _("Available options are:") + "\n" + # "shortname" is an mandatory field of the options, could be '' + msg += "
" + _("Available options are:") + "" for option in self.options: - if 'shortName' in option and len(option['shortName']) > 0: - msg += "\n -%(shortName)1s, --%(longName)s" \ - " (%(optionType)s)\n\t%(helpString)s\n" % option + oos, ool, oot, ooh = self.getDetailOption(option) + if len(oos) > 0: + msg += "\n -%1s, --%s (%s)\n" % (oos, ool, oot) else: - msg += "\n --%(longName)s (%(optionType)s)\n\t%(helpString)s\n" \ - % option + msg += "\n --%s (%s)\n" % (ool, oot) + + msg += "%s\n" % self.indent(ooh, 10) return msg - + + def indent(self, text, amount, car=" "): + """indent multi lines message""" + padding = amount * car + return ''.join(padding + line for line in text.splitlines(True)) + def parse_args(self, argList=None): '''Method that instantiates the class OptResult @@ -210,6 +227,9 @@ class Options(object): if option['result'] is None: option['result'] = list() option['result'].append(opt[1]) + elif optionType == "level": #logger logging levels + option['result'] = opt[1] + # TODO test in (lowercase) debug info warning error critical elif optionType == "list2": if option['result'] is None: option['result'] = list() diff --git a/src/product.py b/src/product.py index 19bebd2..df13ec2 100644 --- a/src/product.py +++ b/src/product.py @@ -23,6 +23,7 @@ import os import re import src.pyconf as PYCONF +import src.utilsSat as UTS AVAILABLE_VCS = ['git', 'svn', 'cvs'] config_expression = "^config-\d+$" @@ -142,7 +143,7 @@ def get_product_config(config, product_name, with_install_dir=True): # If prod_info is still None, it means that there is no product definition # in the config. The user has to provide it. if prod_info is None: - prod_pyconf_path = src.find_file_in_lpath(product_name + ".pyconf", + prod_pyconf_path = UTS.find_file_in_lpath(product_name + ".pyconf", config.PATHS.PRODUCTPATH) if not prod_pyconf_path: msg = _("""\ @@ -171,7 +172,7 @@ Please add a section in it.""") % {"1" : vv, "2" : prod_pyconf_path} "") if "archive_name" not in prod_info.archive_info: arch_name = product_name + "-" + version + ".tar.gz" - arch_path = src.find_file_in_lpath(arch_name, + arch_path = UTS.find_file_in_lpath(arch_name, config.PATHS.ARCHIVEPATH) if not arch_path: msg = _("Archive %(1)s for %(2)s not found.\n") % \ @@ -182,7 +183,7 @@ Please add a section in it.""") % {"1" : vv, "2" : prod_pyconf_path} if (os.path.basename(prod_info.archive_info.archive_name) == prod_info.archive_info.archive_name): arch_name = prod_info.archive_info.archive_name - arch_path = src.find_file_in_lpath( + arch_path = UTS.find_file_in_lpath( arch_name, config.PATHS.ARCHIVEPATH) if not arch_path: @@ -206,7 +207,7 @@ Please provide a 'compil_script' key in its definition.""") % product_name script_name = os.path.basename(script) if script == script_name: # Only a name is given. Search in the default directory - script_path = src.find_file_in_lpath(script_name, + script_path = UTS.find_file_in_lpath(script_name, config.PATHS.PRODUCTPATH, "compil_scripts") if not script_path: @@ -231,7 +232,7 @@ Please provide a 'compil_script' key in its definition.""") % product_name # If only a filename, then search for the patch in the PRODUCTPATH if os.path.basename(patch_path) == patch_path: # Search in the PRODUCTPATH/patches - patch_path = src.find_file_in_lpath(patch, + patch_path = UTS.find_file_in_lpath(patch, config.PATHS.PRODUCTPATH, "patches") if not patch_path: @@ -248,7 +249,7 @@ Please provide a 'compil_script' key in its definition.""") % product_name # in the PRODUCTPATH/env_scripts if os.path.basename(env_script_path) == env_script_path: # Search in the PRODUCTPATH/env_scripts - env_script_path = src.find_file_in_lpath( + env_script_path = UTS.find_file_in_lpath( prod_info.environ.env_script, config.PATHS.PRODUCTPATH, "env_scripts") -- 2.39.2