File size: 3,997 Bytes
34019b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
"""Portable repository checks for Project Calendar."""

from __future__ import annotations

import compileall
import json
import re
import subprocess
import sys
from pathlib import Path


ROOT = Path(__file__).resolve().parents[1]
PYTHON_FILES = [
    "app.py",
    "config.py",
    "excel_export.py",
    "gantt_renderer.py",
    "jira_client.py",
    "scripts/check.py",
]
JSON_FILES = [
    "country_assignments.json",
    "pm_assignments.json",
    "pm_backup_assignments.json",
    "research_backup_assignments.json",
]


def _requirement_name(line: str) -> str:
    line = line.strip()
    for marker in ["==", ">=", "<=", "~=", "!=", ">", "<"]:
        if marker in line:
            return line.split(marker, 1)[0].strip().lower().replace("_", "-")
    return line.lower().replace("_", "-")


def check_python_compile() -> bool:
    return compileall.compile_file(
        str(ROOT / "app.py"), quiet=1, force=True
    ) and all(
        compileall.compile_file(str(ROOT / relative_path), quiet=1, force=True)
        for relative_path in PYTHON_FILES
        if relative_path != "app.py"
    )


def check_json_files() -> list[str]:
    errors: list[str] = []
    for relative_path in JSON_FILES:
        path = ROOT / relative_path
        try:
            with path.open("r", encoding="utf-8") as handle:
                data = json.load(handle)
        except Exception as exc:
            errors.append(f"{relative_path}: {exc}")
            continue
        if not isinstance(data, dict):
            errors.append(f"{relative_path}: expected top-level object")
    return errors


def check_dependency_lock() -> list[str]:
    requirements_path = ROOT / "requirements.txt"
    lock_path = ROOT / "requirements-lock.txt"
    if not lock_path.exists():
        return ["requirements-lock.txt: missing lockfile"]

    direct_requirements = {
        _requirement_name(line)
        for line in requirements_path.read_text(encoding="utf-8").splitlines()
        if line.strip() and not line.strip().startswith("#")
    }
    locked_requirements = {
        _requirement_name(line)
        for line in lock_path.read_text(encoding="utf-8").splitlines()
        if line.strip() and not line.strip().startswith("#")
    }
    missing = sorted(direct_requirements - locked_requirements)
    return [f"requirements-lock.txt: missing pin for {name}" for name in missing]


def check_standards_sync_note() -> list[str]:
    path = ROOT / ".agents" / "policies" / "standards-sync.md"
    if not path.exists():
        return [".agents/policies/standards-sync.md: missing"]
    text = path.read_text(encoding="utf-8")
    errors: list[str] = []
    if not re.search(r"Ref observed locally:\s*`?[0-9a-f]{7,40}`?", text):
        errors.append("standards-sync.md: missing applied standards ref")
    standards_path = Path(r"C:\Users\JulianeMatussek\decode-ai-standards")
    if standards_path.exists() and not (standards_path / ".git").exists():
        errors.append("standards-sync.md: local standards path exists but is not a git checkout")
    return errors


def main() -> int:
    ok = True
    if not check_python_compile():
        print("Python compile check failed", file=sys.stderr)
        ok = False

    json_errors = check_json_files()
    for error in json_errors:
        print(error, file=sys.stderr)
    if json_errors:
        ok = False

    lock_errors = check_dependency_lock()
    for error in lock_errors:
        print(error, file=sys.stderr)
    if lock_errors:
        ok = False

    sync_errors = check_standards_sync_note()
    for error in sync_errors:
        print(error, file=sys.stderr)
    if sync_errors:
        ok = False

    tests = subprocess.run(
        [sys.executable, "-m", "unittest", "discover", "-s", "tests"],
        cwd=ROOT,
        check=False,
    )
    if tests.returncode != 0:
        ok = False

    if ok:
        print("All local checks passed")
        return 0
    return 1


if __name__ == "__main__":
    raise SystemExit(main())