From 42b499290c6e5fd3363013065dce0cc7b999b26b Mon Sep 17 00:00:00 2001 From: Markus Birth Date: Wed, 20 Dec 2017 00:01:55 +0100 Subject: [PATCH] When downloading new prds.json, display list of changes - if any. --- tclcheck_allfull.py | 3 +-- tclcheck_allota.py | 3 +-- tcllib.py | 37 +++++++++++++++++++++++++++++++++++-- 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/tclcheck_allfull.py b/tclcheck_allfull.py index b1939d8..d51898f 100644 --- a/tclcheck_allfull.py +++ b/tclcheck_allfull.py @@ -24,9 +24,8 @@ fc.cltp = fc.CLTP.DESKTOP prdcheck = "" if args.tocheck is None else args.tocheck -print("Loading list of devices...", end="", flush=True) +print("Loading list of devices.") prds = tcllib.FotaCheck.get_devicelist() -print(" OK") print("List of latest FULL firmware by PRD:") diff --git a/tclcheck_allota.py b/tclcheck_allota.py index 67c6158..06a3cec 100644 --- a/tclcheck_allota.py +++ b/tclcheck_allota.py @@ -29,9 +29,8 @@ else: prdcheck = "" if args.tocheck is None else args.tocheck -print("Loading list of devices...", end="", flush=True) +print("Loading list of devices.") prds = tcllib.FotaCheck.get_devicelist() -print(" OK") print("List of latest OTA firmware{} by PRD:".format(force_ver_text)) diff --git a/tcllib.py b/tcllib.py index 97db710..705e613 100644 --- a/tcllib.py +++ b/tcllib.py @@ -247,25 +247,58 @@ class FotaCheck: return hexhash @staticmethod - def get_devicelist(): + def get_devicelist(force=False, output_diff=True): need_download = True + + old_prds = None try: filestat = os.stat(DEVICELIST_FILE) filemtime = filestat.st_mtime if filemtime > time.time() - DEVICELIST_CACHE_SECONDS: need_download = False + with open(DEVICELIST_FILE, "rt") as df: + old_prds = json.load(df) except FileNotFoundError: pass - if need_download: + if need_download or force: prds_json = requests.get(DEVICELIST_URL).text with open(DEVICELIST_FILE, "wt") as df: df.write(prds_json) with open(DEVICELIST_FILE, "rt") as df: prds = json.load(df) + + if old_prds and output_diff: + FotaCheck.print_prd_diff(old_prds, prds) + return prds + @staticmethod + def print_prd_diff(old_prds, new_prds): + added_prds = [prd for prd in new_prds if prd not in old_prds] + removed_prds = [prd for prd in old_prds if prd not in new_prds] + for prd in removed_prds: + print("> Removed device {} (was at {} / OTA: {}).".format(ANSI_RED + prd + ANSI_RESET, old_prds[prd]["last_full"], old_prds[prd]["last_ota"])) + for prd in added_prds: + print("> New device {} ({} / OTA: {}).".format(ANSI_GREEN + prd + ANSI_RESET, new_prds[prd]["last_full"], new_prds[prd]["last_ota"])) + for prd, pdata in new_prds.items(): + if prd in added_prds: + continue + odata = old_prds[prd] + if pdata["last_full"] != odata["last_full"] and pdata["last_ota"] != odata["last_ota"]: + print("> {}: {} ⇨ {} (OTA: {} ⇨ {})".format( + prd, + ANSI_CYAN_DARK + odata["last_full"] + ANSI_RESET, + ANSI_CYAN + pdata["last_full"] + ANSI_RESET, + ANSI_YELLOW_DARK + odata["last_ota"] + ANSI_RESET, + ANSI_YELLOW + pdata["last_ota"] + ANSI_RESET + )) + elif pdata["last_full"] != odata["last_full"]: + print("> {}: {} ⇨ {} (FULL)".format(prd, ANSI_CYAN_DARK + odata["last_full"] + ANSI_RESET, ANSI_CYAN + pdata["last_full"] + ANSI_RESET)) + elif pdata["last_ota"] != odata["last_ota"]: + print("> {}: {} ⇨ {} (OTA)".format(prd, ANSI_YELLOW_DARK + odata["last_ota"] + ANSI_RESET, ANSI_YELLOW + pdata["last_ota"] + ANSI_RESET)) + @staticmethod def get_creds(): creds = {