Example #1
0
    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,
        )
Example #2
0
def test_make_search_scope__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

    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)

    search_scope = make_search_scope(options)

    # 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']
Example #3
0
    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,
        )
Example #4
0
def test_make_search_scope(no_index, suppress_no_index, expected_index_urls):
    """
    :param expected: the expected index_urls value.
    """
    options = pretend.stub(
        find_links=['link1'],
        index_url='default_url',
        extra_index_urls=['url1', 'url2'],
        no_index=no_index,
    )
    search_scope = make_search_scope(
        options, suppress_no_index=suppress_no_index,
    )
    assert search_scope.find_links == ['link1']
    assert search_scope.index_urls == expected_index_urls
Example #5
0
def test_make_search_scope(find_links, no_index, suppress_no_index, expected):
    """
    :param expected: the expected (find_links, index_urls) values.
    """
    expected_find_links, expected_index_urls = expected
    options = pretend.stub(
        find_links=find_links,
        index_url='default_url',
        extra_index_urls=['url1', 'url2'],
        no_index=no_index,
    )
    search_scope = make_search_scope(
        options, suppress_no_index=suppress_no_index,
    )
    assert search_scope.find_links == expected_find_links
    assert search_scope.index_urls == expected_index_urls
Example #6
0
    def _build_package_finder(self, options, session):
        """
        Create a package finder appropriate to this list command.
        """
        search_scope = make_search_scope(options)

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

        return PackageFinder.create(
            search_scope=search_scope,
            selection_prefs=selection_prefs,
            session=session,
        )
Example #7
0
    def _build_package_finder(
            self,
            options,  # type: Values
            session,  # type: PipSession
            platform=None,  # type: Optional[str]
            py_version_info=None,  # type: Optional[Tuple[int, ...]]
            abi=None,  # type: Optional[str]
            implementation=None,  # type: Optional[str]
            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)

        target_python = TargetPython(
            platform=platform,
            py_version_info=py_version_info,
            abi=abi,
            implementation=implementation,
        )

        return PackageFinder.create(
            search_scope=search_scope,
            allow_yanked=True,
            format_control=options.format_control,
            trusted_hosts=options.trusted_hosts,
            allow_all_prereleases=options.pre,
            session=session,
            target_python=target_python,
            prefer_binary=options.prefer_binary,
            ignore_requires_python=ignore_requires_python,
        )
Example #8
0
def pip_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
            search_scope = make_search_scope(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(
                search_scope=search_scope,
                selection_prefs=selection_prefs,
                session=session,
            )
            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

        # Advise "python -m pip" on Windows to avoid issues
        # with overwriting pip.exe.
        if WINDOWS:
            pip_cmd = "python -m pip"
        else:
            pip_cmd = "pip"
        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,
        )