Skip to content

Commit

Permalink
Modernize setup.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Luthaf committed May 7, 2023
1 parent 5c2203b commit 0d9d863
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 75 deletions.
11 changes: 3 additions & 8 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 0 additions & 28 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '''
Expand Down
59 changes: 20 additions & 39 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,15 @@
# -*- coding=utf-8 -*-
import os
import re
import site
import subprocess
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:]

Expand Down Expand Up @@ -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",
Expand All @@ -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,
)
Expand All @@ -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(
Expand All @@ -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": [
Expand Down
26 changes: 26 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 0d9d863

Please sign in to comment.