Salome HOME
fix sat test for python2-3 compatibility
[tools/sat.git] / src / test / scriptTemplate.py
index f3f7de44fa2edcbe72bd95756444f1a1af396779..82e79599f70fb147ff2d79ca5e7c6c98e341e3cf 100644 (file)
@@ -1,10 +1,18 @@
 #!/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}'
@@ -20,70 +28,74 @@ 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()
+with open(r'${resultFile}', 'w') as exec_result:
+  exec_result.write('Open\n')
+
+  for test in listTest:
+    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()
+        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 as 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 as 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')
+
+        pass
+        # testout.close()
+
+      sys.stdout = __stdout__
+      sys.stderr = __stderr__
+      my_tools.writeInFiles(pylog)
+      pass
+      # pylog.close()
+
+  exec_result.write('Close\n')
+  pass
+  # exec_result.close()
 
 if 'PY' not in '${sessionName}':
     import salome_utils
@@ -92,7 +104,8 @@ if 'PY' not in '${sessionName}':
     #                          'salome',
     #                          'killSalome.py')
     #cmd = '{python} {killScript} {port}'.format(python=os.environ['PYTHONBIN'],
-    #                                               killScript=killScript,
-    #                                               port=salome_utils.getPortNumber())
+    #                                       killScript=killScript,
+    #                                       port=salome_utils.getPortNumber())
     cmd = 'killSalome.py {port}'.format( port=salome_utils.getPortNumber())
     os.system(cmd)
+