1
1
Fork 0
mirror of https://github.com/pypa/pip synced 2023-12-13 21:30:23 +01:00

Remove utils/packaging.py's dependence on the current environment.

This commit is contained in:
Chris Jerdonek 2019-05-07 16:10:06 -04:00
parent 919ee314fc
commit 5be4e1481e
4 changed files with 27 additions and 13 deletions

View file

@ -360,7 +360,9 @@ class CandidateEvaluator(object):
link, 'Python version is incorrect') link, 'Python version is incorrect')
return None return None
try: try:
support_this_python = check_requires_python(link.requires_python) support_this_python = check_requires_python(
link.requires_python, version_info=sys.version_info[:3],
)
except specifiers.InvalidSpecifier: except specifiers.InvalidSpecifier:
logger.debug("Package %s has an invalid Requires-Python entry: %s", logger.debug("Package %s has an invalid Requires-Python entry: %s",
link.filename, link.requires_python) link.filename, link.requires_python)

View file

@ -11,6 +11,7 @@ for sub-dependencies
""" """
import logging import logging
import sys
from collections import defaultdict from collections import defaultdict
from itertools import chain from itertools import chain
@ -296,7 +297,7 @@ class Resolver(object):
# Parse and return dependencies # Parse and return dependencies
dist = abstract_dist.dist() dist = abstract_dist.dist()
try: try:
check_dist_requires_python(dist) check_dist_requires_python(dist, version_info=sys.version_info[:3])
except UnsupportedPythonVersion as err: except UnsupportedPythonVersion as err:
if self.ignore_requires_python: if self.ignore_requires_python:
logger.warning(err.args[0]) logger.warning(err.args[0])

View file

@ -1,7 +1,6 @@
from __future__ import absolute_import from __future__ import absolute_import
import logging import logging
import sys
from email.parser import FeedParser from email.parser import FeedParser
from pip._vendor import pkg_resources from pip._vendor import pkg_resources
@ -12,7 +11,7 @@ from pip._internal.utils.misc import display_path
from pip._internal.utils.typing import MYPY_CHECK_RUNNING from pip._internal.utils.typing import MYPY_CHECK_RUNNING
if MYPY_CHECK_RUNNING: if MYPY_CHECK_RUNNING:
from typing import Optional from typing import Optional, Tuple
from email.message import Message from email.message import Message
from pip._vendor.pkg_resources import Distribution from pip._vendor.pkg_resources import Distribution
@ -20,10 +19,13 @@ if MYPY_CHECK_RUNNING:
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
def check_requires_python(requires_python): def check_requires_python(requires_python, version_info):
# type: (Optional[str]) -> bool # type: (Optional[str], Tuple[int, ...]) -> bool
""" """
Check if the python version in use match the `requires_python` specifier. Check if the given Python version matches a `requires_python` specifier.
:param version_info: A tuple of ints representing the Python
major-minor-micro version to check (e.g. `sys.version_info[:3]`).
Returns `True` if the version of python in use matches the requirement. Returns `True` if the version of python in use matches the requirement.
Returns `False` if the version of python in use does not matches the Returns `False` if the version of python in use does not matches the
@ -37,7 +39,7 @@ def check_requires_python(requires_python):
requires_python_specifier = specifiers.SpecifierSet(requires_python) requires_python_specifier = specifiers.SpecifierSet(requires_python)
# We only use major.minor.micro # We only use major.minor.micro
python_version = version.parse('.'.join(map(str, sys.version_info[:3]))) python_version = version.parse('.'.join(map(str, version_info[:3])))
return python_version in requires_python_specifier return python_version in requires_python_specifier
@ -57,16 +59,22 @@ def get_metadata(dist):
return feed_parser.close() return feed_parser.close()
def check_dist_requires_python(dist): def check_dist_requires_python(dist, version_info):
"""
:param version_info: A tuple of ints representing the Python
major-minor-micro version to check (e.g. `sys.version_info[:3]`).
"""
pkg_info_dict = get_metadata(dist) pkg_info_dict = get_metadata(dist)
requires_python = pkg_info_dict.get('Requires-Python') requires_python = pkg_info_dict.get('Requires-Python')
try: try:
if not check_requires_python(requires_python): if not check_requires_python(
requires_python, version_info=version_info,
):
raise exceptions.UnsupportedPythonVersion( raise exceptions.UnsupportedPythonVersion(
"%s requires Python '%s' but the running Python is %s" % ( "%s requires Python '%s' but the running Python is %s" % (
dist.project_name, dist.project_name,
requires_python, requires_python,
'.'.join(map(str, sys.version_info[:3])),) '.'.join(map(str, version_info[:3])),)
) )
except specifiers.InvalidSpecifier as e: except specifiers.InvalidSpecifier as e:
logger.warning( logger.warning(

View file

@ -714,11 +714,14 @@ class TestCheckRequiresPython(object):
fake_dist = Mock( fake_dist = Mock(
has_metadata=lambda _: True, has_metadata=lambda _: True,
get_metadata=lambda _: metadata) get_metadata=lambda _: metadata)
version_info = sys.version_info[:3]
if should_raise: if should_raise:
with pytest.raises(UnsupportedPythonVersion): with pytest.raises(UnsupportedPythonVersion):
check_dist_requires_python(fake_dist) check_dist_requires_python(
fake_dist, version_info=version_info,
)
else: else:
check_dist_requires_python(fake_dist) check_dist_requires_python(fake_dist, version_info=version_info)
class TestGetProg(object): class TestGetProg(object):