-
Notifications
You must be signed in to change notification settings - Fork 126
/
setup.py
163 lines (142 loc) · 4.98 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
#!/usr/local/env python3
# -*- coding: UTF-8 -*-
import os
import subprocess
import sys
import re
from distutils.spawn import find_executable
from setuptools import setup
from setuptools.command.sdist import sdist
from setuptools.command.build_py import build_py
# protoc
from protoc import PROTOC_EXE
# configure the version number
VERSION_MAJOR = None
VERSION_MINOR = None
VERSION_PATCH = None
VERSION_SUFFIX = None
with open("VERSION", "rt") as versionin:
for line in versionin:
if line.startswith("VERSION_MAJOR"):
VERSION_MAJOR = int(line.split("=")[1].strip())
if line.startswith("VERSION_MINOR"):
VERSION_MINOR = int(line.split("=")[1].strip())
if line.startswith("VERSION_PATCH"):
VERSION_PATCH = int(line.split("=")[1].strip())
if line.startswith("VERSION_SUFFIX"):
VERSION_SUFFIX = line.split("=")[1].strip()
package_name = "osi3"
package_path = os.path.join(os.getcwd(), package_name)
class ProtobufGenerator:
@staticmethod
def find_protoc():
"""Locates protoc executable"""
if os.path.exists(PROTOC_EXE):
protoc = PROTOC_EXE
elif "PROTOC" in os.environ and os.path.exists(os.environ["PROTOC"]):
protoc = os.environ["PROTOC"]
else:
protoc = find_executable("protoc")
if protoc is None:
sys.stderr.write(
"protoc not found. Is protobuf-compiler installed? \n"
"Alternatively, you can point the PROTOC environment variable "
"to a local version."
)
sys.exit(1)
return protoc
osi_files = (
"osi_common.proto",
"osi_datarecording.proto",
"osi_detectedlane.proto",
"osi_detectedobject.proto",
"osi_detectedoccupant.proto",
"osi_detectedroadmarking.proto",
"osi_detectedtrafficlight.proto",
"osi_detectedtrafficsign.proto",
"osi_environment.proto",
"osi_featuredata.proto",
"osi_groundtruth.proto",
"osi_hostvehicledata.proto",
"osi_lane.proto",
"osi_logicaldetectiondata.proto",
"osi_logicallane.proto",
"osi_motionrequest.proto",
"osi_object.proto",
"osi_occupant.proto",
"osi_referenceline.proto",
"osi_roadmarking.proto",
"osi_route.proto",
"osi_sensordata.proto",
"osi_sensorspecific.proto",
"osi_sensorview.proto",
"osi_sensorviewconfiguration.proto",
"osi_streamingupdate.proto",
"osi_trafficcommand.proto",
"osi_trafficcommandupdate.proto",
"osi_trafficlight.proto",
"osi_trafficsign.proto",
"osi_trafficupdate.proto",
"osi_version.proto",
)
""" Generate Protobuf Messages """
def generate(self):
sys.stdout.write("Generating Protobuf Version Message\n")
with open("osi_version.proto.in", "rt") as fin:
with open("osi_version.proto", "wt") as fout:
for line in fin:
lineConfigured = line.replace("@VERSION_MAJOR@", str(VERSION_MAJOR))
lineConfigured = lineConfigured.replace(
"@VERSION_MINOR@", str(VERSION_MINOR)
)
lineConfigured = lineConfigured.replace(
"@VERSION_PATCH@", str(VERSION_PATCH)
)
fout.write(lineConfigured)
pattern = re.compile('^import "osi_')
for source in self.osi_files:
with open(source) as src_file:
with open(os.path.join(package_path, source), "w") as dst_file:
for line in src_file:
dst_file.write(
pattern.sub('import "' + package_name + "/osi_", line)
)
for source in self.osi_files:
sys.stdout.write("Protobuf-compiling " + source + "\n")
source_path = os.path.join(package_name, source)
subprocess.check_call([self.find_protoc(), "--python_out=.", source_path])
def maybe_generate(self):
if os.path.exists("osi_version.proto.in"):
self.generate()
class CustomBuildPyCommand(build_py):
def run(self):
ProtobufGenerator().maybe_generate()
build_py.run(self)
class CustomSDistCommand(sdist):
def run(self):
ProtobufGenerator().generate()
sdist.run(self)
try:
os.mkdir(package_path)
except Exception:
pass
try:
with open(os.path.join(package_path, "__init__.py"), "wt") as init_file:
init_file.write(
f"__version__ = '{VERSION_MAJOR}.{VERSION_MINOR}.{VERSION_PATCH}{VERSION_SUFFIX or ''}'\n"
)
except Exception:
pass
setup(
version=str(VERSION_MAJOR)
+ "."
+ str(VERSION_MINOR)
+ "."
+ str(VERSION_PATCH)
+ (VERSION_SUFFIX or ""),
packages=[package_name, "osi3trace"],
cmdclass={
"sdist": CustomSDistCommand,
"build_py": CustomBuildPyCommand,
},
)