Debian Codegraph @

contrib/python-zstandard/setup.py

raw
1 #!/usr/bin/env python3
2 # Copyright (c) 2016-present, Gregory Szorc
3 # All rights reserved.
4 #
5 # This software may be modified and distributed under the terms
6 # of the BSD license. See the LICENSE file for details.
7
8 from __future__ import print_function
9
10 import platform
11 import os
12 import sys
13 from setuptools import setup
14
15 # Python 3.12 dropped distutils from the stdlib. Try to access it via
16 # setuptools.
17 try:
18 from setuptools._distutils.version import LooseVersion
19 except ImportError:
20 from distutils.version import LooseVersion
21
22 if sys.version_info[0:2] < (3, 8):
23 print("Python 3.8+ is required", file=sys.stderr)
24 sys.exit(1)
25
26 # Need change in 1.10 for ffi.from_buffer() to handle all buffer types
27 # (like memoryview).
28 # Need feature in 1.11 for ffi.gc() to declare size of objects so we avoid
29 # garbage collection pitfalls.
30 MINIMUM_CFFI_VERSION = "1.11"
31
32 # Need 1.17+ on 3.13 to avoid deprecated and removed APIs.
33 if sys.version_info[0:2] >= (3, 13):
34 MINIMUM_CFFI_VERSION = "1.17"
35
36 try:
37 import cffi
38
39 # PyPy (and possibly other distros) have CFFI distributed as part of
40 # them.
41 cffi_version = LooseVersion(cffi.__version__)
42 if cffi_version < LooseVersion(MINIMUM_CFFI_VERSION):
43 print(
44 "CFFI %s or newer required (%s found); "
45 "not building CFFI backend" % (MINIMUM_CFFI_VERSION, cffi_version),
46 file=sys.stderr,
47 )
48 cffi = None
49
50 except ImportError:
51 cffi = None
52
53 sys.path.insert(0, ".")
54
55 import setup_zstd
56
57 SUPPORT_LEGACY = False
58 SYSTEM_ZSTD = False
59 WARNINGS_AS_ERRORS = False
60 C_BACKEND = True
61 CFFI_BACKEND = True
62 RUST_BACKEND = False
63
64 if os.environ.get("ZSTD_WARNINGS_AS_ERRORS", ""):
65 WARNINGS_AS_ERRORS = True
66
67 # PyPy doesn't support the C backend.
68 if platform.python_implementation() == "PyPy":
69 C_BACKEND = False
70
71 if "--legacy" in sys.argv:
72 SUPPORT_LEGACY = True
73 sys.argv.remove("--legacy")
74
75 if "--system-zstd" in sys.argv:
76 SYSTEM_ZSTD = True
77 sys.argv.remove("--system-zstd")
78
79 if "--warnings-as-errors" in sys.argv:
80 WARNINGS_AS_ERRORS = True
81 sys.argv.remove("--warning-as-errors")
82
83 if "--no-c-backend" in sys.argv:
84 C_BACKEND = False
85 sys.argv.remove("--no-c-backend")
86
87 if "--no-cffi-backend" in sys.argv:
88 CFFI_BACKEND = False
89 sys.argv.remove("--no-cffi-backend")
90
91 if "--rust-backend" in sys.argv:
92 RUST_BACKEND = True
93 sys.argv.remove("--rust-backend")
94
95 # Code for obtaining the Extension instance is in its own module to
96 # facilitate reuse in other projects.
97 extensions = []
98
99 if C_BACKEND:
100 extensions.append(
101 setup_zstd.get_c_extension(
102 support_legacy=SUPPORT_LEGACY,
103 system_zstd=SYSTEM_ZSTD,
104 warnings_as_errors=WARNINGS_AS_ERRORS,
105 )
106 )
107
108 if RUST_BACKEND:
109 extensions.append(setup_zstd.get_rust_extension())
110
111 if CFFI_BACKEND and cffi:
112 import make_cffi
113
114 extensions.append(make_cffi.ffi.distutils_extension())
115
116 version = None
117
118 with open("c-ext/python-zstandard.h") as fh:
119 for line in fh:
120 if not line.startswith("#define PYTHON_ZSTANDARD_VERSION"):
121 continue
122
123 version = line.split()[2][1:-1]
124 break
125
126 if not version:
127 raise Exception(
128 "could not resolve package version; " "this should never happen"
129 )
130
131 setup(
132 name="zstandard",
133 version=version,
134 description="Zstandard bindings for Python",
135 long_description=open("README.rst").read(),
136 url="https://github.com/indygreg/python-zstandard",
137 author="Gregory Szorc",
138 author_email="[email protected]",
139 license="BSD",
140 python_requires=">=3.8",
141 classifiers=[
142 "Development Status :: 5 - Production/Stable",
143 "Intended Audience :: Developers",
144 "License :: OSI Approved :: BSD License",
145 "Programming Language :: C",
146 "Programming Language :: Python :: 3.8",
147 "Programming Language :: Python :: 3.9",
148 "Programming Language :: Python :: 3.10",
149 "Programming Language :: Python :: 3.11",
150 "Programming Language :: Python :: 3.12",
151 "Programming Language :: Python :: 3.13",
152 ],
153 keywords=["zstandard", "zstd", "compression"],
154 packages=["zstandard"],
155 package_data={"zstandard": ["__init__.pyi", "py.typed"]},
156 ext_modules=extensions,
157 cmdclass={"build_ext": setup_zstd.RustBuildExt},
158 test_suite="tests",
159 install_requires=[
160 # cffi is required on PyPy.
161 "cffi>=%s; platform_python_implementation == 'PyPy'"
162 % MINIMUM_CFFI_VERSION
163 ],
164 extras_require={
165 "cffi": ["cffi>=%s" % MINIMUM_CFFI_VERSION],
166 },
167 tests_require=["hypothesis"],
168 )