Path: blob/master/venv/Lib/site-packages/pip/_internal/utils/packaging.py
811 views
from __future__ import absolute_import12import logging3from email.parser import FeedParser45from pip._vendor import pkg_resources6from pip._vendor.packaging import specifiers, version78from pip._internal.exceptions import NoneMetadataError9from pip._internal.utils.misc import display_path10from pip._internal.utils.typing import MYPY_CHECK_RUNNING1112if MYPY_CHECK_RUNNING:13from typing import Optional, Tuple14from email.message import Message15from pip._vendor.pkg_resources import Distribution161718logger = logging.getLogger(__name__)192021def check_requires_python(requires_python, version_info):22# type: (Optional[str], Tuple[int, ...]) -> bool23"""24Check if the given Python version matches a "Requires-Python" specifier.2526:param version_info: A 3-tuple of ints representing a Python27major-minor-micro version to check (e.g. `sys.version_info[:3]`).2829:return: `True` if the given Python version satisfies the requirement.30Otherwise, return `False`.3132:raises InvalidSpecifier: If `requires_python` has an invalid format.33"""34if requires_python is None:35# The package provides no information36return True37requires_python_specifier = specifiers.SpecifierSet(requires_python)3839python_version = version.parse('.'.join(map(str, version_info)))40return python_version in requires_python_specifier414243def get_metadata(dist):44# type: (Distribution) -> Message45"""46:raises NoneMetadataError: if the distribution reports `has_metadata()`47True but `get_metadata()` returns None.48"""49metadata_name = 'METADATA'50if (isinstance(dist, pkg_resources.DistInfoDistribution) and51dist.has_metadata(metadata_name)):52metadata = dist.get_metadata(metadata_name)53elif dist.has_metadata('PKG-INFO'):54metadata_name = 'PKG-INFO'55metadata = dist.get_metadata(metadata_name)56else:57logger.warning("No metadata found in %s", display_path(dist.location))58metadata = ''5960if metadata is None:61raise NoneMetadataError(dist, metadata_name)6263feed_parser = FeedParser()64# The following line errors out if with a "NoneType" TypeError if65# passed metadata=None.66feed_parser.feed(metadata)67return feed_parser.close()686970def get_requires_python(dist):71# type: (pkg_resources.Distribution) -> Optional[str]72"""73Return the "Requires-Python" metadata for a distribution, or None74if not present.75"""76pkg_info_dict = get_metadata(dist)77requires_python = pkg_info_dict.get('Requires-Python')7879if requires_python is not None:80# Convert to a str to satisfy the type checker, since requires_python81# can be a Header object.82requires_python = str(requires_python)8384return requires_python858687def get_installer(dist):88# type: (Distribution) -> str89if dist.has_metadata('INSTALLER'):90for line in dist.get_metadata_lines('INSTALLER'):91if line.strip():92return line.strip()93return ''949596