| 1 |
import os |
| 2 |
import sys |
| 3 |
import pathlib |
| 4 |
import tempfile |
| 5 |
|
| 6 |
from contextlib import contextmanager |
| 7 |
|
| 8 |
from distutils.errors import CompileError, LinkError |
| 9 |
|
| 10 |
from setuptools.command.build_ext import build_ext |
| 11 |
from setuptools import setup, Extension |
| 12 |
|
| 13 |
|
| 14 |
@contextmanager |
| 15 |
def silent_stderr(): |
| 16 |
"""Shush stderr for receiving unnecessary errors during setup.""" |
| 17 |
devnull = open(os.devnull, "w") |
| 18 |
old = os.dup(sys.stderr.fileno()) |
| 19 |
os.dup2(devnull.fileno(), sys.stderr.fileno()) |
| 20 |
try: |
| 21 |
yield devnull |
| 22 |
finally: |
| 23 |
os.dup2(old, sys.stderr.fileno()) |
| 24 |
|
| 25 |
|
| 26 |
class BuildExt(build_ext): |
| 27 |
"""Custom build_ext to test Kerberose capability.""" |
| 28 |
|
| 29 |
def _have_krb5(self, libs: list) -> bool: |
| 30 |
code = """ |
| 31 |
#include <krb5.h> |
| 32 |
#include <gssapi/gssapi_krb5.h> |
| 33 |
|
| 34 |
int main(void) { |
| 35 |
unsigned int ms = 0; |
| 36 |
krb5_context ctx; |
| 37 |
const char *cname = NULL; |
| 38 |
gss_key_value_set_desc store; |
| 39 |
|
| 40 |
store.count = 0; |
| 41 |
krb5_init_context(&ctx); |
| 42 |
gss_krb5_ccache_name(&ms, cname, NULL); |
| 43 |
return 0; |
| 44 |
} |
| 45 |
""" |
| 46 |
with tempfile.TemporaryDirectory() as tmp_dir: |
| 47 |
name = os.path.join(tmp_dir, "test_krb5") |
| 48 |
src_name = name + ".c" |
| 49 |
with open(src_name, "w") as source: |
| 50 |
source.write(code) |
| 51 |
comp = self.compiler |
| 52 |
try: |
| 53 |
with silent_stderr(): |
| 54 |
if "-coverage" in os.getenv("CFLAGS", ""): |
| 55 |
# If coverage flag is set. |
| 56 |
libs.append("gcov") |
| 57 |
comp.link_executable( |
| 58 |
comp.compile([src_name], output_dir=tmp_dir), |
| 59 |
name, |
| 60 |
libraries=libs, |
| 61 |
library_dirs=self.library_dirs, |
| 62 |
) |
| 63 |
except (CompileError, LinkError): |
| 64 |
return False |
| 65 |
else: |
| 66 |
return True |
| 67 |
|
| 68 |
def build_extensions(self) -> None: |
| 69 |
if sys.platform != "win32": |
| 70 |
if self._have_krb5(["krb5", "gssapi"]): |
| 71 |
self.extensions[0].libraries.extend(["krb5", "gssapi"]) |
| 72 |
self.extensions[0].define_macros.append(("HAVE_KRB5", 1)) |
| 73 |
elif self._have_krb5(["krb5", "gssapi_krb5"]): |
| 74 |
self.extensions[0].libraries.extend(["krb5", "gssapi_krb5"]) |
| 75 |
self.extensions[0].define_macros.append(("HAVE_KRB5", 1)) |
| 76 |
else: |
| 77 |
print( |
| 78 |
"INFO: Kerberos headers and libraries are not found." |
| 79 |
" Additional GSSAPI capabilities won't be installed." |
| 80 |
) |
| 81 |
return super().build_extensions() |
| 82 |
|
| 83 |
|
| 84 |
SOURCES = [ |
| 85 |
"bonsaimodule.c", |
| 86 |
"ldapentry.c", |
| 87 |
"ldapconnectiter.c", |
| 88 |
"ldapconnection.c", |
| 89 |
"ldapmodlist.c", |
| 90 |
"ldap-xplat.c", |
| 91 |
"ldapsearchiter.c", |
| 92 |
"utils.c", |
| 93 |
] |
| 94 |
|
| 95 |
DEPENDS = [ |
| 96 |
"ldapconnection.h", |
| 97 |
"ldapentry.h", |
| 98 |
"ldapconnectiter.h", |
| 99 |
"ldapmodlist.h", |
| 100 |
"ldapsearchiter.h", |
| 101 |
"ldap-xplat.h", |
| 102 |
"utils.h", |
| 103 |
] |
| 104 |
|
| 105 |
MACROS = [] |
| 106 |
if sys.platform == "darwin": |
| 107 |
MACROS.append(("MACOSX", 1)) |
| 108 |
|
| 109 |
if sys.platform == "win32": |
| 110 |
LIBS = ["wldap32", "secur32", "Ws2_32"] |
| 111 |
SOURCES.append("wldap-utf8.c") |
| 112 |
DEPENDS.append("wldap-utf8.h") |
| 113 |
MACROS.append(("WIN32", 1)) |
| 114 |
else: |
| 115 |
LIBS = ["ldap", "lber"] |
| 116 |
|
| 117 |
SOURCES = [os.path.join("src/_bonsai", x) for x in SOURCES] |
| 118 |
DEPENDS = [os.path.join("src/_bonsai", x) for x in DEPENDS] |
| 119 |
|
| 120 |
BONSAI_MODULE = Extension( |
| 121 |
"bonsai._bonsai", |
| 122 |
libraries=LIBS, |
| 123 |
sources=SOURCES, |
| 124 |
depends=DEPENDS, |
| 125 |
define_macros=MACROS, |
| 126 |
) |
| 127 |
|
| 128 |
# Get the absolute path to the directory of setup.py. |
| 129 |
CURRDIR = pathlib.Path(__file__).resolve().parent |
| 130 |
|
| 131 |
# Get long description from the README.rst file. |
| 132 |
with open(CURRDIR / "README.rst") as file: |
| 133 |
LONG_DESC = file.read() |
| 134 |
|
| 135 |
# Get version number from the module's __init__.py file. |
| 136 |
with open(CURRDIR / "src" / "bonsai" / "__init__.py") as src: |
| 137 |
VER = [ |
| 138 |
line.split('"')[1] for line in src.readlines() if line.startswith("__version__") |
| 139 |
][0] |
| 140 |
|
| 141 |
setup( |
| 142 |
name="bonsai", |
| 143 |
version=VER, |
| 144 |
description="Python 3 module for accessing LDAP directory servers.", |
| 145 |
author="noirello", |
| 146 |
author_email="[email protected]", |
| 147 |
url="https://github.com/noirello/bonsai", |
| 148 |
long_description=LONG_DESC, |
| 149 |
long_description_content_type="text/x-rst", |
| 150 |
license="MIT", |
| 151 |
ext_modules=[BONSAI_MODULE], |
| 152 |
cmdclass={"build_ext": BuildExt}, |
| 153 |
package_dir={"bonsai": "src/bonsai"}, |
| 154 |
package_data={"bonsai": ["py.typed"]}, |
| 155 |
packages=[ |
| 156 |
"bonsai", |
| 157 |
"bonsai.active_directory", |
| 158 |
"bonsai.asyncio", |
| 159 |
"bonsai.gevent", |
| 160 |
"bonsai.tornado", |
| 161 |
"bonsai.trio", |
| 162 |
], |
| 163 |
include_package_data=True, |
| 164 |
install_requires=[ |
| 165 |
'typing-extensions >= 4.0.0 ; python_version < "3.8"', |
| 166 |
], |
| 167 |
extras_require={ |
| 168 |
"gevent": ["gevent>=1.4.0"], |
| 169 |
"tornado": ["tornado>=5.1.1"], |
| 170 |
"trio": ["trio>=0.16.0"], |
| 171 |
}, |
| 172 |
keywords=[ |
| 173 |
"python3", |
| 174 |
"ldap", |
| 175 |
"ldap3", |
| 176 |
"python-ldap", |
| 177 |
"libldap", |
| 178 |
"winldap", |
| 179 |
"asyncio", |
| 180 |
"gevent", |
| 181 |
"tornado", |
| 182 |
"trio", |
| 183 |
], |
| 184 |
classifiers=[ |
| 185 |
"Development Status :: 5 - Production/Stable", |
| 186 |
"Intended Audience :: Developers", |
| 187 |
"Intended Audience :: System Administrators", |
| 188 |
"License :: OSI Approved :: MIT License", |
| 189 |
"Operating System :: Microsoft :: Windows", |
| 190 |
"Operating System :: Unix", |
| 191 |
"Programming Language :: C", |
| 192 |
"Programming Language :: Python :: 3 :: Only", |
| 193 |
"Programming Language :: Python :: 3.7", |
| 194 |
"Programming Language :: Python :: 3.8", |
| 195 |
"Programming Language :: Python :: 3.9", |
| 196 |
"Programming Language :: Python :: 3.10", |
| 197 |
"Programming Language :: Python :: 3.11", |
| 198 |
"Programming Language :: Python :: 3.12", |
| 199 |
"Programming Language :: Python :: Implementation :: CPython", |
| 200 |
"Topic :: Software Development :: Libraries :: Python Modules", |
| 201 |
"Topic :: System :: Systems Administration :: Authentication/Directory :: LDAP", |
| 202 |
], |
| 203 |
) |