Skip to content

Commit

Permalink
tidy up setup, force cleanup on install #15
Browse files Browse the repository at this point in the history
  • Loading branch information
timdewhirst committed Oct 5, 2022
1 parent ca05a7b commit b36bcad
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 12 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ To build using pip/setuptools:

* create a venv at the same level as the openpivcore directory
* activate the venv
* run pip against the name of the directory containing the openpiv code e.g. `pip install openpiv-c-qt/`
* run pip against the name of the directory containing the openpiv code e.g. `pip install <path to source>`
* a clean step should happen as part of the build, this can also be run manually with `python setup.py clean`

To build using cmake:

Expand Down
8 changes: 0 additions & 8 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,3 @@ requires = [
"pybind11"
]
build-backend = "setuptools.build_meta"

[tool.cibuildwheel]
# test-command = "pytest {project}/tests"
# test-extras = ["test"]
# test-skip = ["*universal2:arm64"]

# Setuptools bug causes collision between pypy and cpython artifacts
#before-build = "rm -rf {project}/build {project}/*.egg-info"
41 changes: 38 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from glob import glob
import os
import re
import shutil
import subprocess
import sys

from distutils import sysconfig as ds
from setuptools import setup
from setuptools import Command
from setuptools import Extension as _extension
from setuptools.command.build_ext import build_ext as _build_ext

Expand All @@ -17,6 +19,7 @@
"win-arm64": "ARM64",
}

package_root = os.path.dirname(os.path.realpath(__file__))

# A CMakeExtension needs a sourcedir instead of a file list.
# The name must be the _single_ output extension from the CMake build.
Expand All @@ -28,7 +31,14 @@ def __init__(self, name, sourcedir=""):


class CMakeBuild(_build_ext):
def __init__(self, args):
super().__init__(args)
self.clean_cmd = CleanCommand(args)

def build_extension(self, ext):
# force cleanup before we build
self.clean_cmd.run()

extdir = os.path.abspath(os.path.dirname(self.get_ext_fullpath(ext.name)))

# required for auto-detection & inclusion of auxiliary "native" libs
Expand Down Expand Up @@ -73,7 +83,6 @@ def build_extension(self, ext):
pass

else:

# Single config generators are handled "normally"
single_config = any(x in cmake_generator for x in {"NMake", "Ninja"})

Expand Down Expand Up @@ -113,8 +122,31 @@ def build_extension(self, ext):
os.makedirs(build_temp)

subprocess.check_call(["cmake", ext.sourcedir] + cmake_args, cwd=build_temp)
subprocess.check_call(["cmake", "--build", ".", "--clean-first"] + build_args, cwd=build_temp)
subprocess.check_call(["cmake", "--build", "."] + build_args, cwd=build_temp)


class CleanCommand(Command):
"""Custom clean command to tidy up the project root."""
CLEAN_FILES = './build ./dist ./*.pyc ./*.tgz ./*.egg-info'.split(' ')

user_options = []

def initialize_options(self):
pass

def finalize_options(self):
pass

def run(self):
for path_spec in self.CLEAN_FILES:
# Make paths absolute and relative to this path
abs_paths = glob(os.path.normpath(os.path.join(package_root, path_spec)))
for path in [str(p) for p in abs_paths]:
if not path.startswith(package_root):
# Die if path in CLEAN_FILES is absolute + outside this directory
raise ValueError(f"{path} is not a valid path")
print('removing %s' % os.path.relpath(path))
shutil.rmtree(path)

# The information here can also be placed in setup.cfg - better separation of
# logic and declaration, and simpler if you include description/version in a file.
Expand All @@ -126,7 +158,10 @@ def build_extension(self, ext):
description="Python bindings for libopenpivcore",
long_description="",
ext_modules=[ CMakeExtension("pyopenpivcore") ],
cmdclass={"build_ext": CMakeBuild},
cmdclass={
"build_ext": CMakeBuild,
"clean": CleanCommand
},
zip_safe=False,
extras_require={"test": ["pytest>=6.0"]},
python_requires=">=3.6",
Expand Down

0 comments on commit b36bcad

Please sign in to comment.