Completed
Push — master ( e3adfe...420133 )
by John
04:22
created

kernchecker_dict()   A

Complexity

Conditions 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
dl 0
loc 14
rs 9.4285
c 1
b 0
f 0
1
#!/usr/bin/env python3
2
"""Checks BlackBerry's Android kernel repo for available branches."""
3
4
import sys  # load arguments
5
from bbarchivist import decorators  # enter to exit
6
from bbarchivist import networkutils  # check function
7
from bbarchivist import scriptutils  # default parser
8
from bbarchivist import utilities  # lprint
9
10
__author__ = "Thurask"
11
__license__ = "WTFPL v2"
12
__copyright__ = "2015-2017 Thurask"
13
14
15
def grab_args():
16
    """
17
    Parse arguments from argparse/questionnaire.
18
19
    Invoke :func:`kernchecker.kernchecker_main` with those arguments.
20
    """
21
    parser = scriptutils.default_parser("bb-kernchecker", "Kernel version scraper.")
22
    parser.add_argument(
23
        "-u",
24
        "--utils",
25
        help="Check android-utils repo instead",
26
        action="store_true",
27
        default=False)
28
    args = parser.parse_args(sys.argv[1:])
29
    parser.set_defaults()
30
    kernchecker_main(args.utils)
31
32
33
def kernchecker_prep(kernlist):
34
    """
35
    Prepare output from kernel list.
36
37
    :param kernlist: List of kernel branches.
38
    :type kernlist: list(str)
39
    """
40
    splitkerns = [x.split("/") for x in kernlist]
41
    platforms = list({x[0] for x in splitkerns})
42
    kerndict = kernchecker_dict(splitkerns, platforms)
43
    return kerndict
44
45
46
def kernchecker_dict(splitkerns, platforms):
47
    """
48
    Prepare results dictionary.
49
50
    :param splitkerns: Split kernel branches.
51
    :type splitkerns: list(str)
52
53
    :param platforms: List of platform dicts.
54
    :type platforms: list(dict)
55
    """
56
    kerndict = {x: [] for x in platforms}
57
    for kernel in splitkerns:
58
        kerndict[kernel[0]].append("\t{0}".format(kernel[1]))
59
    return kerndict
60
61
62
def kernchecker_main(utils=False):
63
    """
64
    Wrap around :mod:`bbarchivist.networkutils` kernel checking.
65
66
    :param utils: If we're checking utilities rather than kernels.
67
    :type utils: bool
68
    """
69
    scriptutils.slim_preamble("KERNCHECKER")
70
    tocheck = "UTILS" if utils else "KERNELS"
71
    print("\nCHECKING {0}...\n".format(tocheck))
72
    kernlist = networkutils.kernel_scraper(args.utils)
0 ignored issues
show
Comprehensibility Best Practice introduced by
Undefined variable 'args'
Loading history...
73
    kerndict = kernchecker_prep(kernlist)
74
    for board in kerndict.keys():
75
        print(board)
76
        utilities.lprint(sorted(kerndict[board], reverse=True))
77
    decorators.enter_to_exit(True)
78
79
80
if __name__ == "__main__":
81
    grab_args()
82