SWU: Track network connectivity & handle offline scenarios

See also #2011
This commit is contained in:
Gina Häußge committed 2017-07-19 18:42:28 +02:00
1 parent 2bc0c4a77f
commit fb8c56be57
16 files changed
+307 -111

No files matched your search

@@ -13,7 +13,12 @@ BRANCH_HEAD_URL = "https://api.github.com/repos/{user}/{repo}/git/refs/heads/{br
logger = logging.getLogger("octoprint.plugins.softwareupdate.version_checks.github_commit")
def _get_latest_commit(user, repo, branch):
r = requests.get(BRANCH_HEAD_URL.format(user=user, repo=repo, branch=branch), timeout=30)
from ..exceptions import NetworkError
try:
r = requests.get(BRANCH_HEAD_URL.format(user=user, repo=repo, branch=branch), timeout=(3.05, 30))
except requests.ConnectionError as exc:
raise NetworkError(cause=exc)
from . import log_github_ratelimit
log_github_ratelimit(logger, r)
@@ -28,7 +33,7 @@ def _get_latest_commit(user, repo, branch):
return reference["object"]["sha"]
def get_latest(target, check):
def get_latest(target, check, online=True):
from ..exceptions import ConfigurationInvalid
user = check.get("user")
@@ -43,12 +48,16 @@ def get_latest(target, check):
current = check.get("current")
remote_commit = _get_latest_commit(check["user"], check["repo"], branch)
information = dict(local=dict(name="Commit {commit}".format(commit=current if current is not None else "?"), value=current),
remote=dict(name="?", value="?"),
needs_online=not check.get("offline", False))
if not online and information["needs_online"]:
return information, True
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_commit = _get_latest_commit(check["user"], check["repo"], branch)
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))