Files
2026-11-06 21:15:31 +03:00

89 lines
3.5 KiB
Python

import argparse
import hashlib
import os
import re
from pathlib import Path
import subprocess
import sys
import tempfile
import urllib.request
ROOT = Path(__file__).resolve().parents[1]
VERSION = "1.28.0"
SHA256 = "32342e7c1b4600f80df3471da46aee8012d3e1445d5ea1be1fb71289b07cc735"
def formatter():
jar = Path(tempfile.gettempdir()) / f"google-java-format-{VERSION}-all-deps.jar"
if not jar.exists():
url = (
"https://repo.maven.apache.org/maven2/com/google/googlejavaformat/"
f"google-java-format/{VERSION}/{jar.name}"
)
with urllib.request.urlopen(url, timeout=60) as response:
jar.write_bytes(response.read())
if hashlib.sha256(jar.read_bytes()).hexdigest() != SHA256:
raise RuntimeError("Java formatter checksum mismatch")
return jar
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--apply", action="store_true")
args = parser.parse_args()
java_home = os.environ.get("JAVA_HOME")
java = str(Path(java_home) / "bin" / "java") if java_home else "java"
files = sorted(str(p.relative_to(ROOT)) for p in (ROOT / "src").rglob("*.java"))
mode = ["--replace"] if args.apply else ["--dry-run", "--set-exit-if-changed"]
subprocess.run([java, "-jar", str(formatter()), "--aosp", *mode, *files], cwd=ROOT, check=True)
python = [
"worker/pipeline",
"scripts/style.py",
"scripts/capture_evidence.py",
"scripts/check_migration.py",
]
python += sorted(str(p.relative_to(ROOT)) for p in (ROOT / "tests").glob("pipeline_*.py"))
subprocess.run(
[sys.executable, "-m", "black", *([] if args.apply else ["--check"]), *python],
cwd=ROOT,
check=True,
)
check_controllers()
print("PASS Java AOSP / Python Black style")
def check_controllers():
directory = ROOT / "src/main/java/ru/mtusi/messenger/controller"
operations = 0
for path in directory.glob("*Controller.java"):
source = path.read_text(encoding="utf-8")
fields = re.findall(r"private final (\w+Service) (\w+);", source)
if "@RequiredArgsConstructor" not in source or len(fields) != 1:
raise AssertionError(f"Controller DI contract: {path.name}")
bodies = re.findall(r"@Override\s+public [^{}]+\{([^{}]*)\}", source)
if not bodies:
raise AssertionError(f"Controller has no operations: {path.name}")
service = fields[0][1]
for body in bodies:
if body.count(";") != 1 or len(re.findall(rf"\b{service}\.\w+\(", body)) != 1:
raise AssertionError(f"Controller must call one service: {path.name}")
if not re.fullmatch(rf"\s*(?:return )?{service}\.\w+\([^;]*\);\s*", body):
raise AssertionError(f"Controller contains extra logic: {path.name}")
operations += len(bodies)
for path in directory.glob("*Api.java"):
source = path.read_text(encoding="utf-8")
if source.count("/**") != source.count("@Operation(") + 1:
raise AssertionError(f"Missing API documentation: {path.name}")
for path in (ROOT / "src/main/java").rglob("*.java"):
source = path.read_text(encoding="utf-8")
if "@Autowired" in source:
raise AssertionError(f"Use constructor injection: {path.name}")
if re.search(r"^import .*\*;", source, re.MULTILINE):
raise AssertionError(f"Use explicit imports: {path.name}")
print(f"PASS {operations} controller operations: one service call, constructor DI, API Javadoc")
if __name__ == "__main__":
main()