Completed
Push — master ( 3186b0...1efb8b )
by John
02:40
created

verify_gpg_key()   A

Complexity

Conditions 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
c 1
b 0
f 0
dl 0
loc 12
ccs 6
cts 6
cp 1
crap 3
rs 9.4285
1
#!/usr/bin/env python3
2 4
"""This module contains various utilities for the scripts folder."""
3
4 4
import os  # path work
5 4
import getpass  # invisible password
6 4
import argparse  # generic parser
7 4
import sys  # getattr
8 4
import shutil  # folder removal
9 4
import subprocess  # running cfp/cap
10 4
import glob  # file lookup
11 4
import threading  # run stuff in background
12 4
import requests  # session
13 4
from bbarchivist import utilities  # little things
14 4
from bbarchivist import barutils  # file system work
15 4
from bbarchivist import archiveutils  # archive support
16 4
from bbarchivist import bbconstants  # constants
17 4
from bbarchivist import gpgutils  # gpg
18 4
from bbarchivist import hashutils  # file hashes
19 4
from bbarchivist import networkutils  # network tools
20 4
from bbarchivist import textgenerator  # writing text to file
21 4
from bbarchivist import smtputils  # email
22 4
from bbarchivist import sqlutils  # sql
23
24 4
__author__ = "Thurask"
25 4
__license__ = "WTFPL v2"
26 4
__copyright__ = "Copyright 2015-2016 Thurask"
27
28
29 4
def shortversion():
30
    """
31
    Get short app version (Git tag).
32
    """
33 4
    if not getattr(sys, 'frozen', False):
34 4
        ver = bbconstants.VERSION
35
    else:
36 4
        verfile = glob.glob(os.path.join(os.getcwd(), "version.txt"))[0]
37 4
        with open(verfile) as afile:
38 4
            ver = afile.read()
39 4
    return ver
40
41
42 4
def longversion():
43
    """
44
    Get long app version (Git tag + commits + hash).
45
    """
46 4
    if not getattr(sys, 'frozen', False):
47 4
        ver = (bbconstants.LONGVERSION, bbconstants.COMMITDATE)
48
    else:
49 4
        verfile = glob.glob(os.path.join(os.getcwd(), "longversion.txt"))[0]
50 4
        with open(verfile) as afile:
51 4
            ver = afile.read().split("\n")
52 4
    return ver
53
54
55 4
def default_parser(name=None, desc=None, flags=None, vers=None):
56
    """
57
    A generic form of argparse's ArgumentParser.
58
59
    :param name: App name.
60
    :type name: str
61
62
    :param desc: App description.
63
    :type desc: str
64
65
    :param flags: Tuple of sections to add.
66
    :type flags: tuple(str)
67
68
    :param vers: Versions: [git commit hash, git commit date]
69
    :param vers: list(str)
70
    """
71 4
    if vers is None:
72 4
        vers = longversion()
73 4
    parser = argparse.ArgumentParser(
74
        prog=name,
75
        description=desc,
76
        epilog="https://github.com/thurask/bbarchivist")
77 4
    parser.add_argument(
78
        "-v",
79
        "--version",
80
        action="version",
81
        version="{0} {1} committed {2}".format(parser.prog, vers[0], vers[1]))
82 4
    if flags is not None:
83 4
        if "folder" in flags:
84 4
            parser.add_argument(
85
                "-f",
86
                "--folder",
87
                dest="folder",
88
                help="Working folder",
89
                default=None,
90
                metavar="DIR",
91
                type=utilities.file_exists)
92 4
        if "osr" in flags:
93 4
            parser.add_argument(
94
                "os",
95
                help="OS version")
96 4
            parser.add_argument(
97
                "radio",
98
                help="Radio version, 10.x.y.zzzz",
99
                nargs="?",
100
                default=None)
101 4
            parser.add_argument(
102
                "swrelease",
103
                help="Software version, 10.x.y.zzzz",
104
                nargs="?",
105
                default=None)
106 4
    return parser
107
108
109 4
def generic_windows_shim(scriptname, scriptdesc, target, version):
110
    """
111
    Generic CFP/CAP runner; Windows only.
112
113
    :param scriptname: Script name, 'bb-something'.
114 View Code Duplication
    :type scriptname: str
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
115
116
    :param scriptdesc: Script description, i.e. scriptname -h.
117
    :type scriptdesc: str
118
119
    :param target: Path to file to execute.
120
    :type target: str
121
122
    :param version: Version of target.
123
    :type version: str
124
    """
125 4
    parser = default_parser(scriptname, scriptdesc)
126 4
    capver = "|{0}".format(version)
127 4
    parser = external_version(parser, capver)
128 4
    parser.parse_known_args(sys.argv[1:])
129 4
    if utilities.is_windows():
130 4
        subprocess.call([target] + sys.argv[1:])
131
    else:
132 4
        print("Sorry, Windows only.")
133
134
135 4
def external_version(parser, addition):
136
    """
137
    Modify the version string of argparse.ArgumentParser, adding something.
138
139
    :param parser: Parser to modify.
140
    :type parser: argparse.ArgumentParser
141
142
    :param addition: What to add.
143 View Code Duplication
    :type addition: str
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
144
    """
145 4
    verarg = [arg for arg in parser._actions if isinstance(arg, argparse._VersionAction)][0]
146 4
    verarg.version = "{1}{0}".format(addition, verarg.version)
147 4
    return parser
148
149
150 4
def return_radio_version(osversion, radioversion=None):
151
    """
152
    Increment radio version, if need be.
153
154
    :param osversion: OS version.
155
    :type osversion: str
156
157
    :param radioversion: Radio version, None if incremented.
158
    :type radioversion: str
159
    """
160 4
    if radioversion is None:
161 4
        radioversion = utilities.increment(osversion, 1)
162 4
    return radioversion
163
164
165 4
def return_sw_checked(softwareversion, osversion):
166
    """
167
    Check software existence, return boolean.
168
169
    :param softwareversion: Software release version.
170
    :type softwareversion: str
171
172
    :param osversion: OS version.
173
    :type osversion: str
174
    """
175 4
    if softwareversion is None:
176 4
        serv = bbconstants.SERVERS["p"]
177 4
        softwareversion = networkutils.sr_lookup(osversion, serv)
178 4
        if softwareversion == "SR not in system":
179 4
            print("SOFTWARE RELEASE NOT FOUND")
180 4
            cont = utilities.s2b(input("INPUT MANUALLY? Y/N: "))
181 4
            if cont:
182 4
                softwareversion = input("SOFTWARE RELEASE: ")
183 4
                swchecked = False
184
            else:
185 4
                print("\nEXITING...")
186 4
                raise SystemExit  # bye bye
187
        else:
188 4
            swchecked = True
189
    else:
190 4
        swchecked = True
191 4
    return softwareversion, swchecked
192
193
194 4
def return_radio_sw_checked(altsw, radioversion):
195
    """
196
    Check radio software existence, return boolean.
197
198
    :param altsw: Software release version.
199
    :type altsw: str
200
201
    :param radioversion: Radio version.
202
    :type radioversion: str
203
    """
204 4
    if altsw == "checkme":
205 4
        serv = bbconstants.SERVERS["p"]
206 4
        testos = utilities.increment(radioversion, -1)
207 4
        altsw = networkutils.sr_lookup(testos, serv)
208 4
        if altsw == "SR not in system":
209 4
            print("RADIO SOFTWARE RELEASE NOT FOUND")
210 4
            cont = utilities.s2b(input("INPUT MANUALLY? Y/N: "))
211 4
            if cont:
212 4
                altsw = input("SOFTWARE RELEASE: ")
213 4
                altchecked = False
214
            else:
215 4
                print("\nEXITING...")
216 4
                raise SystemExit  # bye bye
217
        else:
218 4
            altchecked = True
219
    else:
220 4
        altchecked = True
221 4
    return altsw, altchecked
222
223
224 4
def check_sw(baseurl, softwareversion, swchecked):
225
    """
226
    Check existence of software release.
227
228
    :param baseurl: Base URL (from http to hashed SW release).
229
    :type basurl: str
230
231
    :param softwareversion: Software release.
232
    :type softwareversion: str
233
234
    :param swchecked: If we checked the sw release already.
235
    :type swchecked: bool
236
    """
237 4
    print("CHECKING SOFTWARE RELEASE AVAILABILITY...")
238 4
    if not swchecked:
239 4
        avlty = networkutils.availability(baseurl)
240 4
        if avlty:
241 4
            print("SOFTWARE RELEASE {0} EXISTS".format(softwareversion))
242
        else:
243 4
            print("SOFTWARE RELEASE {0} NOT FOUND".format(softwareversion))
244 4
            cont = utilities.s2b(input("CONTINUE? Y/N: "))
245 4
            if not cont:
246 4
                print("\nEXITING...")
247 4
                raise SystemExit
248
    else:
249 4
        print("SOFTWARE RELEASE {0} EXISTS".format(softwareversion))
250
251
252 4
def check_radio_sw(alturl, altsw, altchecked):
253
    """
254
    Check existence of radio software release.
255
256
    :param alturl: Radio base URL (from http to hashed SW release).
257
    :type alturl: str
258
259
    :param altsw: Radio software release.
260
    :type altsw: str
261
262
    :param altchecked: If we checked the sw release already.
263
    :type altchecked: bool
264
    """
265 4
    print("CHECKING RADIO SOFTWARE RELEASE...")
266 4
    if not altchecked:
267 4
        altavlty = networkutils.availability(alturl)
268 4
        if altavlty:
269 4
            print("SOFTWARE RELEASE {0} EXISTS".format(altsw))
270
        else:
271 4
            print("SOFTWARE RELEASE {0} NOT FOUND".format(altsw))
272 4
            cont = utilities.s2b(input("CONTINUE? Y/N: "))
273 4
            if not cont:
274 4
                print("\nEXITING...")
275 4
                raise SystemExit
276
    else:
277 4
        print("SOFTWARE RELEASE {0} EXISTS".format(altsw))
278
279
280 4
def check_os_single(osurl, osversion, device):
281
    """
282
    Check existence of single OS link.
283
284
    :param radiourl: Radio URL to check.
285
    :type radiourl: str
286
287
    :param radioversion: Radio version.
288
    :type radioversion: str
289
290
    :param device: Device family.
291
    :type device: int
292
    """
293 4
    osav = networkutils.availability(osurl)
294 4
    if not osav:
295 4
        print("{0} NOT AVAILABLE FOR {1}".format(osversion, bbconstants.DEVICES[device]))
296 4
        cont = utilities.s2b(input("CONTINUE? Y/N: "))
297 4
        if not cont:
298 4
            print("\nEXITING...")
299 4
            raise SystemExit
300
301
302 4
def check_os_bulk(osurls):
303
    """
304
    Check existence of list of OS links.
305
306
    :param osurls: OS URLs to check.
307
    :type osurls: list(str)
308
    """
309 4
    sess = requests.Session()
310 4
    for url in osurls:
311 4
        osav = networkutils.availability(url, sess)
312 4
        if osav:
313 4
            break
314
    else:
315 4
        print("OS VERSION NOT FOUND")
316 4
        cont = utilities.s2b(input("CONTINUE? Y/N: "))
317 4
        if not cont:
318 4
            print("\nEXITING...")
319 4
            raise SystemExit
320
321
322 4
def check_radio_single(radiourl, radioversion):
323
    """
324
    Check existence of single radio link.
325
326
    :param radiourl: Radio URL to check.
327
    :type radiourl: str
328
329
    :param radioversion: Radio version.
330
    :type radioversion: str
331
    """
332 4
    radav = networkutils.availability(radiourl)
333 4
    if not radav:
334 4
        print("RADIO VERSION NOT FOUND")
335 4
        cont = utilities.s2b(input("INPUT MANUALLY? Y/N: "))
336 4
        if cont:
337 4
            rad2 = input("RADIO VERSION: ")
338 4
            radiourl = radiourl.replace(radioversion, rad2)
339 4
            radioversion = rad2
340
        else:
341 4
            going = utilities.s2b(input("KEEP GOING? Y/N: "))
342 4
            if not going:
343 4
                print("\nEXITING...")
344 4
                raise SystemExit
345 4
    return radiourl, radioversion
346
347
348 4
def check_radio_bulk(radiourls, radioversion):
349
    """
350
    Check existence of list of radio links.
351
352
    :param radiourls: Radio URLs to check.
353
    :type radiourls: list(str)
354
355
    :param radioversion: Radio version.
356
    :type radioversion: str
357
    """
358 4
    sess = requests.Session()
359 4
    for url in radiourls:
360 4
        radav = networkutils.availability(url, sess)
361 4
        if radav:
362 4
            break
363
    else:
364 4
        print("RADIO VERSION NOT FOUND")
365 4
        cont = utilities.s2b(input("INPUT MANUALLY? Y/N: "))
366 4
        if cont:
367 4
            rad2 = input("RADIO VERSION: ")
368 4
            radiourls = [url.replace(radioversion, rad2) for url in radiourls]
369 4
            radioversion = rad2
370
        else:
371 4
            going = utilities.s2b(input("KEEP GOING? Y/N: "))
372 4
            if not going:
373 4
                print("\nEXITING...")
374 4
                raise SystemExit
375 4
    return radiourls, radioversion
376
377
378 4
def bulk_avail(urllist):
379
    """
380
    Filter 404 links out of URL list.
381
382
    :param urllist: URLs to check.
383
    :type urllist: list(str)
384
    """
385 4
    sess = requests.Session()
386 4
    url2 = [x for x in urllist if networkutils.availability(x, sess)]
387 4
    return url2
388
389
390 4
def get_baseurls(softwareversion, altsw=None):
391
    """
392
    Generate base URLs for bar links.
393
394
    :param softwareversion: Software version.
395
    :type softwareversion: str
396
397
    :param altsw: Radio software version, if necessary.
398
    :type altsw: str
399
    """
400 4
    baseurl = utilities.create_base_url(softwareversion)
401 4
    alturl = utilities.create_base_url(altsw) if altsw else None
402 4
    return baseurl, alturl
403
404
405 4
def get_sz_executable(compmethod):
406
    """
407
    Get 7z executable.
408
409
    :param compmethod: Compression method.
410
    :type compmethod: str
411
    """
412 4
    if compmethod != "7z":
413 4
        szexe = ""
414
    else:
415 4
        print("CHECKING PRESENCE OF 7ZIP...")
416 4
        psz = utilities.prep_seven_zip(True)
417 4
        if psz:
418 4
            print("7ZIP OK")
419 4
            szexe = utilities.get_seven_zip(False)
420
        else:
421 4
            szexe = ""
422 4
            print("7ZIP NOT FOUND")
423 4
            cont = utilities.s2b(input("CONTINUE? Y/N "))
424 4
            if cont:
425 4
                print("FALLING BACK TO ZIP...")
426 4
                compmethod = "zip"
427
            else:
428 4
                print("\nEXITING...")
429 4
                raise SystemExit  # bye bye
430 4
    return compmethod, szexe
431
432
433 4
def test_bar_files(localdir, urllist):
434
    """
435
    Test bar files after download.
436
437
    :param localdir: Directory.
438
    :type localdir: str
439
440
    :param urllist: List of URLs to check.
441
    :type urllist: list(str)
442
    """
443 4
    brokenlist = []
444 4
    print("TESTING BAR FILES...")
445 4
    for file in os.listdir(localdir):
446 4
        if file.endswith(".bar"):
447 4
            print("TESTING: {0}".format(file))
448 4
            thepath = os.path.abspath(os.path.join(localdir, file))
449 4
            brokens = barutils.bar_tester(thepath)
450 4
            if brokens is not None:
451 4
                os.remove(brokens)
452 4
                for url in urllist:
453 4
                    if brokens in url:
454 4
                        brokenlist.append(url)
455 4
    if brokenlist:
456 4
        print("SOME FILES ARE BROKEN!")
457 4
        utilities.lprint(brokenlist)
458 4
        raise SystemExit
459
    else:
460 4
        print("BAR FILES DOWNLOADED OK")
461
462
463 4
def test_signed_files(localdir):
464
    """
465
    Test signed files after extract.
466
467
    :param localdir: Directory.
468
    :type localdir: str
469
    """
470 4
    print("TESTING SIGNED FILES...")
471 4
    for file in os.listdir(localdir):
472 4
        if file.endswith(".bar"):
473 4
            print("TESTING: {0}".format(file))
474 4
            signname, signhash = barutils.retrieve_sha512(os.path.join(localdir, file))
475 4
            sha512ver = barutils.verify_sha512(os.path.join(localdir, signname.decode("utf-8")), signhash)
476 4
            if not sha512ver:
477 4
                print("{0} IS BROKEN".format((file)))
478 4
                break
479
    else:
480 4
        print("ALL FILES EXTRACTED OK")
481
482
483 4
def test_loader_files(localdir):
484
    """
485
    Test loader files after creation.
486
487
    :param localdir: Directory.
488
    :type localdir: str
489
    """
490 4
    if not utilities.is_windows():
491 4
        pass
492
    else:
493 4
        print("TESTING LOADER FILES...")
494 4
        brokens = utilities.verify_bulk_loaders(localdir)
495 4
        if brokens:
496 4
            print("BROKEN FILES:")
497 4
            utilities.lprint(brokens)
498 4
            raise SystemExit
499
        else:
500 4
            print("ALL FILES CREATED OK")
501
502
503 4
def test_single_loader(loaderfile):
504
    """
505
    Test single loader file after creation.
506
507
    :param loaderfile: File to check.
508
    :type loaderfile: str
509
    """
510 4
    if not utilities.is_windows():
511 4
        pass
512
    else:
513 4
        print("TESTING LOADER...")
514 4
        if not utilities.verify_loader_integrity(loaderfile):
515 4
            print("{0} IS BROKEN!".format(os.path.basename(loaderfile)))
516 4
            raise SystemExit
517
        else:
518 4
            print("LOADER CREATED OK")
519
520
521 4
def prod_avail(results, mailer=False, osversion=None, password=None):
522
    """
523
    Clean availability for production lookups for autolookup script.
524
525
    :param results: Result dict.
526
    :type results: dict(str: str)
527
528
    :param mailer: If we're mailing links. Default is false.
529
    :type mailer: bool
530
531
    :param osversion: OS version.
532
    :type osversion: str
533
534
    :param password: Email password.
535
    :type password: str
536
    """
537 4
    prel = results['p']
538 4
    if prel != "SR not in system" and prel is not None:
539 4
        pav = "PD"
540 4
        baseurl = utilities.create_base_url(prel)
541 4
        avail = networkutils.availability(baseurl)
542 4
        is_avail = "Available" if avail else "Unavailable"
543 4
        prod_avail_mailprep(prel, avail, osversion, mailer, password)
544
    else:
545 4
        pav = "  "
546 4
        is_avail = "Unavailable"
547 4
    return prel, pav, is_avail
548
549
550 4
def prod_avail_mailprep(prel, avail, osversion=None, mailer=False, password=None):
551
    """
552
    Do SQL/SMTP prep work after a good production lookup hit.
553
554
    :param prel: Software lookup result.
555
    :type prel: str
556
557
    :param avail: If software lookup result is available for download.
558
    :type avail: bool
559
560
    :param osversion: OS version.
561
    :type osversion: str
562
563
    :param mailer: If we're mailing links. Default is false.
564
    :type mailer: bool
565
566
    :param password: Email password.
567
    :type password: str
568
    """
569 4
    if avail and mailer:
570 4
        sqlutils.prepare_sw_db()
571 4
        if not sqlutils.check_exists(osversion, prel):
572 4
            rad = utilities.increment(osversion, 1)
573 4
            linkgen(osversion, rad, prel, temp=True)
574 4
            smtputils.prep_email(osversion, prel, password)
575
576
577 4
def linkgen(osversion, radioversion=None, softwareversion=None, altsw=None, temp=False, sdk=False):
578
    """
579
    Generate debrick/core/radio links for given OS, radio, software release.
580
581
    :param osversion: OS version, 10.x.y.zzzz.
582
    :type osversion: str
583
584
    :param radioversion: Radio version, 10.x.y.zzzz. Can be guessed.
585
    :type radioversion: str
586
587
    :param softwareversion: Software version, 10.x.y.zzzz. Can be guessed.
588
    :type softwareversion: str
589
590
    :param altsw: Radio software release, if not the same as OS.
591
    :type altsw: str
592
593
    :param temp: If file we write to is temporary. Default is False.
594
    :type temp: bool
595
596
    :param sdk: If we specifically want SDK images. Default is False.
597
    :type sdk: bool
598
    """
599 4
    radioversion = return_radio_version(osversion, radioversion)
600 4
    softwareversion, swc = return_sw_checked(softwareversion, osversion)
601 4
    del swc
602 4
    if altsw is not None:
603 4
        altsw, aswc = return_radio_sw_checked(altsw, radioversion)
604 4
        del aswc
605 4
    baseurl = utilities.create_base_url(softwareversion)
606 4
    oses, cores, radios = textgenerator.url_gen(osversion, radioversion, softwareversion)
607 4
    if altsw is not None:
608 4
        del radios
609 4
        dbks, cors, radios = textgenerator.url_gen(osversion, radioversion, altsw)
610 4
        del dbks
611 4
        del cors
612 4
    avlty = networkutils.availability(baseurl)
613 4
    if sdk:
614 4
        oses2 = {key: val.replace("factory_sfi", "sdk") for key, val in oses.items()}
615 4
        cores2 = {key: val.replace("factory_sfi", "sdk") for key, val in cores.items()}
616 4
        oses = {key: val.replace("verizon_sfi", "sdk") for key, val in oses2.items()}
617 4
        cores = {key: val.replace("verizon_sfi", "sdk") for key, val in cores2.items()}
618 4
    prargs = (softwareversion, osversion, radioversion, oses, cores, radios, avlty, False, None, temp, altsw)
619 4
    lthr = threading.Thread(target=textgenerator.write_links, args=prargs)
620 4
    lthr.start()
621
622
623 4
def clean_swrel(swrelset):
624
    """
625
    Clean a list of software release lookups.
626
627
    :param swrelset: List of software releases.
628
    :type swrelset: set(str)
629
    """
630 4
    for i in swrelset:
631 4
        if i != "SR not in system" and i is not None:
632 4
            swrelease = i
633 4
            break
634
    else:
635 4
        swrelease = ""
636 4
    return swrelease
637
638
639 4
def autolookup_logger(record, out):
640
    """
641
    Write autolookup results to file.
642
643
    :param record: The file to log to.
644
    :type record: str
645
646
    :param out: Output block.
647
    :type out: str
648
    """
649 4
    with open(record, "a") as rec:
650 4
        rec.write("{0}\n".format(out))
651
652
653 4
def autolookup_printer(out, avail, log=False, quiet=False, record=None):
654
    """
655
    Print autolookup results, logging if specified.
656
657
    :param out: Output block.
658
    :type out: str
659
660
    :param avail: Availability. Can be "Available" or "Unavailable".
661
    :type avail: str
662
663
    :param log: If we're logging to file.
664
    :type log: bool
665
666
    :param quiet: If we only note available entries.
667
    :type quiet: bool
668
669
    :param record: If we're logging, the file to log to.
670
    :type record: str
671
    """
672 4
    if not quiet:
673 4
        avail = "Available"  # force things
674 4
    if avail.lower() == "available":
675 4
        if log:
676 4
            lthr = threading.Thread(target=autolookup_logger, args=(record, out))
677 4
            lthr.start()
678 4
        print(out)
679
680
681 4
def autolookup_output_sql(osversion, swrelease, avail, sql=False):
682
    """
683
    Add OS to SQL database.
684
685
    :param osversion: OS version.
686
    :type osversion: str
687
688
    :param swrelease: Software release.
689
    :type swrelease: str
690
691
    :param avail: "Unavailable" or "Available".
692
    :type avail: str
693
694
    :param sql: If we're adding this to our SQL database.
695
    :type sql: bool
696
    """
697 4
    if sql:
698 4
        sqlutils.prepare_sw_db()
699 4
        if not sqlutils.check_exists(osversion, swrelease):
700 4
            sqlutils.insert(osversion, swrelease, avail.lower())
701
702
703 4
def autolookup_output(osversion, swrelease, avail, avpack, sql=False):
704
    """
705
    Prepare autolookup block, and add to SQL database.
706
707
    :param osversion: OS version.
708
    :type osversion: str
709
710
    :param swrelease: Software release.
711
    :type swrelease: str
712
713
    :param avail: "Unavailable" or "Available".
714
    :type avail: str
715
716
    :param avpack: Availabilities: alpha 1 and 2, beta 1 and 2, production.
717
    :type avpack: list(str)
718
719
    :param sql: If we're adding this to our SQL database.
720
    :type sql: bool
721
    """
722 4
    othr = threading.Thread(target=autolookup_output_sql, args=(osversion, swrelease, avail, sql))
723 4
    othr.start()
724 4
    avblok = "[{0}|{1}|{2}|{3}|{4}]".format(*avpack)
725 4
    out = "OS {0} - SR {1} - {2} - {3}".format(osversion, swrelease, avblok, avail)
726 4
    return out
727
728
729 4
def clean_barlist(cleanfiles, stoppers):
730
    """
731
    Remove certain bars from barlist based on keywords.
732
733
    :param cleanfiles: List of files to clean.
734
    :type cleanfiles: list(str)
735
736
    :param stoppers: List of keywords (i.e. bar names) to exclude.
737
    :type stoppers: list(str)
738
    """
739 4
    finals = [link for link in cleanfiles if all(word not in link for word in stoppers)]
740 4
    return finals
741
742
743 4
def prep_export_cchecker(files, npc, hwid, osv, radv, swv, upgrade=False, forced=None):
744
    """
745
    Prepare carrierchecker lookup links to write to file.
746
747
    :param files: List of file URLs.
748
    :type files: list(str)
749
750
    :param npc: MCC + MNC (ex. 302220).
751
    :type npc: int
752
753
    :param hwid: Device hardware ID.
754
    :type hwid: str
755
756
    :param osv: OS version.
757
    :type osv: str
758
759
    :param radv: Radio version.
760
    :type radv: str
761
762
    :param swv: Software release.
763
    :type swv: str
764
765
    :param upgrade: Whether or not to use upgrade files. Default is false.
766
    :type upgrade: bool
767
768
    :param forced: Force a software release. None to go for latest.
769
    :type forced: str
770
    """
771 4
    if not upgrade:
772 4
        newfiles = networkutils.carrier_query(npc, hwid, True, False, forced)
773 4
        cleanfiles = newfiles[3]
774
    else:
775 4
        cleanfiles = files
776 4
    osurls, coreurls, radiourls = textgenerator.url_gen(osv, radv, swv)
777 4
    stoppers = ["8960", "8930", "8974", "m5730", "winchester"]
778 4
    finals = clean_barlist(cleanfiles, stoppers)
779 4
    return osurls, coreurls, radiourls, finals
780
781
782 4
def export_cchecker(files, npc, hwid, osv, radv, swv, upgrade=False, forced=None):
783
    """
784
    Write carrierchecker lookup links to file.
785
786
    :param files: List of file URLs.
787
    :type files: list(str)
788
789
    :param npc: MCC + MNC (ex. 302220).
790
    :type npc: int
791
792
    :param hwid: Device hardware ID.
793
    :type hwid: str
794
795
    :param osv: OS version.
796
    :type osv: str
797
798
    :param radv: Radio version.
799
    :type radv: str
800
801
    :param swv: Software release.
802
    :type swv: str
803
804
    :param upgrade: Whether or not to use upgrade files. Default is false.
805
    :type upgrade: bool
806
807
    :param forced: Force a software release. None to go for latest.
808
    :type forced: str
809
    """
810 4
    if files:
811 4
        osurls, coreurls, radiourls, finals = prep_export_cchecker(files, npc, hwid, osv, radv, swv, upgrade, forced)
812 4
        textgenerator.write_links(swv, osv, radv, osurls, coreurls, radiourls, True, True, finals)
813 4
        print("\nFINISHED!!!")
814
    else:
815 4
        print("CANNOT EXPORT, NO SOFTWARE RELEASE")
816
817
818 4
def generate_blitz_links(files, osv, radv, swv):
819
    """
820
    Generate blitz URLs (i.e. all OS and radio links).
821
    :param files: List of file URLs.
822
    :type files: list(str)
823
824
    :param osv: OS version.
825
    :type osv: str
826
827
    :param radv: Radio version.
828
    :type radv: str
829
830
    :param swv: Software release.
831
    :type swv: str
832
    """
833 4
    coreurls = [
834
        utilities.create_bar_url(swv, "winchester.factory_sfi", osv),
835
        utilities.create_bar_url(swv, "qc8960.factory_sfi", osv),
836
        utilities.create_bar_url(swv, "qc8960.factory_sfi", osv),
837
        utilities.create_bar_url(swv, "qc8960.factory_sfi_hybrid_qc8974", osv)
838
    ]
839 4
    radiourls = [
840
        utilities.create_bar_url(swv, "m5730", radv),
841
        utilities.create_bar_url(swv, "qc8960", radv),
842
        utilities.create_bar_url(swv, "qc8960.wtr", radv),
843
        utilities.create_bar_url(swv, "qc8960.wtr5", radv),
844
        utilities.create_bar_url(swv, "qc8930.wtr5", radv),
845
        utilities.create_bar_url(swv, "qc8974.wtr2", radv)
846
    ]
847 4
    return files + coreurls + radiourls
848
849
850 4
def package_blitz(bardir, swv):
851
    """
852
    Package and verify a blitz package.
853
854
    :param bardir: Path to folder containing bar files.
855
    :type bardir: str
856
857
    :param swv: Software version.
858
    :type swv: str
859
    """
860 4
    print("\nCREATING BLITZ...")
861 4
    barutils.create_blitz(bardir, swv)
862 4
    print("\nTESTING BLITZ...")
863 4
    zipver = archiveutils.zip_verify("Blitz-{0}.zip".format(swv))
864 4
    if not zipver:
865 4
        print("BLITZ FILE IS BROKEN")
866 4
        raise SystemExit
867
    else:
868 4
        shutil.rmtree(bardir)
869
870
871 4
def slim_preamble(appname):
872
    """
873
    Standard app name header.
874
875
    :param appname: Name of app.
876
    :type appname: str
877
    """
878 4
    print("~~~{0} VERSION {1}~~~".format(appname.upper(), shortversion()))
879
880
881 4
def standard_preamble(appname, osversion, softwareversion, radioversion, altsw=None):
882
    """
883
    Standard app name, OS, radio and software (plus optional radio software) print block.
884
885
    :param appname: Name of app.
886
    :type appname: str
887
888
    :param osversion: OS version, 10.x.y.zzzz. Required.
889
    :type osversion: str
890
891
    :param radioversion: Radio version, 10.x.y.zzzz. Can be guessed.
892
    :type radioversion: str
893
894
    :param softwareversion: Software release, 10.x.y.zzzz. Can be guessed.
895
    :type softwareversion: str
896
897
    :param altsw: Radio software release, if not the same as OS.
898
    :type altsw: str
899
    """
900 4
    slim_preamble(appname)
901 4
    print("OS VERSION: {0}".format(osversion))
902 4
    print("OS SOFTWARE VERSION: {0}".format(softwareversion))
903 4
    print("RADIO VERSION: {0}".format(radioversion))
904 4
    if altsw is not None:
905 4
        print("RADIO SOFTWARE VERSION: {0}".format(altsw))
906
907
908 4
def verify_gpg_credentials():
909
    """
910
    Read GPG key/pass from file, verify if incomplete.
911
    """
912 4
    gpgkey, gpgpass = gpgutils.gpg_config_loader()
913 4
    if gpgkey is None or gpgpass is None:
914 4
        print("NO PGP KEY/PASS FOUND")
915 4
        cont = utilities.s2b(input("CONTINUE (Y/N)?: "))
916 4
        if cont:
917 4
            gpgkey = verify_gpg_key(gpgkey)
918 4
            gpgpass, writebool = verify_gpg_pass(gpgpass)
919 4
            gpgpass2 = gpgpass if writebool else None
920 4
            gpgutils.gpg_config_writer(gpgkey, gpgpass2)
921
        else:
922 4
            gpgkey = None
923 4
    return gpgkey, gpgpass
924
925
926 4
def verify_gpg_key(gpgkey=None):
927
    """
928
    Verify GPG key.
929
930
    :param gpgkey: Key, use None to take from input.
931
    :type gpgkey: str
932
    """
933 4
    if gpgkey is None:
934 4
        gpgkey = input("PGP KEY (0x12345678): ")
935 4
        if not gpgkey.startswith("0x"):
936 4
            gpgkey = "0x{0}".format(gpgkey)   # add preceding 0x
937 4
    return gpgkey
938
939
940 4
def verify_gpg_pass(gpgpass=None):
941
    """
942
    Verify GPG passphrase.
943
944
    :param gpgpass: Passphrase, use None to take from input.
945
    :type gpgpass: str
946
    """
947 4
    if gpgpass is None:
948 4
        gpgpass = getpass.getpass(prompt="PGP PASSPHRASE: ")
949 4
        writebool = utilities.s2b(input("SAVE PASSPHRASE (Y/N)?:"))
950
    else:
951 4
        writebool = False
952 4
    return gpgpass, writebool
953
954
955 4
def bulk_hash(dirs, compressed=True, deleted=True, radios=True, hashdict=None):
956
    """
957
    Hash files in several folders based on flags.
958
959
    :param dirs: Folders: [OS_bars, radio_bars, OS_exes, radio_exes, OS_zips, radio_zips]
960
    :type dirs: list(str)
961
962
    :param compressed: Whether to hash compressed files. True by default.
963
    :type compressed: bool
964
965
    :param deleted: Whether to delete uncompressed files. True by default.
966
    :type deleted: bool
967
968
    :param radios: Whether to hash radio autoloaders. True by default.
969
    :type radios: bool
970
971
    :param hashdict: Dictionary of hash rules, in ~\bbarchivist.ini.
972
    :type hashdict: dict({str: bool})
973
    """
974 4
    print("HASHING LOADERS...")
975 4
    goargs = [dirs[4], dirs[5], dirs[2], dirs[3]]
976 4
    restargs = [hashdict]
977 4
    utilities.cond_check(hashutils.verifier, goargs, restargs, radios, compressed, deleted)
978
979
980 4
def bulk_verify(dirs, compressed=True, deleted=True, radios=True):
981
    """
982
    Verify files in several folders based on flags.
983
984
    :param dirs: Folders: [OS_bars, radio_bars, OS_exes, radio_exes, OS_zips, radio_zips]
985
    :type dirs: list(str)
986
987
    :param compressed: Whether to hash compressed files. True by default.
988
    :type compressed: bool
989
990
    :param deleted: Whether to delete uncompressed files. True by default.
991
    :type deleted: bool
992
993
    :param radios: Whether to hash radio autoloaders. True by default.
994
    :type radios: bool
995
    """
996 4
    gpgkey, gpgpass = verify_gpg_credentials()
997 4
    if gpgpass is not None and gpgkey is not None:
998 4
        print("VERIFYING LOADERS...")
999 4
        print("KEY: {0}".format(gpgkey))
1000 4
        goargs = [dirs[4], dirs[5], dirs[2], dirs[3]]
1001 4
        restargs = [gpgkey, gpgpass, True]
1002 4
        utilities.cond_check(gpgutils.gpgrunner, goargs, restargs, radios, compressed, deleted)
1003
1004
1005 4
def enn_ayy(quant):
1006
    """
1007
    Cheeky way to put a N/A placeholder for a string.
1008
1009
    :param quant: What to check if it's None.
1010
    :type quant: str
1011
    """
1012 4
    return "N/A" if quant is None else quant
1013
1014
1015 4
def info_header(afile, osver, radio=None, software=None, device=None):
1016
    """
1017
    Write header for info file.
1018
1019
    :param afile: Open file to write to.
1020
    :type afile: File object
1021
1022
    :param osver: OS version, required for both types.
1023
    :type osver: str
1024
1025
    :param radio: Radio version, required for QNX.
1026
    :type radio: str
1027
1028
    :param software: Software release, required for QNX.
1029
    :type software: str
1030
1031
    :param device: Device type, required for Android.
1032
    :type device: str
1033
    """
1034 4
    afile.write("OS: {0}\n".format(osver))
1035 4
    if device:
1036 4
        afile.write("Device: {0}\n".format(enn_ayy(device)))
1037
    else:
1038 4
        afile.write("Radio: {0}\n".format(enn_ayy(radio)))
1039 4
        afile.write("Software: {0}\n".format(enn_ayy(software)))
1040 4
    afile.write("{0}\n".format("~"*40))
1041
1042
1043 4
def prep_info(filepath, osver, device=None):
1044
    """
1045
    Prepare file list for new-style info file.
1046
1047
    :param filepath: Path to folder to analyze.
1048
    :type filepath: str
1049
1050
    :param osver: OS version, required for both types.
1051
    :type osver: str
1052
1053
    :param device: Device type, required for Android.
1054
    :type device: str
1055
    """
1056 4
    fileext = ".zip" if device else ".7z"
1057 4
    files = os.listdir(filepath)
1058 4
    absfiles = [os.path.join(filepath, x) for x in files if x.endswith((fileext, ".exe"))]
1059 4
    fname = os.path.join(filepath, "!{0}_OSINFO!.txt".format(osver))
1060 4
    return fname, absfiles
1061
1062
1063 4
def make_info(filepath, osver, radio=None, software=None, device=None):
1064
    """
1065
    Create a new-style info (names, sizes and hashes) file.
1066
1067
    :param filepath: Path to folder to analyze.
1068
    :type filepath: str
1069
1070
    :param osver: OS version, required for both types.
1071
    :type osver: str
1072
1073
    :param radio: Radio version, required for QNX.
1074
    :type radio: str
1075
1076
    :param software: Software release, required for QNX.
1077
    :type software: str
1078
1079
    :param device: Device type, required for Android.
1080
    :type device: str
1081
    """
1082 4
    fname, absfiles = prep_info(filepath, osver, device)
1083 4
    with open(fname, "w") as afile:
1084 4
        info_header(afile, osver, radio, software, device)
1085 4
        for indx, file in enumerate(absfiles):
1086 4
            write_info(file, indx, len(absfiles), afile)
1087
1088
1089 4
def write_info(infile, index, filecount, outfile):
1090
    """
1091
    Write a new-style info (names, sizes and hashes) file.
1092
1093
    :param infile: Path to file whose name, size and hash are to be written.
1094
    :type infile: str
1095
1096
    :param index: Which file index out of the list of files we're writing.
1097
    :type index: int
1098
1099
    :param filecount: Total number of files we're to write; for excluding terminal newline.
1100
    :type filecount: int
1101
1102
    :param outfile: Open (!!!) file handle. Output file.
1103
    :type outfile: str
1104
    """
1105 4
    fsize = os.stat(infile).st_size
1106 4
    outfile.write("File: {0}\n".format(os.path.basename(infile)))
1107 4
    outfile.write("\tSize: {0} ({1})\n".format(fsize, utilities.fsizer(fsize)))
1108 4
    outfile.write("\tHashes:\n")
1109 4
    outfile.write("\t\tMD5: {0}\n".format(hashutils.hm5(infile).upper()))
1110 4
    outfile.write("\t\tSHA1: {0}\n".format(hashutils.hs1(infile).upper()))
1111 4
    outfile.write("\t\tSHA256: {0}\n".format(hashutils.hs256(infile).upper()))
1112 4
    outfile.write("\t\tSHA512: {0}\n".format(hashutils.hs512(infile).upper()))
1113 4
    if index != filecount - 1:
1114 4
        outfile.write("\n")
1115
1116
1117 4
def bulk_info(dirs, osv, compressed=True, deleted=True, radios=True, rad=None, swv=None, dev=None):
1118
    """
1119
    Generate info files in several folders based on flags.
1120
1121
    :param dirs: Folders: [OS_bars, radio_bars, OS_exes, radio_exes, OS_zips, radio_zips]
1122
    :type dirs: list(str)
1123
1124
    :param osver: OS version, required for both types.
1125
    :type osver: str
1126
1127
    :param compressed: Whether to hash compressed files. True by default.
1128
    :type compressed: bool
1129
1130
    :param deleted: Whether to delete uncompressed files. True by default.
1131
    :type deleted: bool
1132
1133
    :param radios: Whether to hash radio autoloaders. True by default.
1134
    :type radios: bool
1135
1136
    :param rad: Radio version, required for QNX.
1137
    :type rad: str
1138
1139
    :param swv: Software release, required for QNX.
1140
    :type swv: str
1141
1142
    :param dev: Device type, required for Android.
1143
    :type dev: str
1144
    """
1145 4
    print("GENERATING INFO FILES...")
1146 4
    goargs = [dirs[4], dirs[5], dirs[2], dirs[3]]
1147 4
    restargs = [osv, rad, swv, dev]
1148
    utilities.cond_check(make_info, goargs, restargs, radios, compressed, deleted)
1149