From 8b7de97f848f460f88d2997042b1bc6b68cce1d2 Mon Sep 17 00:00:00 2001 From: Markus Birth Date: Thu, 5 Oct 2017 23:51:07 +0200 Subject: [PATCH] Move retry code for checks into tcllib. --- tclcheck_allfull.py | 46 +++++++++++++---------------------- tclcheck_allota.py | 49 ++++++++++++++------------------------ tcllib.py | 58 ++++++++++++++++++++++++++------------------- 3 files changed, 68 insertions(+), 85 deletions(-) diff --git a/tclcheck_allfull.py b/tclcheck_allfull.py index 48d81e1..22deae4 100755 --- a/tclcheck_allfull.py +++ b/tclcheck_allfull.py @@ -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,35 +16,23 @@ 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 - check_xml = fc.do_check() - curef, fv, tv, fw_id, fileid, fn, fsize, fhash = fc.parse_check(check_xml) - txt_tv = tv - 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="") - continue + try: + fc.reset_session() + fc.curef = prd + check_xml = fc.do_check() + curef, fv, tv, fw_id, fileid, fn, fsize, fhash = fc.parse_check(check_xml) + txt_tv = tv + if tv != lastver: + txt_tv = tcllib.ANSI_CYAN + txt_tv + tcllib.ANSI_RESET + print("{}: {} {} ({})".format(prd, txt_tv, fhash, model)) + except RequestException as e: + print("{}: {}".format(prd, str(e))) + continue diff --git a/tclcheck_allota.py b/tclcheck_allota.py index dac3ddb..55c3354 100755 --- a/tclcheck_allota.py +++ b/tclcheck_allota.py @@ -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,31 +23,19 @@ 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) - if force_ver != False: - lastver = force_ver - try: - fc.reset_session() - fc.curef = prd - fc.fv = lastver - 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="") - continue +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: + fc.reset_session() + fc.curef = prd + fc.fv = lastver + 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)) + except RequestException as e: + print("{} ({}): {}".format(prd, lastver, str(e))) + continue diff --git a/tcllib.py b/tcllib.py index 679c267..fa19fee 100644 --- a/tcllib.py +++ b/tcllib.py @@ -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) - 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: - req.raise_for_status() - raise requests.exceptions.HTTPError("HTTP {}.".format(req.status_code), response=req) + 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 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):