File size: 1,953 Bytes
48b5986
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from pathlib import Path
import re
import sys


PROJECT_ROOT = Path(__file__).resolve().parents[1]
MODEL_ROOT = PROJECT_ROOT / "model"
SCRIPT_ROOT = PROJECT_ROOT / "scripts"


def _python_files(root: Path):
    return [path for path in root.rglob("*.py") if "__pycache__" not in path.parts]


def _check_no_onescience_models_imports():
    offenders = []
    for path in _python_files(MODEL_ROOT):
        text = path.read_text(encoding="utf-8")
        if "onescience.models." in text:
            offenders.append(str(path.relative_to(PROJECT_ROOT)))
    return offenders


def _check_no_legacy_imports():
    pattern = re.compile(
        r"^\s*(from\s+(esm|openfold)(\.|\s)|import\s+(esm|openfold)(\.|\s|$))",
        re.MULTILINE,
    )
    offenders = []
    for root in (MODEL_ROOT, SCRIPT_ROOT):
        for path in _python_files(root):
            text = path.read_text(encoding="utf-8")
            if pattern.search(text):
                offenders.append(str(path.relative_to(PROJECT_ROOT)))
    return offenders


def _check_script_bootstrap():
    offenders = []
    for path in _python_files(SCRIPT_ROOT):
        text = path.read_text(encoding="utf-8")
        if "model.esm" in text and '_PROJECT_ROOT / "model"' not in text:
            offenders.append(str(path.relative_to(PROJECT_ROOT)))
    return offenders


def main():
    checks = {
        "onescience.models imports": _check_no_onescience_models_imports(),
        "legacy esm/openfold imports": _check_no_legacy_imports(),
        "script project-root bootstrap": _check_script_bootstrap(),
    }
    failed = {name: offenders for name, offenders in checks.items() if offenders}
    if failed:
        for name, offenders in failed.items():
            print(f"{name}:")
            for offender in offenders:
                print(f"  {offender}")
        return 1

    print("Import boundary checks passed")
    return 0


if __name__ == "__main__":
    sys.exit(main())