blob: e30df8e314eafd7578a0f252ed9f69973292cc42 [file] [log] [blame]
David Lordc76d0da2016-04-11 08:22:11 -07001#!/usr/bin/env python
Armin Ronacher5f6f3df2010-06-22 19:35:26 +02002import os
Armin Ronacherf134ff72016-01-13 22:37:16 +01003import re
4import ast
Armin Ronacher5bda5222010-06-22 19:33:18 +02005import sys
Greg Back0ad62902015-07-14 13:58:39 -05006from setuptools import setup, Extension, Feature
Armin Ronacher5bda5222010-06-22 19:33:18 +02007from distutils.command.build_ext import build_ext
8from distutils.errors import CCompilerError, DistutilsExecError, \
Armin Ronacher026f3172014-04-17 11:05:46 +02009 DistutilsPlatformError
Armin Ronacher115ba372010-06-22 19:21:32 +020010
11
Armin Ronacher5bda5222010-06-22 19:33:18 +020012# fail safe compilation shamelessly stolen from the simplejson
13# setup.py file. Original author: Bob Ippolito
14
Armin Ronacher515ec272011-07-20 09:51:43 +020015is_jython = 'java' in sys.platform
16is_pypy = hasattr(sys, 'pypy_version_info')
17
Armin Ronacherf134ff72016-01-13 22:37:16 +010018with open('markupsafe/__init__.py') as f:
19 version = ast.literal_eval(re.search(
20 '^__version__\s+=\s+(.*?)$(?sm)', f.read()).group(1))
21
Armin Ronacher5bda5222010-06-22 19:33:18 +020022
Greg Back0ad62902015-07-14 13:58:39 -050023speedups = Feature(
24 'optional C speed-enhancement module',
25 standard=True,
Armin Ronacherf134ff72016-01-13 22:37:16 +010026 ext_modules=[
Greg Back0ad62902015-07-14 13:58:39 -050027 Extension('markupsafe._speedups', ['markupsafe/_speedups.c']),
28 ],
29)
30
Matthew Brett5064ad32014-08-02 19:00:36 -070031# Known errors when running build_ext.build_extension method
Armin Ronacher5bda5222010-06-22 19:33:18 +020032ext_errors = (CCompilerError, DistutilsExecError, DistutilsPlatformError)
33if sys.platform == 'win32' and sys.version_info > (2, 6):
Armin Ronacher026f3172014-04-17 11:05:46 +020034 # 2.6's distutils.msvc9compiler can raise an IOError when failing to
35 # find the compiler
36 ext_errors += (IOError,)
Matthew Brett5064ad32014-08-02 19:00:36 -070037# Known errors when running build_ext.run method
38run_errors = (DistutilsPlatformError,)
39if sys.platform == 'darwin':
40 run_errors += (SystemError,)
Armin Ronacher5bda5222010-06-22 19:33:18 +020041
42
43class BuildFailed(Exception):
44 pass
45
46
47class ve_build_ext(build_ext):
48 """This class allows C extension building to fail."""
49
50 def run(self):
51 try:
52 build_ext.run(self)
Matthew Brett5064ad32014-08-02 19:00:36 -070053 except run_errors:
Armin Ronacher5bda5222010-06-22 19:33:18 +020054 raise BuildFailed()
55
56 def build_extension(self, ext):
57 try:
58 build_ext.build_extension(self, ext)
Armin Ronacher1ce02cd2010-06-22 21:53:13 +020059 except ext_errors:
Armin Ronacher5bda5222010-06-22 19:33:18 +020060 raise BuildFailed()
Christoph Zwerschke796b2ea2011-06-05 13:02:02 +020061 except ValueError:
62 # this can happen on Windows 64 bit, see Python issue 7511
63 if "'path'" in str(sys.exc_info()[1]): # works with Python 2 and 3
Christoph Zwerschke4964e772011-06-04 19:28:56 +020064 raise BuildFailed()
65 raise
Armin Ronacher5bda5222010-06-22 19:33:18 +020066
67
Armin Ronacher5f853162010-06-22 21:56:38 +020068def echo(msg=''):
69 sys.stdout.write(msg + '\n')
70
71
Armin Ronacher20706f22010-06-22 19:46:25 +020072readme = open(os.path.join(os.path.dirname(__file__), 'README.rst')).read()
Armin Ronacher5f6f3df2010-06-22 19:35:26 +020073
74
Armin Ronacher5bda5222010-06-22 19:33:18 +020075def run_setup(with_binary):
Greg Back0ad62902015-07-14 13:58:39 -050076 features = {}
77 if with_binary:
78 features['speedups'] = speedups
Armin Ronacher5bda5222010-06-22 19:33:18 +020079 setup(
80 name='MarkupSafe',
Armin Ronacherf134ff72016-01-13 22:37:16 +010081 version=version,
José Carlos García3226ab52016-04-04 01:21:57 +020082 url='http://github.com/pallets/markupsafe',
Armin Ronacher5bda5222010-06-22 19:33:18 +020083 license='BSD',
84 author='Armin Ronacher',
85 author_email='armin.ronacher@active-4.com',
86 description='Implements a XML/HTML/XHTML Markup safe string for Python',
Armin Ronacher20706f22010-06-22 19:46:25 +020087 long_description=readme,
Armin Ronacher5bda5222010-06-22 19:33:18 +020088 zip_safe=False,
89 classifiers=[
90 'Development Status :: 5 - Production/Stable',
91 'Environment :: Web Environment',
92 'Intended Audience :: Developers',
93 'License :: OSI Approved :: BSD License',
94 'Operating System :: OS Independent',
95 'Programming Language :: Python',
96 'Programming Language :: Python :: 3',
97 'Topic :: Internet :: WWW/HTTP :: Dynamic Content',
98 'Topic :: Software Development :: Libraries :: Python Modules',
99 'Topic :: Text Processing :: Markup :: HTML'
100 ],
101 packages=['markupsafe'],
David Lordc76d0da2016-04-11 08:22:11 -0700102 test_suite='tests.suite',
Armin Ronacher5bda5222010-06-22 19:33:18 +0200103 include_package_data=True,
104 cmdclass={'build_ext': ve_build_ext},
Greg Back0ad62902015-07-14 13:58:39 -0500105 features=features,
Armin Ronacher5bda5222010-06-22 19:33:18 +0200106 )
107
108
Armin Ronacher515ec272011-07-20 09:51:43 +0200109def try_building_extension():
110 try:
111 run_setup(True)
112 except BuildFailed:
113 LINE = '=' * 74
114 BUILD_EXT_WARNING = 'WARNING: The C extension could not be ' \
115 'compiled, speedups are not enabled.'
Armin Ronacher5bda5222010-06-22 19:33:18 +0200116
Armin Ronacher515ec272011-07-20 09:51:43 +0200117 echo(LINE)
118 echo(BUILD_EXT_WARNING)
119 echo('Failure information, if any, is above.')
120 echo('Retrying the build without the C extension now.')
121 echo()
Armin Ronacher5bda5222010-06-22 19:33:18 +0200122
Armin Ronacher515ec272011-07-20 09:51:43 +0200123 run_setup(False)
Armin Ronacher5bda5222010-06-22 19:33:18 +0200124
Armin Ronacher515ec272011-07-20 09:51:43 +0200125 echo(LINE)
126 echo(BUILD_EXT_WARNING)
127 echo('Plain-Python installation succeeded.')
128 echo(LINE)
129
130
131if not (is_pypy or is_jython):
132 try_building_extension()
133else:
Armin Ronacher98caea12011-07-20 10:57:46 +0200134 run_setup(False)