forked from Kozea/tinycss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
126 lines (105 loc) · 3.99 KB
/
setup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import os.path
import re
import sys
from distutils.errors import (
CCompilerError, DistutilsExecError, DistutilsPlatformError)
from setuptools import Extension, setup
try:
from Cython.Distutils import build_ext
import Cython.Compiler.Version
CYTHON_INSTALLED = True
except ImportError:
from distutils.command.build_ext import build_ext
CYTHON_INSTALLED = False
ext_errors = (CCompilerError, DistutilsExecError, DistutilsPlatformError)
if sys.platform == 'win32' and sys.version_info > (2, 6):
# 2.6's distutils.msvc9compiler can raise an IOError when failing to
# find the compiler
ext_errors += (IOError,)
class BuildFailed(Exception):
pass
class ve_build_ext(build_ext):
# This class allows C extension building to fail.
def run(self):
try:
build_ext.run(self)
except DistutilsPlatformError:
raise BuildFailed
def build_extension(self, ext):
try:
build_ext.build_extension(self, ext)
except ext_errors:
raise BuildFailed
ROOT = os.path.dirname(__file__)
with open(os.path.join(ROOT, 'tinycss', 'version.py')) as fd:
VERSION = re.search("VERSION = '([^']+)'", fd.read()).group(1)
with open(os.path.join(ROOT, 'README.rst'), 'rb') as fd:
README = fd.read().decode('utf8')
needs_pytest = {'pytest', 'test', 'ptr'}.intersection(sys.argv)
pytest_runner = ['pytest-runner'] if needs_pytest else []
def run_setup(with_extension):
if with_extension:
extension_path = os.path.join('tinycss', 'speedups')
if CYTHON_INSTALLED:
extension_path += '.pyx'
print('Building with Cython %s.' % Cython.Compiler.Version.version)
else:
extension_path += '.c'
if not os.path.exists(extension_path):
print("WARNING: Trying to build without Cython, but "
"pre-generated '%s' does not seem to be available."
% extension_path)
else:
print('Building without Cython.')
kwargs = dict(
cmdclass=dict(build_ext=ve_build_ext),
ext_modules=[Extension('tinycss.speedups',
sources=[extension_path])],
)
else:
kwargs = dict()
setup(
name='tinycss',
version=VERSION,
url='http://tinycss.readthedocs.io/',
license='BSD',
author='Simon Sapin',
author_email='[email protected]',
description='tinycss is a complete yet simple CSS parser for Python.',
long_description=README,
classifiers=[
'Development Status :: 4 - Beta',
'Intended Audience :: Developers',
'License :: OSI Approved :: BSD License',
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: Implementation :: CPython',
'Programming Language :: Python :: Implementation :: PyPy',
],
setup_requires=pytest_runner,
tests_require=[
'pytest-cov', 'pytest-flake8', 'pytest-isort', 'pytest-runner'],
extras_require={'test': (
'pytest-runner', 'pytest-cov', 'pytest-flake8', 'pytest-isort')},
packages=['tinycss', 'tinycss.tests'],
**kwargs
)
try:
run_setup(True)
except BuildFailed:
BUILD_EXT_WARNING = ('WARNING: The extension could not be compiled, '
'speedups are not enabled.')
print('*' * 75)
print(BUILD_EXT_WARNING)
print('Failure information, if any, is above.')
print('Retrying the build without the Cython extension now.')
print('*' * 75)
run_setup(False)
print('*' * 75)
print(BUILD_EXT_WARNING)
print('Plain-Python installation succeeded.')
print('*' * 75)