|
8 | 8 | if sys.version_info < (2, 6):
|
9 | 9 | sys.exit("root_numpy only supports python 2.6 and above")
|
10 | 10 |
|
11 |
| -# check that NumPy is installed |
12 |
| -try: |
13 |
| - import numpy |
14 |
| -except ImportError: |
15 |
| - raise RuntimeError( |
16 |
| - "numpy cannot be imported. numpy must be installed " |
17 |
| - "prior to installing root_numpy") |
| 11 | +if sys.version_info[0] < 3: |
| 12 | + import __builtin__ as builtins |
| 13 | +else: |
| 14 | + import builtins |
18 | 15 |
|
19 | 16 | try:
|
20 | 17 | # try to use setuptools if installed
|
21 | 18 | from pkg_resources import parse_version, get_distribution
|
22 |
| - from setuptools import setup, Extension |
| 19 | + |
23 | 20 | if get_distribution('setuptools').parsed_version < parse_version('0.7'):
|
24 | 21 | # before merge with distribute
|
25 | 22 | raise ImportError
|
| 23 | + |
| 24 | + from setuptools import setup, Extension |
| 25 | + from setuptools.command.build_ext import build_ext as _build_ext |
26 | 26 | except ImportError:
|
27 | 27 | # fall back on distutils
|
28 | 28 | from distutils.core import setup, Extension
|
| 29 | + from distutils.command.build_ext import build_ext as _build_ext |
29 | 30 |
|
30 | 31 | import os
|
31 | 32 | from glob import glob
|
|
66 | 67 | "root-config is not in PATH and ROOTSYS is not set. "
|
67 | 68 | "Is ROOT installed correctly?")
|
68 | 69 |
|
| 70 | +class build_ext(_build_ext): |
| 71 | + def finalize_options(self): |
| 72 | + _build_ext.finalize_options(self) |
| 73 | + # prevent numpy from thinking it is still in its setup process: |
| 74 | + try: |
| 75 | + del builtins.__NUMPY_SETUP__ |
| 76 | + except AttributeError: |
| 77 | + pass |
| 78 | + import numpy |
| 79 | + self.include_dirs.append(numpy.get_include()) |
| 80 | + |
| 81 | + config = { |
| 82 | + 'ROOT_version': str(root_version), |
| 83 | + 'numpy_version': numpy.__version__, |
| 84 | + } |
| 85 | + |
| 86 | + # write config.json |
| 87 | + import json |
| 88 | + with open('root_numpy/config.json', 'w') as config_file: |
| 89 | + json.dump(config, config_file, indent=4) |
| 90 | + |
69 | 91 | librootnumpy = Extension(
|
70 | 92 | 'root_numpy._librootnumpy',
|
71 | 93 | sources=[
|
|
74 | 96 | depends=glob('root_numpy/src/*.h'),
|
75 | 97 | language='c++',
|
76 | 98 | include_dirs=[
|
77 |
| - numpy.get_include(), |
78 | 99 | 'root_numpy/src',
|
79 | 100 | ],
|
80 | 101 | extra_compile_args=root_cflags + [
|
|
101 | 122 | ],
|
102 | 123 | language='c++',
|
103 | 124 | include_dirs=[
|
104 |
| - numpy.get_include(), |
105 | 125 | 'root_numpy/src',
|
106 | 126 | 'root_numpy/tmva/src',
|
107 | 127 | ],
|
|
138 | 158 | if install:
|
139 | 159 | print(__doc__)
|
140 | 160 |
|
141 |
| - config = { |
142 |
| - 'ROOT_version': str(root_version), |
143 |
| - 'numpy_version': numpy.__version__, |
144 |
| - } |
145 |
| - |
146 |
| - # write config.json |
147 |
| - import json |
148 |
| - with open('root_numpy/config.json', 'w') as config_file: |
149 |
| - json.dump(config, config_file, indent=4) |
| 161 | +# Figure out whether to add ``*_requires = ['numpy']``. |
| 162 | +# We don't want to do that unconditionally, because we risk updating |
| 163 | +# an installed numpy which fails too often. Just if it's not installed, we |
| 164 | +# may give it a try. |
| 165 | +try: |
| 166 | + import numpy |
| 167 | +except ImportError: |
| 168 | + build_requires = ['numpy'] |
| 169 | +else: |
| 170 | + build_requires = [] |
150 | 171 |
|
151 | 172 | setup(
|
152 | 173 | name='root_numpy',
|
|
164 | 185 | 'root_numpy': ['testdata/*.root', 'config.json'],
|
165 | 186 | },
|
166 | 187 | ext_modules=ext_modules,
|
| 188 | + cmdclass={'build_ext': build_ext}, |
| 189 | + setup_requires=build_requires, |
| 190 | + install_requires=build_requires, |
| 191 | + extras_require={ |
| 192 | + 'with-numpy': ('numpy',), |
| 193 | + }, |
167 | 194 | zip_safe=False,
|
168 | 195 | classifiers=[
|
169 | 196 | 'Intended Audience :: Science/Research',
|
|
0 commit comments