-
Notifications
You must be signed in to change notification settings - Fork 12
/
run.py
164 lines (129 loc) · 4.98 KB
/
run.py
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/python3
'''
siapp-sdk
SPDX-License-Identifier: MIT
Copyright 2022 Siemens AG
Authors:
Armin Tirala <[email protected]>
'''
import sys
import os
import shutil
import time
import subprocess
import pathlib
from helper import (
BuildArgs,
error_handler,
init_multiarch_qemu,
parse_arguments,
remove_container,
run_subprocess,
)
def _run_program(name, command_list):
if sys.platform == "linux" or sys.platform == "linux2":
pid = subprocess.Popen(command_list, stderr=subprocess.PIPE, start_new_session=True, shell=False)
elif sys.platform == "win32":
temp = ['START', '\"' + name + '\"', 'CMD', '/K'] + command_list
pid = subprocess.call(" ".join(temp), shell=True)
else:
error_handler("unsupported platform")
return pid
def _get_simulation_csv(project_dir, sdk_dir):
'''
Copies the events.csv and discover.csv into the templates directory.
If files do not exist, use the templates.
'''
templates_dir = os.path.join(sdk_dir, 'templates')
events_file_template = os.path.join(templates_dir, 'events.csv')
events_file = os.path.join(project_dir, 'events.csv')
discover_file_template = os.path.join(templates_dir, 'discover.csv')
discover_file = os.path.join(project_dir, 'discover.csv')
if not os.path.exists(events_file):
if not os.path.exists(events_file_template):
error_handler('Could not find events.csv template file ' + str(events_file_template))
else:
print(f"Info: CSV file {events_file} not found. CSV file is generated by template file!")
shutil.copyfile(events_file_template, events_file)
if not os.path.exists(discover_file):
if not os.path.exists(discover_file_template):
error_handler(f"Could not find discover.csv template file {discover_file_template}")
else:
print(f"Info: CSV file {discover_file} not found. CSV file is generated by template file!")
shutil.copyfile(discover_file_template, discover_file)
shutil.copyfile(events_file, os.path.join(templates_dir, 'Simulation', 'events.csv'))
shutil.copyfile(discover_file, os.path.join(templates_dir, 'Simulation', 'discover.csv'))
def _run_simulation(sdk_dir):
command_list = [BuildArgs.tool, 'build', '-t', 'sim', '--platform',
BuildArgs.platforms[0].image,
os.path.join(sdk_dir, 'templates', 'Simulation')]
result = run_subprocess(command_list)
if result.returncode != 0:
error_handler("Could not build simulation docker image!")
command_list = [BuildArgs.tool, 'run', '--name=sim', '-v',
'edgedata:/edgedata', '--platform',
BuildArgs.platforms[0].image,
'-it', 'sim']
return _run_program("Data SIMULATION", command_list)
def _run_siapp(project_dir, container_image):
container_file = os.path.join(project_dir, "Dockerfile")
if not os.path.exists(container_file):
error_handler(f"file {container_file} does not exist!")
command_list = [
BuildArgs.tool,
'run',
'--name=emu',
'--rm',
'--publish-all=true',
'-v',
'edgedata:/edgedata',
'--platform',
BuildArgs.platforms[0].image,
'-it',
container_image]
return _run_program("SIAPP EMULATION", command_list)
def _emulate_port():
text = ''
max_retries = 10
retries = 0
while text == '' and retries < max_retries:
result = run_subprocess([BuildArgs.tool, 'port', 'emu'])
text = result.stdout
retries += 1
time.sleep(1)
if retries >= max_retries:
error_handler("Could not get port of emu container!")
def _wait_for_keyboard_interrupt(sim_pid, emu_pid):
try:
emu_pid.wait()
sim_pid.wait()
except KeyboardInterrupt:
try:
print("Terminate subprocesses")
emu_pid.kill()
sim_pid.kill()
print("Stop emu container:")
subprocess.call([BuildArgs.tool, 'stop', 'emu'])
print("Stop sim container:")
subprocess.call([BuildArgs.tool, 'stop', 'sim'])
exit(0)
except OSError:
pass
def main():
sdk_dir = str(pathlib.Path(__file__).parent.resolve())
project_dir = str(pathlib.Path(BuildArgs.dir).resolve())
container_name = f"{BuildArgs.name}-{BuildArgs.platforms[0].name}"
container_image = f"{container_name}-{BuildArgs.version}"
remove_container("sim", force=True)
remove_container("emu", force=True)
_get_simulation_csv(project_dir, sdk_dir)
sim_pid = _run_simulation(sdk_dir)
emu_pid = _run_siapp(project_dir, container_image)
_emulate_port()
print(f"{os.path.basename(__file__)} - Successfully simulated {container_image}")
if sys.platform == "linux" or sys.platform == "linux2":
_wait_for_keyboard_interrupt(sim_pid, emu_pid)
if __name__ == "__main__":
parse_arguments('run')
init_multiarch_qemu()
main()