Example #1
0
def do_use(project: Project, python: str) -> None:
    """Use the specified python version and save in project config.
    The python can be a version string or interpreter path.
    """
    if Path(python).is_absolute():
        python_path = python
    else:
        python_path = shutil.which(python)
        if not python_path:
            finder = pythonfinder.Finder()
            try:
                python_path = finder.find_python_version(
                    python).path.as_posix()
            except AttributeError:
                raise NoPythonVersion(
                    f"Python {python} is not found on the system.")

    python_version = get_python_version(python_path, True)
    if not project.python_requires.contains(python_version):
        raise NoPythonVersion("The target Python version {} doesn't satisfy "
                              "the Python requirement: {}".format(
                                  python_version, project.python_requires))
    context.io.echo("Using Python interpreter: {} ({})".format(
        context.io.green(python_path), python_version))

    project.config["python"] = Path(python_path).as_posix()
    project.config.save_config()
Example #2
0
def do_use(project: Project, python: str, first: bool = False) -> None:
    """Use the specified python version and save in project config.
    The python can be a version string or interpreter path.
    """
    import pythonfinder

    python = python.strip()
    if python and not all(c.isdigit() for c in python.split(".")):
        if Path(python).exists():
            python_path = find_python_in_path(python)
        else:
            python_path = shutil.which(python)
        if not python_path:
            raise NoPythonVersion(f"{python} is not a valid Python.")
        python_version, is_64bit = get_python_version(python_path, True)
    else:
        finder = pythonfinder.Finder()
        pythons = []
        args = [int(v) for v in python.split(".") if v != ""]
        for i, entry in enumerate(finder.find_all_python_versions(*args)):
            python_version, is_64bit = get_python_version(entry.path.as_posix(), True)
            pythons.append((entry.path.as_posix(), python_version, is_64bit))
        if not pythons:
            raise NoPythonVersion(f"Python {python} is not available on the system.")

        if not first and len(pythons) > 1:
            for i, (path, python_version, is_64bit) in enumerate(pythons):
                stream.echo(
                    f"{i}. {stream.green(path)} "
                    f"({get_python_version_string(python_version, is_64bit)})"
                )
            selection = click.prompt(
                "Please select:",
                type=click.Choice([str(i) for i in range(len(pythons))]),
                default="0",
                show_choices=False,
            )
        else:
            selection = 0
        python_path, python_version, is_64bit = pythons[int(selection)]

    if not project.python_requires.contains(python_version):
        raise NoPythonVersion(
            "The target Python version {} doesn't satisfy "
            "the Python requirement: {}".format(python_version, project.python_requires)
        )
    stream.echo(
        "Using Python interpreter: {} ({})".format(
            stream.green(python_path),
            get_python_version_string(python_version, is_64bit),
        )
    )
    old_path = project.config.get("python.path")
    new_path = python_path
    project.project_config["python.path"] = Path(new_path).as_posix()
    if old_path and Path(old_path) != Path(new_path) and not project.is_global:
        stream.echo(stream.cyan("Updating executable scripts..."))
        project.environment.update_shebangs(new_path)
Example #3
0
def special_character_python(tmpdir):
    finder = pythonfinder.Finder(global_search=True,
                                 system=False,
                                 ignore_unsupported=True)
    python = finder.find_python_version("2")
    python_name = "{0}+".format(python.name)
    python_folder = tmpdir.mkdir(python_name)
    bin_dir = python_folder.mkdir("bin")
    python_path = bin_dir.join("python")
    os.link(python.path.as_posix(), python_path.strpath)
    return python_path
Example #4
0
 def __call__(self, value):
     if os.path.isabs(value):
         return str(value)
     full = which(value)
     if full:
         return full
     import pythonfinder
     python = pythonfinder.Finder().find_python_version(value)
     if python:
         return str(python.path)
     raise ValueError(value)
Example #5
0
def special_character_python(tmp_path):
    finder = pythonfinder.Finder(global_search=False,
                                 system=True,
                                 ignore_unsupported=True,
                                 sort_by_path=True)
    python = finder.find_python_version()
    python_name = "2+"
    python_folder = tmp_path / python_name / "bin"
    python_folder.mkdir(parents=True)
    set_write_bit(str(python_folder))
    python_path = python_folder / "python"
    python_path.symlink_to(python.path)
    return python_path
Example #6
0
def special_character_python(tmpdir):
    finder = pythonfinder.Finder(global_search=False,
                                 system=True,
                                 ignore_unsupported=True,
                                 sort_by_path=True)
    python = finder.find_python_version()
    python_name = "2+"
    python_folder = tmpdir.mkdir(python_name)
    bin_dir = python_folder.mkdir("bin")
    set_write_bit(bin_dir.strpath)
    python_path = bin_dir.join("python")
    os.link(python.path.as_posix(), python_path.strpath)
    return python_path
Example #7
0
def do_use(project: Project, python: str, first: bool = False) -> None:
    """Use the specified python version and save in project config.
    The python can be a version string or interpreter path.
    """
    if not all(c.isdigit() for c in python.split(".")):
        if Path(python).exists():
            python_path = Path(python).absolute().as_posix()
        else:
            python_path = shutil.which(python)
        if not python_path:
            raise NoPythonVersion(f"{python} is not a valid Python.")
        python_version = get_python_version(python_path, True)
    else:
        finder = pythonfinder.Finder()
        pythons = []
        args = [int(v) for v in python.split(".")]
        for i, entry in enumerate(finder.find_all_python_versions(*args)):
            python_version = get_python_version(entry.path.as_posix(), True)
            pythons.append((entry.path.as_posix(), python_version))
        if not pythons:
            raise NoPythonVersion(
                f"Python {python} is not available on the system.")

        if not first and len(pythons) > 1:
            for i, (path, python_version) in enumerate(pythons):
                context.io.echo(
                    f"{i}: {context.io.green(path)} ({python_version})")
            selection = click.prompt(
                "Please select:",
                type=click.Choice([str(i) for i in range(len(pythons))]),
                default="0",
                show_choices=False,
            )
        else:
            selection = 0
        python_path, python_version = pythons[int(selection)]

    if not project.python_requires.contains(python_version):
        raise NoPythonVersion("The target Python version {} doesn't satisfy "
                              "the Python requirement: {}".format(
                                  python_version, project.python_requires))
    context.io.echo("Using Python interpreter: {} ({})".format(
        context.io.green(python_path), python_version))

    project.config["python.path"] = Path(python_path).as_posix()
    project.config.save_config()
Example #8
0
def _build_python_tuples():
    finder = pythonfinder.Finder(global_search=True,
                                 system=False,
                                 ignore_unsupported=True)
    finder_versions = finder.find_all_python_versions()
    versions = []
    for v in finder_versions:
        if not v.is_python:
            continue
        version = v.as_python
        if not version:
            continue
        arch = (version.architecture if version.architecture else
                pythonfinder.environment.SYSTEM_ARCH)
        arch = arch.replace("bit", "")
        if version.name.startswith("python"):
            name = str(version.version)
        else:
            name = version.name
        path = normalize_path(v.path)
        version = str(version.version)
        versions.append(pythoninfo(name, version, path, arch))
    return versions
Example #9
0
def find_by_version(val):
    finder = pythonfinder.Finder()
    return finder.find_python_version(val)