|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +import typer |
| 6 | +from tabulate import tabulate |
| 7 | +from tqdm import tqdm |
| 8 | + |
| 9 | +from t4_devkit.common.sanity import DBException, sanity_check |
| 10 | + |
| 11 | +from .version import version_callback |
| 12 | + |
| 13 | +cli = typer.Typer( |
| 14 | + name="t4sanity", |
| 15 | + no_args_is_help=True, |
| 16 | + context_settings={"help_option_names": ["-h", "--help"]}, |
| 17 | + pretty_exceptions_enable=False, |
| 18 | +) |
| 19 | + |
| 20 | + |
| 21 | +def _run_sanity_check(db_parent: str, *, include_warning: bool = False) -> list[DBException]: |
| 22 | + exceptions: list[DBException] = [] |
| 23 | + |
| 24 | + db_dirs: list[Path] = Path(db_parent).glob("*") |
| 25 | + |
| 26 | + for db_root in tqdm(db_dirs, desc=">>>Sanity checking..."): |
| 27 | + result = sanity_check(db_root, include_warning=include_warning) |
| 28 | + if result: |
| 29 | + exceptions.append(result) |
| 30 | + return exceptions |
| 31 | + |
| 32 | + |
| 33 | +@cli.command() |
| 34 | +def main( |
| 35 | + version: bool = typer.Option( |
| 36 | + False, |
| 37 | + "--version", |
| 38 | + "-v", |
| 39 | + help="Show the application version and exit.", |
| 40 | + callback=version_callback, |
| 41 | + is_eager=True, |
| 42 | + ), |
| 43 | + db_parent: str = typer.Argument(..., help="Path to parent directory of the databases."), |
| 44 | + include_warning: bool = typer.Option( |
| 45 | + False, "-iw", "--include-warning", help="Indicates whether to report any warnings." |
| 46 | + ), |
| 47 | +) -> None: |
| 48 | + exceptions = _run_sanity_check(db_parent, include_warning=include_warning) |
| 49 | + |
| 50 | + headers = ["DatasetID", "Version", "Message"] |
| 51 | + table = [[e.dataset_id, e.version, e.message] for e in exceptions] |
| 52 | + |
| 53 | + print(tabulate(table, headers=headers, tablefmt="pretty")) |
0 commit comments