glibc.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. # This file originally from pip:
  2. # https://github.com/pypa/pip/blob/8f4f15a5a95d7d5b511ceaee9ed261176c181970/src/pip/_internal/utils/glibc.py
  3. from __future__ import absolute_import
  4. import ctypes
  5. import re
  6. import warnings
  7. def glibc_version_string():
  8. "Returns glibc version string, or None if not using glibc."
  9. # ctypes.CDLL(None) internally calls dlopen(NULL), and as the dlopen
  10. # manpage says, "If filename is NULL, then the returned handle is for the
  11. # main program". This way we can let the linker do the work to figure out
  12. # which libc our process is actually using.
  13. process_namespace = ctypes.CDLL(None)
  14. try:
  15. gnu_get_libc_version = process_namespace.gnu_get_libc_version
  16. except AttributeError:
  17. # Symbol doesn't exist -> therefore, we are not linked to
  18. # glibc.
  19. return None
  20. # Call gnu_get_libc_version, which returns a string like "2.5"
  21. gnu_get_libc_version.restype = ctypes.c_char_p
  22. version_str = gnu_get_libc_version()
  23. # py2 / py3 compatibility:
  24. if not isinstance(version_str, str):
  25. version_str = version_str.decode("ascii")
  26. return version_str
  27. # Separated out from have_compatible_glibc for easier unit testing
  28. def check_glibc_version(version_str, required_major, minimum_minor):
  29. # Parse string and check against requested version.
  30. #
  31. # We use a regexp instead of str.split because we want to discard any
  32. # random junk that might come after the minor version -- this might happen
  33. # in patched/forked versions of glibc (e.g. Linaro's version of glibc
  34. # uses version strings like "2.20-2014.11"). See gh-3588.
  35. m = re.match(r"(?P<major>[0-9]+)\.(?P<minor>[0-9]+)", version_str)
  36. if not m:
  37. warnings.warn("Expected glibc version with 2 components major.minor,"
  38. " got: %s" % version_str, RuntimeWarning)
  39. return False
  40. return (int(m.group("major")) == required_major and
  41. int(m.group("minor")) >= minimum_minor)
  42. def have_compatible_glibc(required_major, minimum_minor):
  43. version_str = glibc_version_string()
  44. if version_str is None:
  45. return False
  46. return check_glibc_version(version_str, required_major, minimum_minor)
  47. # platform.libc_ver regularly returns completely nonsensical glibc
  48. # versions. E.g. on my computer, platform says:
  49. #
  50. # ~$ python2.7 -c 'import platform; print(platform.libc_ver())'
  51. # ('glibc', '2.7')
  52. # ~$ python3.5 -c 'import platform; print(platform.libc_ver())'
  53. # ('glibc', '2.9')
  54. #
  55. # But the truth is:
  56. #
  57. # ~$ ldd --version
  58. # ldd (Debian GLIBC 2.22-11) 2.22
  59. #
  60. # This is unfortunate, because it means that the linehaul data on libc
  61. # versions that was generated by pip 8.1.2 and earlier is useless and
  62. # misleading. Solution: instead of using platform, use our code that actually
  63. # works.
  64. def libc_ver():
  65. """Try to determine the glibc version
  66. Returns a tuple of strings (lib, version) which default to empty strings
  67. in case the lookup fails.
  68. """
  69. glibc_version = glibc_version_string()
  70. if glibc_version is None:
  71. return ("", "")
  72. else:
  73. return ("glibc", glibc_version)