Exemplo n.º 1
0
    def get_finder(
        self,
        sources: list[Source] | None = None,
        ignore_requires_python: bool = False,
    ) -> Generator[pip_shims.PackageFinder, None, None]:
        """Return the package finder of given index sources.

        :param sources: a list of sources the finder should search in.
        :param ignore_requires_python: whether to ignore the python version constraint.
        """
        if sources is None:
            sources = self.project.sources

        python_version = self.interpreter.version_tuple
        python_abi_tag = get_python_abi_tag(self.interpreter.executable)
        finder = get_finder(
            sources,
            self.project.cache_dir.as_posix(),
            python_version,
            python_abi_tag,
            ignore_requires_python,
        )
        # Reuse the auth across sessions to avoid prompting repeatly.
        finder.session.auth = self.auth  # type: ignore
        yield finder
        finder.session.close()  # type: ignore
Exemplo n.º 2
0
    def get_finder(
        self,
        sources: Optional[List[Source]] = None,
        ignore_requires_python: bool = False,
    ) -> Generator[pip_shims.PackageFinder, None, None]:
        """Return the package finder of given index sources.

        :param sources: a list of sources the finder should search in.
        :param ignore_requires_python: whether to ignore the python version constraint.
        """
        if sources is None:
            sources = self.project.sources
        for source in sources:
            source["url"] = expand_env_vars_in_auth(source["url"])

        python_version, _ = get_python_version(self.python_executable,
                                               digits=2)
        finder = get_finder(
            sources,
            self.project.cache_dir.as_posix(),
            python_version,
            ignore_requires_python,
        )
        # Reuse the auth across sessions to avoid prompting repeatly.
        finder.session.auth = self.auth
        yield finder
        finder.session.close()
Exemplo n.º 3
0
def parse_requirement_file(filename):
    from pdm.models.pip_shims import install_req_from_parsed_requirement

    finder = get_finder([])
    ireqs = [
        install_req_from_parsed_requirement(pr)
        for pr in parse_requirements(filename, finder.session, finder)
    ]
    return ireqs, finder
Exemplo n.º 4
0
def parse_requirement_file(filename):
    from pip._internal.req.constructors import install_req_from_parsed_requirement

    finder = get_finder([])
    ireqs = [
        install_req_from_parsed_requirement(pr)
        for pr in parse_requirements(filename, finder.session, finder)
    ]
    return ireqs, finder
Exemplo n.º 5
0
def parse_requirement_file(
    filename: str, ) -> Tuple[List[InstallRequirement], PackageFinder]:
    from pdm.models.pip_shims import install_req_from_parsed_requirement

    finder = get_finder([])
    ireqs = [
        install_req_from_parsed_requirement(pr)
        for pr in parse_requirements(filename, finder.session, finder)
    ]
    return ireqs, finder
Exemplo n.º 6
0
def check_update(project: Project) -> None:
    """Check if there is a new version of PDM available"""
    import sys
    from shlex import quote

    from pip._vendor.packaging.version import parse as parse_version

    from pdm.cli.utils import (
        is_homebrew_installation,
        is_pipx_installation,
        is_scoop_installation,
    )
    from pdm.utils import get_finder

    this_version = parse_version(project.core.version)
    candidate = get_finder([]).find_best_candidate("pdm")
    if not candidate.best_candidate or candidate.best_candidate.version <= this_version:
        return

    latest_version = candidate.best_candidate.version
    if is_pipx_installation():  # pragma: no cover
        install_command = "$ pipx upgrade pdm"
    elif is_scoop_installation():  # pragma: no cover
        install_command = "$ scoop update pdm"
    elif is_homebrew_installation():  # pragma: no cover
        install_command = "$ brew upgrade pdm"
    else:
        install_command = f"$ {quote(sys.executable)} -m pip install -U pdm"

    disable_command = "$ pdm config check_update false"

    message = [
        termui.blue(f"\nPDM {termui.cyan(str(this_version))}"),
        termui.blue(
            f" is installed, while {termui.cyan(str(latest_version))}"),
        termui.blue(" is available.\n"),
        termui.blue(f"Please run {termui.green(install_command, bold=True)}"),
        termui.blue(" to upgrade.\n"),
        termui.blue(f"Run {termui.green(disable_command, bold=True)}"),
        termui.blue(" to disable the check."),
    ]
    project.core.ui.echo("".join(message), err=True)
Exemplo n.º 7
0
    def get_finder(
        self,
        sources: Optional[List[Source]] = None,
        ignore_requires_python: bool = False,
    ) -> shims.PackageFinder:
        """Return the package finder of given index sources.

        :param sources: a list of sources the finder should search in.
        :param ignore_requires_python: whether to ignore the python version constraint.
        """
        sources = sources or []
        python_version = get_python_version(self.python_executable)[:2]
        finder = get_finder(
            sources,
            context.cache_dir.as_posix(),
            python_version,
            ignore_requires_python,
        )
        yield finder
        finder.session.close()
Exemplo n.º 8
0
def get_latest_version(project: Project) -> str | None:
    """Get the latest version of PDM from PyPI, cache for 7 days"""
    from pdm.utils import get_finder

    cache_key = hashlib.sha224(sys.executable.encode()).hexdigest()
    cache_file = project.cache("self-check") / cache_key
    if cache_file.exists():
        state = json.loads(cache_file.read_text())
    else:
        state = {}
    current_time = datetime.datetime.utcnow().timestamp()
    if (
        state.get("last-check")
        and current_time - state["last-check"] < 60 * 60 * 24 * 7
    ):
        return cast(str, state["latest-version"])
    candidate = get_finder([], project.cache_dir.as_posix()).find_best_candidate("pdm")
    if not candidate.best_candidate:
        return None
    latest_version = str(candidate.best_candidate.version)
    state.update({"latest-version": latest_version, "last-check": current_time})
    cache_file.write_text(json.dumps(state))
    return latest_version
Exemplo n.º 9
0
def get_local_finder(*args, **kwargs):
    finder = get_finder(*args, **kwargs)
    finder.session.mount("http://fixtures.test/", LocalFileAdapter(FIXTURES))
    return finder
Exemplo n.º 10
0
def parse_requirement_file(filename):
    finder = get_finder([])
    ireqs = list(parse_requirements(filename, finder.session, finder))
    return ireqs, finder