~ibmcharmers/charms/xenial/ibm-cinder-storwize-svc/trunk

« back to all changes in this revision

Viewing changes to .tox/py35/lib/python3.5/site-packages/pip/utils/glibc.py

  • Committer: Ankammarao
  • Date: 2017-03-06 05:11:42 UTC
  • Revision ID: achittet@in.ibm.com-20170306051142-dpg27z4es1k56hfn
Marked tests folder executable

Show diffs side-by-side

added added

removed removed

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