def tags(cls): from packaging import tags for tag in tags.sys_tags(): yield tag # FIXME: Work around for https://github.com/pypa/packaging/pull/319 and Big Sur if sys.platform == 'darwin' and Version.from_string(platform.mac_ver()[0]) > Version(10): for override in tags.mac_platforms(version=(10, 16)): for tag in tags.sys_tags(): if not tag.platform: pass yield tags.Tag(tag.interpreter, tag.abi, override)
def test_generic_sys_tags(monkeypatch): monkeypatch.setattr(platform, "system", lambda: "Generic") monkeypatch.setattr(tags, "_interpreter_name", lambda: "generic") result = list(tags.sys_tags()) expected = tags.Tag("py{}0".format(sys.version_info[0]), "none", "any") assert result[-1] == expected
def find_wheel_url(project: str, version: str, content: str): project = project.replace("-", "_") tags = set(sys_tags()) links = LinkFinder.extract_links(content) not_matched = [] found = False for link in links: link = posixpath.basename(unquote_plus(link)) if not link.endswith(".whl"): continue wproject, wversion, wtags = link[:-4].split("-", 2) if wproject.lower() != project.lower(): continue # Add to list so we can print it at the end if nothing matches not_matched.append(link) if wversion != version: continue for wtag in parse_tag(wtags): if wtag in tags: print("Found matching wheel", link) return True if not found: print("Did not find matching wheels in:") for link in not_matched: print("-", link) return False
def is_compatible(self): if self.filename.endswith("py3-none-any.whl"): return True for tag in sys_tags(): if tag in self.tags: return True return False
def test_generic(self, monkeypatch): monkeypatch.setattr(platform, "system", lambda: "Generic") monkeypatch.setattr(tags, "interpreter_name", lambda: "generic") result = list(tags.sys_tags()) expected = tags.Tag("py{}0".format(sys.version_info[0]), "none", "any") assert result[-1] == expected
def ptvsd_folder_name(): """Return the folder name for the bundled PTVSD wheel compatible with the new debug adapter.""" with open(REQUIREMENTS, "r", encoding="utf-8") as reqsfile: for line in reqsfile: pkgreq = Requirement(line) if pkgreq.name == "ptvsd": specs = pkgreq.specifier try: spec, = specs version = spec.version except: # Fallpack to use base PTVSD path. print(PYTHONFILES, end="") return break try: for tag in sys_tags(): folder_name = f"ptvsd-{version}-{tag.interpreter}-{tag.abi}-{tag.platform}" folder_path = os.path.join(PYTHONFILES, folder_name) if os.path.exists(folder_path): print(folder_path, end="") return except: # Fallback to use base PTVSD path no matter the exception. print(PYTHONFILES, end="") return # Default fallback to use base PTVSD path. print(PYTHONFILES, end="")
def get_supported(): """Return a list of supported tags.""" tags = [(t.interpreter, t.abi, t.platform) for t in sys_tags()] for t in list(tags): if t[1].startswith('cp'): tags.append((t[0], t[1] + 'm', t[2])) # Copy tags with 'm' added to the abi return tags
def get_binary_tag(): """ Return most-specific binary extension wheel tag 'interpreter-abi-arch' """ from packaging import tags return str( next(tag for tag in tags.sys_tags() if not "manylinux" in tag.platform))
def test_pypy_first_none_any_tag(self, monkeypatch): # When building the complete list of pypy tags, make sure the first # <interpreter>-none-any one is pp3-none-any monkeypatch.setattr(tags, "interpreter_name", lambda: "pp") for tag in tags.sys_tags(): if tag.abi == "none" and tag.platform == "any": break assert tag == tags.Tag("pp3", "none", "any")
def get_abi3_tag(): """ Return first abi3 tag, or the first binary tag if abi3 is not supported. """ from packaging import tags try: return str( next(tag for tag in tags.sys_tags() if "abi3" in tag.abi and "manylinux" not in tag.platform)) except StopIteration: return get_binary_tag()
def test_sys_tags_on_mac_pypy(monkeypatch): if platform.python_implementation() != "PyPy": monkeypatch.setattr(platform, "python_implementation", lambda: "PyPy") monkeypatch.setattr(tags, "_pypy_interpreter", lambda: "pp360") if platform.system() != "Darwin": monkeypatch.setattr(platform, "system", lambda: "Darwin") monkeypatch.setattr(tags, "_mac_platforms", lambda: ["macosx_10_5_x86_64"]) interpreter = tags._pypy_interpreter() abi = tags._generic_abi() platforms = tags._mac_platforms() result = list(tags.sys_tags()) assert result[0] == tags.Tag(interpreter, abi, platforms[0]) assert result[-1] == tags.Tag("py{}0".format(sys.version_info[0]), "none", "any")
def tag(self): if self._package.build_script: tag = next(sys_tags()) tag = (tag.interpreter, tag.abi, tag.platform) else: platform = "any" if self.supports_python2(): impl = "py2.py3" else: impl = "py3" tag = (impl, "none", platform) return "-".join(tag)
def get_tag(self): # bdist sets self.plat_name if unset, we should only use it for purepy # wheels if the user supplied it. if self.plat_name_supplied: plat_name = self.plat_name elif self.root_is_pure: plat_name = 'any' else: # macosx contains system version in platform name so need special handle if self.plat_name and not self.plat_name.startswith("macosx"): plat_name = self.plat_name else: # on macosx always limit the platform name to comply with any # c-extension modules in bdist_dir, since the user can specify # a higher MACOSX_DEPLOYMENT_TARGET via tools like CMake # on other platforms, and on macosx if there are no c-extension # modules, use the default platform name. plat_name = get_platform(self.bdist_dir) if plat_name in ('linux-x86_64', 'linux_x86_64') and sys.maxsize == 2147483647: plat_name = 'linux_i686' plat_name = plat_name.lower().replace('-', '_').replace('.', '_') if self.root_is_pure: if self.universal: impl = 'py2.py3' else: impl = self.python_tag tag = (impl, 'none', plat_name) else: impl_name = tags.interpreter_name() impl_ver = tags.interpreter_version() impl = impl_name + impl_ver # We don't work on CPython 3.1, 3.0. if self.py_limited_api and (impl_name + impl_ver).startswith('cp3'): impl = self.py_limited_api abi_tag = 'abi3' else: abi_tag = str(get_abi_tag()).lower() tag = (impl, abi_tag, plat_name) supported_tags = [(t.interpreter, t.abi, t.platform) for t in tags.sys_tags()] assert tag in supported_tags, "would build wheel with unsupported tag {}".format( tag) return tag
def is_compatible(package): """ Check whether the given python package is a wheel compatible with the current platform and python interpreter. Compatibility is based on https://www.python.org/dev/peps/pep-0425/ """ try: w = parse_wheel_filename(package) for systag in tags.sys_tags(): for tag in w.tag_triples(): if systag in tags.parse_tag(tag): return True except InvalidFilenameError: return False
def test_sys_tags_on_mac_cpython(monkeypatch): if platform.python_implementation() != "CPython": monkeypatch.setattr(platform, "python_implementation", lambda: "CPython") monkeypatch.setattr(tags, "_cpython_abi", lambda py_version: "cp33m") if platform.system() != "Darwin": monkeypatch.setattr(platform, "system", lambda: "Darwin") monkeypatch.setattr(tags, "_mac_platforms", lambda: ["macosx_10_5_x86_64"]) abi = tags._cpython_abi(sys.version_info[:2]) platforms = tags._mac_platforms() result = list(tags.sys_tags()) assert result[0] == tags.Tag( "cp{major}{minor}".format(major=sys.version_info[0], minor=sys.version_info[1]), abi, platforms[0], ) assert result[-1] == tags.Tag("py{}0".format(sys.version_info[0]), "none", "any")
def test_sys_tags_linux_cpython(monkeypatch): if platform.python_implementation() != "CPython": monkeypatch.setattr(platform, "python_implementation", lambda: "CPython") monkeypatch.setattr(tags, "_cpython_abi", lambda py_version: "cp33m") if platform.system() != "Linux": monkeypatch.setattr(platform, "system", lambda: "Linux") monkeypatch.setattr(tags, "_linux_platforms", lambda: ["linux_x86_64"]) abi = tags._cpython_abi(sys.version_info[:2]) platforms = tags._linux_platforms() result = list(tags.sys_tags()) expected_interpreter = "cp{major}{minor}".format(major=sys.version_info[0], minor=sys.version_info[1]) assert result[0] == tags.Tag(expected_interpreter, abi, platforms[0]) expected = tags.Tag("py{}0".format(sys.version_info[0]), "none", "any") assert result[-1] == expected
def test_linux_cpython(self, mock_interpreter_name, monkeypatch): if mock_interpreter_name("CPython"): monkeypatch.setattr(tags, "_cpython_abis", lambda *a: ["cp33m"]) if platform.system() != "Linux": monkeypatch.setattr(platform, "system", lambda: "Linux") monkeypatch.setattr(tags, "_linux_platforms", lambda: ["linux_x86_64"]) abis = list(tags._cpython_abis(sys.version_info[:2])) platforms = list(tags._linux_platforms()) result = list(tags.sys_tags()) expected_interpreter = "cp" + tags._version_nodot(sys.version_info[:2]) assert len(abis) == 1 assert result[0] == tags.Tag(expected_interpreter, abis[0], platforms[0]) expected = tags.Tag( "py" + tags._version_nodot((sys.version_info[0], 0)), "none", "any" ) assert result[-1] == expected
def test_mac_cpython(self, mock_interpreter_name, monkeypatch): if mock_interpreter_name("CPython"): monkeypatch.setattr(tags, "_cpython_abis", lambda *a: ["cp33m"]) if platform.system() != "Darwin": monkeypatch.setattr(platform, "system", lambda: "Darwin") monkeypatch.setattr(tags, "mac_platforms", lambda: ["macosx_10_5_x86_64"]) abis = tags._cpython_abis(sys.version_info[:2]) platforms = list(tags.mac_platforms()) result = list(tags.sys_tags()) assert len(abis) == 1 assert result[0] == tags.Tag( "cp" + tags._version_nodot(sys.version_info[:2]), abis[0], platforms[0] ) assert result[-1] == tags.Tag( "py" + tags._version_nodot((sys.version_info[0], 0)), "none", "any" )
def test_folder_exists(self, capsys): # Return the first constructed folder path as existing. patcher = patch("os.path.exists") mock_exists = patcher.start() mock_exists.side_effect = lambda p: True tag = next(sys_tags()) folder = "ptvsd-{}-{}-{}".format(tag.interpreter, tag.abi, tag.platform) ptvsd_folder_name() patcher.stop() expected = os.path.join(PYTHONFILES, folder) captured = capsys.readouterr() assert captured.out == expected
def test_sys_tags_linux_cpython(monkeypatch): if platform.python_implementation() != "CPython": monkeypatch.setattr(platform, "python_implementation", lambda: "CPython") monkeypatch.setattr(tags, "_cpython_abi", lambda py_version: "cp33m") if platform.system() != "Linux": monkeypatch.setattr(platform, "system", lambda: "Linux") monkeypatch.setattr(tags, "_linux_platforms", lambda: ["linux_x86_64"]) abi = tags._cpython_abi(sys.version_info[:2]) platforms = tags._linux_platforms() result = list(tags.sys_tags()) expected_interpreter = "cp{major}{minor}".format( major=sys.version_info[0], minor=sys.version_info[1] ) assert result[0] == tags.Tag(expected_interpreter, abi, platforms[0]) expected = tags.Tag("py{}0".format(sys.version_info[0]), "none", "any") assert result[-1] == expected
def get_tag(self): # bdist sets self.plat_name if unset, we should only use it for purepy # wheels if the user supplied it. if self.plat_name_supplied: plat_name = self.plat_name elif self.root_is_pure: plat_name = 'any' else: # macosx contains system version in platform name so need special handle if self.plat_name and not self.plat_name.startswith("macosx"): plat_name = self.plat_name else: plat_name = get_platform(self.bdist_dir) if plat_name in ('linux-x86_64', 'linux_x86_64') and sys.maxsize == 2147483647: plat_name = 'linux_i686' plat_name = plat_name.replace('-', '_').replace('.', '_') if self.root_is_pure: if self.universal: impl = 'py2.py3' else: impl = self.python_tag tag = (impl, 'none', plat_name) else: impl_name = tags.interpreter_name() impl_ver = tags.interpreter_version() impl = impl_name + impl_ver # We don't work on CPython 3.1, 3.0. if self.py_limited_api and (impl_name + impl_ver).startswith('cp3'): impl = self.py_limited_api abi_tag = 'abi3' else: abi_tag = str(get_abi_tag()).lower() abi_tag = self.abi_tag tag = (impl, abi_tag, plat_name) supported_tags = [(t.interpreter, t.abi, t.platform) for t in tags.sys_tags()] # XXX switch to this alternate implementation for non-pure: if not self.py_limited_api: assert tag == supported_tags[0], "%s != %s" % ( tag, supported_tags[0]) # assert tag in supported_tags, "would build wheel with unsupported tag {}".format(tag) return tag
def ptvsd_folder_name(): """Return the folder name for the bundled PTVSD wheel compatible with the new debug adapter.""" try: for tag in sys_tags(): folder_name = f"ptvsd-{tag.interpreter}-{tag.abi}-{tag.platform}" folder_path = os.path.join(PYTHONFILES, folder_name) if os.path.exists(folder_path): print(folder_path, end="") return except: # Fallback to use base PTVSD path no matter the exception. print(PYTHONFILES, end="") return # Default fallback to use base PTVSD path. print(PYTHONFILES, end="")
def test_sys_tags_on_mac_cpython(monkeypatch): if platform.python_implementation() != "CPython": monkeypatch.setattr(platform, "python_implementation", lambda: "CPython") monkeypatch.setattr(tags, "_cpython_abis", lambda py_version: ["cp33m"]) if platform.system() != "Darwin": monkeypatch.setattr(platform, "system", lambda: "Darwin") monkeypatch.setattr(tags, "_mac_platforms", lambda: ["macosx_10_5_x86_64"]) abis = tags._cpython_abis(sys.version_info[:2]) platforms = tags._mac_platforms() result = list(tags.sys_tags()) assert len(abis) == 1 assert result[0] == tags.Tag( "cp{major}{minor}".format(major=sys.version_info[0], minor=sys.version_info[1]), abis[0], platforms[0], ) assert result[-1] == tags.Tag("py{}0".format(sys.version_info[0]), "none", "any")
def test_windows_cpython(self, mock_interpreter_name, monkeypatch): if mock_interpreter_name("CPython"): monkeypatch.setattr(tags, "_cpython_abis", lambda *a: ["cp33m"]) if platform.system() != "Windows": monkeypatch.setattr(platform, "system", lambda: "Windows") monkeypatch.setattr(tags, "_generic_platforms", lambda: ["win_amd64"]) abis = list(tags._cpython_abis(sys.version_info[:2])) platforms = list(tags._generic_platforms()) result = list(tags.sys_tags()) interpreter = "cp{major}{minor}".format(major=sys.version_info[0], minor=sys.version_info[1]) assert len(abis) == 1 expected = tags.Tag(interpreter, abis[0], platforms[0]) assert result[0] == expected expected = tags.Tag("py{}0".format(sys.version_info[0]), "none", "any") assert result[-1] == expected
import sys from packaging import tags to_check = "--" found = False if len(sys.argv) > 1: to_check = sys.argv[1] for t in tags.sys_tags(): start = "-".join(str(t).split("-")[:2]) if to_check.lower() == start: print("Wheel tag {0} matches installed version {1}.".format( to_check, t)) found = True break if not found: print("Wheel tag {0} not found in installed version tags {1}.".format( to_check, [str(t) for t in tags.sys_tags()])) exit(1)
def is_compatible(self): '''Is the wheel is compatible with the current platform?''' supported_tags = set( (t.interpreter, t.abi, t.platform) for t in sys_tags()) return next((True for t in self.tags() if t in supported_tags), False)
def test_iterator(self): assert isinstance(tags.sys_tags(), collections_abc.Iterator)
def get_tag(self): # bdist sets self.plat_name if unset, we should only use it for purepy # wheels if the user supplied it. if self.plat_name_supplied: plat_name = self.plat_name elif self.root_is_pure: plat_name = 'any' else: # macosx contains system version in platform name so need special handle if self.plat_name and not self.plat_name.startswith("macosx"): plat_name = self.plat_name else: # on macosx always limit the platform name to comply with any # c-extension modules in bdist_dir, since the user can specify # a higher MACOSX_DEPLOYMENT_TARGET via tools like CMake # on other platforms, and on macosx if there are no c-extension # modules, use the default platform name. plat_name = get_platform(self.bdist_dir) if plat_name in ('linux-x86_64', 'linux_x86_64') and sys.maxsize == 2147483647: plat_name = 'linux_i686' # To allow uploading to pypi, we need the wheel name # to contain 'manylinux1'. # The wheel which will be uploaded to pypi will be # built on RHEL7, so it doesn't completely qualify for # manylinux1 support, but it's the minimum requirement # for building Qt. We only enable this for x64 limited # api builds (which are the only ones uploaded to # pypi). # TODO: Add actual distro detection, instead of # relying on limited_api option. if (plat_name in ('linux-x86_64', 'linux_x86_64') and sys.maxsize > 2147483647 and (self.py_limited_api or sys.version_info[0] == 2)): plat_name = 'manylinux1_x86_64' plat_name = plat_name.replace('-', '_').replace('.', '_') if self.root_is_pure: if self.universal: impl = 'py2.py3' else: impl = self.python_tag tag = (impl, 'none', plat_name) else: impl_name = tags.interpreter_name() impl_ver = tags.interpreter_version() impl = impl_name + impl_ver # We don't work on CPython 3.1, 3.0. if self.py_limited_api and (impl_name + impl_ver).startswith('cp3'): impl = self.py_limited_api abi_tag = "abi3" if sys.platform != "win32" else "none" else: abi_tag = str(get_abi_tag()).lower() tag = (impl, abi_tag, plat_name) supported_tags = [(t.interpreter, t.abi, t.platform) for t in tags.sys_tags()] # XXX switch to this alternate implementation for non-pure: if (self.py_limited_api) or (plat_name in ('manylinux1_x86_64') and sys.version_info[0] == 2): return tag assert tag in supported_tags, ( "would build wheel with unsupported tag {}".format(tag)) return tag
def get_supported_tags(self): # type: () -> List[Tag] return list(sys_tags())
def get_supported_tags(self) -> List[Tag]: return list(sys_tags())
def get_supported_platforms(platforms: Iterable[str]) -> Set[str]: if not platforms: platforms = {t.platform for t in sys_tags()} else: platforms = set(platforms) return platforms