From 4110287349b34b435f46349891d2c5ff2f618fe3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Wed, 4 Nov 2020 11:41:19 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Support=20central=20local=20pip=20c?= =?UTF-8?q?ommand=20in=20server.pip?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Plugin Manager, Software Update and Backup will use that if set. Otherwise the old default of using LocalPipCaller is used. Not exposed via the UI as this REALLY should only be used by people who know what they are doing and the majority of users out there will never ever have to use it. Implements #3700 --- docs/configuration/config_yaml.rst | 6 +++++ src/octoprint/plugins/backup/__init__.py | 26 +++++++++++++++---- .../plugins/pluginmanager/__init__.py | 7 ++--- .../plugins/softwareupdate/__init__.py | 17 ++++++++++-- .../plugins/softwareupdate/updaters/pip.py | 13 +++------- src/octoprint/settings.py | 1 + src/octoprint/util/pip.py | 7 +++++ 7 files changed, 58 insertions(+), 19 deletions(-) diff --git a/docs/configuration/config_yaml.rst b/docs/configuration/config_yaml.rst index 6a242bf7b..06a328bf4 100644 --- a/docs/configuration/config_yaml.rst +++ b/docs/configuration/config_yaml.rst @@ -945,6 +945,12 @@ Use the following settings to configure the server: # Command to shut down the system OctoPrint is running on, defaults to being unset systemShutdownCommand: sudo shutdown -h now + # pip command associated with OctoPrint, used for installing plugins and updates, + # if unset (default) the command will be autodetected based on the current python + # executable - unless you have a really special setup this is the right way to do + # it and there should be no need to ever even touch this setting + localPipCommand: None + # Configuration of the regular online connectivity check onlineCheck: # whether the online check is enabled, defaults to false due to valid privacy concerns diff --git a/src/octoprint/plugins/backup/__init__.py b/src/octoprint/plugins/backup/__init__.py index ab57aa5dc..2b2b3bf94 100644 --- a/src/octoprint/plugins/backup/__init__.py +++ b/src/octoprint/plugins/backup/__init__.py @@ -13,7 +13,7 @@ from octoprint.server import NO_CONTENT from octoprint.server.util.flask import no_firstrun_access from octoprint.settings import default_settings from octoprint.util import is_hidden_path, to_bytes -from octoprint.util.pip import LocalPipCaller +from octoprint.util.pip import create_pip_caller from octoprint.util.platform import is_os_compatible from octoprint.util.version import ( get_comparable_version, @@ -344,7 +344,13 @@ class BackupPlugin( self._logger.info("Installing plugin {}".format(plugin["id"])) self._send_client_message("installing_plugin", {"plugin": plugin["id"]}) self.__class__._install_plugin( - plugin, force_user=force_user, pip_args=pip_args, on_log=on_log + plugin, + force_user=force_user, + pip_command=self._settings.global_get( + ["server", "commands", "localPipCommand"] + ), + pip_args=pip_args, + on_log=on_log, ) def on_report_unknown_plugins(plugins): @@ -585,7 +591,13 @@ class BackupPlugin( click.echo("Installing plugin {}".format(plugin["id"])) self.__class__._install_plugin( - plugin, force_user=force_user, pip_args=pip_args, on_log=log + plugin, + force_user=force_user, + pip_command=settings.global_get( + ["server", "commands", "localPipCommand"] + ), + pip_args=pip_args, + on_log=log, ) def on_report_unknown_plugins(plugins): @@ -768,7 +780,9 @@ class BackupPlugin( ) @classmethod - def _install_plugin(cls, plugin, force_user=False, pip_args=None, on_log=None): + def _install_plugin( + cls, plugin, force_user=False, pip_command=None, pip_args=None, on_log=None + ): if pip_args is None: pip_args = [] @@ -790,7 +804,9 @@ class BackupPlugin( log("!", *lines) if cls._pip_caller is None: - cls._pip_caller = LocalPipCaller(force_user=force_user) + cls._pip_caller = create_pip_caller( + command=pip_command, force_user=force_user + ) cls._pip_caller.on_log_call = log_call cls._pip_caller.on_log_stdout = log_stdout diff --git a/src/octoprint/plugins/pluginmanager/__init__.py b/src/octoprint/plugins/pluginmanager/__init__.py index f46cf0711..d730aa2b1 100644 --- a/src/octoprint/plugins/pluginmanager/__init__.py +++ b/src/octoprint/plugins/pluginmanager/__init__.py @@ -38,7 +38,7 @@ from octoprint.server.util.flask import ( from octoprint.settings import valid_boolean_trues from octoprint.util import TemporaryDirectory, to_bytes, to_native_str from octoprint.util.net import download_file -from octoprint.util.pip import LocalPipCaller +from octoprint.util.pip import create_pip_caller from octoprint.util.platform import get_os, is_os_compatible from octoprint.util.version import ( get_octoprint_version, @@ -201,8 +201,9 @@ class PluginManagerPlugin( self._notices_cache_ttl = self._settings.get_int(["notices_ttl"]) * 60 self._confirm_disable = self._settings.global_get_boolean(["confirm_disable"]) - self._pip_caller = LocalPipCaller( - force_user=self._settings.get_boolean(["pip_force_user"]) + self._pip_caller = create_pip_caller( + command=self._settings.global_get(["server", "commands", "localPipCommand"]), + force_user=self._settings.get_boolean(["pip_force_user"]), ) self._pip_caller.on_log_call = self._log_call self._pip_caller.on_log_stdout = self._log_stdout diff --git a/src/octoprint/plugins/softwareupdate/__init__.py b/src/octoprint/plugins/softwareupdate/__init__.py index 881198dbc..b10bf1d8a 100644 --- a/src/octoprint/plugins/softwareupdate/__init__.py +++ b/src/octoprint/plugins/softwareupdate/__init__.py @@ -33,7 +33,7 @@ from octoprint.server.util.flask import ( with_revalidation_checking, ) from octoprint.util import dict_merge, get_formatted_size, to_unicode -from octoprint.util.pip import LocalPipCaller +from octoprint.util.pip import create_pip_caller from octoprint.util.version import get_comparable_version, get_python_version_string from . import cli, exceptions, updaters, util, version_checks @@ -292,7 +292,9 @@ class SoftwareUpdatePlugin( def _check_environment(self): import pkg_resources - local_pip = LocalPipCaller() + local_pip = create_pip_caller( + command=self._settings.global_get(["server", "commands", "localPipCommand"]) + ) # check python and setuptools version versions = { @@ -1970,6 +1972,12 @@ class SoftwareUpdatePlugin( elif check.get("pip", None): # we force python unequality check for pip installs, to be able to downgrade result["release_compare"] = "python_unequal" + result["pip_command"] = check.get( + "pip_command", + self._settings.global_get( + ["server", "commands", "localPipCommand"] + ), + ) elif target == "pip": import pkg_resources @@ -1988,6 +1996,11 @@ class SoftwareUpdatePlugin( displayVersion = distribution.version result["displayVersion"] = to_unicode(displayVersion, errors="replace") + result["pip_command"] = check.get( + "pip_command", + self._settings.global_get(["server", "commands", "localPipCommand"]), + ) + else: result["displayName"] = to_unicode(check.get("displayName"), errors="replace") if result["displayName"] is None: diff --git a/src/octoprint/plugins/softwareupdate/updaters/pip.py b/src/octoprint/plugins/softwareupdate/updaters/pip.py index adc7fddd3..106033dcf 100644 --- a/src/octoprint/plugins/softwareupdate/updaters/pip.py +++ b/src/octoprint/plugins/softwareupdate/updaters/pip.py @@ -10,7 +10,7 @@ import logging import pkg_resources -from octoprint.util.pip import PipCaller, UnknownPip +from octoprint.util.pip import UnknownPip, create_pip_caller from octoprint.util.version import get_comparable_version from .. import exceptions @@ -50,7 +50,7 @@ def _get_pip_caller(command=None): if key not in _pip_callers: try: - _pip_callers[key] = PipCaller(configured=command) + _pip_callers[key] = create_pip_caller(command=command) except UnknownPip: _pip_callers[key] = None @@ -58,13 +58,8 @@ def _get_pip_caller(command=None): def perform_update(target, check, target_version, log_cb=None, online=True, force=False): - pip_command = None - if "pip_command" in check: - pip_command = check["pip_command"] - - pip_working_directory = None - if "pip_cwd" in check: - pip_working_directory = check["pip_cwd"] + pip_command = check.get("pip_command") + pip_working_directory = check.get("pip_cwd") if not online and not check.get("offline", False): raise exceptions.CannotUpdateOffline() diff --git a/src/octoprint/settings.py b/src/octoprint/settings.py index 74eeaa116..b6a644698 100644 --- a/src/octoprint/settings.py +++ b/src/octoprint/settings.py @@ -204,6 +204,7 @@ default_settings = { "systemShutdownCommand": None, "systemRestartCommand": None, "serverRestartCommand": None, + "localPipCommand": None, }, "onlineCheck": { "enabled": None, diff --git a/src/octoprint/util/pip.py b/src/octoprint/util/pip.py index 53ff20a14..a9c8d55bd 100644 --- a/src/octoprint/util/pip.py +++ b/src/octoprint/util/pip.py @@ -567,3 +567,10 @@ class LocalPipCaller(PipCaller): self._logger.info("==> pip ok -> {}".format("yes" if ok else "NO!")) return ok, user_flag, virtual_env, install_dir + + +def create_pip_caller(command=None, **kwargs): + if command is None: + return LocalPipCaller(**kwargs) + else: + return PipCaller(configured=command, **kwargs)