|
| 1 | +""" |
| 2 | +Using GSSAPI on Windows requires having an installation of Kerberos for Windows |
| 3 | +(KfW) available in the user's PATH. This module should be imported before |
| 4 | +anything else to check for that installation, add it to the PATH if necessary, |
| 5 | +and throw any errors before they manifest as cryptic missing DLL errors later |
| 6 | +down the import tree. |
| 7 | +""" |
| 8 | + |
| 9 | +import os |
| 10 | +import ctypes |
| 11 | + |
| 12 | +#: Path to normal KfW installed bin folder |
| 13 | +KFW_BIN = os.path.join( |
| 14 | + os.environ.get('ProgramFiles', r'C:\Program Files'), |
| 15 | + 'MIT', 'Kerberos', 'bin', |
| 16 | +) |
| 17 | +#: Download location for KfW |
| 18 | +KFW_DL = "https://web.mit.edu/KERBEROS/dist" |
| 19 | + |
| 20 | + |
| 21 | +def k4w_in_path(): |
| 22 | + """Return if the main GSSAPI DLL for KfW is available in the PATH""" |
| 23 | + try: # to load the main GSSAPI DLL |
| 24 | + ctypes.WinDLL('gssapi64.dll') |
| 25 | + except OSError: # DLL is not in PATH |
| 26 | + return False |
| 27 | + else: # DLL is in PATH, everything should work |
| 28 | + return True |
| 29 | + |
| 30 | + |
| 31 | +def error_not_found(): |
| 32 | + """Raise an OSError detailing that KfW is missing and how to get it""" |
| 33 | + raise OSError( |
| 34 | + "Could not find KfW installation. Please download and install " |
| 35 | + "the 64bit Kerberos for Windows MSI from %s and ensure the " |
| 36 | + "'bin' folder (%s) is in your PATH." |
| 37 | + % (KFW_DL, KFW_BIN) |
| 38 | + ) |
| 39 | + |
| 40 | + |
| 41 | +def configure_windows(): |
| 42 | + """ |
| 43 | + Validate that KfW appears to be installed correctly and add it to the |
| 44 | + PATH if necessary. In the case that it can't be located, raise an error. |
| 45 | + """ |
| 46 | + if k4w_in_path(): |
| 47 | + return # All set, necessary DLLs should be available |
| 48 | + if os.path.exists(KFW_BIN): # In standard location |
| 49 | + os.environ['PATH'] += os.pathsep + KFW_BIN |
| 50 | + if k4w_in_path(): |
| 51 | + return |
| 52 | + error_not_found() |
| 53 | + |
| 54 | + |
| 55 | +if os.name == 'nt': # Make sure we have the required DLLs |
| 56 | + configure_windows() |
0 commit comments