Salome HOME
spns #40790: on Debian distributions, use dpkg-query instead of apt to check system...
[tools/sat.git] / src / test / scriptTemplate.py
1 #!/usr/bin/env python
2 #-*- coding:utf-8 -*-
3
4 """
5 This script is automatically generated by 'command sat test etc...'
6 from ...salomeTools/src/test/scriptTemplate.py
7 """
8
9 import os
10 import sys
11 import traceback
12 import os.path
13 import time as THEBIGTIME
14
15
16 # set path
17 toolsWay = r'${toolsWay}'
18 resourcesWay = r'${resourcesWay}'
19 outWay = r'${sessionDir}'
20 tmpDir = r'${tmpDir}'
21
22 listTest = ${listTest}
23 ignore = ${ignore}
24
25 sys.path.append(toolsWay)
26 from TOOLS import TOOLS_class
27 my_tools = TOOLS_class(resourcesWay, tmpDir, toolsWay)
28
29 from TOOLS import SatNotApplicableError
30
31 # set environement variables
32 os.environ['TT_BASE_RESSOURCES'] = resourcesWay
33 sys.path.append(resourcesWay)
34
35 __stdout__ = sys.stdout
36 __stderr__ = sys.stderr
37
38 with open(r'${resultFile}', 'w') as exec_result:
39   exec_result.write('Open\n')
40   print("wrapper ignore tests: %s" % ignore)
41   for test in listTest:
42     fileTest = os.path.join(outWay, test)
43     # print("test file: %s" % fileTest) # cvw TODO
44     with open(os.path.join(outWay, test[:-3] + ".result.py"), "w") as pylog:
45       with open(os.path.join(outWay, test[:-3] + ".out.py"), "w") as testout:
46         my_tools.init()
47         # print("here set sys.stdout")
48         sys.stdout = testout
49         sys.stderr = testout
50
51         # pylog.write('#!/usr/bin/env python\n')
52         exec_result.write("Run %s " % test)
53         exec_result.flush()
54
55         try:
56             timeStart = THEBIGTIME.time()
57             # cd ..print("begin... %s" % fileTest)
58             # execfile(fileTest, globals(), locals()) obsolete python3
59             with open(fileTest) as f:
60               # compile associates the filename with the code object making debugging a little easier
61               code = compile(f.read(), fileTest, 'exec')
62               exec(code, globals(), locals())
63             timeTest = THEBIGTIME.time() - timeStart
64             # print("...done %s" % fileTest)
65             timeTest = THEBIGTIME.time() - timeStart
66         except SatNotApplicableError as ex:
67             # print("here SatNotApplicableError")
68             # pylog.write("here SatNotApplicableError")
69             status = "NA"
70             reason = str(ex)
71             exec_result.write("NA\n")
72             timeTest = THEBIGTIME.time() - timeStart
73             pylog.write('status = "NA"\n')
74             pylog.write('time = "' + timeTest.__str__() + '"\n')
75             pylog.write('callback = "%s"\n' % reason)
76         except Exception as ex:
77             # print("here Exception")
78             # pylog.write("here Exception pylog\n")
79             status = "KO"
80             reason = ""
81             if test in ignore:
82                 status = "KF"
83                 reason = "Known Failure = %s\n\n" % ignore[test]
84             exec_result.write("%s\n" % status)
85             timeTest = THEBIGTIME.time() - timeStart
86             pylog.write('status = "%s" \n' % status)
87             pylog.write('time = "' + timeTest.__str__() + '"\n')
88             pylog.write('callback="""' + reason)
89             exc_type, exc_value, exc_traceback = sys.exc_info()
90             traceback.print_exception(exc_type,
91                                       exc_value,
92                                       exc_traceback,
93                                       None,
94                                       file=pylog)
95             pylog.write('"""\n')
96         else:
97             # print("here else")
98             # pylog.write("here else pylog")
99             exec_result.write("OK\n")
100             pylog.write('status = "OK"\n')
101             pylog.write('time = "' + timeTest.__str__() + '"\n')
102
103         pass
104         # print("here testout.flush")
105         testout.flush()
106         # testout.close()
107
108       # print("here restore sys.stdout")
109       sys.stdout = __stdout__
110       sys.stderr = __stderr__
111       my_tools.writeInFiles(pylog)
112       pass
113       pylog.flush()
114       # pylog.close()
115
116   exec_result.write('Close\n')
117   pass
118   # exec_result.close()
119
120 if 'PY' not in '${sessionName}':
121     import salome_utils
122     #killScript = os.path.join(os.environ['KERNEL_ROOT_DIR'],
123     #                          'bin',
124     #                          'salome',
125     #                          'killSalome.py')
126     #cmd = '{python} {killScript} {port}'.format(python=os.environ['PYTHONBIN'],
127     #                                       killScript=killScript,
128     #                                       port=salome_utils.getPortNumber())
129     cmd = 'killSalome.py {port}'.format( port=salome_utils.getPortNumber())
130     os.system(cmd)
131