File size: 3,154 Bytes
4bc63c7 | 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 | """Workspace admission: is this project inside the mission of section 1.1.
Runs before any node. A workspace that fails admission never sees a plan: the answer is
the reason code, and the reason is the deliverable.
"""
import ast
import os
from typing import Any, Dict, List, Optional, Tuple
TEST_HINTS = ("test_", "_test", "tests", "conftest.py")
GENERATED_HINTS = ("_pb2.py", "_pb2_grpc.py", "generated", "__init__.py.bak", ".pyc")
DYNAMIC_ATTR_CALLS = ("getattr", "setattr", "hasattr")
def _is_test_file(path: str) -> bool:
name = os.path.basename(path)
return name.startswith("test_") or name.endswith("_test.py") or name == "conftest.py" \
or os.sep + "tests" + os.sep in path or path.startswith("tests" + os.sep)
def python_files(workspace: str) -> List[str]:
out: List[str] = []
for base, dirs, files in os.walk(workspace):
dirs[:] = [d for d in dirs if d not in ("__pycache__", ".git", ".venv", "build")]
for name in files:
if name.endswith(".py"):
rel = os.path.relpath(os.path.join(base, name), workspace).replace("\\", "/")
out.append(rel)
return sorted(out)
def target_rejects(rel: str, source: str) -> List[str]:
reasons: List[str] = []
if any(h in rel for h in GENERATED_HINTS):
reasons.append("GENERATED_TARGET")
if "\r\n" in source:
reasons.append("CRLF_TARGET")
lines = source.splitlines()
if any(line.startswith("\t") for line in lines):
reasons.append("TAB_INDENTED_TARGET")
try:
source.encode("ascii")
except UnicodeEncodeError:
reasons.append("NOT_ASCII_TARGET")
try:
tree = ast.parse(source)
except SyntaxError:
return reasons
names = {n.id for n in ast.walk(tree) if isinstance(n, ast.Name)}
if names & set(DYNAMIC_ATTR_CALLS):
reasons.append("DYNAMIC_REACH")
return reasons
def admit(workspace: str, targets: Optional[List[str]] = None) -> Dict[str, Any]:
"""Return {"admitted": bool, "reasons": [...], "tests": [...], "targets": [...]}."""
if not os.path.isdir(workspace):
return {"admitted": False, "reasons": ["NO_TEST_FILE"], "tests": [], "targets": []}
files = python_files(workspace)
tests = [f for f in files if _is_test_file(f)]
if not tests:
return {"admitted": False, "reasons": ["NO_TEST_FILE"], "tests": [], "targets": files}
picked = targets or [f for f in files if not _is_test_file(f)]
reasons: List[str] = []
checked: List[str] = []
for rel in picked:
path = os.path.join(workspace, rel)
try:
with open(path, "r", encoding="utf-8", newline="") as handle:
source = handle.read()
except (OSError, UnicodeDecodeError):
reasons.append("NOT_ASCII_TARGET")
continue
found = target_rejects(rel, source)
if found:
reasons.extend(found)
else:
checked.append(rel)
uniq = sorted(set(reasons))
return {"admitted": bool(checked) and not uniq,
"reasons": uniq,
"tests": tests,
"targets": checked}
|