bbarchivist.scripts.tclscan.grab_args()   B
last analyzed

Complexity

Conditions 3

Size

Total Lines 55
Code Lines 49

Duplication

Lines 55
Ratio 100 %

Importance

Changes 0
Metric Value
cc 3
eloc 49
nop 0
dl 55
loc 55
rs 8.669
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 argutils  # arguments
8
from bbarchivist import decorators  # enter to exit
9
from bbarchivist import jsonutils  # json
10
from bbarchivist import networkutilstcl  # lookup
11
from bbarchivist import scriptutilstcl  # script frontends
12
from bbarchivist import utilities  # bool
13
from bbarchivist import xmlutilstcl  # xml handling
14
15
__author__ = "Thurask"
16
__license__ = "WTFPL v2"
17
__copyright__ = "2017-2019 Thurask"
18
19
20 View Code Duplication
def grab_args():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
21
    """
22
    Parse arguments from argparse/questionnaire.
23
24
    Invoke a function with those arguments.
25
    """
26
    if getattr(sys, "frozen", False) and len(sys.argv) == 1:
27
        questionnaire()
28
    else:
29
        parser = argutils.default_parser("bb-tclscan", "Check for updates for TCL devices")
30
        parser.add_argument("prd", help="Only scan one PRD", default=None, nargs="?")
31
        parser.add_argument(
32
            "-l",
33
            "--list",
34
            dest="printlist",
35
            help="List PRDs in database",
36
            action="store_true",
37
            default=False)
38
        parser.add_argument(
39
            "-d",
40
            "--download",
41
            dest="download",
42
            help="Download update, assumes single PRD",
43
            action="store_true",
44
            default=False)
45
        parser.add_argument(
46
            "-o",
47
            "--ota-version",
48
            dest="otaver",
49
            help="Query OTA updates from a given version instead of full OS",
50
            default=None)
51
        parser.add_argument(
52
            "-r",
53
            "--remote",
54
            dest="remote",
55
            help="Get latest OTA versions from remote server",
56
            action="store_true",
57
            default=False)
58
        parser.add_argument(
59
            "-t",
60
            "--device-type",
61
            dest="device",
62
            help="Scan only one device",
63
            default=None,
64
            type=argutils.droidlookup_devicetype)
65
        parser.add_argument(
66
            "-x",
67
            "--export",
68
            dest="export",
69
            help="Write XML to logs folder",
70
            action="store_true",
71
            default=False)
72
        args = parser.parse_args(sys.argv[1:])
73
        parser.set_defaults()
74
        execute_args(args)
75
76
77
def execute_args(args):
78
    """
79
    Get args and decide what to do with them.
80
81
    :param args: Arguments.
82
    :type args: argparse.Namespace
83
    """
84
    if args.printlist:
85
        prddict = jsonutils.load_json("prds")
86
        jsonutils.list_prds(prddict)
87
    elif args.prd is not None:
88
        tclscan_single(args.prd, args.download, args.otaver, args.export, args.remote)
89
    else:
90
        tclscan_main(args.otaver, args.device, args.export, args.remote)
91
92
93
def questionnaire_ota():
94
    """
95
    Ask about OTA versus full scanning.
96
    """
97
    otabool = utilities.i2b("CHECK OTA VERSION (Y/N)?: ")
98
    if otabool:
99
        otaver = input("ENTER OTA VERSION (ex. AAO472): ")
100
    else:
101
        otaver = None
102
    return otaver
103
104
105
def questionnaire_single():
106
    """
107
    Ask about single versus full scanning.
108
    """
109
    singlebool = utilities.i2b("SCAN SINGLE DEVICE (Y/N)?: ")
110
    if singlebool:
111
        singleprd = input("ENTER PRD TO SCAN (ex. PRD-63116-001): ")
112
    else:
113
        singleprd = None
114
    return singleprd
115
116
117
def questionnaire():
118
    """
119
    Questions to ask if no arguments given.
120
    """
121
    singleprd = questionnaire_single()
122
    otaver = questionnaire_ota()
123
    if singleprd is not None:
124
        tclscan_single(singleprd, ota=otaver)
125
    else:
126
        tclscan_main(otaver)
127
    decorators.enter_to_exit(True)
128
129
130
def tclscan_single(curef, download=False, ota=None, export=False, remote=False):
131
    """
132
    Scan one PRD and produce download URL and filename.
133
134
    :param curef: PRD of the phone variant to check.
135
    :type curef: str
136
137
    :param download: If we'll download the file that this returns. Default is False.
138
    :type download: bool
139
140
    :param ota: The starting version if OTA, None if not. Default is None.
141
    :type ota: str
142
143
    :param export: Whether to export XML response to file. Default is False.
144
    :type export: bool
145
146
    :param remote: Whether to get OTA version from remote server. Default is False.
147
    :type remote: bool
148
    """
149
    if remote:
150
        remotedict = networkutilstcl.remote_prd_info()
151
        ota = remotedict.get(curef, None)
152
    mode, fvver = scriptutilstcl.tcl_prep_otaver(ota)
153
    scriptutilstcl.tcl_prd_scan(curef, download, mode=mode, fvver=fvver, export=export, verify=False)
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 OTA, None if not. Default is None.
161
    :type ota: str
162
163
    :param device: Specific PRD to check, None if all will be checked. 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 = networkutilstcl.remote_prd_info()
174
        ota = "LATEST"
175
    mode, fvver = scriptutilstcl.tcl_prep_otaver(ota)
176
    scriptutilstcl.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 devx in prddict.keys():
182
        print("~{0}~".format(devx))
183
        for curef in prddict[devx]:
184
            if remote:
185
                fvver = remotedict.get(curef, "AAA000")
186
            checktext = networkutilstcl.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 = xmlutilstcl.parse_tcl_check(checktext)
191
                del firmwareid, filename, fsize, fhash
192
                scriptutilstcl.tcl_mainscan_printer(curef, tvver, ota)
193
194
195
if __name__ == "__main__":
196
    grab_args()
197