-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathsetup.py
More file actions
156 lines (135 loc) · 4.36 KB
/
setup.py
File metadata and controls
156 lines (135 loc) · 4.36 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import os
import sys
import pathlib
import tempfile
from contextlib import contextmanager
from distutils.errors import CompileError, LinkError
from setuptools.command.build_ext import build_ext
from setuptools import setup, Extension
@contextmanager
def silent_stderr():
"""Shush stderr for receiving unnecessary errors during setup."""
devnull = open(os.devnull, "w")
old = os.dup(sys.stderr.fileno())
os.dup2(devnull.fileno(), sys.stderr.fileno())
try:
yield devnull
finally:
os.dup2(old, sys.stderr.fileno())
class BuildExt(build_ext):
"""Custom build_ext to test Kerberose capability."""
def _have_krb5(self, libs: list) -> bool:
code = """
#include <krb5.h>
#include <gssapi/gssapi_krb5.h>
int main(void) {
unsigned int ms = 0;
krb5_context ctx;
const char *cname = NULL;
gss_key_value_set_desc store;
store.count = 0;
krb5_init_context(&ctx);
gss_krb5_ccache_name(&ms, cname, NULL);
return 0;
}
"""
with tempfile.TemporaryDirectory() as tmp_dir:
name = os.path.join(tmp_dir, "test_krb5")
src_name = name + ".c"
with open(src_name, "w") as source:
source.write(code)
comp = self.compiler
try:
with silent_stderr():
if "-coverage" in os.getenv("CFLAGS", ""):
# If coverage flag is set.
libs.append("gcov")
comp.link_executable(
comp.compile([src_name], output_dir=tmp_dir),
name,
libraries=libs,
library_dirs=self.library_dirs,
)
except (CompileError, LinkError):
return False
else:
return True
def build_extensions(self) -> None:
if sys.platform != "win32":
if self._have_krb5(["krb5", "gssapi"]):
self.extensions[0].libraries.extend(["krb5", "gssapi"])
self.extensions[0].define_macros.append(("HAVE_KRB5", 1))
elif self._have_krb5(["krb5", "gssapi_krb5"]):
self.extensions[0].libraries.extend(["krb5", "gssapi_krb5"])
self.extensions[0].define_macros.append(("HAVE_KRB5", 1))
else:
print(
"INFO: Kerberos headers and libraries are not found."
" Additional GSSAPI capabilities won't be installed."
)
return super().build_extensions()
SOURCES = [
"bonsaimodule.c",
"ldapentry.c",
"ldapconnectiter.c",
"ldapconnection.c",
"ldapmodlist.c",
"ldap-xplat.c",
"ldapsearchiter.c",
"utils.c",
]
DEPENDS = [
"ldapconnection.h",
"ldapentry.h",
"ldapconnectiter.h",
"ldapmodlist.h",
"ldapsearchiter.h",
"ldap-xplat.h",
"utils.h",
]
MACROS = []
if sys.platform == "darwin":
MACROS.append(("MACOSX", 1))
if sys.platform == "win32":
LIBS = ["wldap32", "secur32", "Ws2_32"]
SOURCES.append("wldap-utf8.c")
DEPENDS.append("wldap-utf8.h")
MACROS.append(("WIN32", 1))
else:
LIBS = ["ldap", "lber"]
SOURCES = [os.path.join("src/_bonsai", x) for x in SOURCES]
DEPENDS = [os.path.join("src/_bonsai", x) for x in DEPENDS]
BONSAI_MODULE = Extension(
"bonsai._bonsai",
libraries=LIBS,
sources=SOURCES,
depends=DEPENDS,
define_macros=MACROS,
)
# Get the absolute path to the directory of setup.py.
CURRDIR = pathlib.Path(__file__).resolve().parent
# Get version number from the module's __init__.py file.
with open(CURRDIR / "src" / "bonsai" / "__init__.py") as src:
VER = [
line.split('"')[1] for line in src.readlines() if line.startswith("__version__")
][0]
setup(
name="bonsai",
version=VER,
description="Python 3 module for accessing LDAP directory servers.",
author="noirello",
author_email="noirello@gmail.com",
ext_modules=[BONSAI_MODULE],
cmdclass={"build_ext": BuildExt},
package_dir={"bonsai": "src/bonsai"},
package_data={"bonsai": ["py.typed"]},
packages=[
"bonsai",
"bonsai.active_directory",
"bonsai.asyncio",
"bonsai.gevent",
"bonsai.tornado",
"bonsai.trio",
],
include_package_data=True,
)