Salome HOME
begin fix sat test
[tools/sat.git] / commands / test.py
index bf89e549c5cdd1936c760e6632f728d7d98405b4..8f3dd909b61474fa63e154d19282665ffc9d4790 100644 (file)
@@ -23,6 +23,8 @@ import subprocess
 import datetime
 import gzip
 
+verbose = False
+
 try:
     from hashlib import sha1
 except ImportError:
@@ -44,13 +46,14 @@ parser.add_option('l', 'launcher', 'string', 'launcher',
 parser.add_option('g', 'grid', 'list', 'grids',
     _('Optional: Indicate which grid(s) to test (subdirectory of the test '
       'base).'))
-parser.add_option('s', 'session', 'list', 'sessions',
+parser.add_option('s', 'session', 'list2', 'sessions',
     _('Optional: indicate which session(s) to test (subdirectory of the '
       'grid).'))
 parser.add_option('', 'display', 'string', 'display',
     _("Optional: set the display where to launch SALOME.\n"
 "\tIf value is NO then option --show-desktop=0 will be used to launch SALOME."))
-
+parser.add_option('', 'keep', 'boolean', 'keeptempdir',
+                  _('Optional: keep temporary directory.'))
 def description():
     '''method that is called when salomeTools is called with --help option.
     
@@ -60,7 +63,7 @@ def description():
     return _("The test command runs a test base on a SALOME installation.\n\n"
              "example:\nsat test SALOME-master --grid GEOM --session light")     
 
-def parse_option(args, config):
+def parse_option_old(args, config):
     """ Parse the options and do some verifications about it
     
     :param args List: The list of arguments of the command
@@ -74,8 +77,8 @@ def parse_option(args, config):
         options.launcher = ""
     elif not os.path.isabs(options.launcher):
         if not src.config_has_application(config):
-            raise src.SatException(_("An application is required to use a "
-                                     "relative path with option --appli"))
+            msg = _("An application is required to use a relative path with option --appli")
+            raise src.SatException(msg)
         options.launcher = os.path.join(config.APPLICATION.workdir,
                                         options.launcher)
 
@@ -85,6 +88,39 @@ def parse_option(args, config):
 
     return (options, args)
 
+
+def parse_option(args, config):
+    """ Parse the options and do some verifications about it
+
+    :param args List: The list of arguments of the command
+    :param config Config: The global configuration
+    :return: the options of the current command launch and the full arguments
+    :rtype: Tuple (options, args)
+    """
+    (options, args) = parser.parse_args(args)
+
+    if not options.launcher:
+        options.launcher = ""
+        return (options, args)
+
+    if not os.path.isabs(options.launcher):
+        if not src.config_has_application(config):
+            msg = _("An application is required to use a relative path with option --appli")
+            raise src.SatException(msg)
+        else:
+            options.launcher = os.path.join(config.APPLICATION.workdir, options.launcher)
+            if not os.path.exists(options.launcher):
+                raise src.SatException(_("Launcher not found: %s") %  options.launcher)
+
+    # absolute path
+    launcher = os.path.realpath(os.path.expandvars(options.launcher))
+    if os.path.exists(launcher):
+        options.launcher = launcher
+        return (options, args)
+
+    raise src.SatException(_("Launcher not found: %s") %  options.launcher)
+
+
 def ask_a_path():
     """ 
     """
@@ -164,8 +200,8 @@ def move_test_results(in_dir, what, out_dir, logger):
             os.makedirs(outtestbase)
             #logger.write("  copy testbase %s\n" % testbase, 5)
 
-            for grid_ in [m for m in os.listdir(intestbase) if os.path.isdir(
-                                                os.path.join(intestbase, m))]:
+            for grid_ in [m for m in os.listdir(intestbase) \
+                            if os.path.isdir(os.path.join(intestbase, m))]:
                 # ignore source configuration directories
                 if grid_[:4] == '.git' or grid_ == 'CVS':
                     continue
@@ -243,17 +279,20 @@ def create_test_report(config,
     
     first_time = False
     if not os.path.exists(xml_history_path):
+        print("Log file creation %s" % xml_history_path)
         first_time = True
         root = etree.Element("salome")
         prod_node = etree.Element("product", name=application_name, build=xmlname)
         root.append(prod_node)
     else:
+        print("Log file modification %s" % xml_history_path)
         root = etree.parse(xml_history_path).getroot()
         prod_node = root.find("product")
-    
+
+
     prod_node.attrib["history_file"] = os.path.basename(xml_history_path)
     prod_node.attrib["global_res"] = retcode
-    
+
     if withappli:
         if not first_time:
             for node in (prod_node.findall("version_to_download") + 
@@ -297,12 +336,13 @@ def create_test_report(config,
         
         tt = {}
         for test in config.TESTS:
-            if not tt.has_key(test.testbase):
+            if not test.testbase in tt:
                 tt[test.testbase] = [test]
             else:
                 tt[test.testbase].append(test)
         
         for testbase in tt.keys():
+            if verbose: print("---- create_test_report %s %s" % (testbase, first_time))
             if first_time:
                 gn = add_simple_node(tests, "testbase")
             else:
@@ -349,7 +389,7 @@ def create_test_report(config,
                 
                 mn.attrib["executed_last_time"] = "yes"
                 
-                if not sessions.has_key("%s/%s" % (test.grid, test.session)):
+                if not "%s/%s" % (test.grid, test.session) in sessions:
                     if first_time:
                         tyn = add_simple_node(mn, "session")
                         tyn.attrib['name'] = test.session
@@ -508,12 +548,8 @@ def create_test_report(config,
     if not xmlname.endswith(".xml"):
         xmlname += ".xml"
 
-    src.xmlManager.write_report(os.path.join(dest_path, xmlname),
-                                root,
-                                "test.xsl")
-    src.xmlManager.write_report(xml_history_path,
-                                root,
-                                "test_history.xsl")
+    src.xmlManager.write_report(os.path.join(dest_path, xmlname), root, "test.xsl")
+    src.xmlManager.write_report(xml_history_path, root, "test_history.xsl")
     return src.OK_STATUS
 
 def generate_history_xml_path(config, test_base):
@@ -544,6 +580,7 @@ def run(args, runner, logger):
     '''
     (options, args) = parse_option(args, runner.cfg)
 
+    # the test base is specified either by the application, or by the --base option
     with_application = False
     if runner.cfg.VARS.application != 'None':
         logger.write(_('Running tests on application %s\n') % 
@@ -554,6 +591,7 @@ def run(args, runner, logger):
         raise src.SatException(_('A test base is required. Use the --base '
                                  'option'))
 
+    # the launcher is specified either by the application, or by the --launcher option
     if with_application:
         # check if environment is loaded
         if 'KERNEL_ROOT_DIR' in os.environ:
@@ -625,15 +663,15 @@ def run(args, runner, logger):
     content = "\n".join(lines)
 
     # create hash from context information
-    dirname = sha1(content.encode()).hexdigest()
+    # CVW TODO or not dirname = datetime.datetime.now().strftime("%y%m%d_%H%M%S_") + sha1(content.encode()).hexdigest()[0:8]
+    dirname = sha1(content.encode()).hexdigest()[0:8] # only 8 firsts probably good
     base_dir = os.path.join(tmp_dir, dirname)
     os.makedirs(base_dir)
     os.environ['TT_TMP_RESULT'] = base_dir
 
     # create env_info file
-    f = open(os.path.join(base_dir, 'env_info.py'), "w")
-    f.write(content)
-    f.close()
+    with open(os.path.join(base_dir, 'env_info.py'), "w") as f:
+        f.write(content)
 
     # create working dir and bases dir
     working_dir = os.path.join(base_dir, 'WORK')
@@ -687,7 +725,7 @@ def run(args, runner, logger):
     log_dir = src.get_log_path(runner.cfg)
     out_dir = os.path.join(log_dir, "TEST")
     src.ensure_path_exists(out_dir)
-    name_xml_board = logger.logFileName.split(".")[0] + "board" + ".xml"
+    name_xml_board = logger.logFileName.split(".")[0] + "_board.xml"
     historic_xml_path = generate_history_xml_path(runner.cfg, test_base)
     
     create_test_report(runner.cfg,
@@ -696,19 +734,23 @@ def run(args, runner, logger):
                        retcode,
                        xmlname = name_xml_board)
     xml_board_path = os.path.join(out_dir, name_xml_board)
+
     logger.l_logFiles.append(xml_board_path)
     logger.add_link(os.path.join("TEST", name_xml_board),
                     "board",
                     retcode,
                     "Click on the link to get the detailed test results")
-    
+    logger.write("\nTests board file %s\n" % xml_board_path, 1)
+
     # Add the historic files into the log files list of the command
     logger.l_logFiles.append(historic_xml_path)
-    
-    logger.write(_("Removing the temporary directory: "
-                   "rm -rf %s\n" % test_runner.tmp_working_dir), 5)
-    if os.path.exists(test_runner.tmp_working_dir):
+
+    if not options.keeptempdir:
+      logger.write("Removing the temporary directory: rm -rf %s\n" % test_runner.tmp_working_dir, 5)
+      if os.path.exists(test_runner.tmp_working_dir):
         shutil.rmtree(test_runner.tmp_working_dir)
+    else:
+      logger.write("NOT Removing the temporary directory: rm -rf %s\n" % test_runner.tmp_working_dir, 5)
 
     return retcode