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