from __future__ import annotations from django.core.management.base import BaseCommand, CommandError from pobsync_backend.self_check import collect_self_checks, summarize_self_checks class Command(BaseCommand): help = "Run pobsync runtime self checks for native installs and updates." def add_arguments(self, parser): parser.add_argument( "--fail-on-warning", action="store_true", help="Exit with an error when warnings are present.", ) def handle(self, *args, **options): checks = collect_self_checks() summary = summarize_self_checks(checks) for check in checks: line = f"[{check.status.upper()}] {check.name}: {check.message}" if check.detail: line = f"{line} ({check.detail})" if check.status == "failed": self.stderr.write(line) elif check.status == "warning": self.stderr.write(line) else: self.stdout.write(line) self.stdout.write( "Summary: " f"{summary['ok']} ok, " f"{summary['warning']} warning(s), " f"{summary['failed']} failed, " f"{summary['skipped']} skipped" ) if summary["failed"]: raise CommandError("pobsync install self check failed.") if options["fail_on_warning"] and summary["warning"]: raise CommandError("pobsync install self check reported warnings.")