blob: ccf68c63cdc6494103d17c02d4bb28e05a5d9860 [file] [log] [blame]
Armin Ronacher5f6f3df2010-06-22 19:35:26 +02001import os
Armin Ronacherf134ff72016-01-13 22:37:16 +01002import re
3import ast
Armin Ronacher5bda5222010-06-22 19:33:18 +02004import sys
Greg Back0ad62902015-07-14 13:58:39 -05005from setuptools import setup, Extension, Feature
Armin Ronacher5bda5222010-06-22 19:33:18 +02006from distutils.command.build_ext import build_ext
7from distutils.errors import CCompilerError, DistutilsExecError, \
Armin Ronacher026f3172014-04-17 11:05:46 +02008 DistutilsPlatformError
Armin Ronacher115ba372010-06-22 19:21:32 +02009
10
Armin Ronacher5bda5222010-06-22 19:33:18 +020011# fail safe compilation shamelessly stolen from the simplejson
12# setup.py file. Original author: Bob Ippolito
13
Armin Ronacher515ec272011-07-20 09:51:43 +020014is_jython = 'java' in sys.platform
15is_pypy = hasattr(sys, 'pypy_version_info')
16
Armin Ronacherf134ff72016-01-13 22:37:16 +010017with open('markupsafe/__init__.py') as f:
18 version = ast.literal_eval(re.search(
19 '^__version__\s+=\s+(.*?)$(?sm)', f.read()).group(1))
20
Armin Ronacher5bda5222010-06-22 19:33:18 +020021
Greg Back0ad62902015-07-14 13:58:39 -050022speedups = Feature(
23 'optional C speed-enhancement module',
24 standard=True,
Armin Ronacherf134ff72016-01-13 22:37:16 +010025 ext_modules=[
Greg Back0ad62902015-07-14 13:58:39 -050026 Extension('markupsafe._speedups', ['markupsafe/_speedups.c']),
27 ],
28)
29
Matthew Brett5064ad32014-08-02 19:00:36 -070030# Known errors when running build_ext.build_extension method
Armin Ronacher5bda5222010-06-22 19:33:18 +020031ext_errors = (CCompilerError, DistutilsExecError, DistutilsPlatformError)
32if sys.platform == 'win32' and sys.version_info > (2, 6):
Armin Ronacher026f3172014-04-17 11:05:46 +020033 # 2.6's distutils.msvc9compiler can raise an IOError when failing to
34 # find the compiler
35 ext_errors += (IOError,)
Matthew Brett5064ad32014-08-02 19:00:36 -070036# Known errors when running build_ext.run method
37run_errors = (DistutilsPlatformError,)
38if sys.platform == 'darwin':
39 run_errors += (SystemError,)
Armin Ronacher5bda5222010-06-22 19:33:18 +020040
41
42class BuildFailed(Exception):
43 pass
44
45
46class ve_build_ext(build_ext):
47 """This class allows C extension building to fail."""
48
49 def run(self):
50 try:
51 build_ext.run(self)
Matthew Brett5064ad32014-08-02 19:00:36 -070052 except run_errors:
Armin Ronacher5bda5222010-06-22 19:33:18 +020053 raise BuildFailed()
54
55 def build_extension(self, ext):
56 try:
57 build_ext.build_extension(self, ext)
Armin Ronacher1ce02cd2010-06-22 21:53:13 +020058 except ext_errors:
Armin Ronacher5bda5222010-06-22 19:33:18 +020059 raise BuildFailed()
Christoph Zwerschke796b2ea2011-06-05 13:02:02 +020060 except ValueError:
61 # this can happen on Windows 64 bit, see Python issue 7511
62 if "'path'" in str(sys.exc_info()[1]): # works with Python 2 and 3
Christoph Zwerschke4964e772011-06-04 19:28:56 +020063 raise BuildFailed()
64 raise
Armin Ronacher5bda5222010-06-22 19:33:18 +020065
66
Armin Ronacher5f853162010-06-22 21:56:38 +020067def echo(msg=''):
68 sys.stdout.write(msg + '\n')
69
70
Armin Ronacher20706f22010-06-22 19:46:25 +020071readme = open(os.path.join(os.path.dirname(__file__), 'README.rst')).read()
Armin Ronacher5f6f3df2010-06-22 19:35:26 +020072
73
Armin Ronacher5bda5222010-06-22 19:33:18 +020074def run_setup(with_binary):
Greg Back0ad62902015-07-14 13:58:39 -050075 features = {}
76 if with_binary:
77 features['speedups'] = speedups
Armin Ronacher5bda5222010-06-22 19:33:18 +020078 setup(
79 name='MarkupSafe',
Armin Ronacherf134ff72016-01-13 22:37:16 +010080 version=version,
José Carlos García3226ab52016-04-04 01:21:57 +020081 url='http://github.com/pallets/markupsafe',
Armin Ronacher5bda5222010-06-22 19:33:18 +020082 license='BSD',
83 author='Armin Ronacher',
84 author_email='armin.ronacher@active-4.com',
85 description='Implements a XML/HTML/XHTML Markup safe string for Python',
Armin Ronacher20706f22010-06-22 19:46:25 +020086 long_description=readme,
Armin Ronacher5bda5222010-06-22 19:33:18 +020087 zip_safe=False,
88 classifiers=[
89 'Development Status :: 5 - Production/Stable',
90 'Environment :: Web Environment',
91 'Intended Audience :: Developers',
92 'License :: OSI Approved :: BSD License',
93 'Operating System :: OS Independent',
94 'Programming Language :: Python',
95 'Programming Language :: Python :: 3',
96 'Topic :: Internet :: WWW/HTTP :: Dynamic Content',
97 'Topic :: Software Development :: Libraries :: Python Modules',
98 'Topic :: Text Processing :: Markup :: HTML'
99 ],
100 packages=['markupsafe'],
Armin Ronacherea75ec92010-06-22 19:45:11 +0200101 test_suite='markupsafe.tests.suite',
Armin Ronacher5bda5222010-06-22 19:33:18 +0200102 include_package_data=True,
103 cmdclass={'build_ext': ve_build_ext},
Greg Back0ad62902015-07-14 13:58:39 -0500104 features=features,
Armin Ronacher5bda5222010-06-22 19:33:18 +0200105 )
106
107
Armin Ronacher515ec272011-07-20 09:51:43 +0200108def try_building_extension():
109 try:
110 run_setup(True)
111 except BuildFailed:
112 LINE = '=' * 74
113 BUILD_EXT_WARNING = 'WARNING: The C extension could not be ' \
114 'compiled, speedups are not enabled.'
Armin Ronacher5bda5222010-06-22 19:33:18 +0200115
Armin Ronacher515ec272011-07-20 09:51:43 +0200116 echo(LINE)
117 echo(BUILD_EXT_WARNING)
118 echo('Failure information, if any, is above.')
119 echo('Retrying the build without the C extension now.')
120 echo()
Armin Ronacher5bda5222010-06-22 19:33:18 +0200121
Armin Ronacher515ec272011-07-20 09:51:43 +0200122 run_setup(False)
Armin Ronacher5bda5222010-06-22 19:33:18 +0200123
Armin Ronacher515ec272011-07-20 09:51:43 +0200124 echo(LINE)
125 echo(BUILD_EXT_WARNING)
126 echo('Plain-Python installation succeeded.')
127 echo(LINE)
128
129
130if not (is_pypy or is_jython):
131 try_building_extension()
132else:
Armin Ronacher98caea12011-07-20 10:57:46 +0200133 run_setup(False)