|
4 | 4 |
|
5 | 5 | import sys
|
6 | 6 |
|
7 |
| -# check Python version |
| 7 | +# Check Python version |
8 | 8 | if sys.version_info < (2, 6):
|
9 | 9 | sys.exit("root_numpy only supports python 2.6 and above")
|
10 | 10 |
|
|
14 | 14 | import builtins
|
15 | 15 |
|
16 | 16 | try:
|
17 |
| - # try to use setuptools if installed |
| 17 | + # Try to use setuptools if installed |
| 18 | + from setuptools import setup, Extension |
18 | 19 | from pkg_resources import parse_version, get_distribution
|
19 | 20 |
|
20 | 21 | if get_distribution('setuptools').parsed_version < parse_version('0.7'):
|
21 |
| - # before merge with distribute |
| 22 | + # setuptools is too old (before merge with distribute) |
22 | 23 | raise ImportError
|
23 | 24 |
|
24 |
| - from setuptools import setup, Extension |
25 | 25 | from setuptools.command.build_ext import build_ext as _build_ext
|
| 26 | + from setuptools.command.install import install as _install |
| 27 | + use_setuptools = True |
| 28 | + |
26 | 29 | except ImportError:
|
27 |
| - # fall back on distutils |
| 30 | + # Use distutils instead |
28 | 31 | from distutils.core import setup, Extension
|
29 | 32 | from distutils.command.build_ext import build_ext as _build_ext
|
| 33 | + from distutils.command.install import install as _install |
| 34 | + use_setuptools = False |
30 | 35 |
|
31 | 36 | import os
|
32 | 37 | from glob import glob
|
| 38 | +from contextlib import contextmanager |
33 | 39 |
|
34 | 40 | # Prevent setup from trying to create hard links
|
35 | 41 | # which are not allowed on AFS between directories.
|
|
67 | 73 | "root-config is not in PATH and ROOTSYS is not set. "
|
68 | 74 | "Is ROOT installed correctly?")
|
69 | 75 |
|
| 76 | +@contextmanager |
| 77 | +def version(release=False): |
| 78 | + if not release: |
| 79 | + yield |
| 80 | + else: |
| 81 | + # Remove dev from version in root_numpy/info.py |
| 82 | + import shutil |
| 83 | + print("writing release version in 'root_numpy/info.py'") |
| 84 | + shutil.move('root_numpy/info.py', 'info.tmp') |
| 85 | + dev_info = ''.join(open('info.tmp', 'r').readlines()) |
| 86 | + open('root_numpy/info.py', 'w').write( |
| 87 | + dev_info.replace('.dev0', '')) |
| 88 | + try: |
| 89 | + yield |
| 90 | + finally: |
| 91 | + # Revert root_numpy/info.py |
| 92 | + print("restoring dev version in 'root_numpy/info.py'") |
| 93 | + shutil.move('info.tmp', 'root_numpy/info.py') |
| 94 | + |
| 95 | + |
70 | 96 | class build_ext(_build_ext):
|
71 | 97 | def finalize_options(self):
|
72 | 98 | _build_ext.finalize_options(self)
|
73 |
| - # prevent numpy from thinking it is still in its setup process: |
| 99 | + # Prevent numpy from thinking it is still in its setup process |
74 | 100 | try:
|
75 | 101 | del builtins.__NUMPY_SETUP__
|
76 | 102 | except AttributeError:
|
77 | 103 | pass
|
78 | 104 | import numpy
|
79 | 105 | self.include_dirs.append(numpy.get_include())
|
80 | 106 |
|
| 107 | + |
| 108 | +class install(_install): |
| 109 | + def run(self): |
| 110 | + print(__doc__) |
| 111 | + import numpy |
| 112 | + |
81 | 113 | config = {
|
82 | 114 | 'ROOT_version': str(root_version),
|
83 | 115 | 'numpy_version': numpy.__version__,
|
84 | 116 | }
|
85 | 117 |
|
86 |
| - # write config.json |
| 118 | + # Write version info in config.json |
| 119 | + print("writing 'root_numpy/config.json'") |
87 | 120 | import json
|
88 | 121 | with open('root_numpy/config.json', 'w') as config_file:
|
89 | 122 | json.dump(config, config_file, indent=4)
|
90 | 123 |
|
| 124 | + _install.run(self) |
| 125 | + |
| 126 | + print("removing 'root_numpy/config.json'") |
| 127 | + os.remove('root_numpy/config.json') |
| 128 | + |
| 129 | + |
91 | 130 | librootnumpy = Extension(
|
92 | 131 | 'root_numpy._librootnumpy',
|
93 | 132 | sources=[
|
@@ -134,90 +173,74 @@ def finalize_options(self):
|
134 | 173 | ext_modules.append(librootnumpy_tmva)
|
135 | 174 | packages.append('root_numpy.tmva')
|
136 | 175 |
|
137 |
| -# check for custom args |
138 |
| -filtered_args = [] |
139 |
| -release = False |
140 |
| -install = 'install' in sys.argv |
141 |
| -for arg in sys.argv: |
142 |
| - if arg == '--release': |
143 |
| - # --release sets the version number before installing |
144 |
| - release = True |
| 176 | + |
| 177 | +def setup_package(): |
| 178 | + # Only add numpy to *_requires lists if not already installed to prevent |
| 179 | + # pip from trying to upgrade an existing numpy and failing. |
| 180 | + try: |
| 181 | + import numpy |
| 182 | + except ImportError: |
| 183 | + build_requires = ['numpy'] |
145 | 184 | else:
|
146 |
| - filtered_args.append(arg) |
147 |
| -sys.argv = filtered_args |
148 |
| - |
149 |
| -if release: |
150 |
| - # remove dev from version in root_numpy/info.py |
151 |
| - import shutil |
152 |
| - shutil.move('root_numpy/info.py', 'info.tmp') |
153 |
| - dev_info = ''.join(open('info.tmp', 'r').readlines()) |
154 |
| - open('root_numpy/info.py', 'w').write( |
155 |
| - dev_info.replace('.dev0', '')) |
156 |
| - |
157 |
| -exec(open('root_numpy/info.py').read()) |
158 |
| -if install: |
159 |
| - print(__doc__) |
160 |
| - |
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 = [] |
171 |
| - |
172 |
| -setup( |
173 |
| - name='root_numpy', |
174 |
| - version=__version__, |
175 |
| - description='The interface between ROOT and NumPy', |
176 |
| - long_description=''.join(open('README.rst').readlines()[7:-4]), |
177 |
| - author='the rootpy developers', |
178 |
| - |
179 |
| - license='MIT', |
180 |
| - url='http://rootpy.github.io/root_numpy', |
181 |
| - download_url='http://pypi.python.org/packages/source/r/' |
182 |
| - 'root_numpy/root_numpy-{0}.tar.gz'.format(__version__), |
183 |
| - packages=packages, |
184 |
| - package_data={ |
185 |
| - 'root_numpy': ['testdata/*.root', 'config.json'], |
186 |
| - }, |
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 |
| - }, |
194 |
| - zip_safe=False, |
195 |
| - classifiers=[ |
196 |
| - 'Intended Audience :: Science/Research', |
197 |
| - 'Intended Audience :: Developers', |
198 |
| - 'Topic :: Software Development', |
199 |
| - 'Topic :: Scientific/Engineering', |
200 |
| - 'Topic :: Utilities', |
201 |
| - 'Operating System :: POSIX', |
202 |
| - 'Operating System :: Unix', |
203 |
| - 'Operating System :: MacOS', |
204 |
| - 'License :: OSI Approved :: MIT License', |
205 |
| - 'Programming Language :: Python', |
206 |
| - 'Programming Language :: Python :: 2', |
207 |
| - 'Programming Language :: Python :: 2.6', |
208 |
| - 'Programming Language :: Python :: 2.7', |
209 |
| - 'Programming Language :: Python :: 3', |
210 |
| - 'Programming Language :: Python :: 3.3', |
211 |
| - 'Programming Language :: Python :: 3.4', |
212 |
| - 'Programming Language :: C++', |
213 |
| - 'Programming Language :: Cython', |
214 |
| - 'Development Status :: 5 - Production/Stable', |
215 |
| - ] |
216 |
| -) |
| 185 | + build_requires = [] |
| 186 | + |
| 187 | + if use_setuptools: |
| 188 | + setuptools_options = dict( |
| 189 | + setup_requires=build_requires, |
| 190 | + install_requires=build_requires, |
| 191 | + extras_require={ |
| 192 | + 'with-numpy': ('numpy',), |
| 193 | + }, |
| 194 | + zip_safe=False, |
| 195 | + ) |
| 196 | + else: |
| 197 | + setuptools_options = dict() |
| 198 | + |
| 199 | + setup( |
| 200 | + name='root_numpy', |
| 201 | + version=__version__, |
| 202 | + description='The interface between ROOT and NumPy', |
| 203 | + long_description=''.join(open('README.rst').readlines()[7:-4]), |
| 204 | + author='the rootpy developers', |
| 205 | + |
| 206 | + license='MIT', |
| 207 | + url='http://rootpy.github.io/root_numpy', |
| 208 | + download_url='http://pypi.python.org/packages/source/r/' |
| 209 | + 'root_numpy/root_numpy-{0}.tar.gz'.format(__version__), |
| 210 | + packages=packages, |
| 211 | + package_data={ |
| 212 | + 'root_numpy': ['testdata/*.root', 'config.json'], |
| 213 | + }, |
| 214 | + ext_modules=ext_modules, |
| 215 | + cmdclass={ |
| 216 | + 'build_ext': build_ext, |
| 217 | + 'install': install, |
| 218 | + }, |
| 219 | + classifiers=[ |
| 220 | + 'Intended Audience :: Science/Research', |
| 221 | + 'Intended Audience :: Developers', |
| 222 | + 'Topic :: Software Development', |
| 223 | + 'Topic :: Scientific/Engineering', |
| 224 | + 'Topic :: Utilities', |
| 225 | + 'Operating System :: POSIX', |
| 226 | + 'Operating System :: Unix', |
| 227 | + 'Operating System :: MacOS', |
| 228 | + 'License :: OSI Approved :: MIT License', |
| 229 | + 'Programming Language :: Python', |
| 230 | + 'Programming Language :: Python :: 2', |
| 231 | + 'Programming Language :: Python :: 2.6', |
| 232 | + 'Programming Language :: Python :: 2.7', |
| 233 | + 'Programming Language :: Python :: 3', |
| 234 | + 'Programming Language :: Python :: 3.3', |
| 235 | + 'Programming Language :: Python :: 3.4', |
| 236 | + 'Programming Language :: C++', |
| 237 | + 'Programming Language :: Cython', |
| 238 | + 'Development Status :: 5 - Production/Stable', |
| 239 | + ], |
| 240 | + **setuptools_options |
| 241 | + ) |
217 | 242 |
|
218 |
| -if release: |
219 |
| - # revert root_numpy/info.py |
220 |
| - shutil.move('info.tmp', 'root_numpy/info.py') |
221 | 243 |
|
222 |
| -if install: |
223 |
| - os.remove('root_numpy/config.json') |
| 244 | +with version(release=set(['sdist', 'register']).intersection(sys.argv[1:])): |
| 245 | + exec(open('root_numpy/info.py').read()) |
| 246 | + setup_package() |
0 commit comments