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_finder_priority_nonegg_over_eggfragments(): """Test PackageFinder prefers non-egg links over "#egg=" links""" req = install_req_from_line('bar==1.0', None) links = ['http://foo/bar.py#egg=bar-1.0', 'http://foo/bar-1.0.tar.gz'] finder = PackageFinder.create(links, [], session=PipSession()) with patch.object(finder, "_get_pages", lambda x, y: []): all_versions = finder.find_all_candidates(req.name) assert all_versions[0].location.url.endswith('tar.gz') assert all_versions[1].location.url.endswith('#egg=bar-1.0') link = finder.find_requirement(req, False) assert link.url.endswith('tar.gz') links.reverse() finder = PackageFinder.create(links, [], session=PipSession()) with patch.object(finder, "_get_pages", lambda x, y: []): all_versions = finder.find_all_candidates(req.name) assert all_versions[0].location.url.endswith('tar.gz') assert all_versions[1].location.url.endswith('#egg=bar-1.0') link = finder.find_requirement(req, False) assert link.url.endswith('tar.gz')
def test_finder_only_installs_stable_releases(data): """ Test PackageFinder only accepts stable versioned releases by default. """ req = install_req_from_line("bar", None) # using a local index (that has pre & dev releases) finder = PackageFinder.create( [], [data.index_url("pre")], session=PipSession(), ) link = finder.find_requirement(req, False) assert link.url.endswith("bar-1.0.tar.gz"), link.url # using find-links links = ["https://foo/bar-1.0.tar.gz", "https://foo/bar-2.0b1.tar.gz"] finder = PackageFinder.create(links, [], session=PipSession()) with patch.object(finder, "_get_pages", lambda x, y: []): link = finder.find_requirement(req, False) assert link.url == "https://foo/bar-1.0.tar.gz" links.reverse() finder = PackageFinder.create(links, [], session=PipSession()) with patch.object(finder, "_get_pages", lambda x, y: []): link = finder.find_requirement(req, False) assert link.url == "https://foo/bar-1.0.tar.gz"
def make_test_finder( find_links=None, # type: Optional[List[str]] index_urls=None, # type: Optional[List[str]] allow_all_prereleases=False, # type: bool session=None, # type: Optional[PipSession] target_python=None, # type: Optional[TargetPython] ): # type: (...) -> PackageFinder """ Create a PackageFinder for testing purposes. """ link_collector = make_test_link_collector( find_links=find_links, index_urls=index_urls, session=session, ) selection_prefs = SelectionPreferences( allow_yanked=True, allow_all_prereleases=allow_all_prereleases, ) return PackageFinder.create( link_collector=link_collector, selection_prefs=selection_prefs, target_python=target_python, )
def test_unpinned_hash_checking(self, data): """Make sure prepare_files() raises an error when a requirement is not version-pinned in hash-checking mode. """ reqset = RequirementSet() # Test that there must be exactly 1 specifier: reqset.add_requirement( get_processed_req_from_line( 'simple --hash=sha256:a90427ae31f5d1d0d7ec06ee97d9fcf2d0fc9a786985' '250c1c83fd68df5911dd', lineno=1, )) # Test that the operator must be ==: reqset.add_requirement( get_processed_req_from_line( 'simple2>1.0 --hash=sha256:3ad45e1e9aa48b4462af0' '123f6a7e44a9115db1ef945d4d92c123dfe21815a06', lineno=2, )) finder = PackageFinder.create( [data.find_links], [], session=PipSession(), ) resolver = self._basic_resolver(finder) assert_raises_regexp( HashErrors, # Make sure all failing requirements are listed: r'versions pinned with ==. These do not:\n' r' simple .* \(from -r file \(line 1\)\)\n' r' simple2>1.0 .* \(from -r file \(line 2\)\)', resolver.resolve, reqset)
def test_finder_only_installs_data_require(data): """ Test whether the PackageFinder understand data-python-requires This can optionally be exposed by a simple-repository to tell which distribution are compatible with which version of Python by adding a data-python-require to the anchor links. See pep 503 for more information. """ # using a local index (that has pre & dev releases) finder = PackageFinder.create( [], [data.index_url("datarequire")], session=PipSession(), ) links = finder.find_all_candidates("fakepackage") expected = ['1.0.0', '9.9.9'] if (2, 7) < sys.version_info < (3, ): expected.append('2.7.0') elif sys.version_info > (3, 3): expected.append('3.3.0') assert {str(v.version) for v in links} == set(expected)
def _build_package_finder( self, options, # type: Values session, # type: PipSession platform=None, # type: Optional[str] python_versions=None, # type: Optional[List[str]] abi=None, # type: Optional[str] implementation=None # type: Optional[str] ): # type: (...) -> PackageFinder """ Create a package finder appropriate to this requirement command. """ index_urls = [options.index_url] + options.extra_index_urls if options.no_index: logger.debug( 'Ignoring indexes: %s', ','.join(redact_password_from_url(url) for url in index_urls), ) index_urls = [] return PackageFinder.create( find_links=options.find_links, format_control=options.format_control, index_urls=index_urls, trusted_hosts=options.trusted_hosts, allow_all_prereleases=options.pre, session=session, platform=platform, versions=python_versions, abi=abi, implementation=implementation, prefer_binary=options.prefer_binary, )
def test_no_partial_name_match(data): """Finder requires the full project name to match, not just beginning.""" finder = PackageFinder.create([data.find_links], [], session=PipSession()) req = install_req_from_line("gmpy") found = finder.find_requirement(req, False) assert found.url.endswith("gmpy-1.15.tar.gz"), found
def test_find_all_candidates_find_links_and_index(data): finder = PackageFinder.create([data.find_links], [data.index_url('simple')], session=PipSession()) versions = finder.find_all_candidates('simple') # first the find-links versions then the page versions assert [str(v.version) for v in versions] == ['3.0', '2.0', '1.0', '1.0']
def make_test_finder( find_links=None, # type: Optional[List[str]] index_urls=None, # type: Optional[List[str]] allow_all_prereleases=False, # type: bool trusted_hosts=None, # type: Optional[Iterable[str]] session=None, # type: Optional[PipSession] target_python=None, # type: Optional[TargetPython] ): # type: (...) -> PackageFinder """ Create a PackageFinder for testing purposes. """ if find_links is None: find_links = [] if index_urls is None: index_urls = [] if session is None: session = PipSession() search_scope = SearchScope.create( find_links=find_links, index_urls=index_urls, ) return PackageFinder.create( search_scope=search_scope, allow_yanked=True, allow_all_prereleases=allow_all_prereleases, trusted_hosts=trusted_hosts, session=session, target_python=target_python, )
def test_no_mpkg(data): """Finder skips zipfiles with "macosx10" in the name.""" finder = PackageFinder.create([data.find_links], [], session=PipSession()) req = install_req_from_line("pkgwithmpkg") found = finder.find_requirement(req, False) assert found.url.endswith("pkgwithmpkg-1.0.tar.gz"), found
def _build_package_finder( self, options, # type: Values session, # type: PipSession target_python=None, # type: Optional[TargetPython] ignore_requires_python=None, # type: Optional[bool] ): # type: (...) -> PackageFinder """ Create a package finder appropriate to this requirement command. :param ignore_requires_python: Whether to ignore incompatible "Requires-Python" values in links. Defaults to False. """ search_scope = make_search_scope(options) selection_prefs = SelectionPreferences( allow_yanked=True, format_control=options.format_control, allow_all_prereleases=options.pre, prefer_binary=options.prefer_binary, ignore_requires_python=ignore_requires_python, ) return PackageFinder.create( search_scope=search_scope, selection_prefs=selection_prefs, session=session, target_python=target_python, )
def test_get_index_urls_locations(): """Check that the canonical name is on all indexes""" finder = PackageFinder.create( [], ['file://index1/', 'file://index2'], session=PipSession()) locations = finder._get_index_urls_locations( install_req_from_line('Complex_Name').name) assert locations == ['file://index1/complex-name/', 'file://index2/complex-name/']
def test_sort_locations_non_existing_path(): """ Test that a non-existing path is ignored. """ finder = PackageFinder.create([], [], session=PipSession()) files, urls = finder._sort_locations( [os.path.join('this', 'doesnt', 'exist')]) assert not urls and not files, "nothing should have been found"
def test_sort_locations_file_not_find_link(data): """ Test that a file:// url dir that's not a find-link, doesn't get a listdir run """ finder = PackageFinder.create([], [], session=PipSession()) files, urls = finder._sort_locations([data.index_url("empty_with_pkg")]) assert urls and not files, "urls, but not files should have been found"
def test_tilde(): """Finder can accept a path with ~ in it and will normalize it.""" session = PipSession() with patch('pip._internal.index.os.path.exists', return_value=True): finder = PackageFinder.create(['~/python-pkgs'], [], session=session) req = install_req_from_line("gmpy") with pytest.raises(DistributionNotFound): finder.find_requirement(req, False)
def test_sort_locations_file_expand_dir(data): """ Test that a file:// dir gets listdir run with expand_dir """ finder = PackageFinder.create([data.find_links], [], session=PipSession()) files, urls = finder._sort_locations([data.find_links], expand_dir=True) assert files and not urls, ( "files and not urls should have been found at find-links url: %s" % data.find_links)
def test_finder_installs_pre_releases_with_version_spec(): """ Test PackageFinder only accepts stable versioned releases by default. """ req = install_req_from_line("bar>=0.0.dev0", None) links = ["https://foo/bar-1.0.tar.gz", "https://foo/bar-2.0b1.tar.gz"] finder = PackageFinder.create(links, [], session=PipSession()) with patch.object(finder, "_get_pages", lambda x, y: []): link = finder.find_requirement(req, False) assert link.url == "https://foo/bar-2.0b1.tar.gz" links.reverse() finder = PackageFinder.create(links, [], session=PipSession()) with patch.object(finder, "_get_pages", lambda x, y: []): link = finder.find_requirement(req, False) assert link.url == "https://foo/bar-2.0b1.tar.gz"
def _build_package_finder(self, options, index_urls, session): """ Create a package finder appropriate to this list command. """ return PackageFinder.create( find_links=options.find_links, index_urls=index_urls, allow_all_prereleases=options.pre, trusted_hosts=options.trusted_hosts, session=session, )
def test_create__allow_yanked(self, allow_yanked): """ Test that the _allow_yanked attribute is set correctly. """ selection_prefs = SelectionPreferences(allow_yanked=allow_yanked) finder = PackageFinder.create( search_scope=SearchScope([], []), selection_prefs=selection_prefs, session=PipSession(), ) assert finder._allow_yanked == allow_yanked
def test_duplicates_sort_ok(data): """Finder successfully finds one of a set of duplicates in different locations""" finder = PackageFinder.create( [data.find_links, data.find_links2], [], session=PipSession(), ) req = install_req_from_line("duplicate") found = finder.find_requirement(req, False) assert found.url.endswith("duplicate-1.0.tar.gz"), found
def _build_package_finder(self, options, session): """ Create a package finder appropriate to this list command. """ link_collector = make_link_collector(session, options=options) # Pass allow_yanked=False to ignore yanked versions. selection_prefs = SelectionPreferences( allow_yanked=False, allow_all_prereleases=options.pre) return PackageFinder.create(link_collector=link_collector, selection_prefs=selection_prefs)
def test_create__allow_yanked(self, allow_yanked): """ Test that allow_yanked is passed to CandidateEvaluator. """ search_scope = SearchScope([], []) finder = PackageFinder.create( search_scope=search_scope, allow_yanked=allow_yanked, session=object(), ) evaluator = finder.candidate_evaluator assert evaluator._allow_yanked == allow_yanked
def _build_package_finder(self, options, session): """ Create a package finder appropriate to this list command. """ search_scope = make_search_scope(options) return PackageFinder.create( search_scope=search_scope, allow_all_prereleases=options.pre, trusted_hosts=options.trusted_hosts, session=session, )
def test_backend(tmpdir, data): """Check we can call a requirement's backend successfully""" project_dir = make_project(tmpdir, backend="dummy_backend") req = InstallRequirement(None, None, source_dir=project_dir) req.load_pyproject_toml() env = BuildEnvironment() finder = PackageFinder.create([data.backends], [], session=PipSession()) env.install_requirements(finder, ["dummy_backend"], 'normal', "Installing") conflicting, missing = env.check_requirements(["dummy_backend"]) assert not conflicting and not missing assert hasattr(req.pep517_backend, 'build_wheel') with env: assert req.pep517_backend.build_wheel("dir") == "Backend called"
def test_wheel_over_sdist_priority(self, data): """ Test wheels have priority over sdists. `test_link_sorting` also covers this at lower level """ req = install_req_from_line("priority") finder = PackageFinder.create( [data.find_links], [], session=PipSession(), ) found = finder.find_requirement(req, True) assert found.url.endswith("priority-1.0-py2.py3-none-any.whl"), found
def test_finder_detects_latest_already_satisfied_find_links(data): """Test PackageFinder detects latest already satisfied using find-links""" req = install_req_from_line('simple', None) # the latest simple in local pkgs is 3.0 latest_version = "3.0" satisfied_by = Mock(location="/path", parsed_version=parse_version(latest_version), version=latest_version) req.satisfied_by = satisfied_by finder = PackageFinder.create([data.find_links], [], session=PipSession()) with pytest.raises(BestVersionAlreadyInstalled): finder.find_requirement(req, True)
def test_create__ignore_requires_python(self, ignore_requires_python): """ Test that the _ignore_requires_python attribute is set correctly. """ selection_prefs = SelectionPreferences( allow_yanked=True, ignore_requires_python=ignore_requires_python, ) finder = PackageFinder.create( search_scope=SearchScope([], []), selection_prefs=selection_prefs, session=PipSession(), ) assert finder._ignore_requires_python == ignore_requires_python
def test_create__link_collector(self): """ Test that the _link_collector attribute is set correctly. """ link_collector = LinkCollector( session=PipSession(), search_scope=SearchScope([], []), ) finder = PackageFinder.create( link_collector=link_collector, selection_prefs=SelectionPreferences(allow_yanked=True), ) assert finder._link_collector is link_collector
def test_create__target_python_none(self): """ Test passing target_python=None. """ finder = PackageFinder.create( search_scope=SearchScope([], []), selection_prefs=SelectionPreferences(allow_yanked=True), session=PipSession(), 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