From 8ad0aadb52b9ef354cad1b33bd4882ae2fbdb8d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Mon, 12 Feb 2018 14:31:49 +0100 Subject: [PATCH] SWU: Workaround for pip problem with eggs Starting with pip 7.x pip runs into an issue if a software package formerly installed as a Python egg (= zip file stored in site-packages, referenced through easy-install.pth, e.g. through a stand alone "python setup.py install") is to be updated to a newer version. The issue arises since the egg is still on the python path after the replacement has taken place, and pip tries to read from it without error handling for the fact that this file is now not longer existing. The source of the problem appears to be with both the bundled as well as the stock versions of pkg_resources and pip's usage of them to perform its management duties. It's currently unclear if the problem is a bug or a feature, even though it strongly looks like the first one. In any case, during tests it was found that this problem can be worked around by simply performing a second "pip install" with same parameters right after the failed first one. This patch detects this scenario based on the logged error message and performs the second install if deemed necessary. This is a preparational step to allow upgrade of the pyserial dependency (and other dependencies currently installed as eggs as part of OctoPrint's former "python setup.py install" installation procedure) with OctoPrint 1.3.8, to finalize the fix for #2333. --- .../plugins/softwareupdate/updaters/pip.py | 41 ++++++++++++++++--- 1 file changed, 35 insertions(+), 6 deletions(-) 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))