Test Failed
Push — master ( 33284b...c56792 )
by John
02:53
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 OTA, None if 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, download, mode=mode, fvver=fvver, export=export, verify=False)
152
153
154
def tclscan_main(ota=None, device=None, export=False, remote=False):
1 ignored issue
show
Comprehensibility introduced by
This function exceeds the maximum number of variables (17/15).
Loading history...
155
    """
156
    Scan every PRD and produce latest versions.
157
158
    :param ota: The starting version if OTA, None if not. Default is None.
159
    :type ota: str
160
161
    :param device: Specific PRD to check, None if all will be checked. Default is None.
162
    :type device: str
163
164
    :param export: Whether to export XML response to file. Default is False.
165
    :type export: bool
166
167
    :param remote: Whether to get OTA version from remote server. Default is False.
168
    :type remote: bool
169
    """
170
    if remote:
171
        remotedict = networkutils.remote_prd_info()
172
        ota = "LATEST"
173
    mode, fvver = scriptutils.tcl_prep_otaver(ota)
174
    scriptutils.tcl_mainscan_preamble(ota)
175
    prddict = jsonutils.load_json("prds")
176
    if device is not None:
177
        prddict = {device: prddict[device]}
178
    sess = requests.Session()
179
    for devx in prddict.keys():
180
        print("~{0}~".format(devx))
181
        for curef in prddict[devx]:
182
            if remote:
183
                fvver = remotedict.get(curef, "AAA000")
1 ignored issue
show
introduced by
The variable remotedict does not seem to be defined in case remote on line 170 is False. Are you sure this can never be the case?
Loading history...
184
            checktext = networkutils.tcl_check(curef, sess, mode=mode, fvver=fvver, export=export)
185
            if checktext is None:
186
                continue
187
            else:
188
                tvver, firmwareid, filename, fsize, fhash = networkutils.parse_tcl_check(checktext)
189
                del firmwareid, filename, fsize, fhash
190
                scriptutils.tcl_mainscan_printer(curef, tvver, ota)
191
192
193
if __name__ == "__main__":
194
    grab_args()
195