build_rule_playbooks.parse_args()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 59
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 30
nop 0
dl 0
loc 59
ccs 0
cts 12
cp 0
crap 2
rs 9.16
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
#!/usr/bin/python3
2
3
import argparse
4
import ssg.playbook_builder
5
import os
6
7
8
def parse_args():
9
    p = argparse.ArgumentParser()
10
    p.add_argument(
11
        "--input-dir",
12
        help="Input directory that contains all Ansible remediations "
13
        "snippets for the product we are building. "
14
        "e.g. ~/scap-security-guide/build/fedora/fixes/ansible. "
15
        "If --input-dir is not specified, it is derived from --ssg-root."
16
    )
17
    p.add_argument(
18
        "--output-dir",
19
        help="Output directory to which the rule-based Ansible playbooks "
20
        "will be generated. "
21
        "e.g. ~/scap-security-guide/build/fedora/playbooks. "
22
        "If --output-dir is not specified, it is derived from --ssg-root."
23
    )
24
    p.add_argument(
25
        "--resolved-rules-dir",
26
        help="Directory that contains preprocessed rules in YAML format "
27
        "eg. ~/scap-security-guide/build/fedora/rules. "
28
        "If --resolved-rules-dir is not specified, it is derived from "
29
        "--ssg-root."
30
    )
31
    p.add_argument(
32
        "--resolved-profiles-dir",
33
        help="Directory that contains preprocessed profiles in YAML format "
34
        "eg. ~/scap-security-guide/build/fedora/profiles. "
35
        "If --resolved-profiles-dir is not specified, it is derived from "
36
        "--ssg-root."
37
    )
38
    p.add_argument(
39
        "--ssg-root", required=True,
40
        help="Directory containing the source tree. "
41
        "e.g. ~/scap-security-guide/"
42
    )
43
    p.add_argument(
44
        "--product", required=True,
45
        help="ID of the product for which we are building Playbooks. "
46
        "e.g.: 'fedora'"
47
    )
48
    p.add_argument(
49
        "--build-config-yaml", required=True,
50
        help="YAML file with information about the build configuration. "
51
        "e.g.: ~/scap-security-guide/build/build_config.yml "
52
        "needed for autodetection of profile root"
53
    )
54
    p.add_argument(
55
        "--profile",
56
        help="Generate Playbooks only for given Profile ID. Accepts profile "
57
        "ID in the short form, eg. 'ospp'. If not specified, Playbooks are "
58
        "built for all available profiles."
59
    )
60
    p.add_argument(
61
        "--rule",
62
        help="Generate Ansible Playbooks only for given rule specified by "
63
             "a shortened Rule ID, eg. 'package_sendmail_removed'. "
64
             "If not specified, Playbooks are built for every rule."
65
    )
66
    return p.parse_args()
67
68
69
def main():
70
    args = parse_args()
71
    product_yaml = os.path.join(args.ssg_root, "products", args.product, "product.yml")
72
    if args.input_dir:
73
        input_dir = args.input_dir
74
    else:
75
        input_dir = os.path.join(
76
            args.ssg_root, "build", args.product, "fixes", "ansible"
77
        )
78
    if args.output_dir:
79
        output_dir = args.output_dir
80
    else:
81
        output_dir = os.path.join(
82
            args.ssg_root, "build", args.product, "playbooks"
83
        )
84
    if args.resolved_rules_dir:
85
        resolved_rules_dir = args.resolved_rules_dir
86
    else:
87
        resolved_rules_dir = os.path.join(
88
            args.ssg_root, "build", args.product, "rules"
89
        )
90
    if args.resolved_profiles_dir:
91
        resolved_profiles_dir = args.resolved_profiles_dir
92
    else:
93
        resolved_profiles_dir = os.path.join(
94
            args.ssg_root, "build", args.product, "profiles"
95
        )
96
    playbook_builder = ssg.playbook_builder.PlaybookBuilder(
97
        product_yaml, input_dir, output_dir, resolved_rules_dir, resolved_profiles_dir,
98
        args.build_config_yaml
99
    )
100
    playbook_builder.build(args.profile, args.rule)
101
102
103
if __name__ == "__main__":
104
    main()
105