|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from subprocess import CalledProcessError |
| 4 | +from typing import TYPE_CHECKING |
| 5 | +from typing import ClassVar |
| 6 | +from typing import cast |
| 7 | + |
| 8 | +import pbs_installer as pbi |
| 9 | + |
| 10 | +from cleo.helpers import argument |
| 11 | +from cleo.helpers import option |
| 12 | +from poetry.core.constraints.version.version import Version |
| 13 | +from poetry.core.version.exceptions import InvalidVersionError |
| 14 | + |
| 15 | +from poetry.config.config import Config |
| 16 | +from poetry.console.commands.command import Command |
| 17 | +from poetry.console.commands.python.remove import PythonRemoveCommand |
| 18 | +from poetry.console.exceptions import ConsoleMessage |
| 19 | +from poetry.console.exceptions import PoetryRuntimeError |
| 20 | +from poetry.utils.env.python_manager import PoetryPythonPathProvider |
| 21 | + |
| 22 | + |
| 23 | +if TYPE_CHECKING: |
| 24 | + from cleo.io.inputs.argument import Argument |
| 25 | + from cleo.io.inputs.option import Option |
| 26 | + |
| 27 | + |
| 28 | +BAD_PYTHON_INSTALL_INFO = [ |
| 29 | + "This could happen because you are missing platform dependencies required.", |
| 30 | + "Please refer to https://gregoryszorc.com/docs/python-build-standalone/main/running.html#runtime-requirements " |
| 31 | + "for more information about the necessary requirements.", |
| 32 | + "Please remove the failing Python installation using <c1>poetry python remove <version></> before continuing.", |
| 33 | +] |
| 34 | + |
| 35 | + |
| 36 | +class PythonInstallCommand(Command): |
| 37 | + name = "python install" |
| 38 | + |
| 39 | + arguments: ClassVar[list[Argument]] = [ |
| 40 | + argument("python", "The python version to install.") |
| 41 | + ] |
| 42 | + |
| 43 | + options: ClassVar[list[Option]] = [ |
| 44 | + option("clean", "c", "Cleanup installation if check fails.", flag=True), |
| 45 | + option( |
| 46 | + "free-threaded", "f", "Use free-threaded version if available.", flag=True |
| 47 | + ), |
| 48 | + option( |
| 49 | + "implementation", |
| 50 | + "i", |
| 51 | + "Python implementation to use. (cpython, pypy)", |
| 52 | + flag=False, |
| 53 | + default="cpython", |
| 54 | + ), |
| 55 | + option( |
| 56 | + "reinstall", "r", "Reinstall if installation already exists.", flag=True |
| 57 | + ), |
| 58 | + ] |
| 59 | + |
| 60 | + description = "Install the specified Python version from the Python Standalone Builds project." |
| 61 | + |
| 62 | + def handle(self) -> int: |
| 63 | + request = self.argument("python") |
| 64 | + impl = self.option("implementation").lower() |
| 65 | + reinstall = self.option("reinstall") |
| 66 | + free_threaded = self.option("free-threaded") |
| 67 | + |
| 68 | + try: |
| 69 | + version = Version.parse(request) |
| 70 | + except (ValueError, InvalidVersionError): |
| 71 | + self.io.write_error_line( |
| 72 | + f"<error>Invalid Python version requested <b>{request}</></error>" |
| 73 | + ) |
| 74 | + return 1 |
| 75 | + |
| 76 | + if free_threaded and version < Version.parse("3.13.0"): |
| 77 | + self.io.write_error_line("") |
| 78 | + self.io.write_error_line( |
| 79 | + "Free threading is not supported for Python versions prior to <c1>3.13.0</>.\n\n" |
| 80 | + "See https://docs.python.org/3/howto/free-threading-python.html for more information." |
| 81 | + ) |
| 82 | + self.io.write_error_line("") |
| 83 | + return 1 |
| 84 | + |
| 85 | + try: |
| 86 | + pyver, _ = pbi.get_download_link( |
| 87 | + request, implementation=impl, free_threaded=free_threaded |
| 88 | + ) |
| 89 | + except ValueError: |
| 90 | + self.io.write_error_line( |
| 91 | + "No suitable standalone build found for the requested Python version." |
| 92 | + ) |
| 93 | + return 1 |
| 94 | + |
| 95 | + version = Version.from_parts( |
| 96 | + major=pyver.major, minor=pyver.minor, patch=pyver.micro |
| 97 | + ) |
| 98 | + |
| 99 | + provider: PoetryPythonPathProvider = cast( |
| 100 | + PoetryPythonPathProvider, PoetryPythonPathProvider.create() |
| 101 | + ) |
| 102 | + bad_executables = set() |
| 103 | + |
| 104 | + for python in provider.find_pythons(): |
| 105 | + try: |
| 106 | + if python.implementation.lower() != impl: |
| 107 | + continue |
| 108 | + |
| 109 | + if version == Version.parse(str(python.version)): |
| 110 | + if reinstall: |
| 111 | + break |
| 112 | + self.io.write_line( |
| 113 | + "Python version already installed at " |
| 114 | + f"<b>{PoetryPythonPathProvider.installation_dir(version, impl)}</>.\n" |
| 115 | + ) |
| 116 | + self.io.write_line( |
| 117 | + f"Use <c1>--reinstall</> to install anyway, " |
| 118 | + f"or use <c1>poetry python remove {version}</> first." |
| 119 | + ) |
| 120 | + return 1 |
| 121 | + except CalledProcessError: |
| 122 | + bad_executables.add(python.executable) |
| 123 | + |
| 124 | + if bad_executables: |
| 125 | + raise PoetryRuntimeError( |
| 126 | + reason="One or more installed version do not work on your system. This is not a Poetry issue.", |
| 127 | + messages=[ |
| 128 | + ConsoleMessage("\n".join(e.as_posix() for e in bad_executables)) |
| 129 | + .indent(" - ") |
| 130 | + .make_section("Failing Executables") |
| 131 | + .wrap("info"), |
| 132 | + *[ |
| 133 | + ConsoleMessage(m).wrap("warning") |
| 134 | + for m in BAD_PYTHON_INSTALL_INFO |
| 135 | + ], |
| 136 | + ], |
| 137 | + ) |
| 138 | + |
| 139 | + request_title = f"<c1>{request}</> (<b>{impl}</>)" |
| 140 | + |
| 141 | + try: |
| 142 | + self.io.write(f"Downloading and installing {request_title} ... ") |
| 143 | + # this can be broken into download, and install_file if required to make |
| 144 | + # use of Poetry's own mechanics for download and unpack |
| 145 | + pbi.install( |
| 146 | + request, |
| 147 | + Config().python_installation_dir, |
| 148 | + True, |
| 149 | + implementation=impl, |
| 150 | + free_threaded=free_threaded, |
| 151 | + ) |
| 152 | + except ValueError: |
| 153 | + self.io.write("<fg=red>Failed</>\n") |
| 154 | + self.io.write_error_line("") |
| 155 | + self.io.write_error_line( |
| 156 | + "No suitable standalone build found for the requested Python version." |
| 157 | + ) |
| 158 | + self.io.write_error_line("") |
| 159 | + return 1 |
| 160 | + |
| 161 | + self.io.write("<fg=green>Done</>\n") |
| 162 | + |
| 163 | + self.io.write(f"Testing {request_title} ... ") |
| 164 | + |
| 165 | + provider = PoetryPythonPathProvider( |
| 166 | + PoetryPythonPathProvider.installation_bin_paths(version, impl) |
| 167 | + ) |
| 168 | + |
| 169 | + for python in provider.find_pythons(): |
| 170 | + try: |
| 171 | + # this forces a python -c command internally in pbs-installer |
| 172 | + _ = python.version |
| 173 | + except CalledProcessError as e: |
| 174 | + self.io.write("<fg=red>Failed</>\n") |
| 175 | + |
| 176 | + installation_dir = PoetryPythonPathProvider.installation_dir( |
| 177 | + version, impl |
| 178 | + ) |
| 179 | + if installation_dir.exists() and self.option("clean"): |
| 180 | + PythonRemoveCommand.remove_python_installation( |
| 181 | + request, impl, self.io |
| 182 | + ) |
| 183 | + |
| 184 | + raise PoetryRuntimeError.create( |
| 185 | + reason="The installed version did not work on your system. This is not a Poetry issue.", |
| 186 | + exception=e, |
| 187 | + info=[ |
| 188 | + ConsoleMessage(f"{m}\n").wrap("info").text |
| 189 | + for m in BAD_PYTHON_INSTALL_INFO |
| 190 | + ], |
| 191 | + ) |
| 192 | + |
| 193 | + self.io.write("<fg=green>Done</>\n") |
| 194 | + |
| 195 | + return 0 |
0 commit comments