Skip to content

Commit 227f56d

Browse files
committed
RF: refactor setup.py to use info variable reader
Use function to read variables from `info.py` file into object, making namespace clearer.
1 parent b8017c9 commit 227f56d

File tree

2 files changed

+51
-21
lines changed

2 files changed

+51
-21
lines changed

nisext/sexts.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,3 +265,32 @@ def run(self):
265265
continue
266266
with open(bat_file, 'wt') as fobj:
267267
fobj.write(bat_contents)
268+
269+
270+
class Bunch(object):
271+
def __init__(self, vars):
272+
for key, name in vars.items():
273+
if key.startswith('__'):
274+
continue
275+
self.__dict__[key] = name
276+
277+
278+
def read_vars_from(ver_file):
279+
""" Read variables from Python text file
280+
281+
Parameters
282+
----------
283+
ver_file : str
284+
Filename of file to read
285+
286+
Returns
287+
-------
288+
info_vars : Bunch instance
289+
Bunch object where variables read from `ver_file` appear as
290+
attributes
291+
"""
292+
# Use exec for compabibility with Python 3
293+
ns = {}
294+
with open(ver_file, 'rt') as fobj:
295+
exec(fobj.read(), ns)
296+
return Bunch(ns)

setup.py

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@
2929
from distutils.core import setup
3030

3131
# Commit hash writing, and dependency checking
32-
from nisext.sexts import get_comrec_build, package_check, install_scripts_bat
32+
from nisext.sexts import (get_comrec_build, package_check, install_scripts_bat,
33+
read_vars_from)
3334
cmdclass = {'build_py': get_comrec_build('nibabel'),
3435
'install_scripts': install_scripts_bat}
3536

36-
# Get version and release info, which is all stored in nibabel/info.py
37-
ver_file = os.path.join('nibabel', 'info.py')
38-
exec(open(ver_file).read())
37+
# Get project related strings.
38+
INFO = read_vars_from(pjoin('nibabel', 'info.py'))
3939

4040
# Prepare setuptools args
4141
if 'setuptools' in sys.modules:
@@ -53,31 +53,32 @@
5353
pkg_chk = package_check
5454

5555
# Do dependency checking
56-
pkg_chk('numpy', NUMPY_MIN_VERSION)
56+
pkg_chk('numpy', INFO.NUMPY_MIN_VERSION)
57+
pkg_chk('six', INFO.SIX_MIN_VERSION)
5758
custom_pydicom_messages = {'missing opt': 'Missing optional package "%s"'
5859
' provided by package "pydicom"'
5960
}
6061
pkg_chk('dicom',
61-
PYDICOM_MIN_VERSION,
62+
INFO.PYDICOM_MIN_VERSION,
6263
optional='dicom',
6364
messages = custom_pydicom_messages)
6465

6566
def main(**extra_args):
66-
setup(name=NAME,
67-
maintainer=MAINTAINER,
68-
maintainer_email=MAINTAINER_EMAIL,
69-
description=DESCRIPTION,
70-
long_description=LONG_DESCRIPTION,
71-
url=URL,
72-
download_url=DOWNLOAD_URL,
73-
license=LICENSE,
74-
classifiers=CLASSIFIERS,
75-
author=AUTHOR,
76-
author_email=AUTHOR_EMAIL,
77-
platforms=PLATFORMS,
78-
version=VERSION,
79-
requires=REQUIRES,
80-
provides=PROVIDES,
67+
setup(name=INFO.NAME,
68+
maintainer=INFO.MAINTAINER,
69+
maintainer_email=INFO.MAINTAINER_EMAIL,
70+
description=INFO.DESCRIPTION,
71+
long_description=INFO.LONG_DESCRIPTION,
72+
url=INFO.URL,
73+
download_url=INFO.DOWNLOAD_URL,
74+
license=INFO.LICENSE,
75+
classifiers=INFO.CLASSIFIERS,
76+
author=INFO.AUTHOR,
77+
author_email=INFO.AUTHOR_EMAIL,
78+
platforms=INFO.PLATFORMS,
79+
version=INFO.VERSION,
80+
requires=INFO.REQUIRES,
81+
provides=INFO.PROVIDES,
8182
packages = ['nibabel',
8283
'nibabel.externals',
8384
'nibabel.externals.tests',

0 commit comments

Comments
 (0)