state-hub/scripts/generate_custodian_brief.py

35 lines
1.4 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
"""Render a current State Hub orientation brief without Git operations or hub writes."""
import argparse
import json
from pathlib import Path
import re
from consistency_check import _write_custodian_brief
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--repo-path', type=Path, required=True)
parser.add_argument('--repo-slug', required=True)
parser.add_argument('--api-base', default='http://127.0.0.1:8000')
parser.add_argument('--check', action='store_true')
args = parser.parse_args()
try:
if not re.fullmatch(r'[a-z0-9]+(?:-[a-z0-9]+)*', args.repo_slug):
raise ValueError('canonical repository slug required')
if not args.repo_path.is_dir():
raise ValueError('repository path must exist')
if (args.repo_path / '.custodian-brief.md').is_symlink():
raise ValueError('refuse redirected brief')
changed = _write_custodian_brief(args.api_base, args.repo_slug, str(args.repo_path),
strict=True, check=args.check)
except (OSError, ValueError, KeyError, TypeError) as exc:
parser.exit(1, f'Brief generation refused: {exc}\n')
print(json.dumps({'repo': args.repo_slug, 'status': 'updated' if changed else 'current',
'source': 'state-hub-read-model', 'git_operations': False}))
if __name__ == '__main__':
main()