1import os
2from setuptools import setup, Extension
3import sys
4import platform
5import warnings
6import codecs
7from distutils.command.build_ext import build_ext
8from distutils.errors import CCompilerError
9from distutils.errors import DistutilsPlatformError, DistutilsExecError
10from _pyrsistent_version import __version__
11
12readme_path = os.path.join(os.path.dirname(__file__), 'README.rst')
13with codecs.open(readme_path, encoding='utf8') as f:
14    readme = f.read()
15
16extensions = []
17if platform.python_implementation() == 'CPython':
18    extensions = [Extension('pvectorc', sources=['pvectorcmodule.c'])]
19
20needs_pytest = {'pytest', 'test', 'ptr'}.intersection(sys.argv)
21pytest_runner = ['pytest-runner'] if needs_pytest else []
22
23
24class custom_build_ext(build_ext):
25    """Allow C extension building to fail."""
26
27    warning_message = """
28********************************************************************************
29WARNING: Could not build the %s.
30         Pyrsistent will still work but performance may be degraded.
31         %s
32********************************************************************************
33"""
34
35    def run(self):
36        try:
37            build_ext.run(self)
38        except Exception:
39            e = sys.exc_info()[1]
40            sys.stderr.write('%s\n' % str(e))
41            sys.stderr.write(self.warning_message % ("extension modules", "There was an issue with your platform configuration - see above."))
42
43    def build_extension(self, ext):
44        name = ext.name
45        try:
46            build_ext.build_extension(self, ext)
47        except Exception:
48            e = sys.exc_info()[1]
49            sys.stderr.write('%s\n' % str(e))
50            sys.stderr.write(self.warning_message % ("%s extension module" % name, "The output above this warning shows how the compilation failed."))
51
52setup(
53    name='pyrsistent',
54    version=__version__,
55    description='Persistent/Functional/Immutable data structures',
56    long_description=readme,
57    author='Tobias Gustafsson',
58    author_email='tobias.l.gustafsson@gmail.com',
59    url='http://github.com/tobgu/pyrsistent/',
60    license='MIT',
61    license_files=['LICENCE.mit'],
62    py_modules=['_pyrsistent_version'],
63    classifiers=[
64        'Intended Audience :: Developers',
65        'License :: OSI Approved :: MIT License',
66        'Operating System :: OS Independent',
67        'Programming Language :: Python :: 3.5',
68        'Programming Language :: Python :: 3.6',
69        'Programming Language :: Python :: 3.7',
70        'Programming Language :: Python :: Implementation :: PyPy',
71    ],
72    test_suite='tests',
73    tests_require=['pytest<5', 'hypothesis<5'],
74    scripts=[],
75    setup_requires=pytest_runner,
76    ext_modules=extensions,
77    cmdclass={'build_ext': custom_build_ext},
78    install_requires=['six'],
79    packages=['pyrsistent'],
80    package_data={'pyrsistent': ['py.typed', '__init__.pyi', 'typing.pyi']},
81)
82