File size: 1,411 Bytes
ef987bb | 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 | """Locate the declared Python analysis and run it with bounded time."""
import os
import subprocess
import sys
from pathlib import Path
def entry_script(root):
declaration = root / "entrypoint.txt"
if declaration.is_file():
name = declaration.read_text(encoding="utf-8").strip()
assert name and not any(character in name for character in "\r\n"), "entrypoint.txt needs one filename"
entry = root / name
assert entry.parent == root and entry.suffix == ".py", "declare a root Python script"
assert entry.is_file(), f"declared script {name!r} does not exist"
else:
scripts = list(root.glob("*.py"))
conventional = [path for path in scripts if path.name in {"solve.py", "main.py"}]
options = conventional or scripts
assert len(options) == 1, "provide one unambiguous entry or declare it in entrypoint.txt"
entry = options[0]
assert entry.resolve().parent == root.resolve(), "entry script must be inside the project root"
return entry
def run_analysis(root):
entry = entry_script(root)
env = {name: value for name, value in os.environ.items()
if name.lower() not in {"http_proxy", "https_proxy", "all_proxy"}}
env["PYTHONHASHSEED"] = "1"
subprocess.run([sys.executable, entry.name], cwd=root, env=env, timeout=60, check=True)
if __name__ == "__main__":
run_analysis(Path.cwd())
|