File: creationgraph.py

package info (click to toggle)
0ad 0.27.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 171,928 kB
  • sloc: cpp: 194,011; javascript: 19,098; ansic: 15,066; python: 6,328; sh: 1,695; perl: 1,575; java: 533; xml: 415; php: 192; makefile: 99
file content (70 lines) | stat: -rw-r--r-- 2,766 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/env python3
from os import chdir
from pathlib import Path
from re import split
from subprocess import run
from sys import exit

from scriptlib import SimulTemplateEntity, find_files, warn


def find_entities(vfs_root):
    base = vfs_root / "public" / "simulation" / "templates"
    return [
        str(fp.relative_to(base).with_suffix(""))
        for (_, fp) in find_files(vfs_root, ["public"], "simulation/templates", "xml")
    ]


def main():
    vfs_root = Path(__file__).resolve().parents[3] / "binaries" / "data" / "mods"
    simul_templates_path = Path("simulation/templates")
    simul_template_entity = SimulTemplateEntity(vfs_root)
    with open("creation.dot", "w", encoding="utf-8") as dot_f:
        dot_f.write("digraph G {\n")
        files = sorted(find_entities(vfs_root))
        for f in files:
            if f.startswith("template_"):
                continue
            print(f"# {f}...")
            entity = simul_template_entity.load_inherited(simul_templates_path, f, ["public"])
            if (
                entity.find("Builder") is not None
                and entity.find("Builder").find("Entities") is not None
            ):
                entities = (
                    entity.find("Builder")
                    .find("Entities")
                    .text.replace("{civ}", entity.find("Identity").find("Civ").text)
                )
                builders = split(r"\s+", entities.strip())
                for builder in builders:
                    if Path(builder) in files:
                        warn(f"Invalid Builder reference: {f} -> {builder}")
                    dot_f.write(f'"{f}" -> "{builder}" [color=green];\n')
            if (
                entity.find("TrainingQueue") is not None
                and entity.find("TrainingQueue").find("Entities") is not None
            ):
                entities = (
                    entity.find("TrainingQueue")
                    .find("Entities")
                    .text.replace("{civ}", entity.find("Identity").find("Civ").text)
                )
                training_queues = split(r"\s+", entities.strip())
                for training_queue in training_queues:
                    if Path(training_queue) in files:
                        warn(f"Invalid TrainingQueue reference: {f} -> {training_queue}")
                    dot_f.write(f'"{f}" -> "{training_queue}" [color=blue];\n')
        dot_f.write("}\n")
    if run(["dot", "-V"], capture_output=True, check=False).returncode == 0:
        exit(
            run(
                ["dot", "-Tpng", "creation.dot", "-o", "creation.png"], text=True, check=False
            ).returncode
        )


if __name__ == "__main__":
    chdir(Path(__file__).resolve().parent)
    main()