Completed
Push — master ( 46bd64...8f2ad6 )
by John
05:43
created

questionnaire_ota()   A

Complexity

Conditions 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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