50 lines
1.7 KiB
Python
50 lines
1.7 KiB
Python
#!/usr/bin/env python3
|
|
"""Check local documentation links and reachability of nested contracts."""
|
|
from pathlib import Path
|
|
import re
|
|
import sys
|
|
|
|
root = Path(__file__).resolve().parent.parent
|
|
patterns = root / 'rules/patterns'
|
|
documents = list(root.glob('*.md')) + list((root / 'rules').rglob('*.md'))
|
|
links = {}
|
|
errors = []
|
|
|
|
for document in documents:
|
|
source = document.read_text()
|
|
if len(re.findall(r'^```', source, flags=re.M)) % 2:
|
|
errors.append(f'{document.relative_to(root)}: unclosed code block')
|
|
text = re.sub(r'```.*?```', '', source, flags=re.S)
|
|
targets = []
|
|
for reference in re.findall(r'\[[^\]]*\]\(([^)]+)\)', text):
|
|
if re.match(r'[a-zA-Z]+:|#', reference):
|
|
continue
|
|
target = (document.parent / reference.split('#')[0]).resolve()
|
|
if not target.exists():
|
|
errors.append(f'{document.relative_to(root)}: missing {reference}')
|
|
targets.append(target)
|
|
links[document.resolve()] = targets
|
|
|
|
# Top-level patterns are checked against the bootstrap router by lint.sh.
|
|
# Every nested contract must also be reachable through that pattern's entry.
|
|
for pattern in patterns.iterdir():
|
|
if not pattern.is_dir():
|
|
continue
|
|
seen = set()
|
|
pending = [(pattern / 'contract.md').resolve()]
|
|
while pending:
|
|
path = pending.pop()
|
|
if path in seen:
|
|
continue
|
|
seen.add(path)
|
|
pending.extend(links.get(path, []))
|
|
for contract in pattern.rglob('contract.md'):
|
|
if contract.resolve() not in seen:
|
|
errors.append(f'{contract.relative_to(root)}: unreachable from pattern contract')
|
|
|
|
for error in errors:
|
|
print('FAIL:', error)
|
|
if not errors:
|
|
print('OK: local documentation links and nested contracts')
|
|
sys.exit(bool(errors))
|