-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsetup.py
More file actions
216 lines (182 loc) · 7.25 KB
/
setup.py
File metadata and controls
216 lines (182 loc) · 7.25 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import os
import os.path
import platform
import re
import subprocess
import sys
try:
from setuptools import setup, find_packages
except ImportError:
from gssapi_ez_setup import use_setuptools
use_setuptools()
from setuptools import setup, find_packages
import pkg_resources
from setuptools.command.build_py import build_py as _build_py
from setuptools.command.develop import develop as _develop
def _strip_unknown_cflags(cflags):
"""Strip out cflags that ctypesgen doesn't understand."""
return tuple(
flag for flag in cflags
if any(flag.startswith(prefix) for prefix in (
"-Wl,-L", "-Wl,-R", "-Wl,--rpath", "-l", "-I", "-L", "-R", "--rpath",
))
)
def _find_gssapi_h(cflags):
gcc_target = subprocess.check_output(["gcc", "-dumpmachine"]).strip()
default_paths = [
"/usr/local/include",
"/usr/{target}/include".format(target=gcc_target),
"/usr/include"
]
extra_paths = [
flag[2:]
for flag in cflags
if flag.startswith('-I')
]
for include_path in (os.path.join("gssapi", "gssapi.h"), "gssapi.h"):
for search_path in (extra_paths + default_paths):
full_header = os.path.join(search_path, include_path)
if os.path.isfile(full_header):
return full_header
def _patch_struct_packing(filename, packing):
comment_matcher = re.compile(r"^# .+\.h: \d+$")
struct_matcher = re.compile(r"^class [a-zA-Z_][a-zA-Z0-9_]*\(Structure\):$")
expect_struct = False
with open(filename, 'r') as infile:
for line in infile:
yield line
if expect_struct:
if struct_matcher.match(line):
yield ' _pack_ = {0}\n'.format(packing)
expect_struct = False
if comment_matcher.match(line):
expect_struct = True
def _initialize_options(self):
self.compile_flags = ()
self.patch_struct_pack = None
self.ctypesgen_cpp = "gcc -E -D__attribute__\\(x\\)="
self.gssapi_h_locations = []
def _finalize_options(self):
if os.path.isdir('/System/Library/Frameworks/GSS.framework'):
# Build using GSS.framework on Mac OS X 10.7+
self.gssapi_h_locations = []
for header in ('gssapi.h', 'gssapi_oid.h', 'gssapi_protos.h'):
path_in_framework = os.path.join('/System/Library/Frameworks/GSS.framework/Headers', header)
if os.path.isfile(path_in_framework):
self.gssapi_h_locations.append(path_in_framework)
self.compile_flags = ('-lGSS',)
self.cpp_extra_flags = ('-framework GSS',)
else:
# Build using libgssapi on other POSIX systems
try:
config_compile_flags = subprocess.check_output(["krb5-config", "--cflags", "gssapi"]).split()
config_link_flags = subprocess.check_output(["krb5-config", "--libs", "gssapi"]).split()
except:
try:
config_compile_flags = subprocess.check_output(["pkg-config", "--cflags", "gss"]).split()
config_link_flags = subprocess.check_output(["pkg-config", "--libs", "gss"]).split()
except:
config_compile_flags = []
config_link_flags = []
config_compile_flags = [ f.decode() for f in config_compile_flags ]
config_link_flags = [ f.decode() for f in config_link_flags ]
self.compile_flags = (_strip_unknown_cflags(config_compile_flags)
+ _strip_unknown_cflags(config_link_flags))
self.cpp_extra_flags = tuple(config_compile_flags)
currentplatform = platform.system()
if currentplatform == 'Darwin':
self.patch_struct_pack = 2
machine = platform.machine()
define = {
"x86_64": "TARGET_CPU_X86_64",
"i386": "TARGET_CPU_X86",
"ppc64": "TARGET_CPU_PPC64",
"ppc": "TARGET_CPU_PPC",
}[machine]
self.ctypesgen_cpp = "gcc -E -D{0} -D__attribute__\\(x\\)= {1}".format(define, " ".join(self.cpp_extra_flags))
else:
self.ctypesgen_cpp = "gcc -E -D__attribute__\\(x\\)= {0}".format(" ".join(self.cpp_extra_flags))
self.compile_flags += ("-i", "uid_t")
if not self.gssapi_h_locations:
self.gssapi_h_locations = [_find_gssapi_h(self.compile_flags)]
def _generate_headers(self, target):
if not any(self.gssapi_h_locations):
raise RuntimeError(
"GSSAPI headers could not be found. "
"Please install the GSSAPI C library development headers for your OS."
)
ctypesgen_dist = pkg_resources.get_distribution(
pkg_resources.Requirement.parse('ctypesgen==0.r125')
)
try:
script_str = ctypesgen_dist.get_metadata('scripts/ctypesgen.py')
ctypesgen_command = [sys.executable, "-", "--cpp", self.ctypesgen_cpp]
except:
script_str = None
ctypesgen_command = ["ctypesgen.py", "--cpp", self.ctypesgen_cpp]
new_env = dict(os.environ)
new_env['PYTHONPATH'] = ctypesgen_dist.location
ctypesgen_command.extend(self.compile_flags)
ctypesgen_command.extend(["-o", target])
ctypesgen_command.extend(self.gssapi_h_locations)
if script_str:
ctypesgen_proc = subprocess.Popen(ctypesgen_command, stdin=subprocess.PIPE, env=new_env)
ctypesgen_proc.communicate(script_str)
if ctypesgen_proc.returncode != 0:
raise subprocess.CalledProcessError(ctypesgen_proc.returncode, ctypesgen_command)
else:
subprocess.check_call(ctypesgen_command)
if self.patch_struct_pack is not None:
patched_source = "".join(_patch_struct_packing(
target, self.patch_struct_pack
))
with open(target, 'w') as outfile:
outfile.write(patched_source)
class build_py(_build_py):
def initialize_options(self):
_build_py.initialize_options(self)
_initialize_options(self)
def finalize_options(self):
_build_py.finalize_options(self)
_finalize_options(self)
def run(self):
target = _build_py.get_module_outfile(self, self.build_lib, ['gssapi', 'headers'], "gssapi_h")
target_dir = os.path.dirname(target)
_build_py.mkpath(self, target_dir)
_generate_headers(self, target)
_build_py.run(self)
class develop(_develop):
def initialize_options(self):
_develop.initialize_options(self)
_initialize_options(self)
def finalize_options(self):
_develop.finalize_options(self)
_finalize_options(self)
def run(self):
_develop.run(self)
target = os.path.join(self.egg_path, 'gssapi', 'headers', 'gssapi_h.py')
_generate_headers(self, target)
setup(
name="python-gssapi",
version="0.4.1",
cmdclass={
"build_py": build_py,
"develop": develop,
},
packages=find_packages(exclude=["tests.*", "tests"]),
py_modules=['gssapi_ez_setup'],
setup_requires=[
'ctypesgen==0.r125',
],
install_requires=[
'pyasn1>=0.1.2',
'six>=1.5.0'
],
# metadata for upload to PyPI
author="Hugh Cole-Baker",
author_email="[email protected]",
description="An object-oriented interface to GSSAPI for Python",
license="BSD",
keywords="gssapi kerberos",
url="https://github.com/sigmaris/python-gssapi",
)