Test Failed
Push — master ( 5a89d3...a9ebfc )
by Jan
02:25 queued 11s
created

utils.autoprodtyper.autoprodtype()   A

Complexity

Conditions 3

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 8
nop 4
dl 0
loc 10
ccs 0
cts 8
cp 0
crap 12
rs 10
c 0
b 0
f 0
1
#!/usr/bin/env python
2
3
import sys
4
import os
5
import argparse
6
import json
7
8
import ssg.build_yaml
9
import ssg.products
10
import ssg.rules
11
import ssg.yaml
12
import ssg.utils
13
import ssg.rule_yaml
14
15
from mod_prodtype import add_products
16
17
SSG_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
18
19
20
def parse_args():
21
    parser = argparse.ArgumentParser(description="Automatically add a missing product "
22
                                     "reference to the prodtype key of a rule.yml based "
23
                                     "on inclusion in a specified profile")
24
    parser.add_argument("-j", "--json", type=str, action="store",
25
                        default="build/rule_dirs.json", help="File to read "
26
                        "json output of rule_dir_json from (defaults to "
27
                        "build/rule_dirs.json")
28
    parser.add_argument("-c", "--build-config-yaml", default="build/build_config.yml",
29
                        help="YAML file with information about the build configuration. "
30
                        "Defaults to build/build_config.yml")
31
    parser.add_argument("-p", "--profiles-root",
32
                        help="Override where to look for profile files.")
33
    parser.add_argument("product", type=str, help="Product to add prod types for")
34
    parser.add_argument("profile", type=str, help="Profile to iterate over")
35
36
    return parser.parse_args()
37
38
39
def autoprodtype(env_yaml, rule_dirs, profile_path, product):
40
    profile = ssg.build_yaml.ProfileWithInlinePolicies.from_yaml(profile_path, env_yaml)
41
42
    for rule_id in profile.selected + profile.unselected:
43
        if rule_id not in rule_dirs:
44
            msg = "Unable to find rule in rule_dirs.json: {0}"
45
            msg = msg.format(rule_id)
46
            raise ValueError(msg)
47
48
        add_products(rule_dirs[rule_id], [product], silent=True)
49
50
51
def main():
52
    args = parse_args()
53
54
    json_file = open(args.json, 'r')
55
    all_rules = json.load(json_file)
56
57
    linux_products, other_products = ssg.products.get_all(SSG_ROOT)
58
    all_products = linux_products.union(other_products)
59
    if args.product not in all_products:
60
        msg = "Unknown product {0}: check SSG_ROOT and try again"
61
        msg = msg.format(args.product)
62
        raise ValueError(msg)
63
64
    product_base = os.path.join(SSG_ROOT, args.product)
65
    product_yaml = os.path.join(product_base, "product.yml")
66
    env_yaml = ssg.yaml.open_environment(args.build_config_yaml, product_yaml)
67
68
    profiles_root = os.path.join(product_base, "profiles")
69
    if args.profiles_root:
70
        profiles_root = args.profiles_root
71
72
    profile_filename = args.profile + ".profile"
73
    profile_path = os.path.join(profiles_root, profile_filename)
74
    if not os.path.exists(profile_path):
75
        msg = "Unknown profile {0}: check profile, --profiles-root, and try again. "
76
        msg += "Note that profiles should not include '.profile' suffix."
77
        msg = msg.format(args.profile)
78
        raise ValueError(msg)
79
80
    autoprodtype(env_yaml, all_rules, profile_path, args.product)
81
82
83
if __name__ == "__main__":
84
    main()
85