0ad/source/tools/entity/creationgraph.py
Dunedan e36c6a31fe
Enable additional ruff rules
In the ruff config file added in #6954 explicitly selecting the ruff
rules to check was missed, resulting in ruff only checking a very small
subset of its available rules. That hasn't been desired, so this is the
first of a series of commits enabling more rules. In this PR all rules
whose violations can be either automatically fixed by ruff or are
trivial to fix manually get enabled. For the follow up PRs it's intended
to focus on one area of rules per PR to gradually improve the Python
code quality.
2024-08-25 06:29:39 +02:00

70 lines
2.7 KiB
Python
Executable file

#!/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") 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()