Source code for pex.pep425

# Copyright 2014 Pants project contributors (see CONTRIBUTORS.md).# Licensed under the Apache License, Version 2.0 (see LICENSE)."""PEP425 handling for pexPEP425 (http://legacy.python.org/dev/peps/pep-0425/) describes a tagging system used to determinewhether or not a distribution's platform is compatible with the current platform. It is thetagging system used to describe platform compatibility for wheel files."""frompkg_resourcesimportget_supported_platformfrom.platformsimportPlatform

[docs]defplatform_iterator(cls,platform):"""Iterate over all compatible platform tags of a supplied platform tag. :param platform: the platform tag to iterate over """ifcls.is_macosx_platform(platform):forplatincls.iter_compatible_osx_platforms(platform):yieldplatelse:yieldplatform

classPEP425(object):# noqaINTERPRETER_TAGS={'CPython':'cp','Jython':'jy','PyPy':'pp','IronPython':'ip',}@classmethoddefget_implementation_tag(cls,interpreter_subversion):returncls.INTERPRETER_TAGS.get(interpreter_subversion)@classmethoddefget_version_tag(cls,interpreter_version):return''.join(map(str,interpreter_version[:2]))@classmethoddeftranslate_platform_to_tag(cls,platform):returnplatform.replace('.','_').replace('-','_')@classmethoddefget_platform_tag(cls):returncls.translate_platform_to_tag(get_supported_platform())# TODO(wickman) This implementation is technically incorrect but we need to be able to# predict the supported tags of an interpreter that may not be on this machine or# of a different platform. Alternatively we could store the manifest of supported tags# of a targeted platform in a file to be more correct.@classmethoddef_iter_supported_tags(cls,impl,version,platform):"""Given a set of tags, iterate over supported tags. :param impl: Python implementation tag e.g. cp, jy, pp. :param version: E.g. '26', '33' :param platform: Platform as from :function:`pkg_resources.get_supported_platform`, for example 'linux-x86_64' or 'macosx-10.4-x86_64'. :returns: Iterator over (pyver, abi, platform) tuples. """# Predict soabi for reasonable interpreters. This is technically wrong but essentially right.abis=[]ifimpl=='cp'andversion.startswith('3'):abis.extend(['cp%s'%version,'cp%sdmu'%version,'cp%sdm'%version,'cp%sdu'%version,'cp%sd'%version,'cp%smu'%version,'cp%sm'%version,'cp%su'%version,'abi3'])major_version=int(version[0])minor_versions=[]forminorinrange(int(version[1]),-1,-1):minor_versions.append('%d%d'%(major_version,minor))platforms=list(PEP425Extras.platform_iterator(cls.translate_platform_to_tag(platform)))# interpreter specificforpinplatforms:forabiinabis:yield('%s%s'%(impl,version),abi,p)# everything elseforpinplatforms+['any']:foriin('py',impl):yield('%s%d'%(i,major_version),'none',p)forminor_versioninminor_versions:yield('%s%s'%(i,minor_version),'none',p)@classmethoddefiter_supported_tags(cls,identity,platform=get_supported_platform()):"""Iterate over the supported tag tuples of this interpreter. :param identity: python interpreter identity over which tags should iterate. :type identity: :class:`PythonIdentity` :param platform: python platform over which tags should iterate, by default the current platform. :returns: Iterator over valid PEP425 tag tuples. """impl_tag=cls.get_implementation_tag(identity.interpreter)vers_tag=cls.get_version_tag(identity.version)tag_iterator=cls._iter_supported_tags(impl_tag,vers_tag,platform)fortagintag_iterator:yieldtag