|
| 1 | +"""Generate and work with PEP 425 Compatibility Tags.""" |
| 2 | + |
| 3 | +import sys |
| 4 | + |
| 5 | +try: |
| 6 | + import sysconfig |
| 7 | +except ImportError: # pragma nocover |
| 8 | + # Python < 2.7 |
| 9 | + import distutils.sysconfig as sysconfig |
| 10 | +import distutils.util |
| 11 | + |
| 12 | + |
| 13 | +def get_abbr_impl(): |
| 14 | + """Return abbreviated implementation name.""" |
| 15 | + if hasattr(sys, 'pypy_version_info'): |
| 16 | + pyimpl = 'pp' |
| 17 | + elif sys.platform.startswith('java'): |
| 18 | + pyimpl = 'jy' |
| 19 | + elif sys.platform == 'cli': |
| 20 | + pyimpl = 'ip' |
| 21 | + else: |
| 22 | + pyimpl = 'cp' |
| 23 | + return pyimpl |
| 24 | + |
| 25 | + |
| 26 | +def get_impl_ver(): |
| 27 | + """Return implementation version.""" |
| 28 | + impl_ver = sysconfig.get_config_var("py_version_nodot") |
| 29 | + if not impl_ver: |
| 30 | + impl_ver = ''.join(map(str, sys.version_info[:2])) |
| 31 | + return impl_ver |
| 32 | + |
| 33 | + |
| 34 | +def get_platform(): |
| 35 | + """Return our platform name 'win32', 'linux_x86_64'""" |
| 36 | + # XXX remove distutils dependency |
| 37 | + return distutils.util.get_platform().replace('.', '_').replace('-', '_') |
| 38 | + |
| 39 | + |
| 40 | +def get_supported(versions=None): |
| 41 | + """Return a list of supported tags for each version specified in |
| 42 | + `versions`. |
| 43 | +
|
| 44 | + :param versions: a list of string versions, of the form ["33", "32"], |
| 45 | + or None. The first version will be assumed to support our ABI. |
| 46 | + """ |
| 47 | + supported = [] |
| 48 | + |
| 49 | + # Versions must be given with respect to the preference |
| 50 | + if versions is None: |
| 51 | + versions = [] |
| 52 | + major = sys.version_info[0] |
| 53 | + # Support all previous minor Python versions. |
| 54 | + for minor in range(sys.version_info[1], -1, -1): |
| 55 | + versions.append(''.join(map(str, (major, minor)))) |
| 56 | + |
| 57 | + impl = get_abbr_impl() |
| 58 | + |
| 59 | + abis = [] |
| 60 | + |
| 61 | + soabi = sysconfig.get_config_var('SOABI') |
| 62 | + if soabi and soabi.startswith('cpython-'): |
| 63 | + abis[0:0] = ['cp' + soabi.split('-', 1)[-1]] |
| 64 | + |
| 65 | + abi3s = set() |
| 66 | + import imp |
| 67 | + for suffix in imp.get_suffixes(): |
| 68 | + if suffix[0].startswith('.abi'): |
| 69 | + abi3s.add(suffix[0].split('.', 2)[1]) |
| 70 | + |
| 71 | + abis.extend(sorted(list(abi3s))) |
| 72 | + |
| 73 | + abis.append('none') |
| 74 | + |
| 75 | + arch = get_platform() |
| 76 | + |
| 77 | + # Current version, current API (built specifically for our Python): |
| 78 | + for abi in abis: |
| 79 | + supported.append(('%s%s' % (impl, versions[0]), abi, arch)) |
| 80 | + |
| 81 | + # No abi / arch, but requires our implementation: |
| 82 | + for i, version in enumerate(versions): |
| 83 | + supported.append(('%s%s' % (impl, version), 'none', 'any')) |
| 84 | + if i == 0: |
| 85 | + # Tagged specifically as being cross-version compatible |
| 86 | + # (with just the major version specified) |
| 87 | + supported.append(('%s%s' % (impl, versions[0][0]), 'none', 'any')) |
| 88 | + |
| 89 | + # No abi / arch, generic Python |
| 90 | + for i, version in enumerate(versions): |
| 91 | + supported.append(('py%s' % (version,), 'none', 'any')) |
| 92 | + if i == 0: |
| 93 | + supported.append(('py%s' % (version[0]), 'none', 'any')) |
| 94 | + |
| 95 | + return supported |
| 96 | + |
| 97 | + |
0 commit comments