1#!/usr/local/bin/python3.8
2# -*- coding: utf-8 -*-
3
4import io
5from itertools import chain
6import os
7import re
8import shutil
9import subprocess
10import sys
11import warnings
12
13from setuptools import setup
14
15
16pkg_name = 'pyodesys'
17url = 'https://github.com/bjodah/' + pkg_name
18license = 'BSD'
19
20RELEASE_VERSION = os.environ.get('%s_RELEASE_VERSION' % pkg_name.upper(), '')  # v*
21
22
23def _path_under_setup(*args):
24    return os.path.join(*args)
25
26release_py_path = _path_under_setup(pkg_name, '_release.py')
27
28if len(RELEASE_VERSION) > 0:
29    if RELEASE_VERSION[0] == 'v':
30        TAGGED_RELEASE = True
31        __version__ = RELEASE_VERSION[1:]
32    else:
33        raise ValueError("Ill formated version")
34else:
35    TAGGED_RELEASE = False
36    # read __version__ attribute from _release.py:
37    exec(open(release_py_path).read())
38    if __version__.endswith('git'):
39        try:
40            _git_version = subprocess.check_output(
41                ['git', 'describe', '--dirty']).rstrip().decode('utf-8').replace('-dirty', '.dirty')
42        except subprocess.CalledProcessError:
43            warnings.warn("A git-archive is being installed - version information incomplete.")
44        else:
45            if 'develop' not in sys.argv:
46                warnings.warn("Using git to derive version: dev-branches may compete.")
47                _ver_tmplt = r'\1.post\2' if os.environ.get('CONDA_BUILD', '0') == '1' else r'\1.post\2+\3'
48                __version__ = re.sub('v([0-9.]+)-(\d+)-(\S+)', _ver_tmplt, _git_version)  # .dev < '' < .post
49
50
51classifiers = [
52    "Development Status :: 4 - Beta",
53    'License :: OSI Approved :: BSD License',
54    'Operating System :: OS Independent',
55    'Topic :: Scientific/Engineering',
56    'Topic :: Scientific/Engineering :: Mathematics',
57    'Programming Language :: Python :: 3.7',
58    'Programming Language :: Python :: 3.8',
59]
60
61submodules = [
62    'pyodesys.native',
63]
64
65tests = [
66    'pyodesys.tests',
67    'pyodesys.native.tests',
68]
69
70with open(_path_under_setup(pkg_name, '__init__.py'), 'rt') as f:
71    short_description = f.read().split('"""')[1].split('\n')[1]
72if not 10 < len(short_description) < 255:
73    warnings.warn("Short description from __init__.py proably not read correctly")
74long_descr = io.open(_path_under_setup('README.rst'), encoding='utf-8').read()
75if not len(long_descr) > 100:
76    warnings.warn("Long description from README.rst probably not read correctly.")
77_author, _author_email = open(_path_under_setup('AUTHORS'), 'rt').readline().split('<')
78
79extras_req = {
80    'integrators': ['pyodeint>=0.10.3', 'pycvodes>=0.13.1', 'pygslodeiv2>=0.9.3'],
81    'native': ['pycompilation>=0.4.9', 'pycodeexport>=0.1.2', 'appdirs'],
82    'docs': ['Sphinx', 'sphinx_rtd_theme', 'numpydoc'],
83    'testing': ['pytest', 'pytest-cov', 'pytest-flakes', 'pytest-pep8', 'rstcheck']
84}
85extras_req['all'] = list(chain(extras_req.values()))
86
87setup_kwargs = dict(
88    name=pkg_name,
89    version=__version__,
90    description=short_description,
91    long_description=long_descr,
92    classifiers=classifiers,
93    author=_author,
94    author_email=_author_email.split('>')[0].strip(),
95    url=url,
96    license=license,
97    packages=[pkg_name] + submodules + tests,
98    include_package_data=True,
99    install_requires=['numpy>=1.16.4', 'scipy>=1.2.3', 'sym>=0.3.4',
100                      'sympy>=1.5.1', 'matplotlib>=2.2.5', 'notebook' + ('>=5.7.8' if 'CI' in os.environ else '')],
101    tests_require=['pytest>=5.4.1'],
102    extras_require=extras_req,
103    python_requires='>=3.7',
104)
105
106if __name__ == '__main__':
107    try:
108        if TAGGED_RELEASE:
109            # Same commit should generate different sdist
110            # depending on tagged version (set $PYODESYS_RELEASE_VERSION)
111            # e.g.:  $ PYODESYS_RELEASE_VERSION=v1.2.3 python setup.py sdist
112            # this will ensure source distributions contain the correct version
113            shutil.move(release_py_path, release_py_path+'__temp__')
114            open(release_py_path, 'wt').write(
115                "__version__ = '{}'\n".format(__version__))
116        setup(**setup_kwargs)
117    finally:
118        if TAGGED_RELEASE:
119            shutil.move(release_py_path+'__temp__', release_py_path)
120