tclfindprd   A
last analyzed

Complexity

Total Complexity 0

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 0
eloc 71
dl 0
loc 93
rs 10
c 0
b 0
f 0
1
#!/usr/bin/env python3
2
# -*- coding: utf-8 -*-
3
4
# pylint: disable=C0111,C0326,C0103
5
6
"""Find new PRDs for a given variant(s)."""
7
8
import collections
9
import sys
10
11
from tcllib import ansi, argparser, devlist
12
from tcllib.devices import DesktopDevice
13
from tcllib.dumpmgr import write_info_if_dumps_found
14
from tcllib.requests import CheckRequest, RequestRunner, ServerVoteSelector
15
16
dpdesc = """
17
    Finds new PRD numbers for all known variants, or specified variants with tocheck. Scan range
18
    can be set by floor and ceiling switches.
19
    """
20
dp = argparser.DefaultParser(__file__, dpdesc)
21
dp.add_argument("tocheck", help="CU Reference #(s) to filter scan results", nargs="*", default=None)
22
dp.add_argument("-f", "--floor", help="Beginning of scan range", dest="floor", nargs="?", type=int, default=0)
23
dp.add_argument("-c", "--ceiling", help="End of scan range", dest="ceiling", nargs="?", type=int, default=999)
24
dp.add_argument("-l", "--local", help="Force using local database", dest="local", action="store_true", default=False)
25
dp.add_argument("-np", "--no-prefix", help="Skip 'PRD-' prefix", dest="noprefix", action="store_true", default=False)
26
dp.add_argument("-k2", "--key2", help="V2 syntax", dest="key2mode", action="store_true", default=False)
27
args = dp.parse_args(sys.argv[1:])
28
29
floor = args.floor
30
ceiling = args.ceiling + 1
31
if ceiling < floor:
32
    print("Invalid range!")
33
    raise SystemExit
34
35
print("Loading list of devices...", end="", flush=True)
36
prd_db = devlist.get_devicelist(local=args.local)
37
print(" OK")
38
39
print("Valid PRDs not already in database:")
40
41
prds = [x.replace("APBI-PRD", "").replace("PRD-", "").replace("-", "") for x in prd_db]
42
prdx = list({x[0:5]: x[5:]} for x in prds)
43
prddict = collections.defaultdict(list)
44
for prdc in prdx:
45
    for key, value in prdc.items():
46
        prddict[key].append(value)
47
48
if args.tocheck is not None:
49
    if not isinstance(args.tocheck, list):
50
        args.tocheck = [args.tocheck]
51
    args.tocheck = [toch.replace("APBI-PRD", "").replace("PRD-", "") for toch in args.tocheck]
52
    prdkeys = list(prddict.keys())
53
    for k in prdkeys:
54
        if k not in args.tocheck:
55
            del prddict[k]
56
    for toch in args.tocheck:
57
        if toch not in prddict.keys():
58
            prddict[toch] = []
59
60
dev = DesktopDevice()
61
62
runner = RequestRunner(ServerVoteSelector(), https=False)
63
runner.max_tries = 20
64
65
if args.key2mode:
66
    prefix = "APBI-PRD"
67
    suffix = ""
68
else:
69
    prefix = "" if args.noprefix else "PRD-"
70
    suffix = "-"
71
72
for center in sorted(prddict.keys()):
73
    tails = [int(i) for i in prddict[center]]
74
    safes = [g for g in range(floor, ceiling) if g not in tails]
75
    total_count = len(safes)
76
    done_count = 0
77
    print("Checking {} variant codes for model {}.".format(total_count, center))
78
    for j in safes:
79
        curef = "{}{}{}{:03}".format(prefix, center, suffix, j)
80
        done_count += 1
81
        print("Checking {} ({}/{})".format(curef, done_count, total_count))
82
        print(ansi.UP_DEL, end="")
83
        dev.curef = curef
84
        chk = CheckRequest(dev)
85
        runner.run(chk)
86
        if chk.success:
87
            chkres = chk.get_result()
88
            txt_tv = chkres.tvver
89
            print("{}: {} {}".format(curef, txt_tv, chkres.filehash))
90
91
print("Scan complete.")
92
write_info_if_dumps_found()
93