Completed
Push — master ( 35a5b7...cfb57c )
by John
03:59
created

valid_method()   A

Complexity

Conditions 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
dl 0
loc 13
ccs 7
cts 7
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
#!/usr/bin/env python3
2 4
"""This module is used for miscellaneous utilities."""
3
4 5
import os  # path work
5 5
import argparse  # argument parser for filters
6 5
import platform  # platform info
7 5
import glob  # cap grabbing
8 5
import hashlib  # base url creation
9 5
import threading  # get thread for spinner
10 5
import time  # spinner delay
11 5
import sys  # streams, version info
12 5
import itertools  # spinners gonna spin
13 5
import subprocess  # loader verification
14 5
from bbarchivist import bbconstants  # cap location, version, filename bits
15 5
from bbarchivist import compat  # backwards compat
16 5
from bbarchivist import dummy  # useless stdout
17 5
from bbarchivist import exceptions  # exceptions
18 5
from bbarchivist import iniconfig  # config parsing
19
20 5
__author__ = "Thurask"
21 5
__license__ = "WTFPL v2"
22 5
__copyright__ = "2015-2017 Thurask"
23
24
25 5
def grab_datafile(datafile):
26
    """
27
    Figure out where a datafile is.
28
29
    :param datafile: Datafile to check.
30
    :type datafile: bbconstants.Datafile
31
    """
32 5
    try:
33 5
        afile = glob.glob(os.path.join(os.getcwd(), datafile.filename))[0]
34 5
    except IndexError:
35 5
        afile = datafile.location if datafile.name == "cfp" else grab_capini(datafile)
36 5
    return os.path.abspath(afile)
37
38
39 5
def grab_capini(datafile):
40
    """
41
    Get cap location from .ini file, and write if it's new.
42
43
    :param datafile: Datafile to check.
44
    :type datafile: bbconstants.Datafile
45
    """
46
    try:
47
        apath = cappath_config_loader()
48
        afile = glob.glob(apath)[0]
49
    except IndexError:
50
        cappath_config_writer(datafile.location)
51
        return bbconstants.CAP.location  # no ini cap
52
    else:
53
        cappath_config_writer(os.path.abspath(afile))
54
        return os.path.abspath(afile)  # ini cap:
55
56
57 5
def grab_cap():
58
    """
59
    Figure out where cap is, local, specified or system-supplied.
60
    """
61 5
    return grab_datafile(bbconstants.CAP)
62
63
64 5
def grab_cfp():
65
    """
66
    Figure out where cfp is, local or system-supplied.
67
    """
68 5
    return grab_datafile(bbconstants.CFP)
69
70
71 5
def new_enough(minver):
72
    """
73
    Check if we're at or above a minimum Python version.
74
75
    :param minver: Minimum Python version (3.minver).
76
    :type minver: int
77
    """
78 5
    return False if minver > sys.version_info[1] else True
79
80
81 5
def dirhandler(directory, defaultdir):
82
    """
83
    If directory is None, turn it into defaultdir.
84
85
    :param directory: Target directory.
86
    :type directory: str
87
88
    :param defaultdir: Default directory.
89
    :type defaultdir: str
90
    """
91 5
    directory = defaultdir if directory is None else directory
92 5
    return directory
93
94
95 5
def fsizer(file_size):
96
    """
97
    Raw byte file size to human-readable string.
98
99
    :param file_size: Number to parse.
100
    :type file_size: float
101
    """
102 5
    fsize = prep_filesize(file_size)
103 5
    for sfix in ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB']:
104 5
        if fsize < 1024.0:
105 5
            size = "{0:3.2f}{1}".format(fsize, sfix)
106 5
            break
107
        else:
108 5
            fsize /= 1024.0
109
    else:
110 5
        size = "{0:3.2f}{1}".format(fsize, 'YB')
111 5
    return size
112
113
114 5
def prep_filesize(file_size):
115
    """
116
    Convert file size to float.
117
118
    :param file_size: Number to parse.
119
    :type file_size: float
120
    """
121 5
    if file_size is None:
122 5
        file_size = 0.0
123 5
    fsize = float(file_size)
124 5
    return fsize
125
126
127 5
def signed_file_args(files):
128
    """
129
    Check if there are between 1 and 6 files supplied to argparse.
130
131
    :param files: List of signed files, between 1 and 6 strings.
132
    :type files: list(str)
133
    """
134 5
    filelist = [file for file in files if file]
135 5
    if not 1 <= len(filelist) <= 6:
136 5
        raise argparse.ArgumentError(argument=None, message="Requires 1-6 signed files")
137 5
    return files
138
139
140 5
def file_exists(file):
141
    """
142
    Check if file exists, raise argparse error if it doesn't.
143
144
    :param file: Path to a file, including extension.
145
    :type file: str
146
    """
147 5
    if not os.path.exists(file):
148 5
        raise argparse.ArgumentError(argument=None, message="{0} not found.".format(file))
149 5
    return file
150
151
152 5
def positive_integer(input_int):
153
    """
154
    Check if number > 0, raise argparse error if it isn't.
155
156
    :param input_int: Integer to check.
157
    :type input_int: str
158
    """
159 5
    if int(input_int) <= 0:
160 5
        info = "{0} is not >=0.".format(str(input_int))
161 5
        raise argparse.ArgumentError(argument=None, message=info)
162 5
    return int(input_int)
163
164
165 5
def valid_method_poptxz(methodlist):
166
    """
167
    Remove .tar.xz support if system is too old.
168
169
    :param methodlist: List of all methods.
170
    :type methodlist: tuple(str)
171
    """
172 5
    if not new_enough(3):
173 5
        methodlist = [x for x in bbconstants.METHODS if x != "txz"]
174 5
    return methodlist
175
176
177 5
def valid_method(method):
178
    """
179
    Check if compression method is valid, raise argparse error if it isn't.
180
181
    :param method: Compression method to check.
182
    :type method: str
183
    """
184 5
    methodlist = bbconstants.METHODS
185 5
    methodlist = valid_method_poptxz(methodlist)
186 5
    if method not in methodlist:
187 5
        info = "Invalid method {0}.".format(method)
188 5
        raise argparse.ArgumentError(argument=None, message=info)
189 5
    return method
190
191
192 5
def valid_carrier(mcc_mnc):
193
    """
194
    Check if MCC/MNC is valid (1-3 chars), raise argparse error if it isn't.
195
196
    :param mcc_mnc: MCC/MNC to check.
197
    :type mcc_mnc: str
198
    """
199 5
    if not str(mcc_mnc).isdecimal():
200 5
        infod = "Non-integer {0}.".format(str(mcc_mnc))
201 5
        raise argparse.ArgumentError(argument=None, message=infod)
202 5
    if len(str(mcc_mnc)) > 3 or len(str(mcc_mnc)) == 0:
203 5
        infol = "{0} is invalid.".format(str(mcc_mnc))
204 5
        raise argparse.ArgumentError(argument=None, message=infol)
205
    else:
206 5
        return mcc_mnc
207
208
209 5
def escreens_pin(pin):
210
    """
211
    Check if given PIN is valid, raise argparse error if it isn't.
212
213
    :param pin: PIN to check.
214
    :type pin: str
215
    """
216 5
    if len(pin) == 8:
217 5
        try:
218 5
            int(pin, 16)  # hexadecimal-ness
219 5
        except ValueError:
220 5
            raise argparse.ArgumentError(argument=None, message="Invalid PIN.")
221
        else:
222 5
            return pin.lower()
223
    else:
224 5
        raise argparse.ArgumentError(argument=None, message="Invalid PIN.")
225
226
227 5
def escreens_duration(duration):
228
    """
229
    Check if Engineering Screens duration is valid.
230
231
    :param duration: Duration to check.
232
    :type duration: int
233
    """
234 5
    if int(duration) in (1, 3, 6, 15, 30):
235 5
        return int(duration)
236
    else:
237 5
        raise argparse.ArgumentError(argument=None, message="Invalid duration.")
238
239
240 5
def droidlookup_hashtype(method):
241
    """
242
    Check if Android autoloader lookup hash type is valid.
243
244
    :param method: None for regular OS links, "sha256/512" for SHA256 or 512 hash.
245
    :type method: str
246
    """
247 5
    if method.lower() in ("sha512", "sha256"):
248 5
        return method.lower()
249
    else:
250 5
        raise argparse.ArgumentError(argument=None, message="Invalid type.")
251
252
253 5
def droidlookup_devicetype(device):
254
    """
255
    Check if Android autoloader device type is valid.
256
257
    :param device: Android autoloader types to check.
258
    :type device: str
259
    """
260 5
    devices = ("Priv", "DTEK50", "DTEK60", "KEYone", "Aurora")
261 5
    if device is None:
262 5
        return None
263
    else:
264 5
        for dev in devices:
265 5
            if dev.lower() == device.lower():
266 5
                return dev
267 5
        raise argparse.ArgumentError(argument=None, message="Invalid device.")
268
269
270 5
def s2b(input_check):
271
    """
272
    Return Boolean interpretation of string input.
273
274
    :param input_check: String to check if it means True or False.
275
    :type input_check: str
276
    """
277 5
    return str(input_check).lower() in ("yes", "true", "t", "1", "y")
278
279
280 5
def is_amd64():
281
    """
282
    Check if script is running on an AMD64 system (Python can be 32/64, this is for subprocess)
283
    """
284 5
    return platform.machine().endswith("64")
285
286
287 5
def is_windows():
288
    """
289
    Check if script is running on Windows.
290
    """
291 5
    return platform.system() == "Windows"
292
293
294 5
def talkaprint(msg, talkative=False):
295
    """
296
    Print only if asked to.
297
298
    :param msg: Message to print.
299
    :type msg: str
300
301
    :param talkative: Whether to output to screen. False by default.
302
    :type talkative: bool
303
    """
304 5
    if talkative:
305 5
        print(msg)
306
307
308 5
def get_seven_zip(talkative=False):
309
    """
310
    Return name of 7-Zip executable.
311
    On POSIX, it MUST be 7za.
312
    On Windows, it can be installed or supplied with the script.
313
    :func:`win_seven_zip` is used to determine if it's installed.
314
315
    :param talkative: Whether to output to screen. False by default.
316
    :type talkative: bool
317
    """
318 5
    return win_seven_zip(talkative) if is_windows() else "7za"
319
320
321 5
def win_seven_zip(talkative=False):
322
    """
323
    For Windows, check where 7-Zip is ("where", pretty much).
324
    Consult registry first for any installed instances of 7-Zip.
325
326
    :param talkative: Whether to output to screen. False by default.
327
    :type talkative: bool
328
    """
329
    talkaprint("CHECKING INSTALLED FILES...", talkative)
330
    try:
331
        path = wsz_registry()
332
    except OSError as exc:
333
        if talkative:
334
            exceptions.handle_exception(exc, xit=None)
335
        talkaprint("TRYING LOCAL FILES...", talkative)
336
        return win_seven_zip_local(talkative)
337
    else:
338
        talkaprint("7ZIP USING INSTALLED FILES", talkative)
339
        return '"{0}"'.format(os.path.join(path[0], "7z.exe"))
340
341
342 5
def wsz_registry():
343
    """
344
    Check Windows registry for 7-Zip executable location.
345
    """
346
    import winreg  # windows registry
347
    hk7z = winreg.OpenKey(winreg.HKEY_CURRENT_USER, "Software\\7-Zip")
348
    path = winreg.QueryValueEx(hk7z, "Path")
349
    return path
350
351
352 5
def win_seven_zip_local(talkative=False):
353
    """
354
    If 7-Zip isn't in the registry, fall back onto supplied executables.
355
    If *those* aren't there, return "error".
356
357
    :param talkative: Whether to output to screen. False by default.
358
    :type talkative: bool
359
    """
360
    filecount = wsz_filecount()
361
    if filecount == 2:
362
        szexe = wsz_local_good(talkative)
363
    else:
364
        szexe = wsz_local_bad(talkative)
365
    return szexe
366
367
368 5
def wsz_filecount():
369
    """
370
    Get count of 7-Zip executables in local folder.
371
    """
372
    filecount = len([x for x in os.listdir(os.getcwd()) if x in ["7za.exe", "7z.exe"]])
373
    return filecount
374
375
376 5
def wsz_local_good(talkative=False):
377
    """
378
    Get 7-Zip exe name if everything is good.
379
380
    :param talkative: Whether to output to screen. False by default.
381
    :type talkative: bool
382
    """
383
    talkaprint("7ZIP USING LOCAL FILES", talkative)
384
    szexe = "7za.64.exe" if is_amd64() else "7za.exe"
385
    return szexe
386
387
388 5
def wsz_local_bad(talkative=False):
389
    """
390
    Handle 7-Zip exe name in case of issues.
391
392
    :param talkative: Whether to output to screen. False by default.
393
    :type talkative: bool
394
    """
395
    talkaprint("NO LOCAL FILES", talkative)
396
    szexe = "error"
397
    return szexe
398
399
400 5
def get_core_count():
401
    """
402
    Find out how many CPU cores this system has.
403
    """
404 5
    try:
405 5
        cores = str(compat.enum_cpus())  # 3.4 and up
406
    except NotImplementedError:
407
        cores = "1"  # 3.2-3.3
408
    else:
409 5
        if compat.enum_cpus() is None:
410
            cores = "1"
411 5
    return cores
412
413
414 5
def prep_seven_zip_path(path, talkative=False):
415
    """
416
    Print p7zip path on POSIX, or notify if not there.
417
418
    :param path: Path to use.
419
    :type path: str
420
421
    :param talkative: Whether to output to screen. False by default.
422
    :type talkative: bool
423
    """
424 5
    if path is None:
425 5
        talkaprint("NO 7ZIP\nPLEASE INSTALL p7zip", talkative)
426 5
        return False
427
    else:
428 5
        talkaprint("7ZIP FOUND AT {0}".format(path), talkative)
429 5
        return True
430
431
432 5
def prep_seven_zip_posix(talkative=False):
433
    """
434
    Check for p7zip on POSIX.
435
436
    :param talkative: Whether to output to screen. False by default.
437
    :type talkative: bool
438
    """
439 5
    try:
440 5
        path = compat.where_which("7za")
441 5
    except ImportError:
442 5
        talkaprint("PLEASE INSTALL SHUTILWHICH WITH PIP", talkative)
443 5
        return False
444
    else:
445 5
        return prep_seven_zip_path(path, talkative)
446
447
448 5
def prep_seven_zip(talkative=False):
449
    """
450
    Check for presence of 7-Zip.
451
    On POSIX, check for p7zip.
452
    On Windows, check for 7-Zip.
453
454
    :param talkative: Whether to output to screen. False by default.
455
    :type talkative: bool
456
    """
457 5
    if is_windows():
458
        return get_seven_zip(talkative) != "error"
459
    else:
460 5
        return prep_seven_zip_posix(talkative)
461
462
463 5
def increment(version, inc=3):
464
    """
465
    Increment version by given number. For repeated lookups.
466
467
    :param version: w.x.y.ZZZZ, becomes w.x.y.(ZZZZ + increment).
468
    :type version: str
469
470
    :param inc: What to increment by. Default is 3.
471
    :type inc: str
472
    """
473 5
    splitos = version.split(".")
474 5
    splitos[3] = int(splitos[3])
475 5
    if splitos[3] > 9996:  # prevent overflow
476 5
        splitos[3] = 0
477 5
    splitos[3] += int(inc)
478 5
    splitos[3] = str(splitos[3])
479 5
    return ".".join(splitos)
480
481
482 5
def stripper(name):
483
    """
484
    Strip fluff from bar filename.
485
486
    :param name: Bar filename, must contain '-nto+armle-v7+signed.bar'.
487
    :type name: str
488
    """
489 5
    return name.replace("-nto+armle-v7+signed.bar", "")
490
491
492 5
def create_base_url(softwareversion):
493
    """
494
    Make the root URL for production server files.
495
496
    :param softwareversion: Software version to hash.
497
    :type softwareversion: str
498
    """
499
    # Hash software version
500 5
    swhash = hashlib.sha1(softwareversion.encode('utf-8'))
501 5
    hashedsoftwareversion = swhash.hexdigest()
502
    # Root of all urls
503 5
    baseurl = "http://cdn.fs.sl.blackberry.com/fs/qnx/production/{0}".format(hashedsoftwareversion)
504 5
    return baseurl
505
506
507 5
def format_app_name(appname):
508
    """
509
    Convert long reverse DNS name to short name.
510
511
    :param appname: Application name (ex. sys.pim.calendar -> "calendar")
512
    :type appname: str
513
    """
514 5
    final = appname.split(".")[-1]
515 5
    return final
516
517
518 5
def create_bar_url(softwareversion, appname, appversion, clean=False):
519
    """
520
    Make the URL for any production server file.
521
522
    :param softwareversion: Software version to hash.
523
    :type softwareversion: str
524
525
    :param appname: Application name, preferably like on server.
526
    :type appname: str
527
528
    :param appversion: Application version.
529
    :type appversion: str
530
531
    :param clean: Whether or not to clean up app name. Default is False.
532
    :type clean: bool
533
    """
534 5
    baseurl = create_base_url(softwareversion)
535 5
    if clean:
536 5
        appname = format_app_name(appname)
537 5
    return "{0}/{1}-{2}-nto+armle-v7+signed.bar".format(baseurl, appname, appversion)
538
539
540 5
def generate_urls(softwareversion, osversion, radioversion, core=False):
541
    """
542
    Generate a list of OS URLs and a list of radio URLs based on input.
543
544
    :param softwareversion: Software version to hash.
545
    :type softwareversion: str
546
547
    :param osversion: OS version.
548
    :type osversion: str
549
550
    :param radioversion: Radio version.
551
    :type radioversion: str
552
553
    :param core: Whether or not to return core URLs as well.
554
    :type core: bool
555
    """
556 5
    osurls = [
557
        create_bar_url(softwareversion, "winchester.factory_sfi.desktop", osversion),
558
        create_bar_url(softwareversion, "qc8960.factory_sfi.desktop", osversion),
559
        create_bar_url(softwareversion, "qc8960.factory_sfi.desktop", osversion),
560
        create_bar_url(softwareversion, "qc8974.factory_sfi.desktop", osversion)
561
    ]
562 5
    radiourls = [
563
        create_bar_url(softwareversion, "m5730", radioversion),
564
        create_bar_url(softwareversion, "qc8960", radioversion),
565
        create_bar_url(softwareversion, "qc8960.omadm", radioversion),
566
        create_bar_url(softwareversion, "qc8960.wtr", radioversion),
567
        create_bar_url(softwareversion, "qc8960.wtr5", radioversion),
568
        create_bar_url(softwareversion, "qc8930.wtr5", radioversion),
569
        create_bar_url(softwareversion, "qc8974.wtr2", radioversion)
570
    ]
571 5
    coreurls = []
572 5
    osurls, radiourls = filter_urls(osurls, radiourls, osversion)
573 5
    if core:
574 5
        coreurls = [x.replace(".desktop", "") for x in osurls]
575 5
    return osurls, radiourls, coreurls
576
577
578 5
def newer_103(splitos, third):
579
    """
580
    Return True if given split OS version is 10.3.X or newer.
581
582
    :param splitos: OS version, split on the dots: [10, 3, 3, 2205]
583
    :type: list(int)
584
585
    :param third: The X in 10.3.X.
586
    :type third: int
587
    """
588 5
    newer = True if ((splitos[1] >= 4) or (splitos[1] == 3 and splitos[2] >= third)) else False
589 5
    return newer
590
591
592 5
def filter_urls(osurls, radiourls, osversion):
593
    """
594
    Filter lists of OS and radio URLs.
595
596
    :param osurls: List of OS URLs.
597
    :type osurls: list(str)
598
599
    :param radiourls: List of radio URLs.
600
    :type radiourls: list(str)
601
602
    :param osversion: OS version.
603
    :type osversion: str
604
    """
605 5
    splitos = [int(i) for i in osversion.split(".")]
606 5
    osurls[2] = filter_1031(osurls[2], splitos, 5)  # Z3 10.3.1+
607 5
    osurls[3] = filter_1031(osurls[3], splitos, 6)  # Passport 10.3.1+
608 5
    osurls[0], radiourls[0] = pop_stl1(osurls[0], radiourls[0], splitos)  # STL100-1 10.3.3+
609 5
    return osurls, radiourls
610
611
612 5
def filter_1031(osurl, splitos, device):
613
    """
614
    Modify URLs to reflect changes in 10.3.1.
615
616
    :param osurl: OS URL to modify.
617
    :type osurl: str
618
619
    :param splitos: OS version, split and cast to int: [10, 3, 2, 2876]
620
    :type splitos: list(int)
621
622
    :param device: Device to use.
623
    :type device: int
624
    """
625 5
    if newer_103(splitos, 1):
626 5
        filterdict = {5: ("qc8960.factory_sfi", "qc8960.factory_sfi_hybrid_qc8x30"), 6: ("qc8974.factory_sfi", "qc8960.factory_sfi_hybrid_qc8974")}
627 5
        osurl = filter_osversion(osurl, device, filterdict)
628 5
    return osurl
629
630
631 5
def pop_stl1(osurl, radiourl, splitos):
632
    """
633
    Replace STL100-1 links in 10.3.3+.
634
635
    :param osurl: OS URL to modify.
636
    :type osurl: str
637
638
    :param radiourl: Radio URL to modify.
639
    :type radiourl: str
640
641
    :param splitos: OS version, split and cast to int: [10, 3, 3, 2205]
642
    :type splitos: list(int)
643
    """
644 5
    if newer_103(splitos, 3):
645 5
        osurl = osurl.replace("winchester", "qc8960")  # duplicates get filtered out later
646 5
        radiourl = radiourl.replace("m5730", "qc8960")
647 5
    return osurl, radiourl
648
649
650 5
def filter_osversion(osurl, device, filterdict):
651
    """
652
    Modify URLs based on device index and dictionary of changes.
653
654
    :param osurl: OS URL to modify.
655
    :type osurl: str
656
657
    :param device: Device to use.
658
    :type device: int
659
660
    :param filterdict: Dictionary of changes: {device : (before, after)}
661
    :type filterdict: dict(int:(str, str))
662
    """
663 5
    if device in filterdict.keys():
664 5
        osurl = osurl.replace(filterdict[device][0], filterdict[device][1])
665 5
    return osurl
666
667
668 5
def generate_lazy_urls(softwareversion, osversion, radioversion, device):
669
    """
670
    Generate a pair of OS/radio URLs based on input.
671
672
    :param softwareversion: Software version to hash.
673
    :type softwareversion: str
674
675
    :param osversion: OS version.
676
    :type osversion: str
677
678
    :param radioversion: Radio version.
679
    :type radioversion: str
680
681
    :param device: Device to use.
682
    :type device: int
683
    """
684 5
    splitos = [int(i) for i in osversion.split(".")]
685 5
    rads = ["m5730", "qc8960", "qc8960.omadm", "qc8960.wtr",
686
            "qc8960.wtr5", "qc8930.wtr4", "qc8974.wtr2"]
687 5
    oses = ["winchester.factory", "qc8960.factory", "qc8960.verizon",
688
            "qc8974.factory"]
689 5
    maps = {0:0, 1:1, 2:2, 3:1, 4:1, 5:1, 6:3}
690 5
    osurl = create_bar_url(softwareversion, "{0}_sfi.desktop".format(oses[maps[device]]), osversion)
691 5
    radiourl = create_bar_url(softwareversion, rads[device], radioversion)
692 5
    osurl = filter_1031(osurl, splitos, device)
693 5
    return osurl, radiourl
694
695
696 5
def bulk_urls(softwareversion, osversion, radioversion, core=False, altsw=None):
697
    """
698
    Generate all URLs, plus extra Verizon URLs.
699
700
    :param softwareversion: Software version to hash.
701
    :type softwareversion: str
702
703
    :param osversion: OS version.
704
    :type osversion: str
705
706
    :param radioversion: Radio version.
707
    :type radioversion: str
708
709
    :param device: Device to use.
710
    :type device: int
711
712
    :param core: Whether or not to return core URLs as well.
713
    :type core: bool
714
715
    :param altsw: Radio software release, if not the same as OS.
716
    :type altsw: str
717
    """
718 5
    baseurl = create_base_url(softwareversion)
719 5
    osurls, radurls, coreurls = generate_urls(softwareversion, osversion, radioversion, core)
720 5
    vzwos, vzwrad = generate_lazy_urls(softwareversion, osversion, radioversion, 2)
721 5
    osurls.append(vzwos)
722 5
    radurls.append(vzwrad)
723 5
    vzwcore = vzwos.replace("sfi.desktop", "sfi")
724 5
    if core:
725 5
        coreurls.append(vzwcore)
726 5
    osurls = list(set(osurls))  # pop duplicates
727 5
    radurls = list(set(radurls))
728 5
    if core:
729 5
        coreurls = list(set(coreurls))
730 5
    radurls = bulk_urls_altsw(radurls, baseurl, altsw)
731 5
    return osurls, coreurls, radurls
732
733
734 5
def bulk_urls_altsw(radurls, baseurl, altsw=None):
735
    """
736
    Handle alternate software release for radio.
737
738
    :param radurls: List of radio URLs.
739
    :type radurls: list(str)
740
741
    :param baseurl: Base URL (from http to hashed SW release).
742
    :type baseurl: str
743
744
    :param altsw: Radio software release, if not the same as OS.
745
    :type altsw: str
746
    """
747 5
    if altsw is not None:
748 5
        altbase = create_base_url(altsw)
749 5
        radiourls2 = [rad.replace(baseurl, altbase) for rad in radurls]
750 5
        radurls = radiourls2
751 5
        del radiourls2
752 5
    return radurls
753
754
755 5
def line_begin():
756
    """
757
    Go to beginning of line, to overwrite whatever's there.
758
    """
759 5
    sys.stdout.write("\r")
760 5
    sys.stdout.flush()
761
762
763 5
def spinner_clear():
764
    """
765
    Get rid of any spinner residue left in stdout.
766
    """
767 5
    sys.stdout.write("\b \b")
768 5
    sys.stdout.flush()
769
770
771 5
class Spinner(object):
772
    """
773
    A basic spinner using itertools. No need for progress.
774
    """
775
776 5
    def __init__(self):
777 5
        self.wheel = itertools.cycle(['-', '/', '|', '\\'])
778 5
        self.file = dummy.UselessStdout()
779
780 5
    def after(self):
781
        """
782
        Iterate over itertools.cycle, write to file.
783
        """
784 5
        try:
785 5
            self.file.write(next(self.wheel))
786 5
            self.file.flush()
787 5
            self.file.write("\b\r")
788 5
            self.file.flush()
789
        except (KeyboardInterrupt, SystemExit):
790
            self.stop()
791
792 5
    def stop(self):
793
        """
794
        Kill output.
795
        """
796 5
        self.file = dummy.UselessStdout()
797
798
799 5
class SpinManager(object):
800
    """
801
    Wraps around the itertools spinner, runs it in another thread.
802
    """
803
804 5
    def __init__(self):
805 5
        spinner = Spinner()
806 5
        self.spinner = spinner
807 5
        self.thread = threading.Thread(target=self.loop, args=())
808 5
        self.thread.daemon = True
809 5
        self.scanning = False
810 5
        self.spinner.file = dummy.UselessStdout()
811
812 5
    def start(self):
813
        """
814
        Begin the spinner.
815
        """
816 5
        self.spinner.file = sys.stderr
817 5
        self.scanning = True
818 5
        self.thread.start()
819
820 5
    def loop(self):
821
        """
822
        Spin if scanning, clean up if not.
823
        """
824 5
        while self.scanning:
825 5
            time.sleep(0.5)
826 5
            try:
827 5
                line_begin()
828 5
                self.spinner.after()
829
            except (KeyboardInterrupt, SystemExit):
830
                self.scanning = False
831
                self.stop()
832
833 5
    def stop(self):
834
        """
835
        Stop the spinner.
836
        """
837 5
        self.spinner.stop()
838 5
        self.scanning = False
839 5
        spinner_clear()
840 5
        line_begin()
841 5
        if not is_windows():
842 5
            print("\n")
843
844
845 5
def return_and_delete(target):
846
    """
847
    Read text file, then delete it. Return contents.
848
849
    :param target: Text file to read.
850
    :type target: str
851
    """
852 5
    with open(target, "r") as thefile:
853 5
        content = thefile.read()
854 5
    os.remove(target)
855 5
    return content
856
857
858 5
def verify_loader_integrity(loaderfile):
859
    """
860
    Test for created loader integrity. Windows-only.
861
862
    :param loaderfile: Path to loader.
863
    :type loaderfile: str
864
    """
865 5
    if not is_windows():
866 5
        pass
867
    else:
868 5
        excode = None
869 5
        try:
870 5
            with open(os.devnull, 'rb') as dnull:
871 5
                cmd = "{0} fileinfo".format(loaderfile)
872 5
                excode = subprocess.call(cmd, stdout=dnull, stderr=subprocess.STDOUT)
873 5
        except OSError:
874 5
            excode = -1
875 5
        return excode == 0  # 0 if OK, non-zero if something broke
876
877
878 5
def bulkfilter_printer(afile):
879
    """
880
    Print filename and verify a loader file.
881
882
    :param afile: Path to file.
883
    :type afile: str
884
    """
885 5
    print("TESTING: {0}".format(os.path.basename(afile)))
886 5
    if not verify_loader_integrity(afile):
887 5
        return os.path.basename(afile)
888
889
890 5
def bulkfilter(files):
891
    """
892
    Verify all loader files in a given list.
893
894
    :param files: List of files.
895
    :type files: list(str)
896
    """
897 5
    brokens = [bulkfilter_printer(file) for file in files if prepends(os.path.basename(file), bbconstants.PREFIXES, ".exe")]
898 5
    return brokens
899
900
901 5
def verify_bulk_loaders(ldir):
902
    """
903
    Run :func:`verify_loader_integrity` for all files in a dir.
904
905
    :param ldir: Directory to use.
906
    :type ldir: str
907
    """
908 5
    if not is_windows():
909 5
        pass
910
    else:
911 5
        files = verify_bulk_loaders_filefilter(ldir)
912 5
        brokens = verify_bulk_loaders_brokens(files)
913 5
        return brokens
914
915
916 5
def verify_bulk_loaders_filefilter(ldir):
917
    """
918
    Prepare file names for :func:`verify_bulk_loaders`.
919
920
    :param ldir: Directory to use.
921
    :type ldir: str
922
    """
923 5
    files = [os.path.join(ldir, file) for file in os.listdir(ldir) if not os.path.isdir(file)]
924 5
    return files
925
926
927 5
def verify_bulk_loaders_brokens(files):
928
    """
929
    Prepare filtered file list for :func:`verify_bulk_loaders`.
930
931
    :param files: List of files.
932
    :type files: list(str)
933
    """
934 5
    brokens = [file for file in bulkfilter(files) if file]
935 5
    return brokens
936
937
938 5
def list_workers(input_data, workerlimit):
939
    """
940
    Count number of threads, either length of iterable or provided limit.
941
942
    :param input_data: Input data, some iterable.
943
    :type input_data: list
944
945
    :param workerlimit: Maximum number of workers.
946
    :type workerlimit: int
947
    """
948 5
    runners = len(input_data) if len(input_data) < workerlimit else workerlimit
949 5
    return runners
950
951
952 5
def cpu_workers(input_data):
953
    """
954
    Count number of CPU workers, smaller of number of threads and length of data.
955
956
    :param input_data: Input data, some iterable.
957
    :type input_data: list
958
    """
959 5
    return list_workers(input_data, compat.enum_cpus())
960
961
962 5
def prep_logfile():
963
    """
964
    Prepare log file, labeling it with current date. Select folder based on frozen status.
965
    """
966 5
    logfile = "{0}.txt".format(time.strftime("%Y_%m_%d_%H%M%S"))
967 5
    root = os.getcwd() if getattr(sys, 'frozen', False) else os.path.expanduser("~")
968 5
    basefolder = os.path.join(root, "lookuplogs")
969 5
    os.makedirs(basefolder, exist_ok=True)
970 5
    record = os.path.join(basefolder, logfile)
971 5
    open(record, "w").close()
972 5
    return record
973
974
975 5
def prepends(file, pre, suf):
976
    """
977
    Check if filename starts with/ends with stuff.
978
979
    :param file: File to check.
980
    :type file: str
981
982
    :param pre: Prefix(es) to check.
983
    :type pre: str or list or tuple
984
985
    :param suf: Suffix(es) to check.
986
    :type suf: str or list or tuple
987
    """
988 5
    return file.startswith(pre) and file.endswith(suf)
989
990
991 5
def lprint(iterable):
992
    """
993
    A oneliner for 'for item in x: print item'.
994
995
    :param iterable: Iterable to print.
996
    :type iterable: list/tuple
997
    """
998 5
    for item in iterable:
999 5
        print(item)
1000
1001
1002 5
def cappath_config_loader(homepath=None):
1003
    """
1004
    Read a ConfigParser file to get cap preferences.
1005
1006
    :param homepath: Folder containing ini file. Default is user directory.
1007
    :type homepath: str
1008
    """
1009 5
    capini = iniconfig.generic_loader('cappath', homepath)
1010 5
    cappath = capini.get('path', fallback=bbconstants.CAP.location)
1011 5
    return cappath
1012
1013
1014 5
def cappath_config_writer(cappath=None, homepath=None):
1015
    """
1016
    Write a ConfigParser file to store cap preferences.
1017
1018
    :param cappath: Method to use.
1019
    :type cappath: str
1020
1021
    :param homepath: Folder containing ini file. Default is user directory.
1022
    :type homepath: str
1023
    """
1024 5
    cappath = grab_cap() if cappath is None else cappath
1025 5
    results = {"path": cappath}
1026 5
    iniconfig.generic_writer("cappath", results, homepath)
1027
1028
1029 5
def one_and_none(first, second):
1030
    """
1031
    Check if one element in a pair is None and one isn't.
1032
1033
    :param first: To return True, this must be None.
1034
    :type first: str
1035
1036
    :param second: To return True, this mustbe false.
1037
    :type second: str
1038
    """
1039 5
    sentinel = True if first is None and second is not None else False
1040 5
    return sentinel
1041
1042
1043 5
def def_args(dirs):
1044
    """
1045
    Return prepared argument list for most instances of :func:`cond_check:.
1046
1047
    :param dirs: List of directories.
1048
    :type dirs: list(str)
1049
    """
1050 5
    return [dirs[4], dirs[5], dirs[2], dirs[3]]
1051
1052
1053 5
def cond_do(dofunc, goargs, restargs=None, condition=True):
1054
    """
1055
    Do a function, check a condition, then do same function but swap first argument.
1056
1057
    :param dofunc: Function to do.
1058
    :type dofunc: function
1059
1060
    :param goargs: List of variable arguments.
1061
    :type goargs: list(str)
1062
1063
    :param restargs: Rest of arguments, which are constant.
1064
    :type restargs: list(str)
1065
1066
    :param condition: Condition to check in order to use secondarg.
1067
    :type condition: bool
1068
    """
1069 5
    restargs = [] if restargs is None else restargs
1070 5
    dofunc(goargs[0], *restargs)
1071 5
    if condition:
1072 5
        dofunc(goargs[1], *restargs)
1073
1074
1075 5
def cond_check(dofunc, goargs, restargs=None, condition=True, checkif=True, checkifnot=True):
1076
    """
1077
    Do :func:`cond_do` based on a condition, then do it again based on a second condition.
1078
1079
    :param dofunc: Function to do.
1080
    :type dofunc: function
1081
1082
    :param goargs: List of variable arguments.
1083
    :type goargs: list(str)
1084
1085
    :param restargs: Rest of arguments, which are constant.
1086
    :type restargs: list(str)
1087
1088
    :param condition: Condition to check in order to use secondarg.
1089
    :type condition: bool
1090
1091
    :param checkif: Do :func:`cond_do` if this is True.
1092
    :type checkif: bool
1093
1094
    :param checkifnot: Do :func:`cond_do` if this is False.
1095
    :type checkifnot: bool
1096
    """
1097 5
    if checkif:
1098 5
        cond_do(dofunc, goargs[0:2], restargs, condition)
1099 5
    if not checkifnot:
1100
        cond_do(dofunc, goargs[2:4], restargs, condition)
1101