1
0
mirror of https://github.com/mbirth/tcl_ota_check.git synced 2024-11-09 22:06:47 +00:00

Move retry code for checks into tcllib.

This commit is contained in:
Markus Birth 2017-10-05 23:51:07 +02:00
parent 11370f618e
commit 8b7de97f84
Signed by: mbirth
GPG Key ID: A9928D7A098C3A9A
3 changed files with 68 additions and 85 deletions

View File

@ -1,7 +1,7 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# pylint: disable=C0111,C0326
# pylint: disable=C0111,C0326,C0103
import tcllib
from requests.exceptions import RequestException, Timeout
@ -16,16 +16,14 @@ fc.mode = fc.MODE_FULL
# CLTP = 10 (only show actual updates or HTTP 206) / 2010 (always show latest version for MODE_FULL)
#fc.cltp = 10
fc.cltp = 2010
fc.timeout = 20
print("List of latest FULL firmware by PRD:")
with open("prds.txt", "r") as afile:
prdx = afile.read()
prds = list(filter(None, prdx.split("\n")))
with open("prds.txt", "rt") as f:
for prdline in f:
prdline = prdline.strip()
prd, lastver, model = prdline.split(" ", 2)
while len(prds) > 0:
prd, lastver, model = prds[0].split(" ", 2)
try:
fc.reset_session()
fc.curef = prd
@ -35,16 +33,6 @@ while len(prds) > 0:
if tv != lastver:
txt_tv = tcllib.ANSI_CYAN + txt_tv + tcllib.ANSI_RESET
print("{}: {} {} ({})".format(prd, txt_tv, fhash, model))
prds.pop(0)
except Timeout as e:
print("{} failed. (Connection timed out.)".format(prd))
print(tcllib.ANSI_UP_DEL, end="")
continue
except (SystemExit, RequestException) as e:
print("{} failed. ({})".format(prd, str(e)))
if e.response.status_code in [204, 404]:
# No update available or invalid request - remove from queue
prds.pop(0)
else:
print(tcllib.ANSI_UP_DEL, end="")
except RequestException as e:
print("{}: {}".format(prd, str(e)))
continue

View File

@ -1,11 +1,11 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# pylint: disable=C0111,C0326
# pylint: disable=C0111,C0326,C0103
import sys
import tcllib
from requests.exceptions import RequestException, Timeout
from requests.exceptions import RequestException
tcllib.make_escapes_work()
@ -14,7 +14,6 @@ fc.serid = "3531510"
#fc.osvs = "7.1.1"
fc.mode = fc.MODE_OTA
fc.cltp = 10
fc.timeout = 20
force_ver = False
force_ver_text = ""
@ -24,12 +23,10 @@ if len(sys.argv) > 1:
print("List of latest OTA firmware{} by PRD:".format(force_ver_text))
with open("prds.txt", "r") as afile:
prdx = afile.read()
prds = list(filter(None, prdx.split("\n")))
while len(prds) > 0:
prd, lastver, model = prds[0].split(" ", 2)
with open("prds.txt", "r") as f:
for prdline in f:
prdline = prdline.strip()
prd, lastver, model = prdline.split(" ", 2)
if force_ver != False:
lastver = force_ver
try:
@ -39,16 +36,6 @@ while len(prds) > 0:
check_xml = fc.do_check()
curef, fv, tv, fw_id, fileid, fn, fsize, fhash = fc.parse_check(check_xml)
print("{}: {}{} {} ({})".format(prd, fv, tv, fhash, model))
prds.pop(0)
except Timeout as e:
print("{} failed. (Connection timed out.)".format(prd))
print(tcllib.ANSI_UP_DEL, end="")
continue
except (SystemExit, RequestException) as e:
print("{} ({}) failed. ({})".format(prd, lastver, str(e)))
if e.response.status_code in [204, 404]:
# No update available or invalid request - remove from queue
prds.pop(0)
else:
print(tcllib.ANSI_UP_DEL, end="")
except RequestException as e:
print("{} ({}): {}".format(prd, lastver, str(e)))
continue

View File

@ -59,7 +59,15 @@ class FotaCheck:
self.cktp = self.CKTP_CHECKMANUAL
self.rtd = self.RTD_UNROOTED
self.chnl = self.CHNL_WIFI
self.timeout = 10
self.g2master = None
self.master_servers = [
"g2master-us-east.tclclouds.com",
"g2master-us-west.tclclouds.com",
"g2master-eu-west.tclclouds.com",
"g2master-ap-south.tclclouds.com",
"g2master-ap-north.tclclouds.com",
"g2master-sa-east.tclclouds.com",
]
self.reset_session()
def reset_session(self):
@ -77,19 +85,10 @@ class FotaCheck:
tail = "{:06d}".format(random.randint(0, 999999))
return "{}{}".format(str(millis), tail)
@staticmethod
def get_master_server():
master_servers = [
"g2master-us-east.tclclouds.com",
"g2master-us-west.tclclouds.com",
"g2master-eu-west.tclclouds.com",
"g2master-ap-south.tclclouds.com",
"g2master-ap-north.tclclouds.com",
"g2master-sa-east.tclclouds.com",
]
return random.choice(master_servers)
def get_master_server(self):
return random.choice(self.master_servers)
def do_check(self, https=True):
def do_check(self, https=True, timeout=10, max_tries=5):
protocol = "https://" if https else "http://"
url = protocol + self.g2master + "/check.php"
params = OrderedDict()
@ -104,18 +103,27 @@ class FotaCheck:
params["chnl"] = self.chnl
#params["osvs"] = self.osvs
req = self.sess.get(url, params=params, timeout=self.timeout)
last_response = None
for num_try in range(0, max_tries):
try:
req = self.sess.get(url, params=params, timeout=timeout)
last_response = req
if req.status_code == 200:
return req.text
elif req.status_code == 204:
raise requests.exceptions.HTTPError("No update available.", response=req)
elif req.status_code == 404:
raise requests.exceptions.HTTPError("No data for requested CUREF/FV combination.", response=req)
elif req.status_code in (500, 503):
raise requests.exceptions.HTTPError("Server busy, try again later.", response=req)
else:
elif req.status_code not in [500, 503]:
req.raise_for_status()
raise requests.exceptions.HTTPError("HTTP {}.".format(req.status_code), response=req)
except requests.exceptions.Timeout:
pass
# Something went wrong, try a different server
self.g2master = self.get_master_server()
protocol = "https://" if https else "http://"
url = protocol + self.g2master + "/check.php"
raise requests.exceptions.RetryError("Max tries ({}) reached.".format(max_tries), response=last_response)
@staticmethod
def pretty_xml(xmlstr):