-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto-generate GRPC client bindings as part of pip install (#272)
- Loading branch information
Martynas Asipauskas
authored and
GitHub Enterprise
committed
Nov 12, 2024
1 parent
7275344
commit ec59ad6
Showing
10 changed files
with
155 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,21 @@ | ||
[project] | ||
name = "armada_client" | ||
version = "0.3.5" | ||
version = "0.4.5" | ||
description = "Armada gRPC API python client" | ||
readme = "README.md" | ||
requires-python = ">=3.7" | ||
dependencies = ["grpcio==1.66.1", "grpcio-tools==1.66.1", "mypy-protobuf>=3.2.0", "protobuf>=5.26.1,<6.0dev" ] | ||
requires-python = ">=3.9" | ||
dependencies = ["grpcio-tools", "protobuf>3.20,<5.0"] | ||
license = { text = "Apache Software License" } | ||
authors = [{ name = "G-Research Open Source Software", email = "[email protected]" }] | ||
|
||
[project.optional-dependencies] | ||
format = ["black==23.7.0", "flake8==7.0.0", "pylint==2.17.5"] | ||
# note(JayF): sphinx-jekyll-builder was broken by sphinx-markdown-builder 0.6 -- so pin to 0.5.5 | ||
docs = ["sphinx==7.1.2", "sphinx-jekyll-builder==0.3.0", "sphinx-toolbox==3.2.0b1", "sphinx-markdown-builder==0.5.5"] | ||
test = ["pytest==7.3.1", "coverage>=6.5.0", "pytest-asyncio==0.21.1"] | ||
test = ["pytest==7.3.1", "pytest-cov", "pytest-asyncio==0.21.1"] | ||
|
||
[build-system] | ||
requires = ["setuptools"] | ||
requires = ["setuptools", "wheel", "grpcio-tools", "mypy-protobuf", "protobuf>3.20,<5.0"] | ||
build-backend = "setuptools.build_meta" | ||
|
||
[tool.mypy] | ||
|
@@ -39,4 +39,4 @@ omit = [ | |
# py.typed is required for mypy to find type hints in the package | ||
# from: https://mypy.readthedocs.io/en/stable/installed_packages.html#making-pep-561-compatible-packages | ||
[tool.setuptools.package-data] | ||
"*" = ["*.pyi", "py.typed"] | ||
"*" = ["*.pyi", "py.typed", "proto/**/*.proto"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import os | ||
from pathlib import Path | ||
import shutil | ||
import subprocess | ||
import sys | ||
from typing import Dict | ||
from setuptools import setup | ||
import importlib.resources | ||
import re | ||
from setuptools.command.build_py import build_py | ||
|
||
|
||
def generate_grpc_bindings(build_lib: Path): | ||
import grpc_tools.protoc | ||
|
||
proto_include = importlib.resources.path("grpc_tools", "_proto") | ||
proto_files = [ | ||
"google/api/annotations.proto", | ||
"google/api/http.proto", | ||
"github.com/gogo/protobuf/gogoproto/gogo.proto", | ||
"k8s.io/api/core/v1/generated.proto", | ||
"k8s.io/apimachinery/pkg/api/resource/generated.proto", | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/generated.proto", | ||
"k8s.io/apimachinery/pkg/runtime/generated.proto", | ||
"k8s.io/apimachinery/pkg/runtime/schema/generated.proto", | ||
"k8s.io/apimachinery/pkg/util/intstr/generated.proto", | ||
"k8s.io/api/networking/v1/generated.proto", | ||
"armada/event.proto", | ||
"armada/submit.proto", | ||
"armada/health.proto", | ||
"armada/job.proto", | ||
"armada/binoculars.proto", | ||
] | ||
target_root = build_lib.absolute() / "armada_client" | ||
|
||
for proto_file in proto_files: | ||
command = [ | ||
f"-I{proto_include}", | ||
f"-I{target_root / 'proto'}", | ||
f"--python_out={target_root}", | ||
f"--grpc_python_out={target_root}", | ||
f"--mypy_out={target_root}", | ||
str(target_root / "proto" / proto_file), | ||
] | ||
if grpc_tools.protoc.main(command) != 0: | ||
raise Exception(f"grpc_tools.protoc.main: {command} failed") | ||
|
||
shutil.rmtree(target_root / "github.com") | ||
shutil.rmtree(target_root / "k8s.io") | ||
|
||
adjust_import_paths(target_root) | ||
|
||
|
||
def adjust_import_paths(output_dir: Path): | ||
replacements = { | ||
r"from armada": "from armada_client.armada", | ||
r"from github.com": "from armada_client.github.com", | ||
r"from google.api": "from armada_client.google.api", | ||
} | ||
|
||
for file in output_dir.glob("armada/*.py"): | ||
replace_in_file(file, replacements) | ||
for file in output_dir.glob("google/api/*.py"): | ||
replace_in_file(file, replacements) | ||
|
||
replacements = { | ||
r"from k8s.io": "from armada_client.k8s.io", | ||
} | ||
for file in output_dir.glob("../**/*.py"): | ||
replace_in_file(file, replacements) | ||
|
||
replacements = { | ||
r" k8s": " armada_client.k8s", | ||
r"\[k8s": "[armada_client.k8s", | ||
r"import k8s.io": "import armada_client.k8s.io", | ||
} | ||
for file in output_dir.glob("k8s/**/*.pyi"): | ||
replace_in_file(file, replacements) | ||
|
||
|
||
def replace_in_file(file: Path, replacements: Dict[str, str]): | ||
"""Replace patterns in a file based on the replacements dictionary.""" | ||
|
||
content = file.read_text() | ||
for pattern, replacement in replacements.items(): | ||
content = re.sub(pattern, replacement, content) | ||
file.write_text(content) | ||
|
||
|
||
def generate_typings(build_dir: Path): | ||
typings = build_dir.absolute() / "armada_client" / "typings.py" | ||
result = subprocess.run( | ||
args=[ | ||
sys.executable, | ||
str(build_dir.absolute() / "armada_client" / "gen" / "event_typings.py"), | ||
str(typings), | ||
], | ||
env={"PYTHONPATH": str(build_dir.absolute())}, | ||
capture_output=True, | ||
) | ||
if result.returncode != 0: | ||
print(result.stdout) | ||
print(result.stderr) | ||
result.check_returncode() | ||
|
||
|
||
class BuildPackageProtos(build_py): | ||
""" | ||
Generate GRPC code before building the package. | ||
""" | ||
|
||
def run(self): | ||
super().run() | ||
output_dir = Path(".") if self.editable_mode else Path(self.build_lib) | ||
generate_grpc_bindings(output_dir) | ||
generate_typings(output_dir) | ||
|
||
|
||
setup( | ||
cmdclass={ | ||
"build_py": BuildPackageProtos, | ||
"develop": BuildPackageProtos, | ||
}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters