|
1 |
| -from gssapi._utils import import_gssapi_extension |
| 1 | +import os |
| 2 | +import subprocess |
2 | 3 |
|
3 |
| -try: |
4 |
| - import commands |
5 |
| - get_output = commands.getoutput |
6 |
| -except ImportError: |
7 |
| - import subprocess |
| 4 | +from gssapi._utils import import_gssapi_extension |
8 | 5 |
|
9 |
| - def _get_output(*args, **kwargs): |
10 |
| - res = subprocess.check_output(*args, shell=True, **kwargs) |
11 |
| - decoded = res.decode('utf-8') |
12 |
| - return decoded.strip() |
13 | 6 |
|
14 |
| - get_output = _get_output |
| 7 | +def get_output(*args, **kwargs): |
| 8 | + res = subprocess.check_output(*args, shell=True, **kwargs) |
| 9 | + decoded = res.decode('utf-8') |
| 10 | + return decoded.strip() |
15 | 11 |
|
16 | 12 |
|
17 | 13 | def _extension_test(extension_name, extension_text):
|
@@ -47,3 +43,80 @@ def ext_test(self, *args, **kwargs):
|
47 | 43 | return ext_test
|
48 | 44 |
|
49 | 45 | return make_ext_test
|
| 46 | + |
| 47 | + |
| 48 | +_PLUGIN_DIR = None |
| 49 | + |
| 50 | + |
| 51 | +def _find_plugin_dir(): |
| 52 | + global _PLUGIN_DIR |
| 53 | + if _PLUGIN_DIR is not None: |
| 54 | + return _PLUGIN_DIR |
| 55 | + |
| 56 | + # if we've set a LD_LIBRARY_PATH, use that first |
| 57 | + ld_path_raw = os.environ.get('LD_LIBRARY_PATH') |
| 58 | + if ld_path_raw is not None: |
| 59 | + # first, try assuming it's just a normal install |
| 60 | + |
| 61 | + ld_paths = [path for path in ld_path_raw.split(':') if path] |
| 62 | + |
| 63 | + for ld_path in ld_paths: |
| 64 | + if not os.path.exists(ld_path): |
| 65 | + continue |
| 66 | + |
| 67 | + _PLUGIN_DIR = _decide_plugin_dir( |
| 68 | + _find_plugin_dirs_installed(ld_path)) |
| 69 | + if _PLUGIN_DIR is None: |
| 70 | + _PLUGIN_DIR = _decide_plugin_dir( |
| 71 | + _find_plugin_dirs_src(ld_path)) |
| 72 | + |
| 73 | + if _PLUGIN_DIR is not None: |
| 74 | + break |
| 75 | + |
| 76 | + # if there was no LD_LIBRARY_PATH, or the above failed |
| 77 | + if _PLUGIN_DIR is None: |
| 78 | + # if we don't have a LD_LIBRARY_PATH, just search in |
| 79 | + # $prefix/lib |
| 80 | + |
| 81 | + lib_dir = os.path.join(get_output('krb5-config --prefix'), 'lib') |
| 82 | + _PLUGIN_DIR = _decide_plugin_dir(_find_plugin_dirs_installed(lib_dir)) |
| 83 | + |
| 84 | + if _PLUGIN_DIR is not None: |
| 85 | + _PLUGIN_DIR = os.path.normpath(_PLUGIN_DIR) |
| 86 | + return _PLUGIN_DIR |
| 87 | + else: |
| 88 | + return None |
| 89 | + |
| 90 | + |
| 91 | +def _decide_plugin_dir(dirs): |
| 92 | + if dirs is None: |
| 93 | + return None |
| 94 | + |
| 95 | + # the shortest path is probably more correct |
| 96 | + shortest_first = sorted(dirs, key=len) |
| 97 | + |
| 98 | + for path in shortest_first: |
| 99 | + # check to see if it actually contains .so files |
| 100 | + if get_output('find %s -name "*.so"' % path): |
| 101 | + return path |
| 102 | + |
| 103 | + return None |
| 104 | + |
| 105 | + |
| 106 | +def _find_plugin_dirs_installed(search_path): |
| 107 | + options_raw = get_output('find %s/ -type d ' |
| 108 | + '-path "*/krb5/plugins"' % search_path) |
| 109 | + |
| 110 | + if options_raw: |
| 111 | + return options_raw.split('\n') |
| 112 | + else: |
| 113 | + return None |
| 114 | + |
| 115 | + |
| 116 | +def _find_plugin_dirs_src(search_path): |
| 117 | + options_raw = get_output('find %s/../ -type d -name plugins' % search_path) |
| 118 | + |
| 119 | + if options_raw: |
| 120 | + return options_raw.split('\n') |
| 121 | + else: |
| 122 | + return None |
0 commit comments