Skip to content

Commit a286b38

Browse files
committed
Modernize python build setup, protoc dependency
This moves most meta-data to pyproject.toml, automates the version handling and makes it available at runtime, depends on protoc via the protoc-wheel-0 package, makes sdist and bdist distributions work correctly, and removes dependencies that are not build dependencies, but rather used only in CI. Signed-off-by: Pierre R. Mai <pmai@pmsf.de>
1 parent f9de56c commit a286b38

File tree

6 files changed

+116
-50
lines changed

6 files changed

+116
-50
lines changed

.github/workflows/protobuf.yml

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,12 @@ jobs:
4545
- name: Install Python Dependencies
4646
run: |
4747
python -m pip install --upgrade pip
48-
python -m pip install -r requirements_develop.txt
48+
python -m pip install build
49+
python -m pip install -r requirements_tests.txt
4950
5051
- name: Check black format
51-
run: black --check --diff .
52+
run: |
53+
black --check --diff .
5254
5355
- name: Install Doxygen
5456
run: sudo apt-get install doxygen graphviz
@@ -88,10 +90,14 @@ jobs:
8890
- name: Prepare C++ Build
8991
run: mkdir build
9092

91-
# Versioning
92-
- name: Get versioning
93+
- name: Add Development Version Suffix
94+
if: ${{ !startsWith(github.ref, 'refs/tags') }}
95+
run: |
96+
echo "VERSION_SUFFIX = .dev`date -u '+%Y%m%d%H%M%S'`" >> VERSION
97+
98+
- name: Get git Version
9399
id: get_version
94-
run: echo "VERSION=$(git describe --always)" >> $GITHUB_OUTPUT
100+
run: echo "VERSION=$(git describe --tags --always)" >> $GITHUB_OUTPUT
95101

96102
- name: Prepare Documentation Build
97103
run: |
@@ -108,7 +114,7 @@ jobs:
108114
run: cmake --build . --config Release -j 4
109115

110116
- name: Build Python
111-
run: python setup.py build && python setup.py sdist
117+
run: python -m build
112118

113119
- name: Install Python
114120
run: python -m pip install .
@@ -148,7 +154,10 @@ jobs:
148154
python-version: '3.8'
149155

150156
- name: Install Python Dependencies
151-
run: python -m pip install --upgrade pip setuptools wheel pyyaml
157+
run: |
158+
python -m pip install --upgrade pip
159+
python -m pip install build
160+
python -m pip install -r requirements_tests.txt
152161
153162
- name: Cache Dependencies
154163
id: cache-depends
@@ -187,6 +196,11 @@ jobs:
187196
bash convert-to-proto3.sh
188197
rm *.pb2
189198
199+
- name: Add Development Version Suffix
200+
if: ${{ !startsWith(github.ref, 'refs/tags') }}
201+
run: |
202+
echo "VERSION_SUFFIX = .dev`date -u '+%Y%m%d%H%M%S'`" >> VERSION
203+
190204
- name: Configure C++ Build
191205
working-directory: build
192206
run: cmake ${{ env.PROTOBUF_VARIANT =='' && '-DCMAKE_CXX_STANDARD=17' }} ..
@@ -196,7 +210,7 @@ jobs:
196210
run: cmake --build . --config Release -j 4
197211

198212
- name: Build Python
199-
run: python setup.py build && python setup.py sdist
213+
run: python -m build
200214

201215
- name: Install Python
202216
run: python -m pip install .

.gitignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ build
2020
cmake_install.cmake
2121
install_manifest.txt
2222
osi_version.proto
23-
version.py
24-
pyproject.toml
2523

2624
compile_commands.json
2725

MANIFEST.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
include VERSION

pyproject.toml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
[build-system]
2+
requires = [
3+
"setuptools",
4+
"wheel",
5+
"protoc-wheel-0==24.4",
6+
]
7+
build-backend = "setuptools.build_meta"
8+
9+
[project]
10+
name = "open-simulation-interface"
11+
description = "ASAM Open Simulation Interface Python Bindings."
12+
authors = [
13+
{name = "ASAM Open Simulation Interface Project", email = "osi@asam.net"},
14+
]
15+
maintainers = [
16+
{name = "ASAM Open Simulation Interface Project", email = "osi@asam.net"},
17+
]
18+
dependencies = [
19+
"protobuf>=4.24.4",
20+
]
21+
22+
license = {file = "LICENSE"}
23+
readme = "README.md"
24+
classifiers = [
25+
"License :: OSI Approved :: Mozilla Public License 2.0 (MPL 2.0)",
26+
]
27+
dynamic = ["version"]
28+
29+
[project.urls]
30+
Homepage = "https://github.com/OpenSimulationInterface/open-simulation-interface"
31+
Repository = "https://github.com/OpenSimulationInterface/open-simulation-interface.git"
32+
"Bug Tracker" = "https://github.com/OpenSimulationInterface/open-simulation-interface/issues"
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
1-
setuptools
2-
wheel
31
pyyaml
42
black==23.12.1

setup.py

Lines changed: 61 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,41 @@
77
import re
88
from distutils.spawn import find_executable
99

10-
# setuptool is dependend on wheel package
1110
from setuptools import setup
11+
from setuptools.command.sdist import sdist
1212
from setuptools.command.build_py import build_py
1313

14-
# configure the version number
15-
from shutil import copyfile
16-
17-
copyfile("VERSION", "version.py")
18-
from version import *
14+
# protoc
15+
from protoc import PROTOC_EXE
1916

20-
with open("osi_version.proto.in", "rt") as fin:
21-
with open("osi_version.proto", "wt") as fout:
22-
for line in fin:
23-
lineConfigured = line.replace("@VERSION_MAJOR@", str(VERSION_MAJOR))
24-
lineConfigured = lineConfigured.replace(
25-
"@VERSION_MINOR@", str(VERSION_MINOR)
26-
)
27-
lineConfigured = lineConfigured.replace(
28-
"@VERSION_PATCH@", str(VERSION_PATCH)
29-
)
30-
fout.write(lineConfigured)
17+
# configure the version number
18+
VERSION_MAJOR = None
19+
VERSION_MINOR = None
20+
VERSION_PATCH = None
21+
VERSION_SUFFIX = None
22+
with open("VERSION", "rt") as versionin:
23+
for line in versionin:
24+
if line.startswith("VERSION_MAJOR"):
25+
VERSION_MAJOR = int(line.split("=")[1].strip())
26+
if line.startswith("VERSION_MINOR"):
27+
VERSION_MINOR = int(line.split("=")[1].strip())
28+
if line.startswith("VERSION_PATCH"):
29+
VERSION_PATCH = int(line.split("=")[1].strip())
30+
if line.startswith("VERSION_SUFFIX"):
31+
VERSION_SUFFIX = line.split("=")[1].strip()
3132

3233
package_name = "osi3"
3334
package_path = os.path.join(os.getcwd(), package_name)
3435

3536

36-
class GenerateProtobufCommand(build_py):
37+
class ProtobufGenerator:
3738
@staticmethod
3839
def find_protoc():
3940
"""Locates protoc executable"""
4041

41-
if "PROTOC" in os.environ and os.path.exists(os.environ["PROTOC"]):
42+
if os.path.exists(PROTOC_EXE):
43+
protoc = PROTOC_EXE
44+
elif "PROTOC" in os.environ and os.path.exists(os.environ["PROTOC"]):
4245
protoc = os.environ["PROTOC"]
4346
else:
4447
protoc = find_executable("protoc")
@@ -89,7 +92,19 @@ def find_protoc():
8992

9093
""" Generate Protobuf Messages """
9194

92-
def run(self):
95+
def generate(self):
96+
sys.stdout.write("Generating Protobuf Version Message\n")
97+
with open("osi_version.proto.in", "rt") as fin:
98+
with open("osi_version.proto", "wt") as fout:
99+
for line in fin:
100+
lineConfigured = line.replace("@VERSION_MAJOR@", str(VERSION_MAJOR))
101+
lineConfigured = lineConfigured.replace(
102+
"@VERSION_MINOR@", str(VERSION_MINOR)
103+
)
104+
lineConfigured = lineConfigured.replace(
105+
"@VERSION_PATCH@", str(VERSION_PATCH)
106+
)
107+
fout.write(lineConfigured)
93108
pattern = re.compile('^import "osi_')
94109
for source in self.osi_files:
95110
with open(source) as src_file:
@@ -103,38 +118,46 @@ def run(self):
103118
source_path = os.path.join(package_name, source)
104119
subprocess.check_call([self.find_protoc(), "--python_out=.", source_path])
105120

121+
def maybe_generate(self):
122+
if os.path.exists("osi_version.proto.in"):
123+
self.generate()
124+
125+
126+
class CustomBuildPyCommand(build_py):
127+
def run(self):
128+
ProtobufGenerator().maybe_generate()
106129
build_py.run(self)
107130

108131

132+
class CustomSDistCommand(sdist):
133+
def run(self):
134+
ProtobufGenerator().generate()
135+
sdist.run(self)
136+
137+
109138
try:
110139
os.mkdir(package_path)
111140
except Exception:
112141
pass
113142

114143
try:
115-
open(os.path.join(package_path, "__init__.py"), "a").close()
144+
with open(os.path.join(package_path, "__init__.py"), "wt") as init_file:
145+
init_file.write(
146+
f"__version__ = '{VERSION_MAJOR}.{VERSION_MINOR}.{VERSION_PATCH}{VERSION_SUFFIX or ''}'\n"
147+
)
116148
except Exception:
117149
pass
118150

119151
setup(
120-
name="open-simulation-interface",
121-
version=str(VERSION_MAJOR) + "." + str(VERSION_MINOR) + "." + str(VERSION_PATCH),
122-
description="A generic interface for the environmental perception of"
123-
"automated driving functions in virtual scenarios.",
124-
author="Carlo van Driesten, Timo Hanke, Nils Hirsenkorn,"
125-
"Pilar Garcia-Ramos, Mark Schiementz, Sebastian Schneider",
126-
author_email="Carlo.van-Driesten@bmw.de, Timo.Hanke@bmw.de,"
127-
"Nils.Hirsenkorn@tum.de, Pilar.Garcia-Ramos@bmw.de,"
128-
"Mark.Schiementz@bmw.de, Sebastian.SB.Schneider@bmw.de",
152+
version=str(VERSION_MAJOR)
153+
+ "."
154+
+ str(VERSION_MINOR)
155+
+ "."
156+
+ str(VERSION_PATCH)
157+
+ (VERSION_SUFFIX or ""),
129158
packages=[package_name],
130-
install_requires=["protobuf"],
131159
cmdclass={
132-
"build_py": GenerateProtobufCommand,
160+
"sdist": CustomSDistCommand,
161+
"build_py": CustomBuildPyCommand,
133162
},
134-
url="https://github.com/OpenSimulationInterface/open-simulation-interface",
135-
license="MPL 2.0",
136-
classifiers=[
137-
"License :: OSI Approved :: Mozilla Public License 2.0 (MPL 2.0)",
138-
],
139-
data_files=[("", ["LICENSE"])],
140163
)

0 commit comments

Comments
 (0)