Completed
Push — master ( c0a25d...3bf3d6 )
by John
03:47 queued 38s
created

tclscan_single()   A

Complexity

Conditions 2

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 2
Metric Value
cc 2
c 3
b 0
f 2
dl 0
loc 20
rs 9.4285
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-2018 Thurask"
15
16
17 View Code Duplication
def grab_args():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
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
        parser.add_argument(
56
            "-x",
57
            "--export",
58
            dest="export",
59
            help="Write XML to logs folder",
60
            action="store_true",
61
            default=False)
62
        args = parser.parse_args(sys.argv[1:])
63
        parser.set_defaults()
64
        execute_args(args)
65
66
67
def execute_args(args):
68
    """
69
    Get args and decide what to do with them.
70
71
    :param args: Arguments.
72
    :type args: argparse.Namespace
73
    """
74
    if args.printlist:
75
        prddict = jsonutils.load_json("prds")
76
        jsonutils.list_prds(prddict)
77
    elif args.prd is not None:
78
        tclscan_single(args.prd, args.download, args.otaver, args.export)
79
    else:
80
        tclscan_main(args.otaver, args.device, args.export)
81
82
83
def questionnaire_ota():
84
    """
85
    Ask about OTA versus full scanning.
86
    """
87
    otabool = utilities.i2b("CHECK OTA VERSION (Y/N)?: ")
88
    if otabool:
89
        otaver = input("ENTER OTA VERSION (ex. AAO472): ")
90
    else:
91
        otaver = None
92
    return otaver
93
94
95
def questionnaire_single():
96
    """
97
    Ask about single versus full scanning.
98
    """
99
    singlebool = utilities.i2b("SCAN SINGLE DEVICE (Y/N)?: ")
100
    if singlebool:
101
        singleprd = input("ENTER PRD TO SCAN (ex. PRD-63116-001): ")
102
    else:
103
        singleprd = None
104
    return singleprd
105
106
107
def questionnaire():
108
    """
109
    Questions to ask if no arguments given.
110
    """
111
    singleprd = questionnaire_single()
112
    otaver = questionnaire_ota()
113
    if singleprd is not None:
114
        tclscan_single(singleprd, ota=otaver)
115
    else:
116
        tclscan_main(otaver)
117
    decorators.enter_to_exit(True)
118
119
120
def tclscan_single(curef, download=False, ota=None, export=False):
121
    """
122
    Scan one PRD and produce download URL and filename.
123
124
    :param curef: PRD of the phone variant to check.
125
    :type curef: str
126
127
    :param download: If we'll download the file that this returns. Default is False.
128
    :type download: bool
129
130
    :param ota: The starting version if we're checking OTA updates, None if we're not. Default is None.
131
    :type ota: str
132
133
    :param export: Whether to export XML response to file. Default is False.
134
    :type export: bool
135
    """
136
    mode, fvver = scriptutils.tcl_prep_otaver(ota)
137
    scriptutils.tcl_prd_scan(curef, False, mode=mode, fvver=fvver, export=export)
138
    if download:
139
        print("LARGE DOWNLOAD DOESN'T WORK YET")
140
141
142
def tclscan_main(ota=None, device=None, export=False):
143
    """
144
    Scan every PRD and produce latest versions.
145
146
    :param ota: The starting version if we're checking OTA updates, None if we're not. Default is None.
147
    :type ota: str
148
149
    :param device: The device to check if we're not checking all of them, None if we are. Default is None.
150
    :type device: str
151
152
    :param export: Whether to export XML response to file. Default is False.
153
    :type export: bool
154
    """
155
    mode, fvver = scriptutils.tcl_prep_otaver(ota)
156
    scriptutils.tcl_mainscan_preamble(ota)
157
    prddict = jsonutils.load_json("prds")
158
    if device is not None:
159
        prddict = {device: prddict[device]}
160
    sess = requests.Session()
161
    for device in prddict.keys():
162
        print("~{0}~".format(device))
163
        for curef in prddict[device]:
164
            checktext = networkutils.tcl_check(curef, sess, mode=mode, fvver=fvver, export=export)
165
            if checktext is None:
166
                continue
167
            else:
168
                tvver, firmwareid, filename, fsize, fhash = networkutils.parse_tcl_check(checktext)
169
                del firmwareid, filename, fsize, fhash
170
                scriptutils.tcl_mainscan_printer(curef, tvver, ota)
171
172
173
if __name__ == "__main__":
174
    grab_args()
175