-
Notifications
You must be signed in to change notification settings - Fork 11
/
run.py
191 lines (138 loc) · 5.65 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/usr/bin/python3
'''
siapp-sdk
SPDX-License-Identifier: MIT
Copyright 2022 Siemens AG
Authors:
Armin Tirala <armin.tirala@siemens.com>
'''
import sys
import os
import shutil
import time
import subprocess
import pathlib
def _helper():
print('SYNOPSIS: run.py PROJECTPATH [--name NAME] [--version VERSION]')
def _errorhandler(str):
print("ERROR: " + str)
exit(-1)
def _default_version():
return '0.0.1'
def _tool():
return 'docker'
def _containerfile():
return 'Dockerfile'
def _arch_postfix():
return 'arm32v7'
def _force_container_remove(container):
subprocess.call([_tool(), 'rm', container, '--force'], stderr=subprocess.PIPE, stdout=subprocess.PIPE)
# def preexec_function():
# import signal
# signal.signal(signal.SIGINT, signal.SIG_IGN)
# pid = subprocess.Popen(command_list, stderr=subprocess.PIPE, preexec_fn=preexec_function, shell=False)
def _run_program(name, command_list):
if sys.platform == "linux" or sys.platform == "linux2":
pid = subprocess.Popen(command_list, stderr=subprocess.PIPE, preexec_fn=os.setpgrp, shell=False)
elif sys.platform == "win32":
temp = ['START', '\"' + name + '\"', 'CMD', '/K'] + command_list
pid = subprocess.call(" ".join(temp), shell=True)
else:
_errorhandler("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):
_errorhandler('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):
_errorhandler(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 = [_tool(), 'build', '-t', 'sim', os.path.join(sdk_dir, 'templates', 'Simulation')]
if subprocess.call(command_list) != 0:
_errorhandler("Could not build simulation docker image!")
command_list = [_tool(), 'run', '--name=sim', '-v', 'edgedata:/edgedata', '--platform', 'linux/arm/v7', '-it', 'sim']
return _run_program("Data SIMULATION", command_list)
def _run_siapp(projet_dir, container_image):
container_file = os.path.join(projet_dir, f"{_containerfile()}.{_arch_postfix()}")
if not os.path.exists(container_file):
_errorhandler(f"file {container_file} does not exist!")
command_list = [
_tool(),
'run',
'--name=emu',
'--rm',
'--publish-all=true',
'-v',
'edgedata:/edgedata',
'--platform',
'linux/arm/v7',
'-it',
container_image]
return _run_program("SIAPP EMULATION", command_list)
def _emulate_port():
subprocess.call([_tool(), 'port', 'emu'])
def _wait_for_keyboard_interupt(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([_tool(), 'stop', 'emu'])
print("Stop sim container:")
subprocess.call([_tool(), 'stop', 'sim'])
exit(0)
except OSError:
pass
def main(dir, name, version):
sdk_dir = str(pathlib.Path(__file__).parent.resolve())
project_dir = str(pathlib.Path(dir).resolve())
container_name = f"{name}-{_arch_postfix()}"
container_image = f"{container_name}-{version}"
_force_container_remove("sim")
_force_container_remove("emu")
_get_simulation_csv(project_dir, sdk_dir)
sim_pid = _run_simulation(sdk_dir)
emu_pid = _run_siapp(project_dir, container_image)
time.sleep(2)
_emulate_port()
print(f"{os.path.basename(__file__)} - Successfully simulated {container_image}")
if sys.platform == "linux" or sys.platform == "linux2":
_wait_for_keyboard_interupt(sim_pid, emu_pid)
if __name__ == "__main__":
arglen = len(sys.argv)
if arglen > 1:
dir = sys.argv[1]
dir_name = pathlib.Path(dir).resolve().name
name = dir_name.lower().replace(' ', '')
version = _default_version()
for i, additional_argument in enumerate(sys.argv):
if ("--name" in additional_argument or "-name" in additional_argument)and arglen > i:
name = sys.argv[i + 1].lower().replace(' ', '')
if ("--version" in additional_argument or "-version" in additional_argument) and arglen > i:
version = sys.argv[i + 1].lower().replace(' ', '')
main(dir, name, version)
else:
_helper()
_errorhandler("project directory path not specified!")