Passed
Push — master ( 65b335...62b6dd )
by Matěj
02:43 queued 11s
created

compile_profiles   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 58
dl 0
loc 81
ccs 0
cts 48
cp 0
rs 10
c 0
b 0
f 0
wmc 12

5 Functions

Rating   Name   Duplication   Size   Complexity  
A create_parser() 0 20 1
A get_env_yaml() 0 6 3
A make_name_to_profile_mapping() 0 12 3
A main() 0 13 3
A get_profile_files_from_root() 0 8 2
1
from __future__ import print_function
2
3
import argparse
4
import sys
5
import os.path
6
from glob import glob
7
8
import ssg.build_yaml
9
10
def create_parser():
11
    parser = argparse.ArgumentParser()
12
    parser.add_argument("profile_file", nargs="*")
13
    parser.add_argument(
14
        "--build-config-yaml",
15
        help="YAML file with information about the build configuration. "
16
        "e.g.: ~/scap-security-guide/build/build_config.yml "
17
        "needed for autodetection of profile root"
18
    )
19
    parser.add_argument(
20
        "--product-yaml",
21
        help="YAML file with information about the product we are building. "
22
        "e.g.: ~/scap-security-guide/rhel7/product.yml "
23
        "needed for autodetection of profile root"
24
    )
25
    parser.add_argument(
26
        "--output", "-o", default="{name}.profile",
27
        help="The template for saving processed profile files."
28
    )
29
    return parser
30
31
32
def make_name_to_profile_mapping(profile_files, env_yaml):
33
    name_to_profile = {}
34
    for f in profile_files:
35
        try:
36
            p = ssg.build_yaml.ResolvableProfile.from_yaml(f, env_yaml)
37
            name_to_profile[p.id_] = p
38
        except Exception as exc:
39
            # The profile is probably doc-incomplete
40
            msg = "Not building profile from {fname}: {err}".format(
41
                fname=f, err=str(exc))
42
            print(msg, file=sys.stderr)
43
    return name_to_profile
44
45
46
def get_env_yaml(build_config_yaml, product_yaml):
47
    if build_config_yaml is None or product_yaml is None:
48
        return None
49
50
    env_yaml = ssg.yaml.open_environment(build_config_yaml, product_yaml)
51
    return env_yaml
52
53
54
def get_profile_files_from_root(env_yaml, product_yaml):
55
    profile_files = []
56
    if env_yaml:
57
        base_dir = os.path.dirname(product_yaml)
58
        profiles_root = ssg.utils.required_key(env_yaml, "profiles_root")
59
        profile_files = glob("{base_dir}/{profiles_root}/*.profile"
60
                             .format(profiles_root=profiles_root, base_dir=base_dir))
61
    return profile_files
62
63
64
def main():
65
    parser = create_parser()
66
    args = parser.parse_args()
67
    env_yaml = get_env_yaml(args.build_config_yaml, args.product_yaml)
68
69
    profile_files = get_profile_files_from_root(env_yaml, args.product_yaml)
70
    profile_files.extend(args.profile_file)
71
    profiles = make_name_to_profile_mapping(profile_files, env_yaml)
72
    for pname in profiles:
73
        profiles[pname].resolve(profiles)
74
75
    for name, p in profiles.items():
76
        p.dump_yaml(args.output.format(name=name))
77
78
79
if __name__ == "__main__":
80
    main()
81