2018-02-03 20:40:17 +00:00
|
|
|
#!/usr/bin/env python3
|
2018-02-03 00:36:08 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2018-02-03 20:40:17 +00:00
|
|
|
# pylint: disable=C0111,C0326,C0103
|
|
|
|
|
2018-02-03 00:36:08 +00:00
|
|
|
import time
|
|
|
|
from collections import OrderedDict
|
2018-02-03 20:24:36 +00:00
|
|
|
|
2018-02-03 00:36:08 +00:00
|
|
|
import requests
|
|
|
|
from defusedxml import ElementTree
|
|
|
|
|
2018-02-03 20:24:36 +00:00
|
|
|
|
2018-02-03 12:46:17 +00:00
|
|
|
class TclCheckMixin:
|
2018-02-03 00:36:08 +00:00
|
|
|
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()
|
2018-02-03 20:24:36 +00:00
|
|
|
params["id"] = self.serid
|
2018-02-03 00:36:08 +00:00
|
|
|
params["curef"] = self.curef
|
2018-02-03 20:24:36 +00:00
|
|
|
params["fv"] = self.fvver
|
|
|
|
params["mode"] = self.mode.value
|
|
|
|
params["type"] = self.ftype
|
|
|
|
params["cltp"] = self.cltp.value
|
|
|
|
params["cktp"] = self.cktp.value
|
|
|
|
params["rtd"] = self.rtd.value
|
|
|
|
params["chnl"] = self.chnl.value
|
|
|
|
#params["osvs"] = self.osvs
|
|
|
|
#params["ckot"] = self.ckot.value
|
2018-02-03 00:36:08 +00:00
|
|
|
|
|
|
|
last_response = None
|
2018-02-03 20:24:36 +00:00
|
|
|
for _ in range(0, max_tries):
|
2018-02-03 00:36:08 +00:00
|
|
|
try:
|
|
|
|
reqtime_start = time.perf_counter()
|
|
|
|
req = self.sess.get(url, params=params, timeout=timeout)
|
|
|
|
reqtime = time.perf_counter() - reqtime_start
|
|
|
|
reqtime_avg = self.check_time_avg()
|
|
|
|
self.check_time_add(reqtime)
|
|
|
|
last_response = req
|
|
|
|
if req.status_code == 200:
|
|
|
|
self.master_server_vote_on_time(reqtime, reqtime_avg)
|
|
|
|
req.encoding = "utf-8" # Force encoding as server doesn't give one
|
|
|
|
self.write_dump(req.text)
|
|
|
|
return req.text
|
|
|
|
elif req.status_code == 204:
|
|
|
|
self.master_server_vote_on_time(reqtime, reqtime_avg)
|
|
|
|
raise requests.exceptions.HTTPError("No update available.", response=req)
|
|
|
|
elif req.status_code == 404:
|
|
|
|
self.master_server_vote_on_time(reqtime, reqtime_avg)
|
|
|
|
raise requests.exceptions.HTTPError("No data for requested CUREF/FV combination.", response=req)
|
|
|
|
elif req.status_code not in [500, 502, 503]:
|
|
|
|
self.master_server_downvote()
|
|
|
|
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.master_server_downvote()
|
|
|
|
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 parse_check(xmlstr):
|
|
|
|
root = ElementTree.fromstring(xmlstr)
|
|
|
|
curef = root.find("CUREF").text
|
2018-02-03 20:24:36 +00:00
|
|
|
fvver = root.find("VERSION").find("FV").text
|
|
|
|
tvver = root.find("VERSION").find("TV").text
|
2018-02-03 00:36:08 +00:00
|
|
|
fw_id = root.find("FIRMWARE").find("FW_ID").text
|
|
|
|
fileinfo = root.find("FIRMWARE").find("FILESET").find("FILE")
|
2018-02-03 20:24:36 +00:00
|
|
|
fileid = fileinfo.find("FILE_ID").text
|
2018-02-03 00:36:08 +00:00
|
|
|
filename = fileinfo.find("FILENAME").text
|
|
|
|
filesize = fileinfo.find("SIZE").text
|
|
|
|
filehash = fileinfo.find("CHECKSUM").text
|
2018-02-03 20:24:36 +00:00
|
|
|
return curef, fvver, tvver, fw_id, fileid, filename, filesize, filehash
|