From 27664383133f0b01b7369894fbd284fed9f4fce6 Mon Sep 17 00:00:00 2001 From: Helio Chissini de Castro Date: Thu, 18 Jun 2026 14:14:49 +0200 Subject: [PATCH] feat(tools): Add simple ort-validate script Signed-off-by: Helio Chissini de Castro --- pyproject.toml | 5 +++- src/tools/__init__.py | 0 src/tools/ort_validate.py | 61 +++++++++++++++++++++++++++++++++++++++ uv.lock | 2 +- 4 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 src/tools/__init__.py create mode 100644 src/tools/ort_validate.py diff --git a/pyproject.toml b/pyproject.toml index 4455c59..2ae6a08 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "python-ort" -version = "0.9.0" +version = "0.9.1" description = "A Python Ort model serialization library" readme = "README.md" requires-python = ">=3.10" @@ -23,6 +23,9 @@ dependencies = [ "pydantic>=2.13.4", ] +[project.scripts] +ort-validate = "tools.ort_validate:main" + [dependency-groups] dev = [ "datamodel-code-generator[http]>=0.64.0", diff --git a/src/tools/__init__.py b/src/tools/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/tools/ort_validate.py b/src/tools/ort_validate.py new file mode 100644 index 0000000..8a3d018 --- /dev/null +++ b/src/tools/ort_validate.py @@ -0,0 +1,61 @@ +# SPDX-FileCopyrightText: 2025 Helio Chissini de Castro +# SPDX-License-Identifier: MIT +# +import logging +import sys +from pathlib import Path +from typing import TypeVar + +import click +from pydantic import BaseModel, ValidationError + +from ort import RepositoryConfiguration, ort_yaml_load +from ort.models import LicenseClassifications + +logger = logging.getLogger() + +ModelT = TypeVar("ModelT", bound=BaseModel) + + +def load_and_validate(model: type[ModelT], datafile: str) -> None: + """Load ``datafile`` as YAML, validate it into ``model`` and pretty-print the result.""" + try: + with Path(datafile).open() as fd: + data = ort_yaml_load(fd) + parsed = model.model_validate(data or {}) + logger.debug(parsed.model_dump_json(indent=2)) + logger.info(f"Successfully validated {datafile} as {model.__name__}.") + except ValidationError: + logger.error("Validation error while parsing the ORT result:") + sys.exit(1) + except OSError as e: + logger.error(f"Error while opening the file {datafile}: {e}") + sys.exit(1) + + +@click.group() +@click.option("-d", "--debug", is_flag=True, help="Enable debug logging.") +def main(debug: bool = False) -> None: + logging.basicConfig(level=logging.DEBUG) + if debug: + logging.basicConfig(level=logging.DEBUG) + pass + + +@click.command() +@click.argument("datafile") +def license_classifications(datafile): + load_and_validate(LicenseClassifications, datafile) + + +@click.command() +@click.argument("datafile") +def repository_configuration(datafile): + load_and_validate(RepositoryConfiguration, datafile) + + +main.add_command(repository_configuration) +main.add_command(license_classifications) + +if __name__ == "__main__": + main() diff --git a/uv.lock b/uv.lock index 710578f..322288b 100644 --- a/uv.lock +++ b/uv.lock @@ -597,7 +597,7 @@ wheels = [ [[package]] name = "python-ort" -version = "0.9.0" +version = "0.9.1" source = { editable = "." } dependencies = [ { name = "license-expression" },