Skip to content

Commit d04afcf

Browse files
authored
SW-527 mr beam tag based updates (#6)
* SW-995 change versioning file for netconnectd plugin (mrbeam#31) * SW-650 create update script for netconnectd plugin (mrbeam#30)
1 parent 9c55c00 commit d04afcf

File tree

4 files changed

+189
-2
lines changed

4 files changed

+189
-2
lines changed

octoprint_netconnectd/scripts/update_script.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
from octoprint.plugins.softwareupdate import exceptions
1717
from octoprint.settings import _default_basedir
1818

19-
from octoprint_mrbeam.util.pip_util import get_version_of_pip_module, \
20-
get_pip_caller # TODO check how to be independent of mrbeam plugin
19+
from octoprint_netconnectd.util.pip_util import get_version_of_pip_module, \
20+
get_pip_caller
2121

2222
from requests.adapters import HTTPAdapter
2323
from urllib3 import Retry

octoprint_netconnectd/util/__init__.py

Whitespace-only changes.
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
"""
2+
copy pasta of octoprint_mrbeam/util/cmd_exec.py
3+
only changed the logging to be mrbeam plugin independend
4+
"""
5+
import logging
6+
import subprocess
7+
from logging import DEBUG
8+
9+
10+
def exec_cmd(cmd, log=True, shell=True, loglvl=DEBUG):
11+
"""
12+
Executes a system command
13+
:param cmd:
14+
:return: True if system returncode was 0,
15+
False if the command returned with an error,
16+
None if there was an exception.
17+
"""
18+
_logger = logging.getLogger(__name__ + ".exec_cmd")
19+
code = None
20+
if log:
21+
_logger.log(loglvl, "cmd=%s", cmd)
22+
try:
23+
code = subprocess.call(cmd, shell=shell)
24+
except Exception as e:
25+
_logger.debug(
26+
"Failed to execute command '%s', return code: %s, Exception: %s",
27+
cmd,
28+
code,
29+
e,
30+
)
31+
return None
32+
if code != 0 and log:
33+
_logger.info("cmd= '%s', return code: '%s'", code)
34+
return code == 0
35+
36+
37+
def exec_cmd_output(cmd, log=True, shell=False, loglvl=DEBUG):
38+
"""
39+
Executes a system command and returns its output.
40+
:param cmd:
41+
:return: Tuple(String:output , int return_code)
42+
"""
43+
_logger = logging.getLogger(__name__ + "exec_cmd_output")
44+
output = None
45+
code = 0
46+
if log:
47+
_logger.log(loglvl, "cmd='%s'", cmd)
48+
try:
49+
output = subprocess.check_output(cmd, shell=shell, stderr=subprocess.STDOUT)
50+
except subprocess.CalledProcessError as e:
51+
code = e.returncode
52+
53+
if not log:
54+
cmd = cmd[:50] + "..." if len(cmd) > 30 else cmd
55+
if e.output is not None:
56+
output = e.output[:30] + "..." if len(e.output) > 30 else e.output
57+
else:
58+
output = e.output
59+
_logger.log(
60+
loglvl,
61+
"Failed to execute command '%s', return code: %s, output: '%s'",
62+
cmd,
63+
e.returncode,
64+
output,
65+
)
66+
67+
except Exception as e:
68+
code = 99
69+
output = "{e}: {o}".format(e=e, o=output)
70+
_logger.log(
71+
loglvl,
72+
"Failed to execute command '%s', return code: %s, output: '%s'",
73+
cmd,
74+
None,
75+
output,
76+
)
77+
78+
return output, code
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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

Comments
 (0)