From 93d7e8862cbfc6fb781574ddb79213dc64fe3373 Mon Sep 17 00:00:00 2001 From: Christian Van Wambeke Date: Fri, 19 Jan 2018 15:03:35 +0100 Subject: [PATCH] fix AllTestLauncherSat.py and test_*: many errors yet --- AllTestLauncherSat.py | 14 +- commands/__init__.py | 0 commands/config.py | 5 - commands/job.py | 2 +- commands/jobs.py | 2 +- commands/patch.py | 2 +- commands/source.py | 4 +- sat | 51 ++++++- src/__init__.py | 10 ++ src/debug.py | 7 +- src/i18n/fr/LC_MESSAGES/salomeTools.mo | Bin 2217 -> 2251 bytes src/i18n/fr/LC_MESSAGES/salomeTools.po | 15 +- src/i18n/fr/LC_MESSAGES/salomeTools.po_old | 91 ++++++++++++ src/i18n/test_i18n.py | 21 ++- src/options.py | 39 ++++-- src/product.py | 5 +- salomeTools.py => src/salomeTools.py | 155 +++++++++++---------- test/compilation/test_compilation.py | 2 +- test/compilation/test_make.py | 2 +- test/compilation/test_makeinstall.py | 2 +- test/config/test_create_user_pyconf.py | 2 +- test/config/test_option_copy.py | 2 +- test/config/test_option_edit.py | 2 +- test/config/test_option_value.py | 2 +- test/config/test_option_value_2.py | 2 +- test/environ/test_environ.py | 2 +- test/job/test_job.py | 2 +- test/jobs/test_jobs.py | 2 +- test/log/test_launch_browser.py | 2 +- test/log/test_launch_browser2.py | 2 +- test/prepare/test_clean.py | 2 +- test/prepare/test_patch.py | 2 +- test/prepare/test_prepare.py | 2 +- test/prepare/test_source.py | 2 +- test/shell/test_shell.py | 2 +- test/test/test_command.py | 2 +- 36 files changed, 337 insertions(+), 124 deletions(-) create mode 100644 commands/__init__.py mode change 120000 => 100755 sat create mode 100644 src/i18n/fr/LC_MESSAGES/salomeTools.po_old rename salomeTools.py => src/salomeTools.py (87%) diff --git a/AllTestLauncherSat.py b/AllTestLauncherSat.py index d04f427..1a2be07 100755 --- a/AllTestLauncherSat.py +++ b/AllTestLauncherSat.py @@ -65,6 +65,9 @@ import pprint as PP #pretty print debug = False verboseImport = True +# get path to salomeTools sources +satdir = os.path.dirname(os.path.realpath(__file__)) + def errPrint(aStr): """to avoid write in html or xml file log message""" sys.stderr.write(aStr + '\n') @@ -173,11 +176,12 @@ def runOnArgs(args): del sys.path[0] done = False filesForTest[aFile] = (aImport, aModule) - except: + except Exception as e: if done: del sys.path[0] #attention of sys.path appends done = False - errPrint("ERROR: AllTestLauncher: import '%s'" % aFile) + errPrint("ERROR: AllTestLauncher: import '%s':" % aFile) + errPrint(" %s\n" % e) continue listfilesForTest = sorted(filesForTest.keys()) @@ -270,7 +274,7 @@ def getParser(): dir name with absolute or relative path stand for root directory of recursive searching unittest python files ''', - default='.', + default=satdir, metavar='dirPath' ) parser.add_argument( @@ -302,11 +306,15 @@ If name = 'stdout' then all-in-one xml output at 'sys.stdout'. For pipe redirect ################################################################### if __name__ == '__main__': + # Make the src & command package accessible from all code + sys.path.insert(0, satdir) + args = getParser().parse_args(sys.argv[1:]) debug = args.debug directory = os.path.realpath(args.rootPath) if debug: print("INFO: args:\n %s" % PP.pformat(args)) sys.path.insert(0, directory) #supposed to be root of a package + runOnArgs(args) del sys.path[0] diff --git a/commands/__init__.py b/commands/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/commands/config.py b/commands/config.py index 2cab5c5..f1ec2b8 100644 --- a/commands/config.py +++ b/commands/config.py @@ -20,15 +20,10 @@ import os import platform import datetime import shutil -import gettext import sys import src -# internationalization -satdir = os.path.dirname(os.path.realpath(__file__)) -gettext.install('salomeTools', os.path.join(satdir, 'src', 'i18n')) - # Define all possible option for config command : sat config parser = src.options.Options() parser.add_option('v', 'value', 'string', 'value', diff --git a/commands/job.py b/commands/job.py index bb5c88d..4f6b891 100644 --- a/commands/job.py +++ b/commands/job.py @@ -19,7 +19,7 @@ import os import src -import salomeTools +import src.salomeTools # Define all possible option for the make command : sat make parser = src.options.Options() diff --git a/commands/jobs.py b/commands/jobs.py index 7c53d12..0516243 100644 --- a/commands/jobs.py +++ b/commands/jobs.py @@ -1852,7 +1852,7 @@ Use the --list option to get the possible files.""") % config_file traceback.print_tb(exc_traceback, file=fp) fp.seek(0) stack = fp.read() - logger.write("\nTRACEBACK: %s\n" % stack.replace('"',"'"), 1) + logger.write("\nTRACEBACK:\n%s\n" % stack.replace('"',"'"), 1) finally: res = 0 diff --git a/commands/patch.py b/commands/patch.py index 3f18442..280961f 100644 --- a/commands/patch.py +++ b/commands/patch.py @@ -20,7 +20,7 @@ import os import subprocess import src -import prepare +import commands.prepare # Define all possible option for patch command : sat patch parser = src.options.Options() diff --git a/commands/source.py b/commands/source.py index 448774d..973cfd1 100644 --- a/commands/source.py +++ b/commands/source.py @@ -20,7 +20,7 @@ import os import shutil import src -import prepare +import commands.prepare # Define all possible option for patch command : sat patch parser = src.options.Options() @@ -503,7 +503,7 @@ def run(args, runner, logger): logger.write("\n", 2, False) # Get the products list with products informations regarding the options - products_infos = prepare.get_products_list(options, runner.cfg, logger) + products_infos = commands.prepare.get_products_list(options, runner.cfg, logger) # Call to the function that gets all the sources good_result, results = get_all_product_sources(runner.cfg, diff --git a/sat b/sat deleted file mode 120000 index 19cd1ab..0000000 --- a/sat +++ /dev/null @@ -1 +0,0 @@ -salomeTools.py \ No newline at end of file diff --git a/sat b/sat new file mode 100755 index 0000000..ae3cb05 --- /dev/null +++ b/sat @@ -0,0 +1,50 @@ +#!/usr/bin/env python +#-*- coding:utf-8 -*- + +# Copyright (C) 2010-2018 CEA/DEN +# +# 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. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +'''This file is the main entry file to use salomeTools +''' + +import os +import sys + +# get path to salomeTools sources +satdir = os.path.dirname(os.path.realpath(__file__)) + +# Make the src & commands package accessible from all code +sys.path.insert(0, satdir) + + +################################# +# MAIN +################################# +if __name__ == "__main__": + import src + from src.salomeTools import Sat + import src.debug as DBG # Easy print stderr (for DEBUG only) + + _debug = False # Have to be False in production (for DEBUG only) + DBG.push_debug(_debug) + + # instantiate the salomeTools class with correct options + sat = Sat(sys.argv[1:]) + exitCode = sat.execute_command() + DBG.write("sat exit code", src.okToStr(exitCode)) + + sys.exit(exitCode) + diff --git a/src/__init__.py b/src/__init__.py index 9d28e10..5fc438b 100644 --- a/src/__init__.py +++ b/src/__init__.py @@ -48,6 +48,16 @@ TIMEOUT_STATUS = "TIMEOUT" CONFIG_FILENAME = "sat-config.pyconf" +OKSYS = 0 # OKSYS and KOSYS seems equal on linux or windows +KOSYS = 1 +OKMESS = {0: "OK", 1: "KO"} # etc... if useful + +def okToStr(ok): + try: + return OKMESS[ok] + except: + return "UnknownExitCode" + class SatException(Exception): '''rename Exception Class ''' diff --git a/src/debug.py b/src/debug.py index 3d2f0ac..8ea680c 100644 --- a/src/debug.py +++ b/src/debug.py @@ -24,10 +24,11 @@ warning: only for SAT development phase import sys import pprint as PP -_debug = [True] #support push/pop for temporary active outputs +_debug = [False] #support push/pop for temporary active outputs -def write(title, var=""): - if _debug[-1]: +def write(title, var="", force=None): + """write sys.stderr a message if _debug[-1]==True or optionaly force=True""" + if _debug[-1] or force: sys.stderr.write("\n#### DEBUG: %s:\n%s\n" % (title, PP.pformat(var))) return diff --git a/src/i18n/fr/LC_MESSAGES/salomeTools.mo b/src/i18n/fr/LC_MESSAGES/salomeTools.mo index f62038e7e82d0e333e0e6343be0798f27f7cbf2f..85710c2d9e75c93deb986af4f5cd6a1354fdf82c 100644 GIT binary patch delta 478 zcmXBQJ4nMo9LMp0(x#~*YHFzURpQbXi{c{q7!)kx;#v@0RN{-$Lh1uS$RLj55L{fG z3U-vN4h6x*ML`!4C&5v~!NtMvwSV%v&)q*AK#^g<|`43b+iL{>Eh>Jdlq z348GmlW6NADa@l<7tp~KoX2Avzy_N5hEw=~)0mEk^ojVg%p^~viUwX`Kh`jV4IINy zOk*-CGL92iU})cx=0os)&*1-a&Z_>a0zS8zAy5_q?3llF&EImIov|E z@fve@k390l;NUN2Fcohda28d+j5BzS8meL{3DQQ|iK-qos9$Ii^&>#m5h@G#e;amUF_l- ze&Z^dhR8a`P_OqfiRXBN*BHYNhOv)_IKU%Z42f)r)Fn@mrg4Rv*uWIFaSz`yfn&^K z!W3~C;u15gdo$DhU(^?fSRxK)P+#N%_4ykt;$zVN!X>^hBZ^ZtEXK@X5%s|a?%)eL z_=zd}Lp@l0cG@rCG3#4ou1QdeP0o??la5F 1);\n" -#, python-format -msgid "Georges says '%(1)s' for %(2)s." -msgstr "pour %(2)s Hervé dit '%(1)s'." +# for test_i18n.py +msgid "Harvey writes '%(1)s' for %(2)s." +msgstr "pour %(2)s Hervé écrit '%(1)s'." + +# for test_i18n.py +msgid "hello" +msgstr "bonjour" + +# for test_i18n.py +# keep Ooops inexisting in salomeTools.po as not translated +# msgid "Ooops" +# msgstr "NOT TRANSLATED" msgid "shows global help or help on a specific command." msgstr "affiche l'aide générale ou pour une commande spécifique." diff --git a/src/i18n/fr/LC_MESSAGES/salomeTools.po_old b/src/i18n/fr/LC_MESSAGES/salomeTools.po_old new file mode 100644 index 0000000..1101bdf --- /dev/null +++ b/src/i18n/fr/LC_MESSAGES/salomeTools.po_old @@ -0,0 +1,91 @@ +# stuff to fix removed french accentued characters on msgmerge +msgid "" +msgstr "" +"Project-Id-Version: sat5\n" +"Report-Msgid-Bugs-To: andrea@nonymous.net \n" +"POT-Creation-Date: 2016-01-25 18:51+0100\n" +"PO-Revision-Date: 2016-01-25 19:53+0100\n" +"Last-Translator: Andrea Nonymous \n" +"Language-Team: French\n" +"Language: fr\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" +"Plural-Forms: nplurals=2; plural=(n > 1);\n" + +# for test_i18n.py +msgid "Harvey writes '%(1)s' for %(2)s." +msgstr "pour %(2)s Hervé écrit '%(1)s'." + +# for test_i18n.py +msgid "hello" +msgstr "bonjour" + +# for test_i18n.py +# keep Ooops inexisting in salomeTools.po as not translated +# msgid "Ooops" +# msgstr "NOT TRANSLATED" + +msgid "shows global help or help on a specific command." +msgstr "affiche l'aide générale ou pour une commande spécifique." + +msgid "overwrites a configuration parameters." +msgstr "écrase un parametre de configuration." + +msgid "run salomeTools in debug mode." +msgstr "lance salomeTools en mode debug." + +msgid "change output verbose level (default is 3)." +msgstr "change niveau de verbosité (defaut: 3)." + +msgid "batch mode (no question)." +msgstr "mode batch (pas de question)." + +msgid "all traces in the terminal (for example compilation logs)." +msgstr "toutes traces dans le terminal (par exemple: log de compilation)" + +msgid "put the command result and paths to log files." +msgstr "redirige resultats de commandes vers log files" + +msgid " is not a valid command" +msgstr " n'est pas une commande valide" + +msgid "" +"WARNING: the logs_paths_in_file option will not be taken into account.\n" +"Here is the error:" +msgstr "" + +msgid "Usage: " +msgstr "Utilisation : " + +msgid "Available commands are:\n" +msgstr "Les commandes disponibles sont:\n" + +msgid "" +"\n" +"Getting the help for a specific command: " +msgstr "" +"\n" +"Obtenir l'aide d'une commande spécifique : " + +msgid "lsb_release not installed\n" +msgstr "lsb_release n'est pas installé\n" + +msgid "You can define $LSB_PATH to give the path to lsb_release\n" +msgstr "Vous pouvez définir $LSB_PATH pour donner le chemin vers lsb_release\n" + +#, python-format +msgid "Unknown distribution: '%s'\n" +msgstr "Distribution inconnue : '%s'\n" + +msgid "Please add your distribution to src/internal_config/distrib.pyconf\n" +msgstr "" +"SVP ajoutez votre distribution au fichier src/internal_config/distrib." +"pyconf\n" + +msgid " is not a valid option" +msgstr " n'est pas une option valide" + +msgid "Available options are:" +msgstr "Les options disponibles sont:" + diff --git a/src/i18n/test_i18n.py b/src/i18n/test_i18n.py index a9af31b..cf467da 100755 --- a/src/i18n/test_i18n.py +++ b/src/i18n/test_i18n.py @@ -28,13 +28,26 @@ import unittest verbose = False class TestCase(unittest.TestCase): + + def test_001(self): + # first load resources for internationalization + gettext.install('salomeTools', os.path.realpath(os.path.dirname(__file__))) def test_005(self): - # load resources for internationalization - gettext.install('salomeTools', os.path.realpath(os.path.dirname(__file__))) - res = _("Georges says '%(1)s' for %(2)s.") % {"1": "hello", "2": "test"} + res = _("Harvey writes '%(1)s' for %(2)s.") % {"1": "hello", "2": "test_005"} if verbose: print(res) - self.assertEqual(res, "pour test Hervé dit 'hello'.") + self.assertEqual(res, "pour test_005 Hervé écrit 'hello'.") + + def test_010(self): + res = _("Harvey writes '%(1)s' for %(2)s.") % {"1": _("hello"), "2": "test_010"} + if verbose: print(res) + self.assertEqual(res, "pour test_010 Hervé écrit 'bonjour'.") + + def test_020(self): + # keep Ooops inexisting in salomeTools.po as no translation + res = _("Harvey writes '%(1)s' for %(2)s.") % {"1": _("Ooops"), "2": "test_020"} + if verbose: print(res) + self.assertEqual(res, "pour test_020 Hervé écrit 'Ooops'.") if __name__ == '__main__': verbose = False diff --git a/src/options.py b/src/options.py index c152345..22c51f9 100644 --- a/src/options.py +++ b/src/options.py @@ -22,7 +22,9 @@ import getopt import sys import pprint as PP -from . import printcolors +import src +import src.debug as DBG # Easy print stderr (for DEBUG only) +import src.printcolors class OptResult(object): '''An instance of this class will be the object manipulated @@ -63,10 +65,11 @@ class OptResult(object): object.__setattr__(self, name, value) def __repr__(self): - res = "%s(%s)" % (self.__class__.__name__, PP.pformat(self.__dict__)) + aStr = PP.pformat(self.__dict__) + res = "%s(\n %s)" % (self.__class__.__name__, aStr[1:-1]) return res -class Options: +class Options(object): '''Class to manage all salomeTools options ''' def __init__(self): @@ -76,9 +79,9 @@ class Options: # in a list that contains dicts self.options = [] # The list of available option type - self.availableOptions = ["boolean", "string", "int", "float", - "long", "list", "list2"] + self.availableOptions = "boolean string int float long list list2".split() self.default = None + self.results = {} def add_option(self, shortName, longName, optionType, destName, helpString="", default = None): @@ -102,7 +105,7 @@ class Options: if optionType not in self.availableOptions: print("error optionType", optionType, "not available.") - sys.exit(-1) + sys.exit(src.KOSYS) option['optionType'] = optionType option['destName'] = destName @@ -122,7 +125,7 @@ class Options: # for all options, print its values. # "shortname" is an optional field of the options - print(printcolors.printcHeader(_("Available options are:"))) + print(src.printcolors.printcHeader(_("Available options are:"))) for option in self.options: if 'shortName' in option and len(option['shortName']) > 0: print(" -%(shortName)1s, --%(longName)s" @@ -144,6 +147,8 @@ class Options: if argList is None: argList = sys.argv[1:] + DBG.write("parse_args", argList) + DBG.write("options", self.options) # format shortNameOption and longNameOption # to make right arguments to getopt.getopt function shortNameOption = "" @@ -161,7 +166,10 @@ class Options: # call to getopt.getopt function to get the option # passed in the command regarding the available options - optlist, args = getopt.getopt(argList, shortNameOption, longNameOption) + try: + optlist, args = getopt.getopt(argList, shortNameOption, longNameOption) + except: + DBG.write("ERROR", (shortNameOption, longNameOption), True) # instantiate and completing the optResult that will be returned optResult = OptResult() @@ -198,5 +206,20 @@ class Options: # free the option in order to be able to make # a new free call of options (API case) option['result'] = None + + self.results = {"optlist": optlist, "optResult": optResult, "args": args, "argList": argList} + DBG.write("options and results", self) return optResult, args + def __repr__(self): + '''repr for only self.options and self.results (if present) + ''' + aDict = {'options': self.options, 'results': self.results} + aStr = PP.pformat(aDict) + res = "%s(\n %s)" % (self.__class__.__name__, aStr[1:-1]) + return res + + def debug_write(self): + DBG.write("options and results", self, True) + + diff --git a/src/product.py b/src/product.py index 3e9721b..ea886a0 100644 --- a/src/product.py +++ b/src/product.py @@ -147,7 +147,10 @@ def get_product_config(config, product_name, with_install_dir=True): if not prod_pyconf_path: msg = _("""\ No definition found for the product %(1)s. -Please create a %(2)s.pyconf file.""") % {"1": product_name, "2": product_name} +Please create a %(2)s.pyconf file somewhere in:\n%(3)s""") % { + "1": product_name, + "2": product_name, + "3": config.PATHS.PRODUCTPATH } else: msg = _("""\ No definition corresponding to the version %(1)s was found in the file: diff --git a/salomeTools.py b/src/salomeTools.py similarity index 87% rename from salomeTools.py rename to src/salomeTools.py index 219cc5c..f5ca674 100755 --- a/salomeTools.py +++ b/src/salomeTools.py @@ -1,6 +1,7 @@ #!/usr/bin/env python #-*- coding:utf-8 -*- -# Copyright (C) 2010-2012 CEA/DEN + +# Copyright (C) 2010-2018 CEA/DEN # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public @@ -15,10 +16,10 @@ # You should have received a copy of the GNU Lesser General Public # License along with this library; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + '''This file is the main entry file to salomeTools ''' -# python imports import os import sys import re @@ -28,24 +29,31 @@ import types import gettext import traceback -# salomeTools imports -import src -# get path to salomeTools sources -satdir = os.path.dirname(os.path.realpath(__file__)) -cmdsdir = os.path.join(satdir, 'commands') +################################# +# NOT MAIN allowed +################################# +if __name__ == "__main__": + sys.stderr.write("\nERROR: 'salomeTools.py' is not main command entry for sat: use 'sat' instead.\n\n") + KOSYS = 1 # avoid import src + sys.exit(KOSYS) + -# Make the src package accessible from all code -sys.path.append(satdir) -sys.path.append(cmdsdir) +import src.debug as DBG # Easy print stderr (for DEBUG only) -import config -import src.debug as DBG +# get path to src +rootdir = os.path.realpath( os.path.join(os.path.dirname(__file__), "..") ) +DBG.write("sat rootdir", rootdir) +srcdir = os.path.join(rootdir, "src") +cmdsdir = os.path.join(rootdir, "commands") # load resources for internationalization -#es = gettext.translation('salomeTools', os.path.join(satdir, 'src', 'i18n')) -#es.install() -gettext.install('salomeTools', os.path.join(satdir, 'src', 'i18n')) +# DBG.write("TODO", "fix xxsalomeTools to avoid french", True) +gettext.install('salomeTools', os.path.join(srcdir, 'i18n')) + +# salomeTools imports +import src +import commands.config # The possible hooks : # pre is for hooks to be executed before commands @@ -53,8 +61,9 @@ gettext.install('salomeTools', os.path.join(satdir, 'src', 'i18n')) C_PRE_HOOK = "pre" C_POST_HOOK = "post" + def find_command_list(dirPath): - ''' Parse files in dirPath that end with .py : it gives commands list + '''Parse files in dirPath that end with .py : it gives commands list :param dirPath str: The directory path where to search the commands :return: cmd_list : the list containing the commands name @@ -62,15 +71,22 @@ def find_command_list(dirPath): ''' cmd_list = [] for item in os.listdir(dirPath): + if item in ["__init__.py"]: #avoid theses files + continue if item.endswith('.py'): cmd_list.append(item[:-len('.py')]) return cmd_list # The list of valid salomeTools commands -#lCommand = ['config', 'compile', 'prepare'] +#lCommand = ['config', 'compile', 'prepare',...] lCommand = find_command_list(cmdsdir) -# Define all possible option for salomeTools command : sat