Salome HOME
begin fix sat test
[tools/sat.git] / src / test / scriptTemplate.py
index 4f8ebc65514289f911b57febf6fae9ad37ae1315..0481ac1343b6e790930774442f00e5ee3743a9fc 100644 (file)
@@ -1,14 +1,22 @@
 #!/usr/bin/env python
 #-*- coding:utf-8 -*-
 
-import os, sys, traceback
+"""
+This script is automatically generated by 'command sat test etc...'
+from ...salomeTools/src/test/scriptTemplate.py
+"""
+
+import os
+import sys
+import traceback
 import os.path
 import time as THEBIGTIME
 
+
 # set path
 toolsWay = r'${toolsWay}'
 resourcesWay = r'${resourcesWay}'
-outWay = r'${typeDir}'
+outWay = r'${sessionDir}'
 tmpDir = r'${tmpDir}'
 
 listTest = ${listTest}
@@ -20,78 +28,104 @@ my_tools = TOOLS_class(resourcesWay, tmpDir, toolsWay)
 
 from TOOLS import SatNotApplicableError
 
-# on set les variables d'environement
+# set environement variables
 os.environ['TT_BASE_RESSOURCES'] = resourcesWay
 sys.path.append(resourcesWay)
 
-exec_result = open(r'${resultFile}', 'w')
-exec_result.write('Open\n')
-
 __stdout__ = sys.stdout
 __stderr__ = sys.stderr
 
-for test in listTest:
-    pylog = open(os.path.join(outWay, test[:-3] + ".result.py"), "w")
-    testout = open(os.path.join(outWay, test[:-3] + ".out.py"), "w")
-    my_tools.init()
-    sys.stdout = testout
-    sys.stderr = testout
-
-    pylog.write('#-*- coding:utf-8 -*-\n')
-    exec_result.write("Run %s " % test)
-    exec_result.flush()
-
-    try:
-        timeStart = THEBIGTIME.time()
-        execfile(os.path.join(outWay, test), globals(), locals())
-        timeTest = THEBIGTIME.time() - timeStart
-    except SatNotApplicableError, ex:
-        status = "NA"
-        reason = str(ex)
-        exec_result.write("NA\n")
-        timeTest = THEBIGTIME.time() - timeStart
-        pylog.write('status = "NA"\n')
-        pylog.write('time = "' + timeTest.__str__() + '"\n')
-        pylog.write('callback = "%s"\n' % reason)
-    except Exception, ex:
-        status = "KO"
-        reason = ""
-        if ignore.has_key(test):
-            status = "KF"
-            reason = "Known Failure = %s\n\n" % ignore[test]
-        exec_result.write("%s\n" % status)
-        timeTest = THEBIGTIME.time() - timeStart
-        pylog.write('status = "%s" \n' % status)
-        pylog.write('time = "' + timeTest.__str__() + '"\n')
-        pylog.write('callback="""' + reason)
-        exc_type, exc_value, exc_traceback = sys.exc_info()
-        traceback.print_exception(exc_type,
-                                  exc_value,
-                                  exc_traceback,
-                                  None,
-                                  file=pylog)
-        pylog.write('"""\n')
-    else:
-        exec_result.write("OK\n")
-        pylog.write('status = "OK"\n')
-        pylog.write('time = "' + timeTest.__str__() + '"\n')
-
-    testout.close()
-    sys.stdout = __stdout__
-    sys.stderr = __stderr__
-    my_tools.writeInFiles(pylog)
-    pylog.close()
-
-exec_result.write('Close\n')
-exec_result.close()
-
-if 'PY' not in '${typeName}':
+with open(r'${resultFile}', 'w') as exec_result:
+  exec_result.write('Open\n')
+  print("ignore: %s" % ignore)
+  for test in listTest:
+    fileTest = os.path.join(outWay, test)
+    # print("test file: %s" % fileTest) # cvw TODO
+    with open(os.path.join(outWay, test[:-3] + ".result.py"), "w") as pylog:
+      with open(os.path.join(outWay, test[:-3] + ".out.py"), "w") as testout:
+        my_tools.init()
+        print("here set sys.stdout")
+        sys.stdout = testout # cvw TODO
+        sys.stderr = testout # cvw TODO
+
+        # pylog.write('#!/usr/bin/env python\n')
+        exec_result.write("Run %s " % test)
+        exec_result.flush()
+
+        try:
+            timeStart = THEBIGTIME.time()
+            # cd ..print("begin... %s" % fileTest)
+            # execfile(fileTest, globals(), locals()) obsolete python3
+            with open(fileTest) as f:
+              # compile associates the filename with the code object making debugging a little easier
+              code = compile(f.read(), fileTest, 'exec')
+              exec(code, globals(), locals())
+            timeTest = THEBIGTIME.time() - timeStart
+            # print("...done %s" % fileTest)
+            timeTest = THEBIGTIME.time() - timeStart
+        except SatNotApplicableError as ex:
+            # print("here SatNotApplicableError")
+            # pylog.write("here SatNotApplicableError")
+            status = "NA"
+            reason = str(ex)
+            exec_result.write("NA\n")
+            timeTest = THEBIGTIME.time() - timeStart
+            pylog.write('status = "NA"\n')
+            pylog.write('time = "' + timeTest.__str__() + '"\n')
+            pylog.write('callback = "%s"\n' % reason)
+        except Exception as ex:
+            # print("here Exception")
+            # pylog.write("here Exception pylog\n")
+            status = "KO"
+            reason = ""
+            if test in ignore:
+                status = "KF"
+                reason = "Known Failure = %s\n\n" % ignore[test]
+            exec_result.write("%s\n" % status)
+            timeTest = THEBIGTIME.time() - timeStart
+            pylog.write('status = "%s" \n' % status)
+            pylog.write('time = "' + timeTest.__str__() + '"\n')
+            pylog.write('callback="""' + reason)
+            exc_type, exc_value, exc_traceback = sys.exc_info()
+            traceback.print_exception(exc_type,
+                                      exc_value,
+                                      exc_traceback,
+                                      None,
+                                      file=pylog)
+            pylog.write('"""\n')
+        else:
+            # print("here else")
+            # pylog.write("here else pylog")
+            exec_result.write("OK\n")
+            pylog.write('status = "OK"\n')
+            pylog.write('time = "' + timeTest.__str__() + '"\n')
+
+        pass
+        # print("here testout.flush")
+        testout.flush()
+        # testout.close()
+
+      # print("here restore sys.stdout")
+      sys.stdout = __stdout__ # cvw TODO
+      sys.stderr = __stderr__ # cvw TODO
+      my_tools.writeInFiles(pylog)
+      pass
+      pylog.flush()
+      # pylog.close()
+
+  exec_result.write('Close\n')
+  pass
+  # exec_result.close()
+
+if 'PY' not in '${sessionName}':
     import salome_utils
-    killScript = os.path.join(os.environ['KERNEL_ROOT_DIR'],
-                              'bin',
-                              'salome',
-                              'killSalome.py')
-    cmd = '{python} {killScript} {port}'.format(python=os.environ['PYTHONBIN'],
-                                            killScript=killScript,
-                                            port=salome_utils.getPortNumber())
+    #killScript = os.path.join(os.environ['KERNEL_ROOT_DIR'],
+    #                          'bin',
+    #                          'salome',
+    #                          'killSalome.py')
+    #cmd = '{python} {killScript} {port}'.format(python=os.environ['PYTHONBIN'],
+    #                                       killScript=killScript,
+    #                                       port=salome_utils.getPortNumber())
+    cmd = 'killSalome.py {port}'.format( port=salome_utils.getPortNumber())
     os.system(cmd)
+