diff --git a/pdm_build.py b/pdm_build.py new file mode 100644 index 0000000..81ada12 --- /dev/null +++ b/pdm_build.py @@ -0,0 +1,45 @@ +import re +import zipfile +from pathlib import Path + +from Cython.Build import cythonize + +ext_modules = cythonize(["src/dopt_sensor_anomalies/detection.py"]) + + +def pdm_build_initialize(context): + context.ensure_build_dir() + + +def pdm_build_update_setup_kwargs(context, setup_kwargs): + setup_kwargs.update( + ext_modules=ext_modules, + ) + + +def pdm_build_finalize(context, artifact): + print(">>>>>> Context: ", context) + print(">>>>>> Artifact: ", artifact) + pth_artifact = Path(artifact) + + if pth_artifact.suffix == ".whl": + delete_source_files_from_wheel(pth_artifact) + + +def delete_source_files_from_wheel(pth_to_whl: Path): + assert pth_to_whl.exists(), "wheel file not existing" + tmp_dir = pth_to_whl.parent / "tmp" + tmp_dir.mkdir() + filename = pth_to_whl.name + tmp_whl = tmp_dir / filename + pattern = re.compile(r".*\.c$|.*detection.py$") + + with zipfile.ZipFile(pth_to_whl, mode="r") as src: + with zipfile.ZipFile(tmp_whl, mode="w") as dst: + for filename in src.namelist(): + if pattern.match(filename) is None: + data = src.read(filename) + dst.writestr(filename, data) + + tmp_whl.replace(pth_to_whl) + tmp_dir.rmdir()