File size: 734 Bytes
b64d2eb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Execute attached Python files safely-ish and capture final numeric output."""
from __future__ import annotations

import re
import subprocess
import sys
from pathlib import Path
from typing import Optional


def run_python_file(path: Path, timeout: int = 60) -> Optional[str]:
    proc = subprocess.run(
        [sys.executable, str(path)],
        capture_output=True,
        text=True,
        timeout=timeout,
    )
    out = (proc.stdout or "").strip().splitlines()
    if not out:
        return None
    last = out[-1].strip()
    # Prefer a bare integer / float on the last line
    if re.fullmatch(r"-?\d+(\.\d+)?", last):
        return last
    m = re.search(r"-?\d+(\.\d+)?", last)
    return m.group(0) if m else last