1import os
2import platform
3import sys
4from distutils.errors import CCompilerError
5from distutils.errors import DistutilsExecError
6from distutils.errors import DistutilsPlatformError
7
8from setuptools import Extension
9from setuptools import setup
10from setuptools.command.build_ext import build_ext
11
12ext_modules = [Extension("markupsafe._speedups", ["src/markupsafe/_speedups.c"])]
13
14
15class BuildFailed(Exception):
16    pass
17
18
19class ve_build_ext(build_ext):
20    """This class allows C extension building to fail."""
21
22    def run(self):
23        try:
24            build_ext.run(self)
25        except DistutilsPlatformError:
26            raise BuildFailed()
27
28    def build_extension(self, ext):
29        try:
30            build_ext.build_extension(self, ext)
31        except (CCompilerError, DistutilsExecError, DistutilsPlatformError):
32            raise BuildFailed()
33        except ValueError:
34            # this can happen on Windows 64 bit, see Python issue 7511
35            if "'path'" in str(sys.exc_info()[1]):  # works with Python 2 and 3
36                raise BuildFailed()
37            raise
38
39
40def run_setup(with_binary):
41    setup(
42        name="MarkupSafe",
43        cmdclass={"build_ext": ve_build_ext},
44        ext_modules=ext_modules if with_binary else [],
45    )
46
47
48def show_message(*lines):
49    print("=" * 74)
50    for line in lines:
51        print(line)
52    print("=" * 74)
53
54
55if os.environ.get("CIBUILDWHEEL", "0") == "1":
56    run_setup(True)
57elif platform.python_implementation() not in {"PyPy", "Jython"}:
58    try:
59        run_setup(True)
60    except BuildFailed:
61        show_message(
62            "WARNING: The C extension could not be compiled, speedups"
63            " are not enabled.",
64            "Failure information, if any, is above.",
65            "Retrying the build without the C extension now.",
66        )
67        run_setup(False)
68        show_message(
69            "WARNING: The C extension could not be compiled, speedups"
70            " are not enabled.",
71            "Plain-Python build succeeded.",
72        )
73else:
74    run_setup(False)
75    show_message(
76        "WARNING: C extensions are not supported on this Python"
77        " platform, speedups are not enabled.",
78        "Plain-Python build succeeded.",
79    )
80