Skip to content
This repository was archived by the owner on Jan 7, 2023. It is now read-only.

Commit 8526f80

Browse files
authored
Merge pull request #293 from ndawe/master
setup.py improvements
2 parents 0a2c8ff + c85f9aa commit 8526f80

File tree

2 files changed

+117
-94
lines changed

2 files changed

+117
-94
lines changed

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,13 @@ install-user: clean
7474
@$(PYTHON) setup.py install --user
7575

7676
sdist: clean
77-
@$(PYTHON) setup.py sdist --release
77+
@$(PYTHON) setup.py sdist
7878

7979
register:
80-
@$(PYTHON) setup.py register --release
80+
@$(PYTHON) setup.py register
8181

8282
upload: clean
83-
@$(PYTHON) setup.py sdist upload --release
83+
@$(PYTHON) setup.py sdist upload
8484

8585
valgrind: inplace
8686
valgrind --log-file=valgrind.log --tool=memcheck --leak-check=full \

setup.py

Lines changed: 114 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import sys
66

7-
# check Python version
7+
# Check Python version
88
if sys.version_info < (2, 6):
99
sys.exit("root_numpy only supports python 2.6 and above")
1010

@@ -14,22 +14,28 @@
1414
import builtins
1515

1616
try:
17-
# try to use setuptools if installed
17+
# Try to use setuptools if installed
18+
from setuptools import setup, Extension
1819
from pkg_resources import parse_version, get_distribution
1920

2021
if get_distribution('setuptools').parsed_version < parse_version('0.7'):
21-
# before merge with distribute
22+
# setuptools is too old (before merge with distribute)
2223
raise ImportError
2324

24-
from setuptools import setup, Extension
2525
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+
2629
except ImportError:
27-
# fall back on distutils
30+
# Use distutils instead
2831
from distutils.core import setup, Extension
2932
from distutils.command.build_ext import build_ext as _build_ext
33+
from distutils.command.install import install as _install
34+
use_setuptools = False
3035

3136
import os
3237
from glob import glob
38+
from contextlib import contextmanager
3339

3440
# Prevent setup from trying to create hard links
3541
# which are not allowed on AFS between directories.
@@ -67,27 +73,60 @@
6773
"root-config is not in PATH and ROOTSYS is not set. "
6874
"Is ROOT installed correctly?")
6975

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+
7096
class build_ext(_build_ext):
7197
def finalize_options(self):
7298
_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
74100
try:
75101
del builtins.__NUMPY_SETUP__
76102
except AttributeError:
77103
pass
78104
import numpy
79105
self.include_dirs.append(numpy.get_include())
80106

107+
108+
class install(_install):
109+
def run(self):
110+
print(__doc__)
111+
import numpy
112+
81113
config = {
82114
'ROOT_version': str(root_version),
83115
'numpy_version': numpy.__version__,
84116
}
85117

86-
# write config.json
118+
# Write version info in config.json
119+
print("writing 'root_numpy/config.json'")
87120
import json
88121
with open('root_numpy/config.json', 'w') as config_file:
89122
json.dump(config, config_file, indent=4)
90123

124+
_install.run(self)
125+
126+
print("removing 'root_numpy/config.json'")
127+
os.remove('root_numpy/config.json')
128+
129+
91130
librootnumpy = Extension(
92131
'root_numpy._librootnumpy',
93132
sources=[
@@ -134,90 +173,74 @@ def finalize_options(self):
134173
ext_modules.append(librootnumpy_tmva)
135174
packages.append('root_numpy.tmva')
136175

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']
145184
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-
author_email='[email protected]',
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+
author_email='[email protected]',
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+
)
217242

218-
if release:
219-
# revert root_numpy/info.py
220-
shutil.move('info.tmp', 'root_numpy/info.py')
221243

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

Comments
 (0)