bindings: python: don't install test-specific C extension binaries
authorBartosz Golaszewski <bartosz.golaszewski@linaro.org>
Tue, 18 Apr 2023 09:58:57 +0000 (11:58 +0200)
committerBartosz Golaszewski <bartosz.golaszewski@linaro.org>
Tue, 18 Apr 2023 13:08:57 +0000 (15:08 +0200)
We want to ship the source code for C extensions used by the test suite
but not install the built shared objects or put them into the bdist.
Extend the build_ext command to delete the tests from the build directory
right after the extensions have been built and - possibly - installed
into the source tree (if --inplace is set).

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
bindings/python/setup.py

index a53d55faf409f03cff45d038070c299d8a44cd76..66b7908098eb17f3e754942a0d27456cc76afb01 100644 (file)
@@ -1,8 +1,23 @@
 # SPDX-License-Identifier: GPL-2.0-or-later
 # SPDX-FileCopyrightText: 2022 Bartosz Golaszewski <brgl@bgdev.pl>
 
-from os import environ
+from os import environ, path
 from setuptools import setup, Extension, find_packages
+from setuptools.command.build_ext import build_ext as orig_build_ext
+from shutil import rmtree
+
+
+class build_ext(orig_build_ext):
+    """
+    setuptools install all C extentions even if they're excluded in setup().
+    As a workaround - remove the tests directory right after all extensions
+    were built (and possibly copied to the source directory if inplace is set).
+    """
+
+    def run(self):
+        super().run()
+        rmtree(path.join(self.build_lib, "tests"), ignore_errors=True)
+
 
 gpiod_ext = Extension(
     "gpiod._ext",
@@ -46,6 +61,7 @@ setup(
     name="libgpiod",
     packages=find_packages(exclude=["tests", "tests.*"]),
     ext_modules=extensions,
+    cmdclass={"build_ext": build_ext},
     version=__version__,
     author="Bartosz Golaszewski",
     author_email="brgl@bgdev.pl",