Completed
Push — master ( 50882e...e9b093 )
by John
02:40
created

tclscan_main()   C

Complexity

Conditions 7

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 2
Metric Value
cc 7
c 3
b 0
f 2
dl 0
loc 27
rs 5.5
1
#!/usr/bin/env python3
2
"""Check latest update for TCL API devices."""
3
4
import sys  # load arguments
5
import requests  # session
6
from bbarchivist import decorators  # enter to exit
7
from bbarchivist import jsonutils  # json
8
from bbarchivist import networkutils  # lookup
9
from bbarchivist import scriptutils  # default parser
10
from bbarchivist import utilities  # bool
11
12
__author__ = "Thurask"
13
__license__ = "WTFPL v2"
14
__copyright__ = "2017 Thurask"
15
16
17
def grab_args():
18
    """
19
    Parse arguments from argparse/questionnaire.
20
21
    Invoke a function with those arguments.
22
    """
23
    if getattr(sys, "frozen", False) and len(sys.argv) == 1:
24
        questionnaire()
25
    else:
26
        parser = scriptutils.default_parser("bb-tclscan", "Check for updates for TCL devices")
27
        parser.add_argument("prd", help="Only scan one PRD", default=None, nargs="?")
28
        parser.add_argument(
29
            "-l",
30
            "--list",
31
            dest="printlist",
32
            help="List PRDs in database",
33
            action="store_true",
34
            default=False)
35
        parser.add_argument(
36
            "-d",
37
            "--download",
38
            dest="download",
39
            help="Download update, assumes single PRD",
40
            action="store_true",
41
            default=False)
42
        parser.add_argument(
43
            "-o",
44
            "--ota-version",
45
            dest="otaver",
46
            help="Query OTA updates from a given version instead of full OS",
47
            default=None)
48
        parser.add_argument(
49
            "-t",
50
            "--device-type",
51
            dest="device",
52
            help="Scan only one device",
53
            default=None,
54
            type=utilities.droidlookup_devicetype)
55
        args = parser.parse_args(sys.argv[1:])
56
        parser.set_defaults()
57
        execute_args(args)
58
59
60
def execute_args(args):
61
    """
62
    Get args and decide what to do with them.
63
64
    :param args: Arguments.
65
    :type args: argparse.Namespace
66
    """
67
    if args.printlist:
68
        prddict = jsonutils.load_json("prds")
69
        jsonutils.list_prds(prddict)
70
    elif args.prd is not None:
71
        tclscan_single(args.prd, args.download, args.otaver)
72
    else:
73
        tclscan_main(args.otaver, args.device)
74
75
76
def questionnaire_ota():
77
    """
78
    Ask about OTA versus full scanning.
79
    """
80
    otabool = utilities.i2b("CHECK OTA VERSION (Y/N)?: ")
81
    if otabool:
82
        otaver = input("ENTER OTA VERSION (ex. AAO472): ")
83
    else:
84
        otaver = None
85
    return otaver
86
87
88
def questionnaire_single():
89
    """
90
    Ask about single versus full scanning.
91
    """
92
    singlebool = utilities.i2b("SCAN SINGLE OS (Y/N)?: ")
93
    if singlebool:
94
        singleprd = input("ENTER PRD TO SCAN (ex. PRD-63116-001): ")
95
    else:
96
        singleprd = None
97
    return singleprd
98
99
100
def questionnaire():
101
    """
102
    Questions to ask if no arguments given.
103
    """
104
    singleprd = questionnaire_single()
105
    otaver = questionnaire_ota()
106
    if singleprd is not None:
107
        tclscan_single(singleprd, ota=otaver)
108
    else:
109
        tclscan_main(otaver)
110
    decorators.enter_to_exit(True)
111
112
113
def tclscan_single(curef, download=False, ota=None):
114
    """
115
    Scan one PRD and produce download URL and filename.
116
117
    :param curef: PRD of the phone variant to check.
118
    :type curef: str
119
120
    :param download: If we'll download the file that this returns. Default is False.
121
    :type download: bool
122
123
    :param ota: The starting version if we're checking OTA updates, None if we're not. Default is None.
124
    :type ota: str
125
    """
126
    mode, fvver = scriptutils.tcl_prep_otaver(ota)
127
    scriptutils.tcl_prd_scan(curef, False, mode=mode, fvver=fvver)
128
    if download:
129
        print("LARGE DOWNLOAD DOESN'T WORK YET")
130
131
132
def tclscan_main(ota=None, device=None):
133
    """
134
    Scan every PRD and produce latest versions.
135
136
    :param ota: The starting version if we're checking OTA updates, None if we're not. Default is None.
137
    :type ota: str
138
139
    :param device: The device to check if we're not checking all of them, None if we are. Default is None.
140
    :type device: str
141
    """
142
    mode, fvver = scriptutils.tcl_prep_otaver(ota)
143
    scriptutils.tcl_mainscan_preamble(ota)
144
    prddict = jsonutils.load_json("prds")
145
    if device is not None:
146
        prddict2 = {dev: prddict[dev] for dev in prddict.keys() if dev == device}
147
        prddict = prddict2
148
    sess = requests.Session()
149
    for device in prddict.keys():
150
        print("~{0}~".format(device))
151
        for curef in prddict[device]:
152
            checktext = networkutils.tcl_check(curef, sess, mode=mode, fvver=fvver)
153
            if checktext is None:
154
                continue
155
            else:
156
                tvver, firmwareid, filename, fsize, fhash = networkutils.parse_tcl_check(checktext)
157
                del firmwareid, filename, fsize, fhash
158
                scriptutils.tcl_mainscan_printer(curef, tvver, ota)
159
160
161
if __name__ == "__main__":
162
    grab_args()
163