Test Failed
Push — master ( 9f535f...d92246 )
by Jan
02:32 queued 10s
created

utils.render-policy.HtmlOutput.get_result()   A

Complexity

Conditions 1

Size

Total Lines 5
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 5
nop 1
dl 0
loc 5
ccs 0
cts 5
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
#!/usr/bin/python3
2
3
from glob import glob
4
import collections
5
import os
6
import pathlib
7
8
import argparse
9
10
import ssg.build_yaml
11
import ssg.controls
12
import ssg.yaml
13
import ssg.jinja
14
import template_renderer
15
16
17
class HtmlOutput(template_renderer.Renderer):
18
    TEMPLATE_NAME = "controls-template.html"
19
20
    def set_all_rules_with_metadata(self):
21
        compiled_rules = self.built_content_path / "rules"
22
        rule_files = glob("{compiled_rules}/*".format(compiled_rules=compiled_rules))
23
        if not rule_files:
24
            msg = (
25
                "No files found in '{compiled_rules}', please make sure that "
26
                "you select the build dir correctly and that the appropriate product is built."
27
                .format(compiled_rules=compiled_rules)
28
            )
29
            raise ValueError(msg)
30
31
        rules_dict = dict()
32
        for r_file in rule_files:
33
            rule = ssg.build_yaml.Rule.from_yaml(r_file, self.env_yaml)
34
            self._set_rule_relative_definition_location(rule)
35
            rules_dict[rule.id_] = rule
36
        self.template_data["rules"] = rules_dict
37
38
    def set_policy(self, policy_file):
39
        policy = ssg.controls.Policy(policy_file, self.env_yaml, )
40
        policy.load()
41
        self.template_data["policy"] = policy
42
        self.template_data["title"] = (
43
            "Definition of {policy_title} for {product}"
44
            .format(policy_title=policy.title, product=self.product)
45
        )
46
47
48
def parse_args():
49
    parser = HtmlOutput.create_parser(
50
        description="Render a policy file typically located in 'controls' directory "
51
        "of the project to HTML in a context of a product. "
52
        "The product must be built.")
53
    parser.add_argument(
54
        "policy", metavar="FILENAME", help="The policy YAML file")
55
    return parser.parse_args()
56
57
58
if __name__ == "__main__":
59
    args = parse_args()
60
    renderer = HtmlOutput(args.product, args.build_dir)
61
    renderer.set_all_rules_with_metadata()
62
    renderer.set_policy(args.policy)
63
    renderer.output_results(args)
64