Rename index.py's _egg_info_matches(egg_info, canonical_name).

This commit is contained in:
Chris Jerdonek 2019-06-29 22:48:54 -07:00
parent 32dcaa60fd
commit 2f1be4e6ec
2 changed files with 29 additions and 25 deletions

View File

@ -413,7 +413,9 @@ class CandidateEvaluator(object):
return (False, 'No sources permitted for %s' % search.supplied) return (False, 'No sources permitted for %s' % search.supplied)
if not version: if not version:
version = _egg_info_matches(egg_info, search.canonical) version = _extract_version_from_fragment(
egg_info, search.canonical,
)
if not version: if not version:
return (False, 'Missing project version for %s' % search.supplied) return (False, 'Missing project version for %s' % search.supplied)
@ -1117,45 +1119,47 @@ class PackageFinder(object):
return result return result
def _find_name_version_sep(egg_info, canonical_name): def _find_name_version_sep(fragment, canonical_name):
# type: (str, str) -> int # type: (str, str) -> int
"""Find the separator's index based on the package's canonical name. """Find the separator's index based on the package's canonical name.
`egg_info` must be an egg info string for the given package, and :param fragment: A <package>+<version> filename "fragment" (stem) or
`canonical_name` must be the package's canonical name. egg fragment.
:param canonical_name: The package's canonical name.
This function is needed since the canonicalized name does not necessarily This function is needed since the canonicalized name does not necessarily
have the same length as the egg info's name part. An example:: have the same length as the egg info's name part. An example::
>>> egg_info = 'foo__bar-1.0' >>> fragment = 'foo__bar-1.0'
>>> canonical_name = 'foo-bar' >>> canonical_name = 'foo-bar'
>>> _find_name_version_sep(egg_info, canonical_name) >>> _find_name_version_sep(fragment, canonical_name)
8 8
""" """
# Project name and version must be separated by one single dash. Find all # Project name and version must be separated by one single dash. Find all
# occurrences of dashes; if the string in front of it matches the canonical # occurrences of dashes; if the string in front of it matches the canonical
# name, this is the one separating the name and version parts. # name, this is the one separating the name and version parts.
for i, c in enumerate(egg_info): for i, c in enumerate(fragment):
if c != "-": if c != "-":
continue continue
if canonicalize_name(egg_info[:i]) == canonical_name: if canonicalize_name(fragment[:i]) == canonical_name:
return i return i
raise ValueError("{} does not match {}".format(egg_info, canonical_name)) raise ValueError("{} does not match {}".format(fragment, canonical_name))
def _egg_info_matches(egg_info, canonical_name): def _extract_version_from_fragment(fragment, canonical_name):
# type: (str, str) -> Optional[str] # type: (str, str) -> Optional[str]
"""Pull the version part out of a string. """Parse the version string from a <package>+<version> filename
"fragment" (stem) or egg fragment.
:param egg_info: The string to parse. E.g. foo-2.1 :param fragment: The string to parse. E.g. foo-2.1
:param canonical_name: The canonicalized name of the package this :param canonical_name: The canonicalized name of the package this
belongs to. belongs to.
""" """
try: try:
version_start = _find_name_version_sep(egg_info, canonical_name) + 1 version_start = _find_name_version_sep(fragment, canonical_name) + 1
except ValueError: except ValueError:
return None return None
version = egg_info[version_start:] version = fragment[version_start:]
if not version: if not version:
return None return None
return version return version

View File

@ -9,7 +9,7 @@ from pip._internal.download import PipSession
from pip._internal.index import ( from pip._internal.index import (
CandidateEvaluator, HTMLPage, Link, PackageFinder, Search, CandidateEvaluator, HTMLPage, Link, PackageFinder, Search,
_check_link_requires_python, _clean_link, _determine_base_url, _check_link_requires_python, _clean_link, _determine_base_url,
_egg_info_matches, _find_name_version_sep, _get_html_page, _extract_version_from_fragment, _find_name_version_sep, _get_html_page,
) )
from pip._internal.models.candidate import InstallationCandidate from pip._internal.models.candidate import InstallationCandidate
from pip._internal.models.search_scope import SearchScope from pip._internal.models.search_scope import SearchScope
@ -510,7 +510,7 @@ def test_secure_origin(location, trusted, expected):
@pytest.mark.parametrize( @pytest.mark.parametrize(
("egg_info", "canonical_name", "expected"), ("fragment", "canonical_name", "expected"),
[ [
# Trivial. # Trivial.
("pip-18.0", "pip", 3), ("pip-18.0", "pip", 3),
@ -539,13 +539,13 @@ def test_secure_origin(location, trusted, expected):
("zope.interface-", "zope-interface", 14), ("zope.interface-", "zope-interface", 14),
], ],
) )
def test_find_name_version_sep(egg_info, canonical_name, expected): def test_find_name_version_sep(fragment, canonical_name, expected):
index = _find_name_version_sep(egg_info, canonical_name) index = _find_name_version_sep(fragment, canonical_name)
assert index == expected assert index == expected
@pytest.mark.parametrize( @pytest.mark.parametrize(
("egg_info", "canonical_name"), ("fragment", "canonical_name"),
[ [
# A dash must follow the package name. # A dash must follow the package name.
("zope.interface4.5.0", "zope-interface"), ("zope.interface4.5.0", "zope-interface"),
@ -554,15 +554,15 @@ def test_find_name_version_sep(egg_info, canonical_name, expected):
("zope.interface", "zope-interface"), ("zope.interface", "zope-interface"),
], ],
) )
def test_find_name_version_sep_failure(egg_info, canonical_name): def test_find_name_version_sep_failure(fragment, canonical_name):
with pytest.raises(ValueError) as ctx: with pytest.raises(ValueError) as ctx:
_find_name_version_sep(egg_info, canonical_name) _find_name_version_sep(fragment, canonical_name)
message = "{} does not match {}".format(egg_info, canonical_name) message = "{} does not match {}".format(fragment, canonical_name)
assert str(ctx.value) == message assert str(ctx.value) == message
@pytest.mark.parametrize( @pytest.mark.parametrize(
("egg_info", "canonical_name", "expected"), ("fragment", "canonical_name", "expected"),
[ [
# Trivial. # Trivial.
("pip-18.0", "pip", "18.0"), ("pip-18.0", "pip", "18.0"),
@ -594,8 +594,8 @@ def test_find_name_version_sep_failure(egg_info, canonical_name):
("zope.interface", "zope-interface", None), ("zope.interface", "zope-interface", None),
], ],
) )
def test_egg_info_matches(egg_info, canonical_name, expected): def test_extract_version_from_fragment(fragment, canonical_name, expected):
version = _egg_info_matches(egg_info, canonical_name) version = _extract_version_from_fragment(fragment, canonical_name)
assert version == expected assert version == expected