mirror of
https://github.com/mbirth/tcl_ota_check.git
synced 2024-11-10 06:16:46 +00:00
Move retry code for checks into tcllib.
This commit is contained in:
parent
11370f618e
commit
8b7de97f84
@ -1,7 +1,7 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
# pylint: disable=C0111,C0326
|
# pylint: disable=C0111,C0326,C0103
|
||||||
|
|
||||||
import tcllib
|
import tcllib
|
||||||
from requests.exceptions import RequestException, Timeout
|
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)
|
# CLTP = 10 (only show actual updates or HTTP 206) / 2010 (always show latest version for MODE_FULL)
|
||||||
#fc.cltp = 10
|
#fc.cltp = 10
|
||||||
fc.cltp = 2010
|
fc.cltp = 2010
|
||||||
fc.timeout = 20
|
|
||||||
|
|
||||||
print("List of latest FULL firmware by PRD:")
|
print("List of latest FULL firmware by PRD:")
|
||||||
|
|
||||||
with open("prds.txt", "r") as afile:
|
with open("prds.txt", "rt") as f:
|
||||||
prdx = afile.read()
|
for prdline in f:
|
||||||
prds = list(filter(None, prdx.split("\n")))
|
prdline = prdline.strip()
|
||||||
|
prd, lastver, model = prdline.split(" ", 2)
|
||||||
|
|
||||||
while len(prds) > 0:
|
|
||||||
prd, lastver, model = prds[0].split(" ", 2)
|
|
||||||
try:
|
try:
|
||||||
fc.reset_session()
|
fc.reset_session()
|
||||||
fc.curef = prd
|
fc.curef = prd
|
||||||
@ -35,16 +33,6 @@ while len(prds) > 0:
|
|||||||
if tv != lastver:
|
if tv != lastver:
|
||||||
txt_tv = tcllib.ANSI_CYAN + txt_tv + tcllib.ANSI_RESET
|
txt_tv = tcllib.ANSI_CYAN + txt_tv + tcllib.ANSI_RESET
|
||||||
print("{}: {} {} ({})".format(prd, txt_tv, fhash, model))
|
print("{}: {} {} ({})".format(prd, txt_tv, fhash, model))
|
||||||
prds.pop(0)
|
except RequestException as e:
|
||||||
except Timeout as e:
|
print("{}: {}".format(prd, str(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="")
|
|
||||||
continue
|
continue
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
# pylint: disable=C0111,C0326
|
# pylint: disable=C0111,C0326,C0103
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
import tcllib
|
import tcllib
|
||||||
from requests.exceptions import RequestException, Timeout
|
from requests.exceptions import RequestException
|
||||||
|
|
||||||
tcllib.make_escapes_work()
|
tcllib.make_escapes_work()
|
||||||
|
|
||||||
@ -14,7 +14,6 @@ fc.serid = "3531510"
|
|||||||
#fc.osvs = "7.1.1"
|
#fc.osvs = "7.1.1"
|
||||||
fc.mode = fc.MODE_OTA
|
fc.mode = fc.MODE_OTA
|
||||||
fc.cltp = 10
|
fc.cltp = 10
|
||||||
fc.timeout = 20
|
|
||||||
|
|
||||||
force_ver = False
|
force_ver = False
|
||||||
force_ver_text = ""
|
force_ver_text = ""
|
||||||
@ -24,12 +23,10 @@ if len(sys.argv) > 1:
|
|||||||
|
|
||||||
print("List of latest OTA firmware{} by PRD:".format(force_ver_text))
|
print("List of latest OTA firmware{} by PRD:".format(force_ver_text))
|
||||||
|
|
||||||
with open("prds.txt", "r") as afile:
|
with open("prds.txt", "r") as f:
|
||||||
prdx = afile.read()
|
for prdline in f:
|
||||||
prds = list(filter(None, prdx.split("\n")))
|
prdline = prdline.strip()
|
||||||
|
prd, lastver, model = prdline.split(" ", 2)
|
||||||
while len(prds) > 0:
|
|
||||||
prd, lastver, model = prds[0].split(" ", 2)
|
|
||||||
if force_ver != False:
|
if force_ver != False:
|
||||||
lastver = force_ver
|
lastver = force_ver
|
||||||
try:
|
try:
|
||||||
@ -39,16 +36,6 @@ while len(prds) > 0:
|
|||||||
check_xml = fc.do_check()
|
check_xml = fc.do_check()
|
||||||
curef, fv, tv, fw_id, fileid, fn, fsize, fhash = fc.parse_check(check_xml)
|
curef, fv, tv, fw_id, fileid, fn, fsize, fhash = fc.parse_check(check_xml)
|
||||||
print("{}: {} ⇨ {} {} ({})".format(prd, fv, tv, fhash, model))
|
print("{}: {} ⇨ {} {} ({})".format(prd, fv, tv, fhash, model))
|
||||||
prds.pop(0)
|
except RequestException as e:
|
||||||
except Timeout as e:
|
print("{} ({}): {}".format(prd, lastver, str(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="")
|
|
||||||
continue
|
continue
|
||||||
|
42
tcllib.py
42
tcllib.py
@ -59,7 +59,15 @@ class FotaCheck:
|
|||||||
self.cktp = self.CKTP_CHECKMANUAL
|
self.cktp = self.CKTP_CHECKMANUAL
|
||||||
self.rtd = self.RTD_UNROOTED
|
self.rtd = self.RTD_UNROOTED
|
||||||
self.chnl = self.CHNL_WIFI
|
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()
|
self.reset_session()
|
||||||
|
|
||||||
def reset_session(self):
|
def reset_session(self):
|
||||||
@ -77,19 +85,10 @@ class FotaCheck:
|
|||||||
tail = "{:06d}".format(random.randint(0, 999999))
|
tail = "{:06d}".format(random.randint(0, 999999))
|
||||||
return "{}{}".format(str(millis), tail)
|
return "{}{}".format(str(millis), tail)
|
||||||
|
|
||||||
@staticmethod
|
def get_master_server(self):
|
||||||
def get_master_server():
|
return random.choice(self.master_servers)
|
||||||
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 do_check(self, https=True):
|
def do_check(self, https=True, timeout=10, max_tries=5):
|
||||||
protocol = "https://" if https else "http://"
|
protocol = "https://" if https else "http://"
|
||||||
url = protocol + self.g2master + "/check.php"
|
url = protocol + self.g2master + "/check.php"
|
||||||
params = OrderedDict()
|
params = OrderedDict()
|
||||||
@ -104,18 +103,27 @@ class FotaCheck:
|
|||||||
params["chnl"] = self.chnl
|
params["chnl"] = self.chnl
|
||||||
#params["osvs"] = self.osvs
|
#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:
|
if req.status_code == 200:
|
||||||
return req.text
|
return req.text
|
||||||
elif req.status_code == 204:
|
elif req.status_code == 204:
|
||||||
raise requests.exceptions.HTTPError("No update available.", response=req)
|
raise requests.exceptions.HTTPError("No update available.", response=req)
|
||||||
elif req.status_code == 404:
|
elif req.status_code == 404:
|
||||||
raise requests.exceptions.HTTPError("No data for requested CUREF/FV combination.", response=req)
|
raise requests.exceptions.HTTPError("No data for requested CUREF/FV combination.", response=req)
|
||||||
elif req.status_code in (500, 503):
|
elif req.status_code not in [500, 503]:
|
||||||
raise requests.exceptions.HTTPError("Server busy, try again later.", response=req)
|
|
||||||
else:
|
|
||||||
req.raise_for_status()
|
req.raise_for_status()
|
||||||
raise requests.exceptions.HTTPError("HTTP {}.".format(req.status_code), response=req)
|
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
|
@staticmethod
|
||||||
def pretty_xml(xmlstr):
|
def pretty_xml(xmlstr):
|
||||||
|
Loading…
Reference in New Issue
Block a user