-
Notifications
You must be signed in to change notification settings - Fork 1
/
setup.py
211 lines (171 loc) · 7.04 KB
/
setup.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#
# Copyright (C) 2020-2024 Embedded AMS B.V. - All Rights Reserved
#
# This file is part of Embedded Proto.
#
# Embedded Proto is open source software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as published
# by the Free Software Foundation, version 3 of the license.
#
# Embedded Proto is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Embedded Proto. If not, see <https://www.gnu.org/licenses/>.
#
# For commercial and closed source application please visit:
# <https://EmbeddedProto.com/license/>.
#
# Embedded AMS B.V.
# Info:
# info at EmbeddedProto dot com
#
# Postal address:
# Atoomweg 2
# 1627 LE, Hoorn
# the Netherlands
#
import subprocess
import argparse
import os
import venv
import platform
from sys import stderr
import shutil
# Perform a system call to beable to display colors on windows
os.system("")
CGREEN = '\33[92m'
CRED = '\33[91m'
CYELLOW = '\33[93m'
CEND = '\33[0m'
print("Update the submodule Embedded Proto before importing its setup script.", end='')
try:
result = subprocess.run(["git", "submodule", "init"], check=False, capture_output=True)
if result.returncode:
print(" [Fail]")
print(result.stderr.decode("utf-8"), end='', file=stderr)
exit(1)
result = subprocess.run(["git", "submodule", "update"], check=False, capture_output=True)
if result.returncode:
print(" [Fail]")
print(result.stderr.decode("utf-8"), end='', file=stderr)
exit(1)
else:
print(" [Success]")
except OSError:
print(" [Fail]")
print("Unable to find git in your path.")
print("Stopping the setup.")
exit(1)
except Exception as e:
print(" [Fail]")
print("Error: " + str(e), file=stderr)
exit(1)
from EmbeddedProto import setup as EPSetup
####################################################################################
def setup_desktop_script():
try:
# ---------------------------------------
print("Setup the virtual environment for the desktop Python script.", end='')
os.chdir("./desktop")
venv.create("venv", with_pip=True)
print(" [" + CGREEN + "Success" + CEND + "]")
# ---------------------------------------
print("Installing requirement for the desktop Python script.", end='')
on_windows = "Windows" == platform.system()
command = []
if on_windows:
command.append("./venv/Scripts/pip")
else:
command.append("./venv/bin/pip")
command.extend(["install", "-r", "requirements.txt"])
result = subprocess.run(command, check=False, capture_output=True)
if result.returncode:
print(" [" + CRED + "Fail" + CEND + "]")
print(result.stderr.decode("utf-8"), end='', file=stderr)
exit(1)
else:
print(" [" + CGREEN + "Success" + CEND + "]")
os.chdir("..")
except Exception as e:
print(" [" + CRED + "Fail" + CEND + "]")
print("Error: " + str(e), file=stderr)
exit(1)
####################################################################################
def generate_source_code():
print("Generate the source file based on sd_messages.proto.", end='')
try:
os.chdir("EmbeddedProto")
command = ["protoc", "-I../proto", "--eams_out=../msp430fr5994/generated",
"../proto/sd_messages.proto"]
if "Windows" == platform.system():
command.append("--plugin=protoc-gen-eams=protoc-gen-eams.bat")
else:
command.append("--plugin=protoc-gen-eams=protoc-gen-eams")
result = subprocess.run(command, check=False, capture_output=True)
if result.returncode:
print(" [" + CRED + "Fail" + CEND + "]")
print(result.stderr.decode("utf-8"), end='', file=stderr)
exit(1)
os.chdir("..")
result = subprocess.run(["protoc", "-I./proto", "--python_out=./desktop/generated",
"./proto/sd_messages.proto"], check=False, capture_output=True)
if result.returncode:
print(" [" + CRED + "Fail" + CEND + "]")
print(result.stderr.decode("utf-8"), end='', file=stderr)
exit(1)
else:
print(" [" + CGREEN + "Success" + CEND + "]")
except Exception as e:
print(" [" + CRED + "Fail" + CEND + "]")
print("Error: " + str(e), file=stderr)
exit(1)
####################################################################################
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="This script will setup the environment to generate Embedded Proto "
"code in your project.")
parser.add_argument("-g", "--generate", action="store_true",
help="Do not run the Embedded Proto setup. Only generate the source based on *.proto files. "
"Use this after changing uart_messages.proto.")
EPSetup.add_parser_arguments(parser)
EPSetup.display_feedback()
args = parser.parse_args()
# ---------------------------------------
# Create destination folders if not present.
newpath = r"./nucleo-f446re/generated"
if not os.path.exists(newpath):
os.makedirs(newpath)
newpath = r"./desktop/generated"
if not os.path.exists(newpath):
os.makedirs(newpath)
# ---------------------------------------
# Clean excisting venv and other generated files.
if args.clean:
shutil.rmtree("./desktop/venv", ignore_errors=True)
try:
os.remove("./desktop/generated/sd_messages_pb2.py")
except FileNotFoundError:
# This exception we can safely ignore as it means the file was not there. In that case we do not have to remove
# it.
pass
try:
os.remove("./msp430fr5994/generated/sd_messages.h")
except FileNotFoundError:
# This exception we can safely ignore as it means the file was not there. In that case we do not have to remove
# it.
pass
# ---------------------------------------
if not args.generate:
os.chdir("./EmbeddedProto")
# Run the setup script of Embedded Proto it self.
EPSetup.run(args)
os.chdir("..")
# ---------------------------------------
setup_desktop_script()
# ---------------------------------------
generate_source_code()
# ---------------------------------------
# If the script did not exit before here we have completed it with success.
print("Setup completed with success!")