summaryrefslogtreecommitdiff
path: root/threats_by_sector_table.py
blob: 597cf12c265d36789ab4a51b4fb2f00294a75489 (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env python3

# SPDX-License-Identifier: CC0-1.0
#
# Copyright (C) 2024 Wojtek Kosior <koszko@koszko.org>

import yaml
import sys

def read_APT_data(yaml_path):
    if yaml_path:
        with open(yaml_path) as inp:
            return yaml.safe_load(inp)
    else:
        return yaml.safe_load(sys.stdin)

def group_has_sector(group, sector):
    group_sectors = group.get("sectors", [])

    if sector is None:
        return not group_sectors

    for sector_obj in group_sectors:
        if sector_obj["sector"] == sector:
            return True

def group_has_goal(group, goal):
    group_goals = group.get("goals", [])

    if goal is None:
        return not group_goals

    for goal_obj in group_goals:
        if goal_obj["goal"] == goal:
            return True

dashes = ''.join('-' for _ in range(24))

def print_tables(APT_data, out=sys.stdout):
    all_groups = APT_data["groups"]
    all_goals = sorted({goal["goal"]
                        for group in all_groups
                        for goal in group.get("goals", [])})
    all_origins = sorted({group["origin"] for group in all_groups})
    all_sectors = sorted({sector["sector"]
                          for group in all_groups
                          for sector in group.get("sectors", [])})

    separator_line = f"|{'|'.join(dashes for _ in [None] + all_origins)}|"

    first = True

    for sector in all_sectors:
        if first:
            first = False
        else:
            print("\\newpage")

        groups = [group for group in all_groups
                  if group_has_sector(group, sector)]

        def make_group_listing(origin, goal):
            return ", ".join(group["name"] for group in groups
                             if (group["origin"] == origin and
                                 group_has_goal(group, goal)))

        print(f"### {sector}")
        print()
        print(f"|**goal &#92; origin**|**{'**|**'.join(all_origins)}**|")

        for goal in all_goals:
            group_listings = [make_group_listing(origin, goal) or '   '
                              for origin in all_origins]
            print(separator_line)
            print(f"|**{goal}**|{'|'.join(group_listings)}|")

        # Groups with no documented goal (if there are such ones)
        group_listings = [make_group_listing(origin, None)
                          for origin in all_origins]

        if any(group_listings):
            print(separator_line)
            print(f"|*not documented*|{'|'.join(group_listings)}|")

        print()

if __name__ == "__main__":
    print_tables(read_APT_data(None if len(sys.argv) < 2 else sys.argv[1]))