Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In catalog_linter, lint all toml files (graveyard wishlist etc). #2122

Merged
merged 1 commit into from
Mar 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 25 additions & 17 deletions tools/catalog_linter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import json
import sys
from pathlib import Path
from difflib import SequenceMatcher
from typing import Any, Dict, Generator, List, Tuple

Expand All @@ -16,16 +17,27 @@
)


def validate_schema() -> Generator[str, None, None]:
with open(
REPO_APPS_ROOT / "schemas" / "apps.toml.schema.json", encoding="utf-8"
) as file:
apps_catalog_schema = json.load(file)
validator = jsonschema.Draft202012Validator(apps_catalog_schema)
for error in validator.iter_errors(get_catalog()):
def validate_schema(data: dict, schema_path: Path) -> Generator[str, None, None]:
schema = json.load(schema_path.open("r", encoding="utf-8"))
validator = jsonschema.Draft202012Validator(schema)
for error in validator.iter_errors(data):
yield f"at .{'.'.join(error.path)}: {error.message}"


def validate_schema_pretty(data: dict, name: str) -> bool:
schema_path = REPO_APPS_ROOT / "schemas" / f"{name}.schema.json"
has_errors = False
schema_errors = list(validate_schema(data, schema_path))
if schema_errors:
has_errors = True
print(f"Error while validating {name} against schema:")
for error in schema_errors:
print(f" - {error}")
if schema_errors:
print()
return has_errors


def check_app(
app: str, infos: Dict[str, Any]
) -> Generator[Tuple[str, bool], None, None]:
Expand Down Expand Up @@ -88,14 +100,11 @@ def check_all_apps() -> Generator[Tuple[str, List[Tuple[str, bool]]], None, None
def main() -> None:
has_errors = False

schema_errors = list(validate_schema())
if schema_errors:
has_errors = True
print("Error while validating catalog against schema:")
for error in schema_errors:
print(f" - {error}")
if schema_errors:
print()
has_errors |= validate_schema_pretty(get_antifeatures(), "antifeatures.toml")
has_errors |= validate_schema_pretty(get_catalog(), "apps.toml")
has_errors |= validate_schema_pretty(get_categories(), "categories.toml")
has_errors |= validate_schema_pretty(get_graveyard(), "graveyard.toml")
has_errors |= validate_schema_pretty(get_wishlist(), "wishlist.toml")

for app, errors in check_all_apps():
print(f"{app}:")
Expand All @@ -105,8 +114,7 @@ def main() -> None:
level = "error" if is_fatal else "warning"
print(f" - {level}: {error}")

if has_errors:
sys.exit(1)
sys.exit(has_errors)


if __name__ == "__main__":
Expand Down