Skip to content

Commit

Permalink
Merge branch 'inject-build-subcommand'
Browse files Browse the repository at this point in the history
  • Loading branch information
stinovlas committed Feb 1, 2024
2 parents c270a73 + 228715c commit ab55b79
Show file tree
Hide file tree
Showing 15 changed files with 135 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
strategy:
max-parallel: 4
matrix:
python: [3.8, 3.9, "3.10", "3.11", "3.12"]
python: ["3.8", "3.9", "3.10", "3.11", "3.12"]

steps:
- uses: actions/checkout@v2
Expand Down
10 changes: 8 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ classifiers =

[options]
zip_safe = false
packages = find:
packages =
setuptools_grpc
include_package_data = true
python_requires = ~=3.8
install_requires =
Expand All @@ -32,17 +33,22 @@ install_requires =
[options.entry_points]
distutils.commands =
build_grpc = setuptools_grpc.build_grpc:build_grpc
setuptools.finalize_distribution_options =
setuptools_grpc = setuptools_grpc:inject_build

[options.extras_require]
quality =
black
doc8
flake8
isort
tests =
test =
build
pytest
pytest-cov
testfixtures
wheel
wheel-inspect

[isort]
profile = black
Expand Down
12 changes: 12 additions & 0 deletions setuptools_grpc/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
"""Setuptools grpc."""

from setuptools import Command, Distribution

__version__ = "0.5"


def has_grpc(build: Command) -> bool:
build_grpc = build.get_finalized_command("build_grpc")
return bool(build_grpc.proto_files + build_grpc.grpc_files)


def inject_build(dist: Distribution) -> None:
build = dist.get_command_class("build")
build.sub_commands.insert(0, ("build_grpc", has_grpc))
41 changes: 23 additions & 18 deletions setuptools_grpc/build_grpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,23 +61,28 @@ def run(self):
args.extend("-I%s" % x for x in includes)

# Generate protobuf modules
log.info("building protos")
for proto_file in self.proto_files:
log.info("generating %s → %s", proto_file, py_module_name(proto_file))
if protoc_main(
args
+ ["--python_out", self.output_path, "--pyi_out", self.output_path]
+ [os.path.join(self.proto_path, proto_file) for proto_file in self.proto_files]
):
raise RuntimeError("grpc_build failed")
if self.proto_files:
log.info("building protos")
for proto_file in self.proto_files:
log.info("generating %s → %s", proto_file, py_module_name(proto_file))

protoc_args = (
args
+ ["--python_out", self.output_path, "--pyi_out", self.output_path]
+ [os.path.join(self.proto_path, proto_file) for proto_file in self.proto_files]
)
if protoc_main(protoc_args):
raise RuntimeError("grpc_build failed")

# Generate grpc modules
log.info("building grpc")
for grpc_file in self.grpc_files:
log.info("generating %s → %s", grpc_file, grpc_py_module_name(grpc_file))
if protoc_main(
args
+ ["--grpc_python_out", self.output_path]
+ [os.path.join(self.proto_path, grpc_file) for grpc_file in self.grpc_files]
):
raise RuntimeError("grpc_build failed")
if self.grpc_files:
log.info("building grpc")
for grpc_file in self.grpc_files:
log.info("generating %s → %s", grpc_file, grpc_py_module_name(grpc_file))
grpc_protoc_args = (
args
+ ["--grpc_python_out", self.output_path]
+ [os.path.join(self.proto_path, grpc_file) for grpc_file in self.grpc_files]
)
if protoc_main(grpc_protoc_args):
raise RuntimeError("grpc_build failed")
Empty file added tests/__init__.py
Empty file.
Empty file.
10 changes: 10 additions & 0 deletions tests/project_no_config/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[build-system]
requires = ["setuptools"]
build-backend = "setuptools.build_meta"

[project]
name = "example"
version = "0"

[tool.setuptools]
packages = ["example"]
Empty file.
10 changes: 10 additions & 0 deletions tests/project_no_protos/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[build-system]
requires = ["setuptools", "setuptools_grpc"]
build-backend = "setuptools.build_meta"

[project]
name = "example"
version = "0"

[tool.setuptools]
packages = ["example"]
Empty file.
3 changes: 3 additions & 0 deletions tests/project_protos/example/service.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
syntax = "proto3";

message Example {}
10 changes: 10 additions & 0 deletions tests/project_protos/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[build-system]
requires = ["setuptools", "setuptools_grpc"]
build-backend = "setuptools.build_meta"

[project]
name = "example"
version = "0"

[tool.setuptools]
packages = ["example"]
2 changes: 2 additions & 0 deletions tests/project_protos/setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[build_grpc]
proto_files = **/*.proto
53 changes: 53 additions & 0 deletions tests/test_init.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import shutil
from pathlib import Path

import pytest
from build import ProjectBuilder
from setuptools import Distribution
from wheel_inspect import inspect_wheel

from setuptools_grpc import has_grpc, inject_build


@pytest.fixture
def project_no_config(tmp_path):
shutil.copytree(Path(__file__).parent / "project_no_config", tmp_path, dirs_exist_ok=True)
builder = ProjectBuilder(tmp_path)
yield builder.build("wheel", output_directory=tmp_path)


@pytest.fixture
def project_no_protos(tmp_path):
shutil.copytree(Path(__file__).parent / "project_no_protos", tmp_path, dirs_exist_ok=True)
builder = ProjectBuilder(tmp_path)
yield builder.build("wheel", output_directory=tmp_path)


@pytest.fixture
def project_protos(tmp_path):
shutil.copytree(Path(__file__).parent / "project_protos", tmp_path, dirs_exist_ok=True)
builder = ProjectBuilder(tmp_path)
yield builder.build("wheel", output_directory=tmp_path)


def test_no_config(project_no_config):
output = inspect_wheel(project_no_config)
assert output["derived"]["modules"] == ["example"]


def test_no_protos(project_no_protos):
output = inspect_wheel(project_no_protos)
assert output["derived"]["modules"] == ["example"]


def test_protos(project_protos):
output = inspect_wheel(project_protos)
assert output["derived"]["modules"] == ["example", "example.service_pb2"]


def test_inject_build():
dist = Distribution()
build_cls = dist.get_command_class("build")
sub_commands = list(build_cls.sub_commands)
inject_build(dist)
assert build_cls.sub_commands == [("build_grpc", has_grpc)] + sub_commands
9 changes: 3 additions & 6 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
[tox]
minversion = 3.7.0
minversion = 4.4.0
envlist =
quality
py{38,39,310,311,312}
skip_missing_interpreters = true
isolated_build = true

[testenv]
constrain_package_deps = true
package = editable
extras =
tests
test
commands =
pytest -v --cov=setuptools_grpc
pytest -v --cov=./setuptools_grpc {posargs:}

[testenv:quality]
extras =
Expand Down

0 comments on commit ab55b79

Please sign in to comment.