xref: /aosp_15_r20/tools/netsim/testing/netsim-grpc/setup.py (revision cf78ab8cffb8fc9207af348f23af247fb04370a6)
1"""Setup file for netsim-grpc.
2
3Use setup.cfg to configure your project.
4
5This file was generated with PyScaffold 4.3.
6PyScaffold helps you to put up the scaffold of your new Python project.
7Learn more under: https://pyscaffold.org/
8"""
9
10import os
11from os import path
12import subprocess
13import sys
14
15from setuptools import setup
16from setuptools.command.build_py import build_py
17
18
19class ProtoBuild(build_py):
20  """This command automatically compiles all netsim .proto files with `protoc` compiler
21
22  and places generated files under src/netsim_grpc/proto/
23  """
24
25  def run(self):
26    here = path.abspath(path.dirname(__file__))
27    root_dir = path.dirname(path.dirname(here))
28    aosp_dir = path.dirname(path.dirname(root_dir))
29    proto_root_dir = path.join(root_dir, "proto")
30    proto_dir = path.join(proto_root_dir, "netsim")
31    rootcanal_proto_root_dir = path.join(
32        aosp_dir,
33        "packages",
34        "modules",
35        "Bluetooth",
36        "tools",
37        "rootcanal",
38        "proto",
39    )
40    rootcanal_proto_dir = path.join(rootcanal_proto_root_dir, "rootcanal")
41    out_dir = path.join(here, "src", "netsim_grpc", "proto")
42
43    # Rootcanal Protobufs
44    for proto_file in filter(
45        lambda x: x.endswith(".proto"), os.listdir(rootcanal_proto_dir)
46    ):
47      source = path.join(rootcanal_proto_dir, proto_file)
48      output = path.join(out_dir, "rootcanal", proto_file).replace(
49          ".proto", "_pb2.py"
50      )
51
52      if not path.exists(output) or (
53          path.getmtime(source) > path.getmtime(output)
54      ):
55        sys.stderr.write(f"Protobuf-compiling {source}\n")
56
57        subprocess.check_call([
58            sys.executable,
59            "-m",
60            "grpc_tools.protoc",
61            f"-I{proto_root_dir}",
62            f"-I{rootcanal_proto_root_dir}",
63            f"--python_out={out_dir}",
64            f"--grpc_python_out={out_dir}",
65            source,
66        ])
67
68    # Netsim Protobufs
69    for proto_file in filter(
70        lambda x: x.endswith(".proto"), os.listdir(proto_dir)
71    ):
72      source = path.join(proto_dir, proto_file)
73      output = path.join(out_dir, "netsim", proto_file).replace(
74          ".proto", "_pb2.py"
75      )
76
77      if not path.exists(output) or (
78          path.getmtime(source) > path.getmtime(output)
79      ):
80        sys.stderr.write(f"Protobuf-compiling {source}\n")
81
82        subprocess.check_call([
83            sys.executable,
84            "-m",
85            "grpc_tools.protoc",
86            f"-I{proto_root_dir}",
87            f"-I{rootcanal_proto_root_dir}",
88            f"--python_out={out_dir}",
89            f"--grpc_python_out={out_dir}",
90            source,
91        ])
92
93    super().run()
94
95
96if __name__ == "__main__":
97  try:
98    setup(
99        # use_scm_version={"version_scheme": "no-guess-dev", "root": "../../../"},
100        cmdclass={"build_py": ProtoBuild},
101    )
102  except:  # noqa
103    print(
104        "\n\nAn error occurred while building the project, "
105        "please ensure you have the most updated version of setuptools, "
106        "setuptools_scm and wheel with:\n"
107        "   pip install -U setuptools setuptools_scm wheel\n\n"
108    )
109    raise
110