From 0d9d86383d35c336dbce2e3570db13c2319abc0c Mon Sep 17 00:00:00 2001 From: Luthaf Date: Thu, 4 May 2023 15:15:06 +0200 Subject: [PATCH] Modernize setup.py --- .github/workflows/tests.yml | 11 ++----- pyproject.toml | 28 ------------------ setup.py | 59 +++++++++++++------------------------ tox.ini | 26 ++++++++++++++++ 4 files changed, 49 insertions(+), 75 deletions(-) create mode 100644 tox.ini diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index bb28197..a665d18 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -26,19 +26,14 @@ jobs: uses: actions/setup-python@v4 with: python-version: ${{ matrix.python-version }} + - name: setup MSVC command prompt + uses: ilammy/msvc-dev-cmd@v1 - name: install dependencies run: | python -m pip install --upgrade pip pip install tox - - name: run tests (Unix) - if: matrix.os != 'windows-2019' + - name: run tests run: tox - - name: run tests (Windows) - if: matrix.os == 'windows-2019' - run: | - call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Auxiliary\Build\vcvars64.bat" - tox - shell: cmd - name: run extra checks run: | ./scripts/check-used-functions.py diff --git a/pyproject.toml b/pyproject.toml index 2ceb7d4..9116ed2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -2,38 +2,10 @@ requires = [ "setuptools >=44", "wheel >=0.36", - "ninja", "cmake", ] build-backend = "setuptools.build_meta" -[tool.tox] -legacy_tox_ini = """ -[tox] -skipsdist = True - -[testenv] -; this is required on windows to ensure the compiler finds the standard lib -; files (kernel32.lib, etc.) and headers -passenv = - LIB - INCLUDE - LIBPATH -setenv = - CHFL_PY_INTERNAL_CHEMFILES = 1 - -commands = - python setup.py install - coverage run --source=chemfiles -m unittest discover -s tests -p "*.py" - coverage xml -o .tox/coverage.xml - -deps = - ninja - cmake - coverage - numpy -""" - [tool.black] line-length = 88 exclude = ''' diff --git a/setup.py b/setup.py index 948a9b5..93563de 100644 --- a/setup.py +++ b/setup.py @@ -1,4 +1,3 @@ -# -*- coding=utf-8 -*- import os import re import site @@ -6,28 +5,11 @@ import sys from setuptools import Extension, setup +from setuptools.command.bdist_egg import bdist_egg +from setuptools.command.build_ext import build_ext +from setuptools.command.build_py import build_py from wheel.bdist_wheel import bdist_wheel -from distutils.command.build_py import build_py # type: ignore isort: skip -from distutils.command.build_ext import build_ext # type: ignore isort: skip -from distutils.command.install import install as distutils_install # type: ignore isort: skip - -try: - import cmake - - CMAKE_EXECUTABLE = os.path.join(cmake.CMAKE_BIN_DIR, "cmake") - -except ImportError: - CMAKE_EXECUTABLE = "cmake" - -try: - import ninja - - NINJA_EXECUTABLE = os.path.join(ninja.BIN_DIR, "ninja") -except ImportError: - NINJA_EXECUTABLE = "ninja" - - # workaround https://github.com/pypa/pip/issues/7953 site.ENABLE_USER_SITE = "--user" in sys.argv[1:] @@ -59,8 +41,6 @@ def run(self): pass cmake_options = [ - "-GNinja", - f"-DCMAKE_MAKE_PROGRAM={NINJA_EXECUTABLE}", f"-DCMAKE_INSTALL_PREFIX={install_dir}", "-DCMAKE_BUILD_TYPE=Release", "-DBUILD_SHARED_LIBS=ON", @@ -80,7 +60,7 @@ def run(self): cmake_options.append("-DCHFL_PY_INTERNAL_CHEMFILES=ON") subprocess.run( - [CMAKE_EXECUTABLE, source_dir, *cmake_options], + ["cmake", source_dir, *cmake_options], cwd=build_dir, check=True, ) @@ -95,21 +75,25 @@ def run(self): build_dir = os.path.join(ROOT, "build", "cmake-build") subprocess.run( - [CMAKE_EXECUTABLE, "--build", build_dir, "--target", "install"], + ["cmake", "--build", build_dir, "--target", "install"], check=True, ) -def _get_lib_ext(): - if sys.platform.startswith("win32"): - ext = ".dll" - elif sys.platform.startswith("darwin"): - ext = ".dylib" - elif sys.platform.startswith("linux"): - ext = ".so" - else: - raise Exception("Unknown operating system: %s" % sys.platform) - return ext +class bdist_egg_disabled(bdist_egg): + """Disabled version of bdist_egg + + Prevents setup.py install performing setuptools' default easy_install, + which it should never ever do. + """ + + def run(self): + sys.exit( + "Aborting implicit building of eggs. " + + "Use `pip install .` or `python setup.py bdist_wheel && pip " + + "uninstall chemfiles -y && pip install dist/chemfiles-*.whl` " + + "to install from source." + ) setup( @@ -123,10 +107,7 @@ def _get_lib_ext(): "build_py": cmake_configure, "build_ext": cmake_build, "bdist_wheel": universal_wheel, - # HACK: do not use the new setuptools install implementation, it tries - # to install the package with `easy_install`, which fails to resolve the - # freshly installed package and tries to load it from pypi. - "install": distutils_install, + "bdist_egg": bdist_egg if "bdist_egg" in sys.argv else bdist_egg_disabled, }, exclude_package_data={ "chemfiles": [ diff --git a/tox.ini b/tox.ini new file mode 100644 index 0000000..2ec8dfb --- /dev/null +++ b/tox.ini @@ -0,0 +1,26 @@ +[tox] +min_version = 4.0 +envlist = tests + +[testenv:build-chemfiles] +passenv = * +setenv = CHFL_PY_INTERNAL_CHEMFILES = 1 + +deps = + wheel + cmake + +commands = + pip wheel . --no-build-isolation --verbose --no-deps --check-build-dependencies --wheel-dir {envtmpdir}/dist + +[testenv:tests] +package = external +package_env = build-chemfiles + +commands = + coverage run --source=chemfiles -m unittest discover -s tests -p "*.py" + coverage xml -o .tox/coverage.xml + +deps = + coverage + numpy