Completed
Push — master ( fe128a...71463b )
by John
01:12
created

swlookup_main()   B

Complexity

Conditions 4

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 4
c 1
b 0
f 1
dl 0
loc 28
rs 8.5806
1
#!/usr/bin/env python3
2
"""Check BB10 software releases."""
3
4
import sys  # load arguments
5
from bbarchivist import networkutils  # lookup
6
from bbarchivist import decorators  # Ctrl+C wrapping
7
from bbarchivist import utilities  # argument filters
8
from bbarchivist import scriptutils  # default parser
9
10
__author__ = "Thurask"
11
__license__ = "WTFPL v2"
12
__copyright__ = "Copyright 2016 Thurask"
13
14
15
def grab_args():
16
    """
17
    Parse arguments from argparse/questionnaire.
18
    """
19
    if len(sys.argv) > 1:
20
        parser = scriptutils.default_parser("bb-swlookup", "Check software releases")
21
        parser.add_argument("sw", help="Software version, 10.x.y.zzzz")
22
        parser.add_argument(
23
            "-l", "--loop",
24
            dest="recurse",
25
            help="Loop lookup, CTRL-C to quit",
26
            action="store_true",
27
            default=False)
28
        parser.add_argument(
29
            "-c", "--ceiling",
30
            dest="ceiling",
31
            help="When to stop script, default = 9999",
32
            default=9999,
33
            type=int,
34
            choices=range(1, 10000),
35
            metavar="INT")
36
        args = parser.parse_args(sys.argv[1:])
37
        parser.set_defaults()
38
        swlookup_main(
39
            args.sw,
40
            args.recurse,
41
            args.ceiling)
42
    else:
43
        swrel = input("SOFTWARE RELEASE: ")
44
        recurse = utilities.s2b(input("LOOP (Y/N)?: "))
45
        if recurse:
46
            print("Press Ctrl+C to stop loop")
47
        print(" ")
48
        swlookup_main(
49
            swrel,
50
            recurse,
51
            9999)
52
        decorators.enter_to_exit(True)
53
54
55
def terminator(swversion, ceiling, loop=None):
56
    """
57
    Handle KeyboardInterrupt calling.
58
59
    :param swversion: Software version, 10.x.y.zzzz.
60
    :type swversion: str
61
62
    :param loop: Whether or not to automatically lookup. Default is None.
63
    :type loop: bool
64
65
    :param ceiling: When to stop loop. Default is 9999 (i.e. 10.x.y.9999).
66
    :type ceiling: int
67
    """
68
    goloop = True if loop is None else loop
69
    if goloop and int(swversion.split(".")[3]) > ceiling:
70
        raise KeyboardInterrupt
71
72
73
@decorators.wrap_keyboard_except
74
def swlookup_main(swversion, loop=False, ceiling=9999):
75
    """
76
    Check if a software release exists.
77
78
    :param swversion: Software version, 10.x.y.zzzz.
79
    :type swversion: str
80
81
    :param loop: Whether or not to automatically lookup. Default is false.
82
    :type loop: bool
83
84
    :param ceiling: When to stop loop. Default is 9999 (i.e. 10.x.y.9999).
85
    :type ceiling: int
86
    """
87
    scriptutils.slim_preamble("SWLOOKUP")
88
    while True:
89
        terminator(swversion, ceiling, loop)
90
        print("NOW SCANNING: {0}".format(swversion), end="\r")
91
        baseurl = networkutils.create_base_url(swversion)
92
        avail = networkutils.availability(baseurl)
93
        if avail:
94
            print("SW {0} AVAILABLE!".format(swversion))
95
        if not loop:
96
            raise KeyboardInterrupt  # hack, but whatever
97
        else:
98
            terminator(swversion, ceiling)
99
            swversion = utilities.increment(swversion, 1)
100
            continue
101
102
103
if __name__ == "__main__":
104
    grab_args()
105