Files
infrastructure/scripts/ci/check_links.py
T
spektr f48f831e82 M5: statische CI ohne Serverzugang (Gitea Actions) + 2 gebrochene Doku-Links fixen
- .gitea/workflows/ci-static.yml: Jobs secret-scan, yamllint, ansible-syntax,
  markdown-links; Job-Image python:3.13-bookworm per Digest gepinnt
- ci/requirements-ci.txt (ansible-core==2.21.4 wie Control-Node, yamllint==1.37.0),
  .yamllint, scripts/ci/{secret_scan.py,check_links.py,check_ansible_syntax.sh}
- lokal gegen das Repo geprueft: alle 4 Checks gruen
- Linkfixes: GNENC-001 (../ -> ../../), GNAI-018 (ein ../ zu viel)

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
2026-11-11 05:41:40 +01:00

46 lines
1.6 KiB
Python

#!/usr/bin/env python3
"""Prueft relative Markdown-Links aller versionierten .md-Dateien.
Analog zum lokalen Linkvalidator (GNAI-013 Abschnitt 4: broken_local_links=0).
Absolute externe URLs (http/https/mailto) und reine Anker werden nicht geprüft.
"""
import os
import re
import subprocess
import sys
LINK_RE = re.compile(r"\[[^\]]*\]\(([^)#\s]+)(#[^)]*)?\)")
def md_files() -> list[str]:
out = subprocess.run(["git", "ls-files", "-z", "*.md"], capture_output=True, check=True)
return [f for f in out.stdout.decode("utf-8", "surrogateescape").split("\0") if f and not f.startswith(".venv/")]
def main() -> int:
broken = []
checked = 0
for path in md_files():
base = os.path.dirname(path)
with open(path, encoding="utf-8", errors="replace") as fh:
for lineno, line in enumerate(fh, 1):
for m in LINK_RE.finditer(line):
target = m.group(1)
if re.match(r"^[a-zA-Z][a-zA-Z0-9+.-]*:", target): # http:, mailto:, ...
continue
checked += 1
resolved = os.path.normpath(os.path.join(base, target))
if not os.path.exists(resolved):
broken.append(f"{path}:{lineno}: {target}")
if broken:
print(f"LINK-CHECK: {len(broken)} gebrochene Links (geprueft: {checked}):")
for b in broken:
print(" ", b)
return 1
print(f"LINK-CHECK: OK ({checked} relative Links in {len(md_files())} Dateien)")
return 0
if __name__ == "__main__":
sys.exit(main())