From d057d811a7e45a13050b931cb7450d950e946068 Mon Sep 17 00:00:00 2001 From: Evgenii Gorchakov Date: Fri, 23 Aug 2024 13:06:49 +0200 Subject: [PATCH] chore(ci): build wheels (#4) --- .github/workflows/build.yaml | 29 +++++++++++++++++++++++++++++ .github/workflows/ci.yaml | 11 ++++++++++- .pre-commit-config.yaml | 6 +++--- README.md | 2 +- hatch_build.py | 26 +++++++++++++++++++++++++- justfile | 13 +++++++++---- pyproject.toml | 15 ++++++++++----- 7 files changed, 87 insertions(+), 15 deletions(-) create mode 100644 .github/workflows/build.yaml diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml new file mode 100644 index 0000000..18f1692 --- /dev/null +++ b/.github/workflows/build.yaml @@ -0,0 +1,29 @@ +--- +name: build + +on: + push: + branches: [main] + tags: ["*"] + + release: + types: [published] + + workflow_dispatch: + +jobs: + build-wheels: + runs-on: ubuntu-latest + steps: + - name: setup ssh + uses: webfactory/ssh-agent@v0.9.0 + with: + ssh-private-key: ${{ secrets.YAAK_IDL_REPO_SSH_KEY }} + + - name: checkout + uses: actions/checkout@v4 + with: + submodules: recursive + persist-credentials: false + + - uses: hynek/build-and-inspect-python-package@v2.8.0 diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 70c0ef6..df6bed4 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -1,7 +1,13 @@ --- name: CI -on: [push] +on: + push: + branches: [main] + + pull_request: + + workflow_dispatch: jobs: pre-commit: @@ -41,6 +47,9 @@ jobs: - name: uv sync run: uv sync --all-extras --dev + - name: run build hooks + run: uvx --from hatch hatch build --clean --hooks-only --target sdist + - name: install pre-commit run: uv tool install --upgrade pre-commit diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index da283a6..aeba157 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -8,7 +8,7 @@ repos: - id: validate-pyproject - repo: https://github.com/crate-ci/typos - rev: v1.23.6 + rev: v1.23.7 hooks: - id: typos @@ -24,14 +24,14 @@ repos: exclude: examples/config - repo: https://github.com/astral-sh/ruff-pre-commit - rev: v0.6.1 + rev: v0.6.2 hooks: - id: ruff args: [--fix] - id: ruff-format - repo: https://github.com/DetachHead/basedpyright-pre-commit-mirror - rev: 1.16.0 + rev: 1.17.0 hooks: - id: basedpyright diff --git a/README.md b/README.md index c2d92af..8c5b650 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -![](https://github.com/yaak-ai/rbyte/actions/workflows/ci.yaml/badge.svg) +[![CI](https://github.com/yaak-ai/rbyte/actions/workflows/ci.yaml/badge.svg)](https://github.com/yaak-ai/rbyte/actions/workflows/ci.yaml) [![build](https://github.com/yaak-ai/rbyte/actions/workflows/build.yaml/badge.svg)](https://github.com/yaak-ai/rbyte/actions/workflows/build.yaml) # rbyte diff --git a/hatch_build.py b/hatch_build.py index 3205471..bbba4d2 100644 --- a/hatch_build.py +++ b/hatch_build.py @@ -1,3 +1,5 @@ +import logging +from collections.abc import Generator from importlib import resources from pathlib import Path from tempfile import NamedTemporaryFile @@ -7,6 +9,9 @@ from hatchling.builders.hooks.plugin.interface import BuildHookInterface from protoletariat.fdsetgen import Raw +logger = logging.getLogger(__name__) +logging.basicConfig(level=logging.DEBUG) + class BuildYaakIdlProtosHook(BuildHookInterface): # pyright: ignore[reportMissingTypeArgument] PLUGIN_NAME = "build-yaak-idl-protos" @@ -33,10 +38,24 @@ class BuildYaakIdlProtosHook(BuildHookInterface): # pyright: ignore[reportMissi ) YAAK_IDL_PROTOS = ("can.proto", "sensor.proto") + @override + def clean(self, versions: list[str]) -> None: + for path in self._get_yaak_idl_proto_paths(): + if path.exists(): + logger.warning("removing %s", path) + path.unlink() + @override def initialize(self, version: str, build_data: dict[str, Any]) -> None: self._build_yaak_idl_protos() + @classmethod + def _get_yaak_idl_proto_paths(cls) -> Generator[Path, Any, None]: + for proto in cls.YAAK_IDL_PROTOS: + name, *_ = proto.split(".", maxsplit=1) + for ext in (".py", ".pyi"): + yield (cls.YAAK_IDL_PYTHON_OUT / f"{name}_pb2").with_suffix(ext) + @classmethod def _build_yaak_idl_protos(cls) -> None: with NamedTemporaryFile() as descriptor_set_out: @@ -57,7 +76,12 @@ def _build_yaak_idl_protos(cls) -> None: Raw(descriptor_set_out.read()).fix_imports( python_out=cls.YAAK_IDL_PYTHON_OUT, create_package=False, - overwrite_callback=lambda file, text: file.write_text(text), # pyright: ignore[reportArgumentType] + overwrite_callback=cls._overwrite_callback, module_suffixes=["_pb2.py", "_pb2.pyi"], exclude_imports_glob=["google/protobuf/*"], ) + + @staticmethod + def _overwrite_callback(file: Path, text: str) -> None: + logger.warning("overwriting %s", file) + _ = file.write_text(text) diff --git a/justfile b/justfile index e708f7d..acd44e0 100644 --- a/justfile +++ b/justfile @@ -1,21 +1,26 @@ export HYDRA_FULL_ERROR := "1" export PYTHONOPTIMIZE := "1" +export HATCH_BUILD_CLEAN := "1" _default: @just --list --unsorted setup: git submodule update --init --recursive --remote - uv sync --all-extras --dev + uv sync --all-extras for tool in basedpyright ruff pre-commit; do uv tool install --force --upgrade $tool; done uvx pre-commit install --install-hooks +clean: + uvx --from hatch hatch clean + build: - uvx --from build pyproject-build --installer uv --wheel + uvx --from build pyproject-build --installer uv + +build-protos: + uvx --from hatch hatch build --clean --hooks-only --target sdist pre-commit: - uvx pre-commit validate-config - uvx pre-commit install --install-hooks uvx pre-commit run --all-files --color=always generate-example-config: diff --git a/pyproject.toml b/pyproject.toml index d4cee98..9908ef7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -40,7 +40,7 @@ rbyte-build-table = 'rbyte.scripts.build_table:main' rbyte-visualize = 'rbyte.scripts.visualize:main' [build-system] -requires = ["hatchling"] +requires = ["hatchling>=1.25.0"] build-backend = "hatchling.build" [tool.uv] @@ -56,14 +56,19 @@ allow-direct-references = true [tool.hatch.build] reproducible = true -[tool.hatch.build.targets.wheel] -packages = ["src/rbyte"] -artifacts = ["src/rbyte/io/table/yaak/proto"] +[tool.hatch.build.targets.sdist] +include = ["src/rbyte"] exclude = ["src/rbyte/io/table/yaak/idl-repo"] +artifacts = ["src/rbyte/io/table/yaak/proto/*_pb2.py*"] -[tool.hatch.build.hooks.custom] +[tool.hatch.build.targets.sdist.hooks.custom] +enable-by-default = true require-runtime-features = ["build"] +[tool.hatch.build.targets.wheel] +packages = ["src/rbyte"] +artifacts = ["src/rbyte/io/table/yaak/proto/*_pb2.py*"] + [tool.basedpyright] typeCheckingMode = "all" enableTypeIgnoreComments = true