SWU: Track network connectivity & handle offline scenarios
See also #2011
This commit is contained in:
1 parent
2bc0c4a77f
commit
fb8c56be57
16 files changed
+307
-111
No files matched your search
@@ -14,12 +14,18 @@ logger = logging.getLogger("octoprint.plugins.softwareupdate.version_checks.bitb
|
||||
|
||||
|
||||
def _get_latest_commit(user, repo, branch, api_user=None, api_password=None):
|
||||
from ..exceptions import NetworkError
|
||||
|
||||
url = BRANCH_HEAD_URL.format(user=user, repo=repo, branch=branch)
|
||||
headers = {}
|
||||
if api_user is not None and api_password is not None:
|
||||
auth_value = base64.b64encode(b"{user}:{pw}".format(user=api_user, pw=api_password))
|
||||
headers["authorization"] = "Basic {}".format(auth_value)
|
||||
r = requests.get(url, headers=headers)
|
||||
|
||||
try:
|
||||
r = requests.get(url, headers=headers, timeout=(3.05, 30))
|
||||
except requests.ConnectionError as exc:
|
||||
raise NetworkError(cause=exc)
|
||||
|
||||
if not r.status_code == requests.codes.ok:
|
||||
return None
|
||||
@@ -31,7 +37,7 @@ def _get_latest_commit(user, repo, branch, api_user=None, api_password=None):
|
||||
return reference["hash"]
|
||||
|
||||
|
||||
def get_latest(target, check):
|
||||
def get_latest(target, check, online=True):
|
||||
from ..exceptions import ConfigurationInvalid
|
||||
|
||||
if "user" not in check or "repo" not in check:
|
||||
@@ -46,12 +52,18 @@ def get_latest(target, check):
|
||||
|
||||
current = check.get("current")
|
||||
|
||||
remote_commit = _get_latest_commit(check["user"], check["repo"], branch, api_user, api_password)
|
||||
|
||||
information = dict(
|
||||
local=dict(name="Commit {commit}".format(commit=current if current is not None else "unknown"), value=current),
|
||||
remote=dict(name="Commit {commit}".format(commit=remote_commit if remote_commit is not None else "unknown"), value=remote_commit)
|
||||
remote=dict(name="?", value="?"),
|
||||
needs_online=not check.get("offline", False)
|
||||
)
|
||||
if not online and information["needs_online"]:
|
||||
return information, True
|
||||
|
||||
remote_commit = _get_latest_commit(check["user"], check["repo"], branch, api_user, api_password)
|
||||
remote_name = "Commit {commit}".format(commit=remote_commit) if remote_commit is not None else "-"
|
||||
|
||||
information["remote"] = dict(name=remote_name, value=remote_commit)
|
||||
is_current = (current is not None and current == remote_commit) or remote_commit is None
|
||||
|
||||
logger.debug("Target: %s, local: %s, remote: %s" % (target, current, remote_commit))
|
||||
|
||||
Reference in new issue
Block a user