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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
|
#!/usr/bin/env python3
import argparse
import logging
import sys
from concurrent.futures import ThreadPoolExecutor, as_completed
from pathlib import Path
from xml.etree import ElementTree as ET
from lxml import etree
from lxml.etree import DocumentInvalid, ElementTree
from scriptlib import SimulTemplateEntity, find_files
SIMUL_TEMPLATES_PATH = Path("simulation/templates")
ENTITY_RELAXNG_FNAME = "entity.rng"
def init_logger(log_level) -> logging.Logger:
"""Initialize a logger."""
logger = logging.getLogger(__name__)
logger.setLevel(log_level)
handler = logging.StreamHandler(sys.stdout)
handler.setLevel(log_level)
handler.setFormatter(logging.Formatter("%(levelname)s: %(message)s"))
logger.addHandler(handler)
return logger
def validate_template(
simulation_template_entity: SimulTemplateEntity,
template_path: Path,
relaxng_schema: ElementTree,
mod_name: str,
) -> None:
"""Validate a single template."""
entity = simulation_template_entity.load_inherited(
SIMUL_TEMPLATES_PATH, str(template_path.relative_to(SIMUL_TEMPLATES_PATH)), [mod_name]
)
relaxng_schema.assertValid(etree.fromstring(ET.tostring(entity, encoding="utf-8")))
class ValidationError(Exception):
pass
def validate_templates(
logger: logging.Logger,
vfs_root: Path,
mod_name: str,
relaxng_schema_path: Path,
templates: list[Path] | None,
) -> None:
"""Validate templates against the given RELAX NG schema."""
if templates:
templates = [(template, None) for template in templates]
else:
templates = find_files(vfs_root.resolve(), [mod_name], SIMUL_TEMPLATES_PATH, ["xml"])
templates_to_validate = []
for fp, _ in templates:
if fp.stem.startswith("template_"):
continue
template_path = fp.as_posix()
if template_path.startswith(
(
f"{SIMUL_TEMPLATES_PATH.as_posix()}/mixins/",
f"{SIMUL_TEMPLATES_PATH.as_posix()}/special/",
)
):
continue
templates_to_validate.append(fp)
simulation_template_entity = SimulTemplateEntity(vfs_root, logger)
relaxng_schema = etree.RelaxNG(file=relaxng_schema_path)
count, failed = 0, 0
with ThreadPoolExecutor() as executor:
futures = {}
for template_path in templates_to_validate:
future = executor.submit(
validate_template,
simulation_template_entity,
template_path,
relaxng_schema,
mod_name,
)
futures[future] = template_path
for future in as_completed(futures):
count += 1
template_path = futures[future]
logger.debug("Processed %s", template_path)
try:
future.result()
except DocumentInvalid as e:
failed += 1
logger.error("%s: %s", template_path, e) # noqa: TRY400
logger.info("Total: %s; failed: %s", count, failed)
if failed:
raise ValidationError
def main() -> None:
parser = argparse.ArgumentParser(description="Validate templates against a RELAX NG schema.")
parser.add_argument("-m", "--mod-name", required=True, help="The name of the mod to validate.")
parser.add_argument(
"-r",
"--root",
dest="vfs_root",
default=Path(),
type=Path,
help="The path to mod's root location.",
)
parser.add_argument(
"-s",
"--relaxng-schema",
default=Path() / ENTITY_RELAXNG_FNAME,
type=Path,
help="The path to the RELAX NG schema.",
)
parser.add_argument(
"-t",
"--templates",
nargs="*",
type=Path,
help="A list of templates to validate. If omitted all templates will be validated.",
)
parser.add_argument(
"-v", "--verbose", help="Be verbose about the output.", action="store_true"
)
args = parser.parse_args()
log_level = logging.DEBUG if args.verbose else logging.INFO
logger = init_logger(log_level)
if not args.relaxng_schema.exists():
logger.error(
"RELAX NG schema file doesn't exist. Please create the file: %s. You can do that by "
'running "pyrogenesis -dumpSchema" in the "binaries/system" directory',
args.relaxng_schema,
)
sys.exit(1)
try:
validate_templates(
logger, args.vfs_root, args.mod_name, args.relaxng_schema, args.templates
)
except ValidationError:
sys.exit(1)
if __name__ == "__main__":
main()
|