Skip to content

Commit

Permalink
Improve setup.py to pip install numpy unless it's not yet installed.
Browse files Browse the repository at this point in the history
  • Loading branch information
atztogo committed Apr 18, 2020
1 parent cb81b5d commit 54983ce
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 28 deletions.
2 changes: 2 additions & 0 deletions python/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[build-system]
requires = ["setuptools", "wheel", "numpy"]
46 changes: 18 additions & 28 deletions python/setup.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,24 @@
import os
import sys
import sysconfig


setup_type = sys.argv[1]

try:
from setuptools import setup, Extension
from setuptools.command.build_ext import build_ext as _build_ext
from setuptools.command.build_ext import build_ext
use_setuptools = True
print("setuptools is used.")

class build_ext(_build_ext):
def finalize_options(self):
_build_ext.finalize_options(self)
# Prevent numpy from thinking it is still in its setup process:
if sys.version_info[0] >= 3:
import builtins
if hasattr(builtins, '__NUMPY_SETUP__'):
del builtins.__NUMPY_SETUP__
import importlib
import numpy
importlib.reload(numpy)

else:
import __builtin__
if hasattr(__builtin__, '__NUMPY_SETUP__'):
del __builtin__.__NUMPY_SETUP__
import imp
import numpy
imp.reload(numpy)
# Trick to pip install numpy when it's not installed.
# Reference: https://stackoverflow.com/questions/2379898/
class CustomBuildExtCommand(build_ext):
def run(self):
import numpy
self.include_dirs.append(numpy.get_include())
print("setuptools is used.")
build_ext.run(self)

except ImportError:
from distutils.core import setup, Extension
use_setuptools = False
Expand All @@ -42,7 +32,6 @@ def finalize_options(self):
sys.exit(1)

# Workaround Python issue 21121
import sysconfig
config_var = sysconfig.get_config_var("CFLAGS")
if (config_var is not None and
"-Werror=declaration-after-statement" in config_var):
Expand Down Expand Up @@ -85,7 +74,7 @@ def finalize_options(self):

extra_compile_args = []
if setup_type == 'test':
extra_compile_args.append("-UNDEBUG")
extra_compile_args.append("-UNDEBUG")
extra_link_args = []
define_macros = []

Expand Down Expand Up @@ -121,7 +110,8 @@ def finalize_options(self):
break
except ValueError:
nanoversion = 0
version_nums.append(nanoversion)
if nanoversion != 0:
version_nums.append(nanoversion)

if None in version_nums:
print("Failed to get version number in setup.py.")
Expand All @@ -133,7 +123,7 @@ def finalize_options(self):
if use_setuptools:
setup(name='spglib',
version=version,
cmdclass={'build_ext': build_ext},
cmdclass={'build_ext': CustomBuildExtCommand},
setup_requires=['numpy', 'setuptools>=18.0'],
license='BSD-3-Clause',
description='This is the spglib module.',
Expand All @@ -143,12 +133,12 @@ def finalize_options(self):
author_email='atz.togo@gmail.com',
url='http://atztogo.github.io/spglib/',
packages=['spglib'],
install_requires=['numpy'],
install_requires=['numpy', ],
provides=['spglib'],
platforms=['all'],
ext_modules=[extension],
test_suite='nose.collector',
tests_require=['nose'])
tests_require=['nose', 'pyyaml'])
else:
setup(name='spglib',
version=version,
Expand All @@ -165,4 +155,4 @@ def finalize_options(self):
platforms=['all'],
ext_modules=[extension],
test_suite='nose.collector',
tests_require=['nose'])
tests_require=['nose', 'pyyaml'])

0 comments on commit 54983ce

Please sign in to comment.