Commit 079ffe4c authored by Jan Kiene's avatar Jan Kiene
Browse files

add script to check for duplicates in .prm files

parent 6cb48dc1
Loading
Loading
Loading
Loading
+53 −0
Original line number Diff line number Diff line
from collections import Counter
from pathlib import Path
import sys
import pprint

HERE = Path(__file__).parent
CONFIGS_DIR = HERE.joinpath("config")

PRM_FILES = [
    CONFIGS_DIR.joinpath("self_test.prm"),
    CONFIGS_DIR.joinpath("self_test_ltv.prm"),
    CONFIGS_DIR.joinpath("self_test_basop_encoder.prm"),
    CONFIGS_DIR.joinpath("self_test_ltv_basop_encoder.prm"),
]


def main() -> bool:
    duplicates_found = False
    for prm_file in PRM_FILES:
        with open(prm_file) as f:
            contents = f.read()

        # first check if there are complete duplicates (i.e. comment line, encoder cmd and decoder cmd are the same)
        blocks = contents.split("\n\n")
        blocks = [b for b in blocks if len(b.strip()) > 0]
        blocks_counter = Counter(blocks)
        blocks_duplicates = [
            b.split("\n")[0] for b in blocks_counter if blocks_counter[b] > 1
        ]

        # also check if there are duplicates in the command names only
        lines = contents.split("\n")
        commands_lines = [l for l in lines if l.strip().startswith("//")]
        commands_counter = Counter(commands_lines)
        commands_duplicates = [l for l in commands_counter if commands_counter[l] > 1]

        if len(blocks_duplicates) > 0:
            duplicates_found = True
            print(f"Duplicate testcases in {prm_file.name} found:")
            pprint.pp(blocks_duplicates)

        if len(commands_duplicates) > 0:
            duplicates_found = True
            print(f"Duplicate command names in {prm_file.name} found:")
            pprint.pp(commands_duplicates)

        print()

    return duplicates_found


if __name__ == "__main__":
    sys.exit(int(main()))