Test Failed
Push — master ( 00964b...f887a2 )
by Jan
02:30 queued 12s
created

compile_profiles.create_parser()   A

Complexity

Conditions 1

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 16
nop 0
dl 0
loc 25
ccs 0
cts 8
cp 0
crap 2
rs 9.6
c 0
b 0
f 0
1
from __future__ import print_function
2
3
import argparse
4
import logging
5
import sys
6
import os.path
7
from glob import glob
8
9
import ssg.build_profile
10
import ssg.build_yaml
11
import ssg.controls
12
import ssg.products
13
14
def create_parser():
15
    parser = argparse.ArgumentParser()
16
    parser.add_argument("profile_file", nargs="*")
17
    parser.add_argument(
18
        "--build-config-yaml",
19
        help="YAML file with information about the build configuration. "
20
        "e.g.: ~/scap-security-guide/build/build_config.yml "
21
        "needed for autodetection of profile root"
22
    )
23
    parser.add_argument(
24
        "--product-yaml",
25
        help="YAML file with information about the product we are building. "
26
        "e.g.: ~/scap-security-guide/rhel7/product.yml "
27
        "needed for autodetection of profile root"
28
    )
29
    parser.add_argument(
30
        "--output", "-o", default="{name}.profile",
31
        help="The template for saving processed profile files."
32
    )
33
    parser.add_argument(
34
        "--controls-dir",
35
        help="Directory that contains control files with policy controls. "
36
        "e.g.: ~/scap-security-guide/controls",
37
    )
38
    return parser
39
40
41
def get_env_yaml(build_config_yaml, product_yaml):
42
    if build_config_yaml is None or product_yaml is None:
43
        return None
44
45
    env_yaml = ssg.environment.open_environment(build_config_yaml, product_yaml)
46
    return env_yaml
47
48
49
def main():
50
    parser = create_parser()
51
    args = parser.parse_args()
52
    env_yaml = get_env_yaml(args.build_config_yaml, args.product_yaml)
53
54
    build_root = os.path.dirname(args.build_config_yaml)
55
56
    logfile = "{build_root}/{product}/control_profiles.log".format(
57
            build_root=build_root,
58
            product=env_yaml["product"])
59
    logging.basicConfig(filename=logfile, level=logging.INFO)
60
61
    if args.controls_dir:
62
        controls_manager = ssg.controls.ControlsManager(args.controls_dir, env_yaml)
63
        controls_manager.load()
64
65
    profile_files = ssg.products.get_profile_files_from_root(env_yaml, args.product_yaml)
66
    profile_files.extend(args.profile_file)
67
    profiles = ssg.build_profile.make_name_to_profile_mapping(profile_files, env_yaml)
68
    for pname in profiles:
69
        profiles[pname].resolve(profiles, controls_manager)
0 ignored issues
show
introduced by
The variable controls_manager does not seem to be defined in case args.controls_dir on line 61 is False. Are you sure this can never be the case?
Loading history...
70
71
    for name, p in profiles.items():
72
        p.dump_yaml(args.output.format(name=name))
73
74
75
if __name__ == "__main__":
76
    main()
77