Salome HOME
4f8ebc65514289f911b57febf6fae9ad37ae1315
[tools/sat.git] / src / scriptTemplate.py
1 #!/usr/bin/env python
2 #-*- coding:utf-8 -*-
3
4 import os, sys, traceback
5 import os.path
6 import time as THEBIGTIME
7
8 # set path
9 toolsWay = r'${toolsWay}'
10 resourcesWay = r'${resourcesWay}'
11 outWay = r'${typeDir}'
12 tmpDir = r'${tmpDir}'
13
14 listTest = ${listTest}
15 ignore = ${ignore}
16
17 sys.path.append(toolsWay)
18 from TOOLS import TOOLS_class
19 my_tools = TOOLS_class(resourcesWay, tmpDir, toolsWay)
20
21 from TOOLS import SatNotApplicableError
22
23 # on set les variables d'environement
24 os.environ['TT_BASE_RESSOURCES'] = resourcesWay
25 sys.path.append(resourcesWay)
26
27 exec_result = open(r'${resultFile}', 'w')
28 exec_result.write('Open\n')
29
30 __stdout__ = sys.stdout
31 __stderr__ = sys.stderr
32
33 for test in listTest:
34     pylog = open(os.path.join(outWay, test[:-3] + ".result.py"), "w")
35     testout = open(os.path.join(outWay, test[:-3] + ".out.py"), "w")
36     my_tools.init()
37     sys.stdout = testout
38     sys.stderr = testout
39
40     pylog.write('#-*- coding:utf-8 -*-\n')
41     exec_result.write("Run %s " % test)
42     exec_result.flush()
43
44     try:
45         timeStart = THEBIGTIME.time()
46         execfile(os.path.join(outWay, test), globals(), locals())
47         timeTest = THEBIGTIME.time() - timeStart
48     except SatNotApplicableError, ex:
49         status = "NA"
50         reason = str(ex)
51         exec_result.write("NA\n")
52         timeTest = THEBIGTIME.time() - timeStart
53         pylog.write('status = "NA"\n')
54         pylog.write('time = "' + timeTest.__str__() + '"\n')
55         pylog.write('callback = "%s"\n' % reason)
56     except Exception, ex:
57         status = "KO"
58         reason = ""
59         if ignore.has_key(test):
60             status = "KF"
61             reason = "Known Failure = %s\n\n" % ignore[test]
62         exec_result.write("%s\n" % status)
63         timeTest = THEBIGTIME.time() - timeStart
64         pylog.write('status = "%s" \n' % status)
65         pylog.write('time = "' + timeTest.__str__() + '"\n')
66         pylog.write('callback="""' + reason)
67         exc_type, exc_value, exc_traceback = sys.exc_info()
68         traceback.print_exception(exc_type,
69                                   exc_value,
70                                   exc_traceback,
71                                   None,
72                                   file=pylog)
73         pylog.write('"""\n')
74     else:
75         exec_result.write("OK\n")
76         pylog.write('status = "OK"\n')
77         pylog.write('time = "' + timeTest.__str__() + '"\n')
78
79     testout.close()
80     sys.stdout = __stdout__
81     sys.stderr = __stderr__
82     my_tools.writeInFiles(pylog)
83     pylog.close()
84
85 exec_result.write('Close\n')
86 exec_result.close()
87
88 if 'PY' not in '${typeName}':
89     import salome_utils
90     killScript = os.path.join(os.environ['KERNEL_ROOT_DIR'],
91                               'bin',
92                               'salome',
93                               'killSalome.py')
94     cmd = '{python} {killScript} {port}'.format(python=os.environ['PYTHONBIN'],
95                                              killScript=killScript,
96                                              port=salome_utils.getPortNumber())
97     os.system(cmd)