Completed
Push — master ( 974cdd...c16db8 )
by John
01:26
created

archivist_main()   F

Complexity

Conditions 24

Size

Total Lines 172

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 24
c 1
b 0
f 1
dl 0
loc 172
rs 2

How to fix   Long Method    Complexity   

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:

Complexity

Complex classes like archivist_main() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
#!/usr/bin/env python3
2
"""Download bar files, create autoloaders."""
3
4
import os  # filesystem read
5
import sys  # load arguments
6
from bbarchivist import scriptutils  # script stuff
7
from bbarchivist import utilities  # input validation
8
from bbarchivist import decorators  # timer
9
from bbarchivist import barutils  # file/folder work
10
from bbarchivist import archiveutils  # archive work
11
from bbarchivist import networkutils  # download/lookup
12
from bbarchivist import loadergen  # cap, in Python
13
from bbarchivist import hashutils  # hashes, GPG
14
15
__author__ = "Thurask"
16
__license__ = "WTFPL v2"
17
__copyright__ = "Copyright 2015-2016 Thurask"
18
19
20
@decorators.timer
21
def grab_args():
22
    """
23
    Parse arguments from argparse/questionnaire.
24
25
    Invoke :func:`archivist.archivist_main` with those arguments.
26
    """
27
    if len(sys.argv) > 1:
28
        parser = scriptutils.default_parser("bb-archivist", "Create many autoloaders",
29
                                            ("folder", "osr"))
30
        negategroup = parser.add_argument_group(
31
            "negators",
32
            "Disable program functionality")
33
        negategroup.add_argument(
34
            "-no",
35
            "--no-download",
36
            dest="download",
37
            help="Don't download files",
38
            action="store_false",
39
            default=True)
40
        negategroup.add_argument(
41
            "-ni",
42
            "--no-integrity",
43
            dest="integrity",
44
            help="Don't test bar files after download",
45
            action="store_false",
46
            default=True)
47
        negategroup.add_argument(
48
            "-nx",
49
            "--no-extract",
50
            dest="extract",
51
            help="Don't extract bar files",
52
            action="store_false",
53
            default=True)
54
        negategroup.add_argument(
55
            "-nr",
56
            "--no-radios",
57
            dest="radloaders",
58
            help="Don't make radio autoloaders",
59
            action="store_false",
60
            default=True)
61
        negategroup.add_argument(
62
            "-ns",
63
            "--no-rmsigned",
64
            dest="signed",
65
            help="Don't remove signed files",
66
            action="store_false",
67
            default=True)
68
        negategroup.add_argument(
69
            "-nc",
70
            "--no-compress",
71
            dest="compress",
72
            help="Don't compress loaders",
73
            action="store_false",
74
            default=True)
75
        negategroup.add_argument(
76
            "-nd",
77
            "--no-delete",
78
            dest="delete",
79
            help="Don't delete uncompressed loaders",
80
            action="store_false",
81
            default=True)
82
        negategroup.add_argument(
83
            "-nv",
84
            "--no-verify",
85
            dest="verify",
86
            help="Don't verify created loaders",
87
            action="store_false",
88
            default=True)
89
        if not getattr(sys, 'frozen', False):
90
            parser.add_argument(
91
                "-g",
92
                "--gpg",
93
                dest="gpg",
94
                help="Enable GPG signing. Set up GnuPG.",
95
                action="store_true",
96
                default=False)
97
        parser.add_argument(
98
            "-r",
99
            "--radiosw",
100
            dest="altsw",
101
            metavar="SW",
102
            help="Radio software version; use without software to guess",
103
            nargs="?",
104
            const="checkme",
105
            default=None)
106
        parser.add_argument(
107
            "-m",
108
            "--method",
109
            dest="method",
110
            metavar="METHOD",
111
            help="Compression method",
112
            nargs="?",
113
            type=utilities.valid_method,
114
            default=None)
115
        parser.add_argument(
116
            "-c",
117
            "--core",
118
            dest="core",
119
            help="Make core/radio loaders",
120
            default=False,
121
            action="store_true")
122
        parser.set_defaults(compmethod="7z")
123
        args = parser.parse_args(sys.argv[1:])
124
        if args.folder is None:
125
            args.folder = os.getcwd()
126
        if getattr(sys, 'frozen', False):
127
            args.gpg = False
128
            hashdict = hashutils.verifier_config_loader(os.getcwd())
129
            args.method = "7z"
130
        else:
131
            hashdict = hashutils.verifier_config_loader()
132
        hashutils.verifier_config_writer(hashdict)
133
        if args.method is None:
134
            compmethod = archiveutils.compress_config_loader()
135
            archiveutils.compress_config_writer(compmethod)
136
        else:
137
            compmethod = args.method
138
        archivist_main(args.os, args.radio, args.swrelease,
139
                       args.folder, args.radloaders,
140
                       args.compress, args.delete, args.verify,
141
                       hashdict, args.download,
142
                       args.extract, args.signed, compmethod,
143
                       args.gpg, args.integrity, args.altsw, args.core)
144
    else:
145
        questionnaire()
146
147
148
def questionnaire():
149
    """
150
    Questions to ask if no arguments given.
151
    """
152
    localdir = os.getcwd()
153
    osversion = input("OS VERSION (REQUIRED): ")
154
    radioversion = input("RADIO VERSION (PRESS ENTER TO GUESS): ")
155
    if not radioversion:
156
        radioversion = None
157
    softwareversion = input("OS SOFTWARE RELEASE (PRESS ENTER TO GUESS): ")
158
    if not softwareversion:
159
        softwareversion = None
160
    altcheck = utilities.s2b(input("USING ALTERNATE RADIO (Y/N)?: "))
161
    if altcheck:
162
        altsw = input("RADIO SOFTWARE RELEASE (PRESS ENTER TO GUESS): ")
163
        if not altsw:
164
            altsw = "checkme"
165
    else:
166
        altsw = None
167
    radios = utilities.s2b(input("CREATE RADIO LOADERS (Y/N)?: "))
168
    compressed = utilities.s2b(input("COMPRESS LOADERS (Y/N)?: "))
169
    deleted = utilities.s2b(input("DELETE UNCOMPRESSED LOADERS (Y/N)?: ")) if compressed else False
170
    hashed = utilities.s2b(input("GENERATE HASHES (Y/N)?: "))
171
    if getattr(sys, 'frozen', False):
172
        hashdict = hashutils.verifier_config_loader(os.getcwd())
173
        compmethod = "7z"
174
    else:
175
        hashdict = hashutils.verifier_config_loader()
176
        hashutils.verifier_config_writer(hashdict)
177
        compmethod = archiveutils.compress_config_loader()
178
    print(" ")
179
    archivist_main(osversion, radioversion, softwareversion,
180
                   localdir, radios, compressed, deleted, hashed,
181
                   hashdict, download=True, extract=True, signed=True,
182
                   compmethod=compmethod, gpg=False, integrity=True,
183
                   altsw=None, core=False)
184
185
186
def archivist_main(osversion, radioversion=None, softwareversion=None,
187
                   localdir=None, radios=True, compressed=True, deleted=True,
188
                   hashed=True, hashdict=None, download=True,
189
                   extract=True, signed=True, compmethod="7z",
190
                   gpg=False, integrity=True, altsw=None,
191
                   core=False):
192
    """
193
    Wrap around multi-autoloader creation code.
194
    Some combination of creating, downloading, hashing,
195
    compressing and moving autoloaders.
196
197
    :param osversion: OS version, 10.x.y.zzzz. Required.
198
    :type osversion: str
199
200
    :param radioversion: Radio version, 10.x.y.zzzz. Can be guessed.
201
    :type radioversion: str
202
203
    :param softwareversion: Software release, 10.x.y.zzzz. Can be guessed.
204
    :type softwareversion: str
205
206
    :param localdir: Working directory. Local by default.
207
    :type localdir: str
208
209
    :param radios: Whether to create radio autoloaders. True by default.
210
    :type radios: bool
211
212
    :param compressed: Whether to compress files. True by default.
213
    :type compressed: bool
214
215
    :param deleted: Whether to delete uncompressed files. True by default.
216
    :type deleted: bool
217
218
    :param hashed: Whether to hash files. True by default.
219
    :type hashed: bool
220
221
    :param hashdict: Dictionary of hash rules, in ~\bbarchivist.ini.
222
    :type hashdict: dict({str: bool})
223
224
    :param download: Whether to download bar files. True by default.
225
    :type download: bool
226
227
    :param extract: Whether to extract bar files. True by default.
228
    :type extract: bool
229
230
    :param signed: Whether to delete signed files. True by default.
231
    :type signed: bool
232
233
    :param compmethod: Compression method. Default is "7z", fallback "zip".
234
    :type compmethod: str
235
236
    :param gpg: Whether to use GnuPG verification. False by default.
237
    :type gpg: bool
238
239
    :param integrity: Whether to test downloaded bar files. True by default.
240
    :type integrity: bool
241
242
    :param altsw: Radio software release, if not the same as OS.
243
    :type altsw: str
244
245
    :param core: Whether to create a core/radio loader. Default is false.
246
    :type core: bool
247
    """
248
    radioversion = scriptutils.return_radio_version(osversion, radioversion)
249
    softwareversion, swchecked = scriptutils.return_sw_checked(softwareversion, osversion)
250
    if altsw == "checkme":
251
        altsw, altchecked = scriptutils.return_radio_sw_checked(altsw, radioversion)
252
    if localdir is None:
253
        localdir = os.getcwd()
254
    if hashed and hashdict is None:
255
        hashdict = hashutils.verifier_config_loader()
256
        hashutils.verifier_config_writer(hashdict)
257
    scriptutils.standard_preamble("archivist", osversion, softwareversion, radioversion, altsw)
258
259
    # Generate download URLs
260
    baseurl, alturl = scriptutils.get_baseurls(softwareversion, altsw)
261
    osurls, radiourls, cores = utilities.generate_urls(baseurl, osversion, radioversion, True)
262
    osurls = cores if core else osurls
263
    for idx, url in enumerate(osurls):
264
        if "qc8960.factory_sfi" in url:
265
            vzwurl = url
266
            vzwindex = idx
267
            break
268
    if not networkutils.availability(vzwurl):
269
        osurls[vzwindex] = osurls[vzwindex].replace("qc8960.factory_sfi", "qc8960.verizon_sfi")
270
    osurls = list(set(osurls))  # pop duplicates
271
    if altsw:
272
        radiourls2 = [x.replace(baseurl, alturl) for x in radiourls]
273
        radiourls = radiourls2
274
        del radiourls2
275
276
    # Check availability of software releases
277
    scriptutils.check_sw(baseurl, softwareversion, swchecked)
278
    if altsw:
279
        scriptutils.check_radio_sw(alturl, altsw, altchecked)
280
281
    # Check availability of OS, radio
282
    scriptutils.check_os_bulk(osurls)
283
    radiourls, radioversion = scriptutils.check_radio_bulk(radiourls, radioversion)
284
285
    # Get 7z executable
286
    compmethod, szexe = scriptutils.get_sz_executable(compmethod)
287
288
    # Make dirs: bd_o, bd_r, ld_o, ld_r, zd_o, zd_r
289
    dirs = barutils.make_dirs(localdir, osversion, radioversion)
290
291
    # Download files
292
    if download:
293
        print("BEGIN DOWNLOADING...")
294
        networkutils.download_bootstrap(radiourls + osurls, localdir, 3)
295
        print("ALL FILES DOWNLOADED")
296
297
    # Test bar files
298
    if integrity:
299
        urllist = osurls + radiourls
300
        scriptutils.test_bar_files(localdir, urllist)
301
302
    # Extract bar files
303
    if extract:
304
        print("EXTRACTING...")
305
        barutils.extract_bars(localdir)
306
307
    # Test signed files
308
    if integrity:
309
        scriptutils.test_signed_files(localdir)
310
311
    # Move bar files
312
    print("MOVING BAR FILES...")
313
    barutils.move_bars(localdir, dirs[0], dirs[1])
314
315
    # Create loaders
316
    print("GENERATING LOADERS...")
317
    altradio = altsw is not None
318
    loadergen.generate_loaders(osversion, radioversion, radios, localdir, altradio, core)
319
320
    # Test loader files
321
    if integrity:
322
        scriptutils.test_loader_files(localdir)
323
324
    # Remove .signed files
325
    if signed:
326
        print("REMOVING SIGNED FILES...")
327
        barutils.remove_signed_files(localdir)
328
329
    # If compression = true, compress
330
    if compressed:
331
        print("COMPRESSING...")
332
        archiveutils.compress(localdir, compmethod, szexe, True)
333
334
    if integrity and compressed:
335
        print("TESTING ARCHIVES...")
336
        archiveutils.verify(localdir, compmethod, szexe, True)
337
338
    # Move zipped/unzipped loaders
339
    print("MOVING LOADERS...")
340
    barutils.move_loaders(localdir, dirs[2], dirs[3], dirs[4], dirs[5])
341
342
    # Get hashes/signatures (if specified)
343
    if hashed:
344
        scriptutils.bulk_hash(dirs, compressed, deleted, radios, hashdict)
345
    if gpg:
346
        scriptutils.bulk_verify(dirs, compressed, deleted, radios)
347
348
    # Remove uncompressed loaders (if specified)
349
    if deleted:
350
        print("DELETING UNCOMPRESSED LOADERS...")
351
        barutils.remove_unpacked_loaders(dirs[2], dirs[3], radios)
352
353
    # Delete empty folders
354
    print("REMOVING EMPTY FOLDERS...")
355
    barutils.remove_empty_folders(localdir)
356
357
    print("\nFINISHED!")
358
359
360
if __name__ == "__main__":
361
    grab_args()
362
    decorators.enter_to_exit(False)
363