def make_link_collector( session, # type: PipSession options, # type: Values suppress_no_index=False, # type: bool ): # type: (...) -> LinkCollector """ :param session: The Session to use to make requests. :param suppress_no_index: Whether to ignore the --no-index option when constructing the SearchScope object. """ index_urls = [options.index_url] + options.extra_index_urls if options.no_index and not suppress_no_index: logger.debug( 'Ignoring indexes: %s', ','.join(redact_auth_from_url(url) for url in index_urls), ) index_urls = [] # Make sure find_links is a list before passing to create(). find_links = options.find_links or [] search_scope = SearchScope.create( find_links=find_links, index_urls=index_urls, ) link_collector = LinkCollector(session=session, search_scope=search_scope) return link_collector
def _get_finder(): try: return PackageFinder(find_links=[], index_urls=[], session=PipSession()) except TypeError: pass # pip 19.3 from pip._internal.models.search_scope import SearchScope from pip._internal.models.selection_prefs import SelectionPreferences try: return PackageFinder.create( search_scope=SearchScope(find_links=[], index_urls=[]), selection_prefs=SelectionPreferences(allow_yanked=False), session=PipSession(), ) except TypeError: pass from pip._internal.models.target_python import TargetPython try: # pip 19.3.1 from pip._internal.collector import LinkCollector except ImportError: from pip._internal.index.collector import LinkCollector return PackageFinder.create( link_collector=LinkCollector( search_scope=SearchScope(find_links=[], index_urls=[]), session=PipSession(), ), selection_prefs=SelectionPreferences(allow_yanked=False), target_python=TargetPython(), )
def test_create__allow_yanked(self, allow_yanked): """ Test that the _allow_yanked attribute is set correctly. """ link_collector = LinkCollector( session=PipSession(), search_scope=SearchScope([], []), ) selection_prefs = SelectionPreferences(allow_yanked=allow_yanked) finder = PackageFinder.create( link_collector=link_collector, selection_prefs=selection_prefs, ) assert finder._allow_yanked == allow_yanked
def make_test_link_collector( find_links=None, # type: Optional[List[str]] ): # type: (...) -> LinkCollector """ Create a LinkCollector object for testing purposes. """ session = PipSession() search_scope = make_test_search_scope( find_links=find_links, index_urls=[PyPI.simple_url], ) return LinkCollector( session=session, search_scope=search_scope, )
def test_create__ignore_requires_python(self, ignore_requires_python): """ Test that the _ignore_requires_python attribute is set correctly. """ link_collector = LinkCollector( session=PipSession(), search_scope=SearchScope([], []), ) selection_prefs = SelectionPreferences( allow_yanked=True, ignore_requires_python=ignore_requires_python, ) finder = PackageFinder.create( link_collector=link_collector, selection_prefs=selection_prefs, ) assert finder._ignore_requires_python == ignore_requires_python
def test_create__target_python_none(self): """ Test passing target_python=None. """ link_collector = LinkCollector( session=PipSession(), search_scope=SearchScope([], []), ) finder = PackageFinder.create( link_collector=link_collector, selection_prefs=SelectionPreferences(allow_yanked=True), target_python=None, ) # Spot-check the default TargetPython object. actual_target_python = finder._target_python assert actual_target_python._given_py_version_info is None assert actual_target_python.py_version_info == CURRENT_PY_VERSION_INFO
def make_test_link_collector( find_links=None, # type: Optional[List[str]] index_urls=None, # type: Optional[List[str]] session=None, # type: Optional[PipSession] ): # type: (...) -> LinkCollector """ Create a LinkCollector object for testing purposes. """ if session is None: session = PipSession() search_scope = make_test_search_scope( find_links=find_links, index_urls=index_urls, ) return LinkCollector(session=session, search_scope=search_scope)
def create( cls, search_scope, # type: SearchScope selection_prefs, # type: SelectionPreferences session=None, # type: Optional[PipSession] target_python=None, # type: Optional[TargetPython] ): # type: (...) -> PackageFinder """Create a PackageFinder. :param selection_prefs: The candidate selection preferences, as a SelectionPreferences object. :param session: The Session to use to make requests. :param target_python: The target Python interpreter to use when checking compatibility. If None (the default), a TargetPython object will be constructed from the running Python. """ if session is None: raise TypeError( "PackageFinder.create() missing 1 required keyword argument: " "'session'" ) if target_python is None: target_python = TargetPython() candidate_prefs = CandidatePreferences( prefer_binary=selection_prefs.prefer_binary, allow_all_prereleases=selection_prefs.allow_all_prereleases, ) link_collector = LinkCollector( session=session, search_scope=search_scope, ) return cls( candidate_prefs=candidate_prefs, link_collector=link_collector, target_python=target_python, allow_yanked=selection_prefs.allow_yanked, format_control=selection_prefs.format_control, ignore_requires_python=selection_prefs.ignore_requires_python, )
def test_create__target_python(self): """ Test that the _target_python attribute is set correctly. """ link_collector = LinkCollector( session=PipSession(), search_scope=SearchScope([], []), ) target_python = TargetPython(py_version_info=(3, 7, 3)) finder = PackageFinder.create( link_collector=link_collector, selection_prefs=SelectionPreferences(allow_yanked=True), target_python=target_python, ) actual_target_python = finder._target_python # The target_python attribute should be set as is. assert actual_target_python is target_python # Check that the attributes weren't reset. assert actual_target_python.py_version_info == (3, 7, 3)
def test_create__format_control(self): """ Test that the format_control attribute is set correctly. """ link_collector = LinkCollector( session=PipSession(), search_scope=SearchScope([], []), ) format_control = FormatControl(set(), {':all:'}) selection_prefs = SelectionPreferences( allow_yanked=True, format_control=format_control, ) finder = PackageFinder.create( link_collector=link_collector, selection_prefs=selection_prefs, ) actual_format_control = finder.format_control assert actual_format_control is format_control # Check that the attributes weren't reset. assert actual_format_control.only_binary == {':all:'}
def test_make_link_evaluator( self, allow_yanked, ignore_requires_python, only_binary, expected_formats, ): # Create a test TargetPython that we can check for. target_python = TargetPython(py_version_info=(3, 7)) format_control = FormatControl(set(), only_binary) link_collector = LinkCollector( session=PipSession(), search_scope=SearchScope([], []), ) finder = PackageFinder( link_collector=link_collector, target_python=target_python, allow_yanked=allow_yanked, format_control=format_control, ignore_requires_python=ignore_requires_python, ) # Pass a project_name that will be different from canonical_name. link_evaluator = finder.make_link_evaluator('Twine') assert link_evaluator.project_name == 'Twine' assert link_evaluator._canonical_name == 'twine' assert link_evaluator._allow_yanked == allow_yanked assert link_evaluator._ignore_requires_python == ignore_requires_python assert link_evaluator._formats == expected_formats # Test the _target_python attribute. actual_target_python = link_evaluator._target_python # The target_python attribute should be set as is. assert actual_target_python is target_python # For good measure, check that the attributes weren't reset. assert actual_target_python._given_py_version_info == (3, 7) assert actual_target_python.py_version_info == (3, 7, 0)
def test_make_candidate_evaluator( self, allow_all_prereleases, prefer_binary, ): target_python = TargetPython() target_python._valid_tags = [('py36', 'none', 'any')] candidate_prefs = CandidatePreferences( prefer_binary=prefer_binary, allow_all_prereleases=allow_all_prereleases, ) link_collector = LinkCollector( session=PipSession(), search_scope=SearchScope([], []), ) finder = PackageFinder( link_collector=link_collector, target_python=target_python, allow_yanked=True, candidate_prefs=candidate_prefs, ) specifier = SpecifierSet() # Pass hashes to check that _hashes is set. hashes = Hashes({'sha256': [64 * 'a']}) evaluator = finder.make_candidate_evaluator( 'my-project', specifier=specifier, hashes=hashes, ) assert evaluator._allow_all_prereleases == allow_all_prereleases assert evaluator._hashes == hashes assert evaluator._prefer_binary == prefer_binary assert evaluator._project_name == 'my-project' assert evaluator._specifier is specifier assert evaluator._supported_tags == [('py36', 'none', 'any')]
def test_create__candidate_prefs( self, allow_all_prereleases, prefer_binary, ): """ Test that the _candidate_prefs attribute is set correctly. """ link_collector = LinkCollector( session=PipSession(), search_scope=SearchScope([], []), ) selection_prefs = SelectionPreferences( allow_yanked=True, allow_all_prereleases=allow_all_prereleases, prefer_binary=prefer_binary, ) finder = PackageFinder.create( link_collector=link_collector, selection_prefs=selection_prefs, ) candidate_prefs = finder._candidate_prefs assert candidate_prefs.allow_all_prereleases == allow_all_prereleases assert candidate_prefs.prefer_binary == prefer_binary