Download lingologos_thinkingpython/preflight.py from tailot/lingologos-thinkingpython: direct link, hf CLI and curl.
- Browser
- Download file 3.15 kB
-
https://huggingface.co/datasets/tailot/lingologos-thinkingpython/resolve/main/lingologos_thinkingpython/preflight.py
- Command line
-
hf download hf://datasets/tailot/lingologos-thinkingpython/lingologos_thinkingpython/preflight.py
-
curl -L -o preflight.py https://huggingface.co/datasets/tailot/lingologos-thinkingpython/resolve/main/lingologos_thinkingpython/preflight.py
3.15 kB
| """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} | |