|
1
|
|
|
#!/usr/bin/python3 |
|
2
|
|
|
|
|
3
|
|
|
import os |
|
4
|
|
|
import sys |
|
5
|
|
|
|
|
6
|
|
|
import argparse |
|
7
|
|
|
|
|
8
|
|
|
import ssg.build_yaml |
|
9
|
|
|
|
|
10
|
|
|
import tables.table_renderer |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
class HtmlOutput(tables.table_renderer.TableHtmlOutput): |
|
14
|
|
|
TEMPLATE_NAME = "tables/profile_tables_template.html" |
|
15
|
|
|
|
|
16
|
|
|
def __init__(self, * args, ** kwargs): |
|
17
|
|
|
profile_id = kwargs.pop("profile_id") |
|
18
|
|
|
super(HtmlOutput, self).__init__(* args, ** kwargs) |
|
19
|
|
|
|
|
20
|
|
|
profile_path = str(self.built_content_path / "profiles" / (profile_id + ".profile")) |
|
21
|
|
|
self.profile = ssg.build_yaml.Profile.from_yaml(profile_path, self.env_yaml) |
|
22
|
|
|
self.profile_variables = self.profile.variables |
|
23
|
|
|
|
|
24
|
|
|
def _get_var_value(self, varname): |
|
25
|
|
|
try: |
|
26
|
|
|
return self.profile_variables[varname] |
|
27
|
|
|
except KeyError: |
|
28
|
|
|
return super(HtmlOutput, self)._get_var_value(varname) |
|
29
|
|
|
|
|
30
|
|
|
def _get_eligible_rules(self, refcat): |
|
31
|
|
|
eligible_rule_ids = self.profile.selected |
|
32
|
|
|
filenames = [os.path.join(self.rules_root, rid + ".yml") for rid in eligible_rule_ids] |
|
33
|
|
|
return [ssg.build_yaml.Rule.from_yaml(f, self.env_yaml) for f in filenames] |
|
34
|
|
|
|
|
35
|
|
|
def _generate_shortened_ref(self, reference, rule): |
|
36
|
|
|
shortened_ref = super(HtmlOutput, self)._generate_shortened_ref(reference, rule) |
|
37
|
|
|
if shortened_ref == self.DEFAULT_SHORTENED_REF and self.verbose: |
|
38
|
|
|
print("Rule '{rid}' is included in the profile {profile_id}, " |
|
39
|
|
|
"but doesn't contain the {ref_id} reference" |
|
40
|
|
|
.format(rid=rule.id_, profile_id=self.profile.id_, ref_id=reference.id), |
|
41
|
|
|
file=sys.stderr) |
|
42
|
|
|
return shortened_ref |
|
43
|
|
|
|
|
44
|
|
|
def process_rules(self, reference): |
|
45
|
|
|
super(HtmlOutput, self).process_rules(reference) |
|
46
|
|
|
|
|
47
|
|
|
self.template_data["profile"] = self.profile |
|
48
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
def update_parser(parser): |
|
51
|
|
|
parser.add_argument( |
|
52
|
|
|
"profile", metavar="PROFILE_ID", help="The ID of the profile") |
|
53
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
def parse_args(): |
|
56
|
|
|
parser = HtmlOutput.create_parser( |
|
57
|
|
|
"Generate HTML table that maps references to rules in context of a profile " |
|
58
|
|
|
"using compiled rules and compiled profiles as source of data.") |
|
59
|
|
|
tables.table_renderer.update_parser(parser) |
|
60
|
|
|
update_parser(parser) |
|
61
|
|
|
return parser.parse_args() |
|
62
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
if __name__ == "__main__": |
|
65
|
|
|
args = parse_args() |
|
66
|
|
|
renderer = HtmlOutput( |
|
67
|
|
|
args.product, args.build_dir, profile_id=args.profile, verbose=args.verbose) |
|
68
|
|
|
reference = ssg.constants.REFERENCES[args.refcategory] |
|
69
|
|
|
renderer.process_rules(reference) |
|
70
|
|
|
renderer.output_results(args) |
|
71
|
|
|
|