Passed
Push — master ( 816317...cc1553 )
by John
03:01
created

bbarchivist.scripts.swlookup.grab_args()   A

Complexity

Conditions 3

Size

Total Lines 38
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

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