From 1f7e83a46d10915a895f469d1c4c156b8af125f0 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 15 Jun 2023 15:01:11 -1000 Subject: [PATCH] feat: add cython --- build_ext.py | 35 +++++++++++++++++++++++++++++++++++ pyproject.toml | 6 +++++- setup.py | 9 --------- 3 files changed, 40 insertions(+), 10 deletions(-) create mode 100644 build_ext.py delete mode 100644 setup.py diff --git a/build_ext.py b/build_ext.py new file mode 100644 index 0000000..e123444 --- /dev/null +++ b/build_ext.py @@ -0,0 +1,35 @@ +"""Build optional cython modules.""" + +import contextlib +import os +from distutils.command.build_ext import build_ext +from typing import Any + + +class BuildExt(build_ext): + def build_extensions(self) -> None: + with contextlib.suppress(Exception): + super().build_extensions() + + +def build(setup_kwargs: dict[Any, Any]) -> None: + if os.environ.get("SKIP_CYTHON", False): + return + try: + from Cython.Build import cythonize + + setup_kwargs.update( + dict( + ext_modules=cythonize( + [ + "src/kasa_crypt/kasa_crypt.py", + ], + compiler_directives={"language_level": "3"}, # Python 3 + ), + cmdclass=dict(build_ext=BuildExt), + ) + ) + except Exception: + if os.environ.get("REQUIRE_CYTHON"): + raise + pass diff --git a/pyproject.toml b/pyproject.toml index 571a5c6..c762988 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -17,6 +17,10 @@ packages = [ { include = "kasa_crypt", from = "src" }, ] +[tool.poetry.build] +generate-setup-file = true +script = "build_ext.py" + [tool.poetry.urls] "Bug Tracker" = "https://github.com/bdraco/kasa-crypt/issues" "Changelog" = "https://github.com/bdraco/kasa-crypt/blob/main/CHANGELOG.md" @@ -73,5 +77,5 @@ module = "tests.*" allow_untyped_defs = true [build-system] -requires = ["poetry-core>=1.0.0"] +requires = ['setuptools>=65.4.1', 'wheel', 'Cython', "poetry-core>=1.0.0"] build-backend = "poetry.core.masonry.api" diff --git a/setup.py b/setup.py deleted file mode 100644 index 05f898f..0000000 --- a/setup.py +++ /dev/null @@ -1,9 +0,0 @@ -#!/usr/bin/env python - -# This is a shim to allow GitHub to detect the package, build is done with poetry -# Taken from https://github.com/Textualize/rich - -import setuptools - -if __name__ == "__main__": - setuptools.setup(name="kasa-crypt")