|
| 1 | +import os.path |
| 2 | +import sys |
| 3 | + |
1 | 4 | from setuptools import setup, Extension
|
| 5 | +from setuptools.command.build_ext import build_ext as build_ext |
| 6 | + |
| 7 | + |
| 8 | +vi = sys.version_info |
| 9 | +if vi < (3, 5): |
| 10 | + raise RuntimeError('httptools require Python 3.5 or greater') |
| 11 | + |
| 12 | +CFLAGS = ['-O2'] |
| 13 | +ROOT = os.path.dirname(__file__) |
| 14 | + |
| 15 | + |
| 16 | +class httptools_build_ext(build_ext): |
| 17 | + user_options = build_ext.user_options + [ |
| 18 | + ('cython-always', None, |
| 19 | + 'run cythonize() even if .c files are present'), |
| 20 | + ('cython-annotate', None, |
| 21 | + 'Produce a colorized HTML version of the Cython source.'), |
| 22 | + ('cython-directives=', None, |
| 23 | + 'Cythion compiler directives'), |
| 24 | + ('use-system-http-parser', None, |
| 25 | + 'Use the system provided http-parser, instead of the bundled one'), |
| 26 | + ] |
| 27 | + |
| 28 | + boolean_options = build_ext.boolean_options + [ |
| 29 | + 'cython-always', |
| 30 | + 'cython-annotate', |
| 31 | + 'use-system-http-parser', |
| 32 | + ] |
| 33 | + |
| 34 | + def initialize_options(self): |
| 35 | + # initialize_options() may be called multiple times on the |
| 36 | + # same command object, so make sure not to override previously |
| 37 | + # set options. |
| 38 | + if getattr(self, '_initialized', False): |
| 39 | + return |
| 40 | + |
| 41 | + super().initialize_options() |
| 42 | + self.use_system_http_parser = False |
| 43 | + self.cython_always = False |
| 44 | + self.cython_annotate = None |
| 45 | + self.cython_directives = None |
| 46 | + |
| 47 | + def finalize_options(self): |
| 48 | + # finalize_options() may be called multiple times on the |
| 49 | + # same command object, so make sure not to override previously |
| 50 | + # set options. |
| 51 | + if getattr(self, '_initialized', False): |
| 52 | + return |
| 53 | + |
| 54 | + need_cythonize = self.cython_always |
| 55 | + cfiles = {} |
| 56 | + |
| 57 | + for extension in self.distribution.ext_modules: |
| 58 | + for i, sfile in enumerate(extension.sources): |
| 59 | + if sfile.endswith('.pyx'): |
| 60 | + prefix, ext = os.path.splitext(sfile) |
| 61 | + cfile = prefix + '.c' |
| 62 | + |
| 63 | + if os.path.exists(cfile) and not self.cython_always: |
| 64 | + extension.sources[i] = cfile |
| 65 | + else: |
| 66 | + if os.path.exists(cfile): |
| 67 | + cfiles[cfile] = os.path.getmtime(cfile) |
| 68 | + else: |
| 69 | + cfiles[cfile] = 0 |
| 70 | + need_cythonize = True |
| 71 | + |
| 72 | + if need_cythonize: |
| 73 | + try: |
| 74 | + import Cython |
| 75 | + except ImportError: |
| 76 | + raise RuntimeError( |
| 77 | + 'please install Cython to compile httptools from source') |
2 | 78 |
|
3 |
| -with open('README.md') as f: |
| 79 | + if Cython.__version__ < '0.29': |
| 80 | + raise RuntimeError( |
| 81 | + 'httptools requires Cython version 0.29 or greater') |
| 82 | + |
| 83 | + from Cython.Build import cythonize |
| 84 | + |
| 85 | + directives = {} |
| 86 | + if self.cython_directives: |
| 87 | + for directive in self.cython_directives.split(','): |
| 88 | + k, _, v = directive.partition('=') |
| 89 | + if v.lower() == 'false': |
| 90 | + v = False |
| 91 | + if v.lower() == 'true': |
| 92 | + v = True |
| 93 | + |
| 94 | + directives[k] = v |
| 95 | + |
| 96 | + self.distribution.ext_modules[:] = cythonize( |
| 97 | + self.distribution.ext_modules, |
| 98 | + compiler_directives=directives, |
| 99 | + annotate=self.cython_annotate) |
| 100 | + |
| 101 | + super().finalize_options() |
| 102 | + |
| 103 | + self._initialized = True |
| 104 | + |
| 105 | + def build_extensions(self): |
| 106 | + if self.use_system_http_parser: |
| 107 | + self.compiler.add_library('http_parser') |
| 108 | + |
| 109 | + if sys.platform == 'darwin' and \ |
| 110 | + os.path.exists('/opt/local/include'): |
| 111 | + # Support macports on Mac OS X. |
| 112 | + self.compiler.add_include_dir('/opt/local/include') |
| 113 | + else: |
| 114 | + self.compiler.add_include_dir( |
| 115 | + os.path.join(ROOT, 'vendor/http-parser')) |
| 116 | + self.distribution.ext_modules[0].sources.append( |
| 117 | + 'vendor/http-parser/http_parser.c') |
| 118 | + |
| 119 | + super().build_extensions() |
| 120 | + |
| 121 | + |
| 122 | +with open(os.path.join(ROOT, 'README.md')) as f: |
4 | 123 | long_description = f.read()
|
5 | 124 |
|
| 125 | + |
| 126 | +with open(os.path.join(ROOT, 'httptools', '__init__.py')) as f: |
| 127 | + for line in f: |
| 128 | + if line.startswith('__version__ ='): |
| 129 | + _, _, version = line.partition('=') |
| 130 | + VERSION = version.strip(" \n'\"") |
| 131 | + break |
| 132 | + else: |
| 133 | + raise RuntimeError( |
| 134 | + 'unable to read the version from httptools/__init__.py') |
| 135 | + |
| 136 | + |
6 | 137 | setup(
|
7 | 138 | name='httptools',
|
8 |
| - version='0.0.13', |
| 139 | + version=VERSION, |
9 | 140 | description='A collection of framework independent HTTP protocol utils.',
|
10 | 141 | long_description=long_description,
|
11 | 142 | long_description_content_type='text/markdown',
|
|
24 | 155 |
|
25 | 156 | license='MIT',
|
26 | 157 | packages=['httptools', 'httptools.parser'],
|
| 158 | + cmdclass={ |
| 159 | + 'build_ext': httptools_build_ext, |
| 160 | + }, |
27 | 161 | ext_modules=[
|
28 |
| - Extension("httptools.parser.parser", |
29 |
| - ["httptools/parser/parser.c", |
30 |
| - "vendor/http-parser/http_parser.c"], |
31 |
| - extra_compile_args=['-O2']) |
| 162 | + Extension( |
| 163 | + "httptools.parser.parser", |
| 164 | + sources=[ |
| 165 | + "httptools/parser/parser.pyx", |
| 166 | + ], |
| 167 | + extra_compile_args=CFLAGS, |
| 168 | + ), |
32 | 169 | ],
|
33 | 170 | provides=['httptools'],
|
34 |
| - include_package_data=True |
| 171 | + include_package_data=True, |
| 172 | + test_suite='tests.suite' |
35 | 173 | )
|
0 commit comments