1# coding: utf-8
2
3"""
4This file was originally derived from
5https://github.com/pypa/pip/blob/3e713708088aedb1cde32f3c94333d6e29aaf86e/src/pip/_internal/pep425tags.py
6
7The following license covers that code:
8
9Copyright (c) 2008-2018 The pip developers (see AUTHORS.txt file)
10
11Permission is hereby granted, free of charge, to any person obtaining
12a copy of this software and associated documentation files (the
13"Software"), to deal in the Software without restriction, including
14without limitation the rights to use, copy, modify, merge, publish,
15distribute, sublicense, and/or sell copies of the Software, and to
16permit persons to whom the Software is furnished to do so, subject to
17the following conditions:
18
19The above copyright notice and this permission notice shall be
20included in all copies or substantial portions of the Software.
21
22THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29"""
30
31from __future__ import unicode_literals, division, absolute_import, print_function
32
33import sys
34import os
35import ctypes
36import re
37import platform
38
39if sys.version_info >= (2, 7):
40    import sysconfig
41
42if sys.version_info < (3,):
43    str_cls = unicode  # noqa
44else:
45    str_cls = str
46
47
48def _pep425_implementation():
49    """
50    :return:
51        A 2 character unicode string of the implementation - 'cp' for cpython
52        or 'pp' for PyPy
53    """
54
55    return 'pp' if hasattr(sys, 'pypy_version_info') else 'cp'
56
57
58def _pep425_version():
59    """
60    :return:
61        A tuple of integers representing the Python version number
62    """
63
64    if hasattr(sys, 'pypy_version_info'):
65        return (sys.version_info[0], sys.pypy_version_info.major,
66                sys.pypy_version_info.minor)
67    else:
68        return (sys.version_info[0], sys.version_info[1])
69
70
71def _pep425_supports_manylinux():
72    """
73    :return:
74        A boolean indicating if the machine can use manylinux1 packages
75    """
76
77    try:
78        import _manylinux
79        return bool(_manylinux.manylinux1_compatible)
80    except (ImportError, AttributeError):
81        pass
82
83    # Check for glibc 2.5
84    try:
85        proc = ctypes.CDLL(None)
86        gnu_get_libc_version = proc.gnu_get_libc_version
87        gnu_get_libc_version.restype = ctypes.c_char_p
88
89        ver = gnu_get_libc_version()
90        if not isinstance(ver, str_cls):
91            ver = ver.decode('ascii')
92        match = re.match(r'(\d+)\.(\d+)', ver)
93        return match and match.group(1) == '2' and int(match.group(2)) >= 5
94
95    except (AttributeError):
96        return False
97
98
99def _pep425_get_abi():
100    """
101    :return:
102        A unicode string of the system abi. Will be something like: "cp27m",
103        "cp33m", etc.
104    """
105
106    try:
107        soabi = sysconfig.get_config_var('SOABI')
108        if soabi:
109            if soabi.startswith('cpython-'):
110                return 'cp%s' % soabi.split('-')[1]
111            return soabi.replace('.', '_').replace('-', '_')
112    except (IOError, NameError):
113        pass
114
115    impl = _pep425_implementation()
116    suffix = ''
117    if impl == 'cp':
118        suffix += 'm'
119    if sys.maxunicode == 0x10ffff and sys.version_info < (3, 3):
120        suffix += 'u'
121    return '%s%s%s' % (impl, ''.join(map(str_cls, _pep425_version())), suffix)
122
123
124def _pep425tags():
125    """
126    :return:
127        A list of 3-element tuples with unicode strings or None:
128         [0] implementation tag - cp33, pp27, cp26, py2, py2.py3
129         [1] abi tag - cp26m, None
130         [2] arch tag - linux_x86_64, macosx_10_10_x85_64, etc
131    """
132
133    tags = []
134
135    versions = []
136    version_info = _pep425_version()
137    major = version_info[:-1]
138    for minor in range(version_info[-1], -1, -1):
139        versions.append(''.join(map(str, major + (minor,))))
140
141    impl = _pep425_implementation()
142
143    abis = []
144    abi = _pep425_get_abi()
145    if abi:
146        abis.append(abi)
147    abi3 = _pep425_implementation() == 'cp' and sys.version_info >= (3,)
148    if abi3:
149        abis.append('abi3')
150    abis.append('none')
151
152    if sys.platform == 'darwin':
153        plat_ver = platform.mac_ver()
154        ver_parts = plat_ver[0].split('.')
155        minor = int(ver_parts[1])
156        arch = plat_ver[2]
157        if sys.maxsize == 2147483647:
158            arch = 'i386'
159        arches = []
160        while minor > 5:
161            arches.append('macosx_10_%s_%s' % (minor, arch))
162            arches.append('macosx_10_%s_intel' % (minor,))
163            arches.append('macosx_10_%s_universal' % (minor,))
164            minor -= 1
165    else:
166        if sys.platform == 'win32':
167            if 'amd64' in sys.version.lower():
168                arches = ['win_amd64']
169            arches = [sys.platform]
170        elif hasattr(os, 'uname'):
171            (plat, _, _, _, machine) = os.uname()
172            plat = plat.lower().replace('/', '')
173            machine.replace(' ', '_').replace('/', '_')
174            if plat == 'linux' and sys.maxsize == 2147483647:
175                machine = 'i686'
176            arch = '%s_%s' % (plat, machine)
177            if _pep425_supports_manylinux():
178                arches = [arch.replace('linux', 'manylinux1'), arch]
179            else:
180                arches = [arch]
181
182    for abi in abis:
183        for arch in arches:
184            tags.append(('%s%s' % (impl, versions[0]), abi, arch))
185
186    if abi3:
187        for version in versions[1:]:
188            for arch in arches:
189                tags.append(('%s%s' % (impl, version), 'abi3', arch))
190
191    for arch in arches:
192        tags.append(('py%s' % (versions[0][0]), 'none', arch))
193
194    tags.append(('%s%s' % (impl, versions[0]), 'none', 'any'))
195    tags.append(('%s%s' % (impl, versions[0][0]), 'none', 'any'))
196
197    for i, version in enumerate(versions):
198        tags.append(('py%s' % (version,), 'none', 'any'))
199        if i == 0:
200            tags.append(('py%s' % (version[0]), 'none', 'any'))
201
202    tags.append(('py2.py3', 'none', 'any'))
203
204    return tags
205