Path: blob/main/test/lib/python3.9/site-packages/pip/_internal/utils/packaging.py
4804 views
import functools1import logging2import re3from typing import NewType, Optional, Tuple, cast45from pip._vendor.packaging import specifiers, version6from pip._vendor.packaging.requirements import Requirement78NormalizedExtra = NewType("NormalizedExtra", str)910logger = logging.getLogger(__name__)111213def check_requires_python(14requires_python: Optional[str], version_info: Tuple[int, ...]15) -> bool:16"""17Check if the given Python version matches a "Requires-Python" specifier.1819:param version_info: A 3-tuple of ints representing a Python20major-minor-micro version to check (e.g. `sys.version_info[:3]`).2122:return: `True` if the given Python version satisfies the requirement.23Otherwise, return `False`.2425:raises InvalidSpecifier: If `requires_python` has an invalid format.26"""27if requires_python is None:28# The package provides no information29return True30requires_python_specifier = specifiers.SpecifierSet(requires_python)3132python_version = version.parse(".".join(map(str, version_info)))33return python_version in requires_python_specifier343536@functools.lru_cache(maxsize=512)37def get_requirement(req_string: str) -> Requirement:38"""Construct a packaging.Requirement object with caching"""39# Parsing requirement strings is expensive, and is also expected to happen40# with a low diversity of different arguments (at least relative the number41# constructed). This method adds a cache to requirement object creation to42# minimize repeated parsing of the same string to construct equivalent43# Requirement objects.44return Requirement(req_string)454647def safe_extra(extra: str) -> NormalizedExtra:48"""Convert an arbitrary string to a standard 'extra' name4950Any runs of non-alphanumeric characters are replaced with a single '_',51and the result is always lowercased.5253This function is duplicated from ``pkg_resources``. Note that this is not54the same to either ``canonicalize_name`` or ``_egg_link_name``.55"""56return cast(NormalizedExtra, re.sub("[^A-Za-z0-9.-]+", "_", extra).lower())575859