aboutsummaryrefslogtreecommitdiff
path: root/tools/python/maps_generator/checks/check_sections.py
blob: 244912213e0c76a538969fbb4cb7e648ecc19073 (plain) (blame)
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import os
from functools import lru_cache

from maps_generator.checks import check
from mwm import Mwm


class SectionNames:
    def __init__(self, sections):
        self.sections = sections

    def __sub__(self, other):
        return SectionNames(
            {k: self.sections[k] for k in set(self.sections) - set(other.sections)}
        )

    def __lt__(self, other):
        if isinstance(other, int):
            return len(self.sections) < other
        elif isinstance(other, SectionNames):
            return self.sections < other.sections
        assert False, type(other)

    def __gt__(self, other):
        if isinstance(other, int):
            return len(self.sections) > other
        elif isinstance(other, SectionNames):
            return self.sections > other.sections
        assert False, type(other)

    def __len__(self):
        return len(self.sections)

    def __str__(self):
        return str(self.sections)


@lru_cache(maxsize=None)
def read_sections(path: str):
    return Mwm(path, parse=False).sections_info()


def get_appeared_sections_check_set(
    old_path: str, new_path: str
) -> check.CompareCheckSet:
    return check.build_check_set_for_files(
        f"Appeared sections check",
        old_path,
        new_path,
        ext=".mwm",
        do=lambda path: SectionNames(read_sections(path)),
        diff_format=lambda s: ", ".join(f"{k}:{v.size}" for k, v in s.sections.items()),
        format=lambda s: f"number of sections: {len(s.sections)}",
    )


def get_disappeared_sections_check_set(
    old_path: str, new_path: str
) -> check.CompareCheckSet:
    return check.build_check_set_for_files(
        f"Disappeared sections check",
        old_path,
        new_path,
        ext=".mwm",
        do=lambda path: SectionNames(read_sections(path)),
        op=lambda previous, current: previous - current,
        diff_format=lambda s: ", ".join(f"{k}:{v.size}" for k, v in s.sections.items()),
        format=lambda s: f"number of sections: {len(s.sections)}",
    )


def get_sections_existence_check_set(
    old_path: str, new_path: str
) -> check.CompareCheckSet:
    """
    Returns a sections existence check set, that checks appeared and
    disappeared sections between old mwms and new mwms.
    """
    cs = check.CompareCheckSet("Sections existence check")
    cs.add_check(get_appeared_sections_check_set(old_path, new_path))
    cs.add_check(get_disappeared_sections_check_set(old_path, new_path))
    return cs


def _get_sections_set(path):
    sections = set()
    for file in os.listdir(path):
        p = os.path.join(path, file)
        if os.path.isfile(p) and file.endswith(".mwm"):
            sections.update(read_sections(p).keys())
    return sections


def get_sections_size_check_set(old_path: str, new_path: str) -> check.CompareCheckSet:
    """
    Returns a sections size check set, that checks a difference in a size
    of each sections of mwm between old mwms and new mwms.
    """
    sections_set = _get_sections_set(old_path)
    sections_set.update(_get_sections_set(new_path))

    cs = check.CompareCheckSet("Sections size check")

    def make_do(section):
        def do(path):
            sections = read_sections(path)
            if section not in sections:
                return None

            return sections[section].size

        return do

    for section in sections_set:
        cs.add_check(
            check.build_check_set_for_files(
                f"Size of {section} check",
                old_path,
                new_path,
                ext=".mwm",
                do=make_do(section),
            )
        )
    return cs