-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy path_win_config.py
89 lines (73 loc) · 2.88 KB
/
_win_config.py
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
"""
Using GSSAPI on Windows requires having an installation of Kerberos for Windows
(KfW) available in the user's PATH. This module should be imported before
anything else to check for that installation, add it to the PATH if necessary,
and throw any errors before they manifest as cryptic missing DLL errors later
down the import tree.
"""
import os
import shutil
import sys
import ctypes
#: Path to normal KfW installed bin folder
KFW_BIN = os.path.join(
os.environ.get('ProgramFiles', r'C:\Program Files'),
'MIT', 'Kerberos', 'bin',
)
#: Download location for KfW
KFW_DL = "https://web.mit.edu/KERBEROS/dist"
# Mypy needs to run on both Win and non-Win so the missing attribute will fire
# on non-Win and Win will fire with unused ignore. Instead just cache the attr
# by name and use it as needed.
ADD_DLL_DIR = getattr(os, "add_dll_directory", None)
CTYPES_WIN_DLL = getattr(ctypes, "WinDLL", ctypes.CDLL)
def _add_dll_directory(path: str) -> None:
if ADD_DLL_DIR:
ADD_DLL_DIR(path)
def kfw_available() -> bool:
"""Return if the main GSSAPI DLL for KfW can be loaded"""
try: # to load the main GSSAPI DLL
if sys.maxsize > 2**32:
CTYPES_WIN_DLL('gssapi64.dll')
else:
CTYPES_WIN_DLL('gssapi32.dll')
except OSError: # DLL is not in PATH
return False
else: # DLL is in PATH, everything should work
return True
def error_not_found() -> None:
"""Raise an OSError detailing that KfW is missing and how to get it"""
raise OSError(
"Could not find KfW installation. Please download and install "
"the 64bit Kerberos for Windows MSI from %s and ensure the "
"'bin' folder (%s) is in your PATH."
% (KFW_DL, KFW_BIN)
)
def configure_windows() -> None:
"""
Validate that KfW appears to be installed correctly and add it to the
DLL directories/PATH if necessary. In the case that it can't be located,
raise an error.
"""
if kfw_available():
return # All set, necessary DLLs should be available
if os.path.exists(KFW_BIN): # In standard location
try: # to use Python 3.8's DLL handling
_add_dll_directory(KFW_BIN)
except AttributeError: # <3.8, use PATH
os.environ['PATH'] += os.pathsep + KFW_BIN
if kfw_available():
return
# Check if kinit is in the PATH which should lead us to the bin folder
kinit_path = shutil.which('kinit') # KfW provided binary
if kinit_path: # Non-standard install location
try: # Most likely >=3.8, otherwise it would have been found already
_add_dll_directory(os.path.dirname(kinit_path))
except AttributeError: # <3.8, corrupted installation?
pass
else:
if kfw_available():
return
error_not_found()
if os.name == 'nt': # Make sure we have the required DLLs
configure_windows()