From e338f537f578ab97637a51faa9e73f65943e21d0 Mon Sep 17 00:00:00 2001 From: Eric Callahan Date: Fri, 14 Feb 2025 10:39:58 -0500 Subject: [PATCH] pip_utils: handle exceptions when updating pip Don't propagate exceptions when update_pip fails. Some systems may not be able to update pip, and the current version of pip should be sufficient to install packages. Signed-off-by: Eric Callahan --- moonraker/utils/pip_utils.py | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/moonraker/utils/pip_utils.py b/moonraker/utils/pip_utils.py index 030d8ee..94d4c18 100644 --- a/moonraker/utils/pip_utils.py +++ b/moonraker/utils/pip_utils.py @@ -12,6 +12,7 @@ import subprocess import pathlib import shutil import threading +import logging from dataclasses import dataclass # Annotation imports @@ -34,6 +35,9 @@ if TYPE_CHECKING: MIN_PIP_VERSION = (24, 0) MIN_PYTHON_VERSION = (3, 7) +class PipException(Exception): + pass + # Synchronous Subprocess Helpers def _run_subprocess_with_response( cmd: str, @@ -48,7 +52,7 @@ def _run_subprocess_with_response( if proc.returncode == 0: return proc.stdout.strip() err = proc.stderr - raise Exception(f"Failed to run pip command '{cmd}': {err}") + raise PipException(f"Failed to run pip command '{cmd}': {err}") def _process_subproc_output( stdout: IO[str], @@ -80,9 +84,9 @@ def _run_subprocess( process.wait(timeout) ret = process.poll() if ret != 0: - raise Exception(f"Failed to run pip command '{cmd}'") + raise PipException(f"Failed to run pip command '{cmd}'") -@ dataclass(frozen=True) +@dataclass(frozen=True) class PipVersionInfo: pip_version_string: str python_version_string: str @@ -124,7 +128,10 @@ class PipExecutor: def update_pip(self) -> None: pip_ver = ".".join([str(part) for part in MIN_PIP_VERSION]) - self.call_pip(f"install pip=={pip_ver}", 120.) + try: + self.call_pip(f"install pip=={pip_ver}", 120.) + except PipException: + logging.exception("Failed to update pip") def install_packages( self, @@ -209,10 +216,13 @@ class AsyncPipExecutor: async def update_pip(self) -> None: pip_ver = ".".join([str(part) for part in MIN_PIP_VERSION]) shell_cmd = self.get_shell_cmd() - await shell_cmd.run_cmd_async( - f"{self.pip_cmd} install pip=={pip_ver}", - self.notify_callback, timeout=1200., attempts=3, log_stderr=True - ) + try: + await shell_cmd.run_cmd_async( + f"{self.pip_cmd} install pip=={pip_ver}", + self.notify_callback, timeout=1200., attempts=1, log_stderr=True + ) + except shell_cmd.error: + logging.exception("Failed to Update Pip") async def install_packages( self,