Skip to content

Commit b03b1b9

Browse files
tjtgbjlittle
authored andcommitted
Fix installation via setup.py and pip (take 2) (#29)
PEP 517/518 support for setup.py
1 parent 0eba3a8 commit b03b1b9

File tree

5 files changed

+70
-30
lines changed

5 files changed

+70
-30
lines changed

MANIFEST.in

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
# Include the license file
21
include LICENSE
3-
2+
include README.md
3+
include pyproject.toml

pyproject.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[build-system]
2+
# Defined by PEP 518
3+
requires = ["setuptools>=40.8.0", "wheel", "numpy", "Cython"]
4+
# Defined by PEP 517
5+
build-backend = "setuptools.build_meta:__legacy__"

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
setuptools
1+
setuptools>=40.8.0
22
cython
33
numpy

setup.cfg

Lines changed: 0 additions & 5 deletions
This file was deleted.

setup.py

Lines changed: 62 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,85 @@
11
from __future__ import absolute_import, division, print_function
22

3-
from setuptools import setup, find_packages, Extension
3+
import glob
44
import os
5+
import sys
6+
from setuptools import setup, find_packages, Extension
7+
from setuptools.command.build_ext import build_ext as _build_ext
58

6-
import numpy as np
9+
# Python 2 has a different name for builtins.
10+
try:
11+
import builtins
12+
except ImportError:
13+
import __builtin__ as builtins
714

8-
from Cython.Build import cythonize
15+
try:
16+
from Cython.Build import cythonize
17+
except ImportError:
18+
print('Warning: Cython is not available - '
19+
'will be unable to build stratify extensions')
20+
cythonize = None
921

22+
PACKAGE_NAME = 'stratify'
23+
PACKAGE_DIR = os.path.abspath(os.path.dirname(__file__))
24+
CMDS_NOCYTHONIZE = ['clean', 'sdist']
1025

11-
NAME = 'stratify'
12-
DIR = os.path.abspath(os.path.dirname(__file__))
1326

14-
extension_kwargs = {'include_dirs': [np.get_include()]}
15-
cython_coverage_enabled = os.environ.get('CYTHON_COVERAGE', None)
16-
if cython_coverage_enabled:
17-
extension_kwargs.update({'define_macros': [('CYTHON_TRACE_NOGIL', '1')]})
27+
class NumpyBuildExt(_build_ext):
28+
# Delay numpy import so that setup.py can be run without numpy already
29+
# being installed.
30+
def finalize_options(self):
31+
_build_ext.finalize_options(self)
32+
builtins.__NUMPY_SETUP__ = False
33+
import numpy
34+
self.include_dirs.append(numpy.get_include())
1835

19-
extensions = [Extension('{}._vinterp'.format(NAME),
20-
[os.path.join(NAME, '_vinterp.pyx')],
21-
**extension_kwargs),
22-
Extension('{}._conservative'.format(NAME),
23-
[os.path.join(NAME, '_conservative.pyx')],
24-
**extension_kwargs)]
2536

2637
def extract_version():
2738
version = None
28-
fname = os.path.join(DIR, NAME, '__init__.py')
39+
fname = os.path.join(PACKAGE_DIR, PACKAGE_NAME, '__init__.py')
2940
with open(fname) as fd:
3041
for line in fd:
31-
if (line.startswith('__version__')):
42+
if line.startswith('__version__'):
3243
_, version = line.split('=')
3344
version = version.strip()[1:-1] # Remove quotations
3445
break
3546
return version
3647

3748

49+
# Python 2 is not supported by numpy as of version 1.17
50+
# but pip will attempt to install/use newer Python 3-only numpy versions.
51+
numpy_req = 'numpy<1.17' if sys.version_info.major < 3 else 'numpy'
52+
requirements = ['setuptools>=40.8.0', numpy_req, 'Cython']
53+
54+
extension_kwargs = {}
55+
cython_directives = {'binding': True}
56+
cython_coverage_enabled = os.environ.get('CYTHON_COVERAGE', None)
57+
if cythonize and cython_coverage_enabled:
58+
extension_kwargs.update({'define_macros': [('CYTHON_TRACE_NOGIL', '1')]})
59+
cython_directives.update({'linetrace': True})
60+
61+
extensions = []
62+
for source_file in glob.glob('{}/*.pyx'.format(PACKAGE_NAME)):
63+
source_file_nosuf, _ = os.path.splitext(os.path.basename(source_file))
64+
extensions.append(
65+
Extension('{}.{}'.format(PACKAGE_NAME, source_file_nosuf),
66+
sources=[source_file], **extension_kwargs))
67+
68+
if cythonize and not any([arg in CMDS_NOCYTHONIZE for arg in sys.argv]):
69+
extensions = cythonize(extensions, compiler_directives=cython_directives)
70+
71+
3872
setup_args = dict(
39-
name=NAME,
73+
name=PACKAGE_NAME,
4074
description=('Vectorized interpolators that are especially useful for '
4175
'Nd vertical interpolation/stratification of atmospheric '
4276
'and oceanographic datasets'),
77+
author='UK Met Office',
78+
author_email='[email protected]',
79+
url='https://github.com/SciTools-incubator/python-stratify',
4380
version=extract_version(),
44-
ext_modules=cythonize(extensions, compiler_directives={'linetrace': True,
45-
'binding': True}),
81+
cmdclass={'build_ext': NumpyBuildExt},
82+
ext_modules=extensions,
4683
packages=find_packages(),
4784
classifiers=[
4885
'Development Status :: 3 - Alpha',
@@ -56,13 +93,16 @@ def extract_version():
5693
'Programming Language :: Python :: 2',
5794
'Programming Language :: Python :: 2.7',
5895
'Programming Language :: Python :: 3',
59-
'Programming Language :: Python :: 3.4',
6096
'Programming Language :: Python :: 3.5',
97+
'Programming Language :: Python :: 3.6',
98+
'Programming Language :: Python :: 3.7',
6199
'Topic :: Scientific/Engineering',
62100
'Topic :: Scientific/Engineering :: GIS',
63101
],
64102
extras_require={'test': ['nose']},
65-
test_suite='{}.tests'.format(NAME),
103+
setup_requires=requirements,
104+
install_requires=requirements,
105+
test_suite='{}.tests'.format(PACKAGE_NAME),
66106
zip_safe=False,
67107
)
68108

0 commit comments

Comments
 (0)