diff --git a/src/octoprint/plugins/softwareupdate/updaters/pip.py b/src/octoprint/plugins/softwareupdate/updaters/pip.py index 8605e2b3b..6372da151 100644 --- a/src/octoprint/plugins/softwareupdate/updaters/pip.py +++ b/src/octoprint/plugins/softwareupdate/updaters/pip.py @@ -16,6 +16,8 @@ logger = logging.getLogger("octoprint.plugins.softwareupdate.updaters.pip") console_logger = logging.getLogger("octoprint.plugins.softwareupdate.updaters.pip.console") _ALREADY_INSTALLED = "Requirement already satisfied (use --upgrade to upgrade)" +_POTENTIAL_EGG_PROBLEM_POSIX = "No such file or directory" +_POTENTIAL_EGG_PROBLEM_WINDOWS = "The system cannot find the file specified" _pip_callers = dict() _pip_version_dependency_links = pkg_resources.parse_version("1.5") @@ -42,6 +44,10 @@ def perform_update(target, check, target_version, log_cb=None, online=True, forc 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"] + if not online and not check.get("offline", False): raise exceptions.CannotUpdateOffline() @@ -58,6 +64,9 @@ def perform_update(target, check, target_version, log_cb=None, online=True, forc def _log_stderr(*lines): _log(lines, prefix="!", stream="stderr") + def _log_message(*lines): + _log(lines, prefix="#", stream="message") + def _log(lines, prefix=None, stream=None): if log_cb is None: return @@ -72,23 +81,43 @@ def perform_update(target, check, target_version, log_cb=None, online=True, forc logger.debug(u"Target: %s, executing pip install %s" % (target, install_arg)) pip_args = ["install", install_arg] + pip_kwargs = dict() + if pip_working_directory is not None: + pip_kwargs.update(cwd=pip_working_directory) if "dependency_links" in check and check["dependency_links"]: pip_args += ["--process-dependency-links"] - returncode, stdout, stderr = pip_caller.execute(*pip_args) + returncode, stdout, stderr = pip_caller.execute(*pip_args, **pip_kwargs) if returncode != 0: - raise exceptions.UpdateError("Error while executing pip install", (stdout, stderr)) - + # This could be caused by an issue with pip/setuptools: If something (target or dependency of target) was + # installed as an egg at an earlier date (e.g. thanks to just running python setup.py install), pip install + # will throw an error after updating that something to a newer (non-egg) version since it will still have + # the egg on its sys.path and expect to read data from it. Running the exact same command a second time + # solved this during hunt for a workaround, so this is what we'll now do if it indeed looks like this + # specific problem + def is_egg_problem(line): + return (_POTENTIAL_EGG_PROBLEM_POSIX in line or _POTENTIAL_EGG_PROBLEM_WINDOWS in line) and ".egg" in line + + if any(map(lambda x: is_egg_problem(x), stderr)) or any(map(lambda x: is_egg_problem(x), stdout)): + _log_message(u"This looks like an error caused by a specific issue in upgrading Python \"eggs\"", + u"via current versions of pip.", + u"Performing a second install attempt as a work around.") + returncode, stdout, stderr = pip_caller.execute(*pip_args, **pip_kwargs) + if returncode != 0: + raise exceptions.UpdateError("Error while executing pip install", (stdout, stderr)) + else: + raise exceptions.UpdateError("Error while executing pip install", (stdout, stderr)) + if not force and any(map(lambda x: x.strip().startswith(_ALREADY_INSTALLED) and (install_arg in x or install_arg in x.lower()), stdout)): - logger.debug(u"Looks like we were already installed in this version. Forcing a reinstall.") + _log_message(u"Looks like we were already installed in this version. Forcing a reinstall.") force = True if force: logger.debug(u"Target: %s, executing pip install %s --ignore-reinstalled --force-reinstall --no-deps" % (target, install_arg)) pip_args += ["--ignore-installed", "--force-reinstall", "--no-deps"] - - returncode, stdout, stderr = pip_caller.execute(*pip_args) + + returncode, stdout, stderr = pip_caller.execute(*pip_args, **pip_kwargs) if returncode != 0: raise exceptions.UpdateError("Error while executing pip install --force-reinstall", (stdout, stderr))