Repository navigation
Expand file tree
/
Copy pathtest_common.py
More file actions
121 lines (91 loc) · 3.69 KB
/
Copy pathtest_common.py
File metadata and controls
121 lines (91 loc) · 3.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import os
import subprocess
import time
import re
import sys
ESMINI_PATH = os.pardir
sys.path.insert(0, os.path.join(ESMINI_PATH, 'scripts'))
from dat import *
LOG_FILENAME = 'log.txt'
DAT_FILENAME = 'sim.dat'
STDOUT_FILENAME = 'stdout.txt'
TIMEOUT = 40
# Add path to esmini shared library
# needed only on Mac and Linux, Windows looks in folder of the executable
env = os.environ.copy()
if sys.platform == "linux" or sys.platform == "linux2":
env['LD_LIBRARY_PATH'] = ':'.join([env.get('LD_LIBRARY_PATH', ''), os.path.join(ESMINI_PATH, 'bin')])
elif sys.platform == "darwin":
env['DYLD_LIBRARY_PATH'] = ':'.join([env.get('DYLD_LIBRARY_PATH', ''), os.path.join(ESMINI_PATH, 'bin')])
def run_scenario(osc_filename = None, esmini_arguments = None, xosc_str = None, application = None):
if os.path.exists(LOG_FILENAME):
os.remove(LOG_FILENAME)
if os.path.exists(STDOUT_FILENAME):
os.remove(STDOUT_FILENAME)
if application is None:
app = os.path.join(ESMINI_PATH,'bin','esmini')
else:
app = os.path.join(ESMINI_PATH, application)
if osc_filename is not None:
args = [app, '--osc', osc_filename] + esmini_arguments.split()
#print('running: {}'.format(' '.join(args)))
else:
args = [app] + esmini_arguments.split()
if xosc_str is not None:
args += ['--osc_str', xosc_str]
return_code = None
with open(STDOUT_FILENAME, "w") as f:
process = subprocess.Popen(args, cwd=os.path.dirname(os.path.realpath(__file__)),
stdout=f, env=env)
elapsed = 0
while elapsed < TIMEOUT and return_code is None:
return_code = process.poll()
# watch dog
if return_code is None:
time.sleep(1)
elapsed += 1
if return_code is None:
print('timeout ({}s). Terminating scenario ({}).'.format(TIMEOUT, os.path.basename(osc_filename)))
process.kill()
assert False, 'Timeout'
assert return_code == 0
with open(LOG_FILENAME, 'r') as logfile:
return logfile.read()
assert False, 'No log file'
def run_replayer(replayer_arguments = None):
if os.path.exists(LOG_FILENAME):
os.remove(LOG_FILENAME)
if os.path.exists(STDOUT_FILENAME):
os.remove(STDOUT_FILENAME)
app = os.path.join(ESMINI_PATH,'bin','replayer')
return_code = None
args = [app] + replayer_arguments.split()
with open(STDOUT_FILENAME, "w") as f:
process = subprocess.Popen(args, cwd=os.path.dirname(os.path.realpath(__file__)), stdout=f, env=env)
elapsed = 0
while elapsed < TIMEOUT and return_code is None:
return_code = process.poll()
# watch dog
if return_code is None:
time.sleep(1)
elapsed += 1
if return_code is None:
print('timeout ({}s). Terminating scenario ({}).'.format(TIMEOUT, os.path.basename(osc_filename)))
process.kill()
assert False, 'Timeout'
with open(STDOUT_FILENAME, 'r') as logfile:
log = logfile.read()
assert return_code == 0, log
return log
assert False, 'No log file'
def generate_csv(filename=DAT_FILENAME):
# Below is one/the old way of converting dat to csv. Keeping the lines for reference.
# args = [os.path.join(ESMINI_PATH,'bin','dat2csv'), DAT_FILENAME]
# process = subprocess.run(args, cwd=os.path.dirname(os.path.realpath(__file__)))
# Below is the Python way of converting dat to csv
dat = DATFile(filename)
dat.save_csv()
dat.close()
with open(os.path.splitext(filename)[0] + '.csv', "r") as f:
return f.read()
assert False, 'No csv file'