示例#1
0
def finder(data):
    session = PipSession()
    scope = SearchScope([str(data.packages)], [])
    collector = LinkCollector(session, scope)
    prefs = SelectionPreferences(allow_yanked=False)
    finder = PackageFinder.create(collector, prefs)
    yield finder
示例#2
0
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
示例#3
0
 def test_create__candidate_prefs(
     self,
     allow_all_prereleases: bool,
     prefer_binary: bool,
 ) -> None:
     """
     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,
         use_deprecated_html5lib=False,
     )
     candidate_prefs = finder._candidate_prefs
     assert candidate_prefs.allow_all_prereleases == allow_all_prereleases
     assert candidate_prefs.prefer_binary == prefer_binary
示例#4
0
def _get_current_remote_pip_version(session: PipSession,
                                    options: optparse.Values) -> str:
    # Lets use PackageFinder to see what the latest pip version is
    link_collector = LinkCollector.create(
        session,
        options=options,
        suppress_no_index=True,
    )

    # Pass allow_yanked=False so we don't suggest upgrading to a
    # yanked version.
    selection_prefs = SelectionPreferences(
        allow_yanked=False,
        allow_all_prereleases=False,  # Explicitly set to False
    )

    finder = PackageFinder.create(
        link_collector=link_collector,
        selection_prefs=selection_prefs,
    )
    best_candidate = finder.find_best_candidate("pip").best_candidate
    if best_candidate is None:
        return

    return str(best_candidate.version)
示例#5
0
def _instantiate_package_finder(
        pip_session):  # type: (PipSession) -> PackageFinder
    """Instantiate package finder, in a pip>=10 and pip<10 compatible way."""
    try:
        return PackageFinder(find_links=[],
                             session=pip_session,
                             index_urls=_DEFAULT_INDEX_URLS)
    except TypeError:  # API changed in pip>=10
        from pip._internal.models.search_scope import SearchScope
        from pip._internal.models.selection_prefs import SelectionPreferences

        selection_prefs = SelectionPreferences(allow_yanked=True, )
        search_scope = SearchScope([], [])

        try:
            from pip._internal.index.collector import LinkCollector
        except ModuleNotFoundError:
            try:
                from pip._internal.collector import LinkCollector
            except ModuleNotFoundError:  # pip>=19.2<20
                return PackageFinder.create(session=pip_session,
                                            selection_prefs=selection_prefs,
                                            search_scope=search_scope)

        link_collector = LinkCollector(session=pip_session,
                                       search_scope=search_scope)
        return PackageFinder.create(
            link_collector=link_collector,
            selection_prefs=selection_prefs,
        )
示例#6
0
    def _build_package_finder(
        self,
        options: Values,
        session: PipSession,
        target_python: Optional[TargetPython] = None,
        ignore_requires_python: Optional[bool] = None,
    ) -> 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.
        """
        link_collector = LinkCollector.create(session, options=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(
            link_collector=link_collector,
            selection_prefs=selection_prefs,
            target_python=target_python,
        )
示例#7
0
def test_link_collector_create_find_links_expansion(
    mock_expanduser,
    tmpdir,
):
    """
    Test "~" expansion in --find-links paths.
    """

    # This is a mock version of expanduser() that expands "~" to the tmpdir.
    def expand_path(path):
        if path.startswith('~/'):
            path = os.path.join(tmpdir, path[2:])
        return path

    mock_expanduser.side_effect = expand_path

    session = PipSession()
    options = pretend.stub(
        find_links=['~/temp1', '~/temp2'],
        index_url='default_url',
        extra_index_urls=[],
        no_index=False,
    )
    # Only create temp2 and not temp1 to test that "~" expansion only occurs
    # when the directory exists.
    temp2_dir = os.path.join(tmpdir, 'temp2')
    os.mkdir(temp2_dir)

    link_collector = LinkCollector.create(session, options=options)

    search_scope = link_collector.search_scope
    # Only ~/temp2 gets expanded. Also, the path is normalized when expanded.
    expected_temp2_dir = os.path.normcase(temp2_dir)
    assert search_scope.find_links == ['~/temp1', expected_temp2_dir]
    assert search_scope.index_urls == ['default_url']
示例#8
0
文件: index.py 项目: GuyTuval/pip
    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 the index command.
        """
        link_collector = LinkCollector.create(session, options=options)

        # Pass allow_yanked=False to ignore yanked versions.
        selection_prefs = SelectionPreferences(
            allow_yanked=False,
            allow_all_prereleases=options.pre,
            ignore_requires_python=ignore_requires_python,
        )

        return PackageFinder.create(
            link_collector=link_collector,
            selection_prefs=selection_prefs,
            target_python=target_python,
        )
示例#9
0
def test_link_collector_create(
    find_links,
    no_index,
    suppress_no_index,
    expected,
):
    """
    :param expected: the expected (find_links, index_urls) values.
    """
    expected_find_links, expected_index_urls = expected
    session = PipSession()
    options = pretend.stub(
        find_links=find_links,
        index_url='default_url',
        extra_index_urls=['url1', 'url2'],
        no_index=no_index,
    )
    link_collector = LinkCollector.create(
        session,
        options=options,
        suppress_no_index=suppress_no_index,
    )

    assert link_collector.session is session

    search_scope = link_collector.search_scope
    assert search_scope.find_links == expected_find_links
    assert search_scope.index_urls == expected_index_urls
示例#10
0
def test_link_collector_create_find_links_expansion(mock_expanduser: mock.Mock,
                                                    tmpdir: Path) -> None:
    """
    Test "~" expansion in --find-links paths.
    """

    # This is a mock version of expanduser() that expands "~" to the tmpdir.
    def expand_path(path: str) -> str:
        if path.startswith("~/"):
            path = os.path.join(tmpdir, path[2:])
        return path

    mock_expanduser.side_effect = expand_path

    session = PipSession()
    options = mock.Mock(
        find_links=["~/temp1", "~/temp2"],
        index_url="default_url",
        extra_index_urls=[],
        no_index=False,
    )
    # Only create temp2 and not temp1 to test that "~" expansion only occurs
    # when the directory exists.
    temp2_dir = os.path.join(tmpdir, "temp2")
    os.mkdir(temp2_dir)

    link_collector = LinkCollector.create(session, options=options)

    search_scope = link_collector.search_scope
    # Only ~/temp2 gets expanded. Also, the path is normalized when expanded.
    expected_temp2_dir = os.path.normcase(temp2_dir)
    assert search_scope.find_links == ["~/temp1", expected_temp2_dir]
    assert search_scope.index_urls == ["default_url"]
示例#11
0
def test_link_collector_create(
    find_links: List[str],
    no_index: bool,
    suppress_no_index: bool,
    expected: Tuple[List[str], List[str]],
) -> None:
    """
    :param expected: the expected (find_links, index_urls) values.
    """
    expected_find_links, expected_index_urls = expected
    session = PipSession()
    options = mock.Mock(
        find_links=find_links,
        index_url="default_url",
        extra_index_urls=["url1", "url2"],
        no_index=no_index,
    )
    link_collector = LinkCollector.create(
        session,
        options=options,
        suppress_no_index=suppress_no_index,
    )

    assert link_collector.session is session

    search_scope = link_collector.search_scope
    assert search_scope.find_links == expected_find_links
    assert search_scope.index_urls == expected_index_urls
示例#12
0
    def _build_package_finder(
        self,
        options: Values,
        session: PipSession,
        target_python: Optional[TargetPython] = None,
        ignore_requires_python: Optional[bool] = None,
    ) -> PackageFinder:
        """
        Create a package finder appropriate to the index command.
        """
        link_collector = LinkCollector.create(session, options=options)

        # Pass allow_yanked=False to ignore yanked versions.
        selection_prefs = SelectionPreferences(
            allow_yanked=False,
            allow_all_prereleases=options.pre,
            ignore_requires_python=ignore_requires_python,
        )

        return PackageFinder.create(
            link_collector=link_collector,
            selection_prefs=selection_prefs,
            target_python=target_python,
            use_deprecated_html5lib="html5lib" in options.deprecated_features_enabled,
        )
示例#13
0
def test_collect_sources__file_expand_dir(data: TestData) -> None:
    """
    Test that a file:// dir from --find-links becomes _FlatDirectorySource
    """
    collector = LinkCollector.create(
        session=mock.Mock(is_secure_origin=None),  # Shouldn't be used.
        options=mock.Mock(
            index_url="ignored-by-no-index",
            extra_index_urls=[],
            no_index=True,
            find_links=[data.find_links],
        ),
    )
    sources = collector.collect_sources(
        # Shouldn't be used.
        project_name=None,  # type: ignore[arg-type]
        candidates_from_page=None,  # type: ignore[arg-type]
    )
    assert (
        not sources.index_urls
        and len(sources.find_links) == 1
        and isinstance(sources.find_links[0], _FlatDirectorySource)
    ), (
        "Directory source should have been found "
        f"at find-links url: {data.find_links}"
    )
示例#14
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')]
示例#15
0
def finder(data: TestData) -> Iterator[PackageFinder]:
    session = PipSession()
    scope = SearchScope([str(data.packages)], [])
    collector = LinkCollector(session, scope)
    prefs = SelectionPreferences(allow_yanked=False)
    finder = PackageFinder.create(collector,
                                  prefs,
                                  use_deprecated_html5lib=False)
    yield finder
示例#16
0
 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
示例#17
0
文件: test_index.py 项目: hrnciar/pip
    def test_create__link_collector(self) -> None:
        """
        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 get_finder() -> PackageFinder:
    return PackageFinder.create(
        LinkCollector.create(
            session=PipSession(),
            options=Values(defaults=dict(
                no_index=False, index_url="https://pypi.python.org/simple",
                find_links=None, extra_index_urls=[""]))
        ),
        SelectionPreferences(
            allow_yanked=False,
            allow_all_prereleases=True
        ),
        use_deprecated_html5lib=False,
    )
示例#19
0
    def _build_package_finder(self, options, session):
        # type: (Values, PipSession) -> PackageFinder
        """
        Create a package finder appropriate to this list command.
        """
        link_collector = LinkCollector.create(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
        )
示例#20
0
 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
示例#21
0
def make_test_link_collector(
    find_links: Optional[List[str]] = None,
    index_urls: Optional[List[str]] = None,
    session: Optional[PipSession] = None,
) -> 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)
示例#22
0
 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
示例#23
0
def test_collect_sources__non_existing_path():
    """
    Test that a non-existing path is ignored.
    """
    collector = LinkCollector.create(
        session=pretend.stub(is_secure_origin=None),  # Shouldn't be used.
        options=pretend.stub(
            index_url="ignored-by-no-index",
            extra_index_urls=[],
            no_index=True,
            find_links=[os.path.join("this", "doesnt", "exist")],
        ),
    )
    sources = collector.collect_sources(
        project_name=None,  # Shouldn't be used.
        candidates_from_page=None,  # Shouldn't be used.
    )
    assert (not sources.index_urls
            and sources.find_links == [None]), "Nothing should have been found"
示例#24
0
    def _build_package_finder(self, options: Values,
                              session: PipSession) -> PackageFinder:
        """
        Create a package finder appropriate to this list command.
        """
        link_collector = LinkCollector.create(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,
            use_deprecated_html5lib="html5lib"
            in options.deprecated_features_enabled,
        )
示例#25
0
 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)
示例#26
0
    def test_make_link_evaluator(
        self,
        allow_yanked: bool,
        ignore_requires_python: bool,
        only_binary: Set[str],
        expected_formats: FrozenSet[str],
    ) -> None:
        # 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,
            use_deprecated_html5lib=False,
        )

        # 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)
示例#27
0
def test_collect_sources__file_not_find_link(data):
    """
    Test that a file:// dir from --index-url doesn't become _FlatDirectorySource
    run
    """
    collector = LinkCollector.create(
        session=pretend.stub(is_secure_origin=None),  # Shouldn't be used.
        options=pretend.stub(
            index_url=data.index_url("empty_with_pkg"),
            extra_index_urls=[],
            no_index=False,
            find_links=[],
        ),
    )
    sources = collector.collect_sources(
        project_name="",
        candidates_from_page=None,  # Shouldn't be used.
    )
    assert (not sources.find_links and len(sources.index_urls) == 1
            and isinstance(sources.index_urls[0], _IndexDirectorySource)
            ), "Directory specified as index should be treated as a page"
示例#28
0
 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:'}
示例#29
0
    def test_make_candidate_evaluator(
        self,
        allow_all_prereleases: bool,
        prefer_binary: bool,
    ) -> None:
        target_python = TargetPython()
        target_python._valid_tags = [Tag("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,
            use_deprecated_html5lib=False,
        )

        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 == [Tag("py36", "none", "any")]
def pip_self_version_check(session, options):
    # type: (PipSession, optparse.Values) -> None
    """Check for an update for pip.

    Limit the frequency of checks to once per week. State is stored either in
    the active virtualenv or in the user's USER_CACHE_DIR keyed off the prefix
    of the pip script path.
    """
    installed_version = get_installed_version("pip")
    if not installed_version:
        return

    pip_version = packaging_version.parse(installed_version)
    pypi_version = None

    try:
        state = SelfCheckState(cache_dir=options.cache_dir)

        current_time = datetime.datetime.utcnow()
        # Determine if we need to refresh the state
        if "last_check" in state.state and "pypi_version" in state.state:
            last_check = datetime.datetime.strptime(state.state["last_check"],
                                                    SELFCHECK_DATE_FMT)
            if (current_time - last_check).total_seconds() < 7 * 24 * 60 * 60:
                pypi_version = state.state["pypi_version"]

        # Refresh the version if we need to or just see if we need to warn
        if pypi_version is None:
            # Lets use PackageFinder to see what the latest pip version is
            link_collector = LinkCollector.create(
                session,
                options=options,
                suppress_no_index=True,
            )

            # Pass allow_yanked=False so we don't suggest upgrading to a
            # yanked version.
            selection_prefs = SelectionPreferences(
                allow_yanked=False,
                allow_all_prereleases=False,  # Explicitly set to False
            )

            finder = PackageFinder.create(
                link_collector=link_collector,
                selection_prefs=selection_prefs,
            )
            best_candidate = finder.find_best_candidate("pip").best_candidate
            if best_candidate is None:
                return
            pypi_version = str(best_candidate.version)

            # save that we've performed a check
            state.save(pypi_version, current_time)

        remote_version = packaging_version.parse(pypi_version)

        local_version_is_older = (
            pip_version < remote_version
            and pip_version.base_version != remote_version.base_version
            and was_installed_by_pip('pip'))

        # Determine if our pypi_version is older
        if not local_version_is_older:
            return

        # We cannot tell how the current pip is available in the current
        # command context, so be pragmatic here and suggest the command
        # that's always available. This does not accommodate spaces in
        # `sys.executable`.
        pip_cmd = "{} -m pip".format(sys.executable)
        logger.warning(
            "You are using pip version %s; however, version %s is "
            "available.\nYou should consider upgrading via the "
            "'%s install --upgrade pip' command.", pip_version, pypi_version,
            pip_cmd)
    except Exception:
        logger.debug(
            "There was an error checking the latest version of pip",
            exc_info=True,
        )