Completed
Push — master ( a42aad...9def7b )
by John
03:23
created

tclscan_main()   C

Complexity

Conditions 7

Size

Total Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

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