| 1 |
|
| 2 |
import os |
| 3 |
import re |
| 4 |
import platform |
| 5 |
import sys |
| 6 |
import sysconfig |
| 7 |
from setuptools import find_packages, setup |
| 8 |
from setuptools.command.build_ext import build_ext |
| 9 |
|
| 10 |
base_dir = os.path.dirname(os.path.abspath(__file__)) |
| 11 |
long_description = ( |
| 12 |
open("README.rst").read() + '\n\n' + open("HISTORY.rst").read() |
| 13 |
) |
| 14 |
|
| 15 |
with open(os.path.join(base_dir, "src", "brotlicffi", "__init__.py")) as f: |
| 16 |
__version__ = re.search(r"__version__ = \"([^\"]+)\"", f.read()).group(1) |
| 17 |
|
| 18 |
|
| 19 |
class BuildClibBeforeExt(build_ext): |
| 20 |
""" Setuptools `develop` command (used by `pip install -e .`) only calls |
| 21 |
`build_ext`, unlike `install` which calls `build` and all its related |
| 22 |
sub-commands. Linking the CFFI extension with the libbrotli static library |
| 23 |
fails since the `build_clib` command is not called in the former case. |
| 24 |
|
| 25 |
This custom `build_ext` class ensures that `build_clib` command is run |
| 26 |
before the CFFI extension module is compiled. |
| 27 |
|
| 28 |
https://github.com/pypa/pip/issues/4523 |
| 29 |
""" |
| 30 |
|
| 31 |
def get_source_files(self): |
| 32 |
filenames = build_ext.get_source_files(self) |
| 33 |
filenames.extend(depends) |
| 34 |
return filenames |
| 35 |
|
| 36 |
def run(self): |
| 37 |
self.run_command("build_clib") |
| 38 |
build_ext.run(self) |
| 39 |
|
| 40 |
|
| 41 |
depends = [] |
| 42 |
libraries = [] |
| 43 |
USE_SHARED_BROTLI = os.environ.get("USE_SHARED_BROTLI") |
| 44 |
if USE_SHARED_BROTLI != "1": |
| 45 |
sources = [] |
| 46 |
|
| 47 |
for root, _, filenames in os.walk("libbrotli/c"): |
| 48 |
root_parts = os.path.split(root) |
| 49 |
if "fuzz" in root_parts or "tools" in root_parts: |
| 50 |
continue |
| 51 |
for filename in filenames: |
| 52 |
relpath = os.path.relpath(os.path.join(root, filename), base_dir) |
| 53 |
if filename.endswith(".h"): |
| 54 |
depends.append(relpath) |
| 55 |
elif filename.endswith(".c"): |
| 56 |
sources.append(relpath) |
| 57 |
|
| 58 |
libraries = [ |
| 59 |
("libbrotli", { |
| 60 |
"include_dirs": [ |
| 61 |
"libbrotli/c/include", |
| 62 |
"libbrotli/c/common", |
| 63 |
"libbrotli/c", |
| 64 |
"src/brotlicffi" |
| 65 |
], |
| 66 |
"depends": depends, |
| 67 |
"sources": sources, |
| 68 |
}), |
| 69 |
] |
| 70 |
|
| 71 |
cmdclass = {'build_ext': BuildClibBeforeExt} |
| 72 |
if sys.version_info > (3,) and platform.python_implementation() == "CPython": |
| 73 |
try: |
| 74 |
import wheel.bdist_wheel |
| 75 |
except ImportError: |
| 76 |
pass |
| 77 |
else: |
| 78 |
|
| 79 |
if not bool(sysconfig.get_config_var("Py_GIL_DISABLED")): |
| 80 |
class BDistWheel(wheel.bdist_wheel.bdist_wheel): |
| 81 |
def finalize_options(self): |
| 82 |
self.py_limited_api = "cp3{}".format(sys.version_info[1]) |
| 83 |
wheel.bdist_wheel.bdist_wheel.finalize_options(self) |
| 84 |
cmdclass['bdist_wheel'] = BDistWheel |
| 85 |
|
| 86 |
setup( |
| 87 |
name="brotlicffi", |
| 88 |
version=__version__, |
| 89 |
|
| 90 |
description="Python CFFI bindings to the Brotli library", |
| 91 |
long_description=long_description, |
| 92 |
url="https://github.com/python-hyper/brotlicffi", |
| 93 |
license="MIT", |
| 94 |
|
| 95 |
author="Cory Benfield", |
| 96 |
author_email="[email protected]", |
| 97 |
|
| 98 |
setup_requires=[ |
| 99 |
"cffi>=1.0.0; python_version<'3.13'", |
| 100 |
"cffi>=1.17.0; python_version>='3.13'", |
| 101 |
], |
| 102 |
install_requires=[ |
| 103 |
"cffi>=1.0.0; python_version<'3.13'", |
| 104 |
"cffi>=1.17.0; python_version>='3.13'", |
| 105 |
], |
| 106 |
python_requires=">=3.8", |
| 107 |
cffi_modules=["src/brotlicffi/_build.py:ffi"], |
| 108 |
packages=find_packages('src'), |
| 109 |
package_dir={'': 'src'}, |
| 110 |
ext_package="brotlicffi", |
| 111 |
libraries=libraries, |
| 112 |
|
| 113 |
zip_safe=False, |
| 114 |
|
| 115 |
cmdclass=cmdclass, |
| 116 |
|
| 117 |
classifiers=[ |
| 118 |
"Programming Language :: Python :: Implementation :: CPython", |
| 119 |
"Programming Language :: Python :: Implementation :: PyPy", |
| 120 |
"Programming Language :: Python :: 3", |
| 121 |
"Programming Language :: Python :: 3.8", |
| 122 |
"Programming Language :: Python :: 3.9", |
| 123 |
"Programming Language :: Python :: 3.10", |
| 124 |
"Programming Language :: Python :: 3.11", |
| 125 |
"Programming Language :: Python :: 3.12", |
| 126 |
"Programming Language :: Python :: 3.13", |
| 127 |
"Programming Language :: Python :: 3.14", |
| 128 |
"Programming Language :: Python :: Free Threading :: 2 - Beta", |
| 129 |
] |
| 130 |
) |