-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.py
50 lines (44 loc) · 1.63 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
import subprocess
from contextlib import suppress
from pathlib import Path
from setuptools import Command, setup
from setuptools.command.build import build
class CustomCommand(Command):
def initialize_options(self) -> None:
self.bdist_dir = None
self.proto_msgs_path = None
self.pkg_name = None
def finalize_options(self) -> None:
self.pkg_name = self.distribution.get_name().replace('-', '_')
self.proto_msgs_path = Path("proto")
with suppress(Exception):
self.bdist_dir = Path(self.get_finalized_command("bdist_wheel").bdist_dir)
def get_source_files(self) ->'list[str]':
if self.proto_msgs_path.is_dir():
return [str(path) for path in self.proto_msgs_path.glob('*.proto')]
else:
return []
def run(self) -> None:
if self.bdist_dir:
# Create package structure
output_dir = self.bdist_dir / self.pkg_name
output_dir.mkdir(parents=True, exist_ok=True)
# generate python classes
protoc_call = [
"python3",
"-m",
"grpc_tools.protoc",
f"--proto_path={self.proto_msgs_path}",
f"--python_out={output_dir}",
f"--pyi_out={output_dir}",
*self.get_source_files(),
]
subprocess.call(protoc_call)
with open(f"{output_dir}/__init__.py", "w"):
pass
class CustomBuild(build):
sub_commands = [('build_custom', None)] + build.sub_commands
setup(
packages=[],
cmdclass={'build': CustomBuild, 'build_custom': CustomCommand}
)