You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

glibc.py 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. # The following comment should be removed at some point in the future.
  2. # mypy: strict-optional=False
  3. from __future__ import absolute_import
  4. import os
  5. import re
  6. import warnings
  7. from pip._internal.utils.typing import MYPY_CHECK_RUNNING
  8. if MYPY_CHECK_RUNNING:
  9. from typing import Optional, Tuple
  10. def glibc_version_string():
  11. # type: () -> Optional[str]
  12. "Returns glibc version string, or None if not using glibc."
  13. return glibc_version_string_confstr() or glibc_version_string_ctypes()
  14. def glibc_version_string_confstr():
  15. # type: () -> Optional[str]
  16. "Primary implementation of glibc_version_string using os.confstr."
  17. # os.confstr is quite a bit faster than ctypes.DLL. It's also less likely
  18. # to be broken or missing. This strategy is used in the standard library
  19. # platform module:
  20. # https://github.com/python/cpython/blob/fcf1d003bf4f0100c9d0921ff3d70e1127ca1b71/Lib/platform.py#L175-L183
  21. try:
  22. # os.confstr("CS_GNU_LIBC_VERSION") returns a string like "glibc 2.17":
  23. _, version = os.confstr("CS_GNU_LIBC_VERSION").split()
  24. except (AttributeError, OSError, ValueError):
  25. # os.confstr() or CS_GNU_LIBC_VERSION not available (or a bad value)...
  26. return None
  27. return version
  28. def glibc_version_string_ctypes():
  29. # type: () -> Optional[str]
  30. "Fallback implementation of glibc_version_string using ctypes."
  31. try:
  32. import ctypes
  33. except ImportError:
  34. return None
  35. # ctypes.CDLL(None) internally calls dlopen(NULL), and as the dlopen
  36. # manpage says, "If filename is NULL, then the returned handle is for the
  37. # main program". This way we can let the linker do the work to figure out
  38. # which libc our process is actually using.
  39. process_namespace = ctypes.CDLL(None)
  40. try:
  41. gnu_get_libc_version = process_namespace.gnu_get_libc_version
  42. except AttributeError:
  43. # Symbol doesn't exist -> therefore, we are not linked to
  44. # glibc.
  45. return None
  46. # Call gnu_get_libc_version, which returns a string like "2.5"
  47. gnu_get_libc_version.restype = ctypes.c_char_p
  48. version_str = gnu_get_libc_version()
  49. # py2 / py3 compatibility:
  50. if not isinstance(version_str, str):
  51. version_str = version_str.decode("ascii")
  52. return version_str
  53. # Separated out from have_compatible_glibc for easier unit testing
  54. def check_glibc_version(version_str, required_major, minimum_minor):
  55. # type: (str, int, int) -> bool
  56. # Parse string and check against requested version.
  57. #
  58. # We use a regexp instead of str.split because we want to discard any
  59. # random junk that might come after the minor version -- this might happen
  60. # in patched/forked versions of glibc (e.g. Linaro's version of glibc
  61. # uses version strings like "2.20-2014.11"). See gh-3588.
  62. m = re.match(r"(?P<major>[0-9]+)\.(?P<minor>[0-9]+)", version_str)
  63. if not m:
  64. warnings.warn("Expected glibc version with 2 components major.minor,"
  65. " got: %s" % version_str, RuntimeWarning)
  66. return False
  67. return (int(m.group("major")) == required_major and
  68. int(m.group("minor")) >= minimum_minor)
  69. def have_compatible_glibc(required_major, minimum_minor):
  70. # type: (int, int) -> bool
  71. version_str = glibc_version_string()
  72. if version_str is None:
  73. return False
  74. return check_glibc_version(version_str, required_major, minimum_minor)
  75. # platform.libc_ver regularly returns completely nonsensical glibc
  76. # versions. E.g. on my computer, platform says:
  77. #
  78. # ~$ python2.7 -c 'import platform; print(platform.libc_ver())'
  79. # ('glibc', '2.7')
  80. # ~$ python3.5 -c 'import platform; print(platform.libc_ver())'
  81. # ('glibc', '2.9')
  82. #
  83. # But the truth is:
  84. #
  85. # ~$ ldd --version
  86. # ldd (Debian GLIBC 2.22-11) 2.22
  87. #
  88. # This is unfortunate, because it means that the linehaul data on libc
  89. # versions that was generated by pip 8.1.2 and earlier is useless and
  90. # misleading. Solution: instead of using platform, use our code that actually
  91. # works.
  92. def libc_ver():
  93. # type: () -> Tuple[str, str]
  94. """Try to determine the glibc version
  95. Returns a tuple of strings (lib, version) which default to empty strings
  96. in case the lookup fails.
  97. """
  98. glibc_version = glibc_version_string()
  99. if glibc_version is None:
  100. return ("", "")
  101. else:
  102. return ("glibc", glibc_version)