From 4456f76032186c8ad1ebe9dc7a5320a717e31ce9 Mon Sep 17 00:00:00 2001 From: Markus Birth Date: Fri, 2 Feb 2018 03:33:53 +0100 Subject: [PATCH] Start modularising tcllib. --- .gitignore | 2 +- tclcheck.py | 3 ++- tclcheck_allfull.py | 3 ++- tclcheck_allota.py | 3 ++- tclcheck_findprd.py | 3 ++- tclcheck_findprd2.py | 3 ++- tclcheck_findver.py | 3 ++- tclchksum.py | 3 ++- tcldown.py | 3 ++- tcllib.py => tcllib/__init__.py | 20 +++----------------- tcllib/argparser.py | 19 +++++++++++++++++++ 11 files changed, 39 insertions(+), 26 deletions(-) rename tcllib.py => tcllib/__init__.py (96%) create mode 100644 tcllib/argparser.py diff --git a/.gitignore b/.gitignore index 6fc9c26..6c7e31e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ -/__pycache__/ +__pycache__/ /logs/ /headers/ /.nvimlog diff --git a/tclcheck.py b/tclcheck.py index ab12159..c3a6cd2 100755 --- a/tclcheck.py +++ b/tclcheck.py @@ -7,6 +7,7 @@ import os import random import sys import tcllib +import tcllib.argparser fc = tcllib.FotaCheck() fc.serid = "3531510" @@ -16,7 +17,7 @@ dpdesc = """ Checks for the latest FULL updates for the specified PRD number or for an OTA from the version specified as fvver. """ -dp = tcllib.DefaultParser(__file__, dpdesc) +dp = tcllib.argparser.DefaultParser(__file__, dpdesc) dp.add_argument("prd", nargs=1, help="CU Reference #, e.g. PRD-63117-011") dp.add_argument("fvver", nargs="?", help="Firmware version to check for OTA updates, e.g. AAM481 (omit to run FULL check)", default="AAA000") dp.add_argument("-i", "--imei", help="use specified IMEI instead of default", type=str) diff --git a/tclcheck_allfull.py b/tclcheck_allfull.py index 7cb8d11..6922991 100644 --- a/tclcheck_allfull.py +++ b/tclcheck_allfull.py @@ -5,6 +5,7 @@ import sys import tcllib +import tcllib.argparser from requests.exceptions import RequestException, Timeout tcllib.make_escapes_work() @@ -18,7 +19,7 @@ dpdesc = """ Checks for the latest FULL updates for all PRD numbers or only for the PRD specified as prd. """ -dp = tcllib.DefaultParser(__file__, dpdesc) +dp = tcllib.argparser.DefaultParser(__file__, dpdesc) dp.add_argument("-p", "--prd", help="CU Reference # to filter scan results", dest="tocheck", nargs="?", default=None, metavar="PRD") args = dp.parse_args(sys.argv[1:]) diff --git a/tclcheck_allota.py b/tclcheck_allota.py index fa2713b..598155f 100644 --- a/tclcheck_allota.py +++ b/tclcheck_allota.py @@ -7,6 +7,7 @@ import json import requests import sys import tcllib +import tcllib.argparser from requests.exceptions import RequestException tcllib.make_escapes_work() @@ -21,7 +22,7 @@ dpdesc = """ Checks for the latest OTA updates for all PRD numbers or only for the PRD specified as prd. Initial software version can be specified with forcever. """ -dp = tcllib.DefaultParser(__file__, dpdesc) +dp = tcllib.argparser.DefaultParser(__file__, dpdesc) dp.add_argument("forcever", help="Initial software version to check for OTA updates, e.g. AAM481", nargs="?", default=None) dp.add_argument("-p", "--prd", help="CU Reference # to filter scan results", dest="tocheck", nargs="?", default=None, metavar="PRD") args = dp.parse_args(sys.argv[1:]) diff --git a/tclcheck_findprd.py b/tclcheck_findprd.py index 90cccc6..490207e 100755 --- a/tclcheck_findprd.py +++ b/tclcheck_findprd.py @@ -6,6 +6,7 @@ import collections import sys import tcllib +import tcllib.argparser from requests.exceptions import RequestException, Timeout tcllib.make_escapes_work() @@ -23,7 +24,7 @@ dpdesc = """ Finds new PRD numbers for all known variants, or specified variants with tocheck. Scan range can be set by floor and ceiling switches. """ -dp = tcllib.DefaultParser(__file__, dpdesc) +dp = tcllib.argparser.DefaultParser(__file__, dpdesc) dp.add_argument("tocheck", help="CU Reference # to filter scan results", nargs="?", default=None) dp.add_argument("-f", "--floor", help="Beginning of scan range", dest="floor", nargs="?", type=int, default=0) dp.add_argument("-c", "--ceiling", help="End of scan range", dest="ceiling", nargs="?", type=int, default=999) diff --git a/tclcheck_findprd2.py b/tclcheck_findprd2.py index 99f93b4..7b709df 100644 --- a/tclcheck_findprd2.py +++ b/tclcheck_findprd2.py @@ -6,6 +6,7 @@ import collections import sys import tcllib +import tcllib.argparser from requests.exceptions import RequestException, Timeout # Variants to scan for @@ -26,7 +27,7 @@ dpdesc = """ Finds new PRD numbers for a range of variants. Scan range can be set by floor and ceiling switches. """ -dp = tcllib.DefaultParser(__file__, dpdesc) +dp = tcllib.argparser.DefaultParser(__file__, dpdesc) dp.add_argument("floor", nargs="?", help="Model number to start with", type=int, default=63116) dp.add_argument("ceiling", nargs="?", help="Model number to end with", type=int, default=99999) args = dp.parse_args(sys.argv[1:]) diff --git a/tclcheck_findver.py b/tclcheck_findver.py index e499d17..fcbdc4e 100755 --- a/tclcheck_findver.py +++ b/tclcheck_findver.py @@ -5,6 +5,7 @@ import sys import tcllib +import tcllib.argparser from requests.exceptions import RequestException, Timeout tcllib.make_escapes_work() @@ -17,7 +18,7 @@ dpdesc = """ Finds all valid OTA updates for a given PRD. Scan range can be set by startver and endver switches. """ -dp = tcllib.DefaultParser(__file__, dpdesc) +dp = tcllib.argparser.DefaultParser(__file__, dpdesc) dp.add_argument("prd", help="CU Reference #, e.g. PRD-63117-011") dp.add_argument("startver", help="Beginning of scan range", nargs="?", default="AAA000") dp.add_argument("endver", help="End of scan range", nargs="?", default="AAZ999") diff --git a/tclchksum.py b/tclchksum.py index c392a57..c022717 100644 --- a/tclchksum.py +++ b/tclchksum.py @@ -7,6 +7,7 @@ import os import random import sys import tcllib +import tcllib.argparser fc = tcllib.FotaCheck() @@ -25,7 +26,7 @@ encslaves = [ dpdesc = """ Returns the checksum for a given firmware URI. """ -dp = tcllib.DefaultParser(__file__, dpdesc) +dp = tcllib.argparser.DefaultParser(__file__, dpdesc) dp.add_argument("uri", help="URI to firmware, starts with '/body/...'") args = dp.parse_args(sys.argv[1:]) diff --git a/tcldown.py b/tcldown.py index 64f8a8a..a9b566d 100644 --- a/tcldown.py +++ b/tcldown.py @@ -7,6 +7,7 @@ import os import random import sys import tcllib +import tcllib.argparser fc = tcllib.FotaCheck() fc.serid = "3531510" @@ -15,7 +16,7 @@ fc.serid = "3531510" dpdesc = """ Downloads the given firmware file. """ -dp = tcllib.DefaultParser(__file__, dpdesc) +dp = tcllib.argparser.DefaultParser(__file__, dpdesc) dp.add_argument("prd", nargs=1, help="CU Reference #, e.g. PRD-63117-011") dp.add_argument("targetversion", nargs=1, help="Firmware version to download, e.g. AAN990") dp.add_argument("fwid", nargs=1, help="Numeric firmware file id, e.g. 268932") diff --git a/tcllib.py b/tcllib/__init__.py similarity index 96% rename from tcllib.py rename to tcllib/__init__.py index 4fa4c54..93ad56d 100644 --- a/tcllib.py +++ b/tcllib/__init__.py @@ -2,7 +2,6 @@ # pylint: disable=C0111,C0326 -import argparse import base64 import binascii import enum @@ -14,7 +13,6 @@ import os import platform import random import time -import webbrowser import xml.dom.minidom import zlib from collections import OrderedDict @@ -56,21 +54,6 @@ def make_escapes_work(): def default_enum(enumname, vardict): return enum.IntEnum(enumname, vardict, module=__name__, qualname="tcllib.FotaCheck.{}".format(enumname)) - -class DefaultParser(argparse.ArgumentParser): - def __init__(self, appname, desc=None): - super().__init__(prog=appname, description=desc, epilog="https://github.com/mbirth/tcl_ota_check") - self.add_argument("--webdb", help="open web database in browser and exit", action="store_true") - - def parse_args(self, args=None, namespace=None): - if set(args) & {"--webdb"}: # if they intersect - webbrowser.open("https://tclota.birth-online.de/", new=2) - raise SystemExit - else: - argx = super().parse_args(args, namespace) - return argx - - class FotaCheck: VDKEY = b"eJwdjwEOwDAIAr8kKFr//7HhmqXp8AIIDrYAgg8byiUXrwRJRXja+d6iNxu0AhUooDCN9rd6rDLxmGIakUVWo3IGCTRWqCAt6X4jGEIUAxgN0eYWnp+LkpHQAg/PsO90ELsy0Npm/n2HbtPndFgGEV31R9OmT4O4nrddjc3Qt6nWscx7e+WRHq5UnOudtjw5skuV09pFhvmqnOEIs4ljPeel1wfLYUF4\n" CKTP = default_enum("CKTP", ["AUTO", "MANUAL"]) @@ -433,6 +416,9 @@ class FotaCheck: req.encoding = "utf-8" # Force encoding as server doesn't give one self.write_dump(req.text) # 2abfa6f6507044fec995efede5d818e62a0b19b5 means ERROR (invalid ADDRESS!) + if "2abfa6f6507044fec995efede5d818e62a0b19b5" in req.text: + print("INVALID URI: {}".format(uri)) + raise SystemExit return req.text else: print("CHECKSUM: " + repr(req)) diff --git a/tcllib/argparser.py b/tcllib/argparser.py new file mode 100644 index 0000000..bfe63d3 --- /dev/null +++ b/tcllib/argparser.py @@ -0,0 +1,19 @@ +# -*- coding: utf-8 -*- + +# pylint: disable=C0111,C0326 + +import argparse +import webbrowser + +class DefaultParser(argparse.ArgumentParser): + def __init__(self, appname, desc=None): + super().__init__(prog=appname, description=desc, epilog="https://github.com/mbirth/tcl_ota_check") + self.add_argument("--webdb", help="open web database in browser and exit", action="store_true") + + def parse_args(self, args=None, namespace=None): + if set(args) & {"--webdb"}: # if they intersect + webbrowser.open("https://tclota.birth-online.de/", new=2) + raise SystemExit + else: + argx = super().parse_args(args, namespace) + return argx