|
| 1 | +""" |
| 2 | +copy pasta of octoprint_mrbeam/util/pip_util.py |
| 3 | +only changed the logging to be mrbeam plugin independend |
| 4 | +""" |
| 5 | +import logging |
| 6 | + |
| 7 | +from octoprint.plugins.softwareupdate.updaters.pip import _get_pip_caller |
| 8 | +from octoprint.util.pip import PipCaller |
| 9 | +from cmd_exec import exec_cmd_output |
| 10 | + |
| 11 | +DISABLE_PIP_CHECK = "--disable-pip-version-check" |
| 12 | +DISABLE_PY_WARNING = "--no-python-version-warning" |
| 13 | + |
| 14 | +# Dictionary of package versions available at different locations |
| 15 | +# { |
| 16 | +# /home/pi/oprint/bin/pip : { |
| 17 | +# "OctoPrint x.x.x", |
| 18 | +# ... |
| 19 | +# }, |
| 20 | +# /usr/share/iobeam/venv/bin/pip : { |
| 21 | +# "iobeam y.y.y", |
| 22 | +# ... |
| 23 | +# } |
| 24 | +# } |
| 25 | +_pip_package_version_lists = {} |
| 26 | + |
| 27 | + |
| 28 | +def get_version_of_pip_module(pip_name, pip_command=None, disable_pip_ver_check=True): |
| 29 | + _logger = logging.getLogger(__name__ + ".get_version_of_pip_module") |
| 30 | + global _pip_package_version_lists |
| 31 | + version = None |
| 32 | + returncode = -1 |
| 33 | + if pip_command is None: |
| 34 | + pip_command = "pip" |
| 35 | + elif isinstance(pip_command, list): |
| 36 | + pip_command = " ".join(pip_command) |
| 37 | + # Checking for pip version outdate takes extra time and text output. |
| 38 | + # NOTE: Older versions of pip do not have the --no-python-version-warning flag |
| 39 | + for disabled in [ |
| 40 | + DISABLE_PIP_CHECK, |
| 41 | + ]: # DISABLE_PY_WARNING]: |
| 42 | + if disable_pip_ver_check and not disabled in pip_command: |
| 43 | + pip_command += " " + disabled |
| 44 | + venv_packages = _pip_package_version_lists.get(pip_command, None) |
| 45 | + |
| 46 | + if venv_packages is None: |
| 47 | + # perform a pip discovery and remember it for next time |
| 48 | + command = "{pip_command} list".format(pip_command=pip_command) |
| 49 | + _logger.debug("refreshing list of installed packages (%s list)", pip_command) |
| 50 | + output, returncode = exec_cmd_output(command, shell=True, log=False) |
| 51 | + if returncode == 0: |
| 52 | + venv_packages = output.splitlines() |
| 53 | + _pip_package_version_lists[pip_command] = venv_packages |
| 54 | + elif returncode == 127: |
| 55 | + _logger.error( |
| 56 | + "`%s` was not found in local $PATH (returncode %s)", |
| 57 | + pip_command, |
| 58 | + returncode, |
| 59 | + ) |
| 60 | + return None |
| 61 | + else: |
| 62 | + _logger.warning("`%s list` returned code %s", pip_command, returncode) |
| 63 | + return None |
| 64 | + # Go through the package list available in our venv |
| 65 | + for line in venv_packages: |
| 66 | + token = line.split() |
| 67 | + if len(token) >= 2 and token[0] == pip_name: |
| 68 | + version = token[1] |
| 69 | + break |
| 70 | + _logger.debug("%s==%s", pip_name, version) |
| 71 | + return version |
| 72 | + |
| 73 | + |
| 74 | +def get_pip_caller(venv, _logger=None): |
| 75 | + """ |
| 76 | + gets the pip caller of the givenv venv |
| 77 | +
|
| 78 | + Args: |
| 79 | + venv: path to venv |
| 80 | + _logger: logger to log call, stdout and stderr of the pip caller |
| 81 | +
|
| 82 | + Returns: |
| 83 | + PipCaller of the venv |
| 84 | + """ |
| 85 | + pip_caller = _get_pip_caller(command=venv) |
| 86 | + if not isinstance(pip_caller, PipCaller): |
| 87 | + raise RuntimeError("Can't run pip", None) |
| 88 | + |
| 89 | + def _log_call(*lines): |
| 90 | + _log(lines, prefix=" ", stream="call") |
| 91 | + |
| 92 | + def _log_stdout(*lines): |
| 93 | + _log(lines, prefix=">", stream="stdout") |
| 94 | + |
| 95 | + def _log_stderr(*lines): |
| 96 | + _log(lines, prefix="!", stream="stderr") |
| 97 | + |
| 98 | + def _log(lines, prefix=None, stream=None, strip=True): |
| 99 | + if strip: |
| 100 | + lines = map(lambda x: x.strip(), lines) |
| 101 | + for line in lines: |
| 102 | + print(u"{} {}".format(prefix, line)) |
| 103 | + |
| 104 | + if _logger is not None: |
| 105 | + pip_caller.on_log_call = _log_call |
| 106 | + pip_caller.on_log_stdout = _log_stdout |
| 107 | + pip_caller.on_log_stderr = _log_stderr |
| 108 | + |
| 109 | + return pip_caller |
0 commit comments