Similar to `from ctypes.util import find_library`, but try to return full path if possible.
(name)
| 232 | |
| 233 | |
| 234 | def find_library_full_path(name): |
| 235 | """ |
| 236 | Similar to `from ctypes.util import find_library`, but try |
| 237 | to return full path if possible. |
| 238 | """ |
| 239 | from ctypes.util import find_library |
| 240 | |
| 241 | if os.name == "posix" and sys.platform == "darwin": |
| 242 | # on Mac, ctypes already returns full path |
| 243 | return find_library(name) |
| 244 | |
| 245 | def _use_proc_maps(name): |
| 246 | """ |
| 247 | Find so from /proc/pid/maps |
| 248 | Only works with libraries that has already been loaded. |
| 249 | But this is the most accurate method -- it finds the exact library that's being used. |
| 250 | """ |
| 251 | procmap = os.path.join('/proc', str(os.getpid()), 'maps') |
| 252 | if not os.path.isfile(procmap): |
| 253 | return None |
| 254 | try: |
| 255 | with open(procmap, 'r') as f: |
| 256 | for line in f: |
| 257 | line = line.strip().split(' ') |
| 258 | sofile = line[-1] |
| 259 | |
| 260 | basename = os.path.basename(sofile) |
| 261 | if 'lib' + name + '.so' in basename: |
| 262 | if os.path.isfile(sofile): |
| 263 | return os.path.realpath(sofile) |
| 264 | except IOError: |
| 265 | # can fail in certain environment (e.g. chroot) |
| 266 | # if the pids are incorrectly mapped |
| 267 | pass |
| 268 | |
| 269 | # The following two methods come from https://github.com/python/cpython/blob/master/Lib/ctypes/util.py |
| 270 | def _use_ld(name): |
| 271 | """ |
| 272 | Find so with `ld -lname -Lpath`. |
| 273 | It will search for files in LD_LIBRARY_PATH, but not in ldconfig. |
| 274 | """ |
| 275 | cmd = "ld -t -l{} -o {}".format(name, os.devnull) |
| 276 | ld_lib_path = os.environ.get('LD_LIBRARY_PATH', '') |
| 277 | for d in ld_lib_path.split(':'): |
| 278 | cmd = cmd + " -L " + d |
| 279 | result, ret = subproc_call(cmd + '|| true') |
| 280 | expr = r'[^\(\)\s]*lib%s\.[^\(\)\s]*' % re.escape(name) |
| 281 | res = re.search(expr, result.decode('utf-8')) |
| 282 | if res: |
| 283 | res = res.group(0) |
| 284 | if not os.path.isfile(res): |
| 285 | return None |
| 286 | return os.path.realpath(res) |
| 287 | |
| 288 | def _use_ldconfig(name): |
| 289 | """ |
| 290 | Find so in `ldconfig -p`. |
| 291 | It does not handle LD_LIBRARY_PATH. |
nothing calls this directly
no test coverage detected