-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathrun_all_playgrounds.py
More file actions
95 lines (75 loc) · 3.21 KB
/
Copy pathrun_all_playgrounds.py
File metadata and controls
95 lines (75 loc) · 3.21 KB
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
86
87
88
89
90
91
92
93
94
95
import subprocess
import os
import sys
from datetime import datetime
# Paths
playgrounds_DIR = os.path.join(os.getcwd(), "playgrounds")
# To this for WSL:
OUTPUT_DIR = "/mnt/c/Users/ben/Niagara4.11/JENEsys"
# The LOG_FILE line can remain the same, it will be created in your current directory
LOG_FILE = "/mnt/c/Users/ben/Niagara4.11/JENEsys/run_playgrounds_log.txt"
STOP_ON_ERROR = True
def write_log(lines):
"""Helper function to write logs to the file."""
with open(LOG_FILE, "a") as f:
# Add a timestamp to each log session
f.write(
f"\n--- Log session: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')} ---\n"
)
f.writelines(line + "\n" for line in lines)
def main():
log_lines = []
# Walk all subdirectories under playgrounds_DIR
scripts = [
os.path.relpath(
os.path.join(root, f), start=os.getcwd()
) # relative to project root
for root, _, files in os.walk(playgrounds_DIR)
for f in files
if f.endswith(".py")
]
print(f"Found {len(scripts)} example scripts. Running them now...\n")
for script_path in scripts:
# Convert path to module notation (playgrounds.simple_logic.addition_complicated)
module_name = script_path[:-3].replace(
os.sep, "."
) # strip ".py" and replace / with .
cmd = [sys.executable, "-m", module_name, "-o", OUTPUT_DIR]
log_lines.append(f"\n=== Running: {module_name} ===")
print(f"[RUNNING] {module_name} -> Output: {OUTPUT_DIR}")
try:
# Key Change 1: Set check=True to automatically raise an error on failure
result = subprocess.run(cmd, capture_output=True, text=True, check=True)
# This code will only be reached if the script runs successfully
msg = f"[SUCCESS] {module_name}\n{result.stdout}"
print(msg)
log_lines.append(msg)
# Key Change 2: Catch the specific error for a failed subprocess
except subprocess.CalledProcessError as e:
err = (
f"[ERROR] {module_name} halted the execution.\n"
f"Exit Code: {e.returncode}\n"
f"--- STDERR ---\n{e.stderr}\n"
f"--- STDOUT ---\n{e.stdout}"
)
print(err)
log_lines.append(err)
# Key Change 3: Write to log and exit immediately
print("\nWriting logs and exiting due to error.")
write_log(log_lines)
if STOP_ON_ERROR:
sys.exit(1) # Exit with a non-zero status code to indicate failure
except Exception as e:
# This catches other errors, like the script not being found
err_msg = f"[CRITICAL EXCEPTION] Could not run {module_name} -> {str(e)}"
print(err_msg)
log_lines.append(err_msg)
print("\nWriting logs and exiting due to critical exception.")
write_log(log_lines)
if STOP_ON_ERROR:
sys.exit(1) # Exit with a non-zero status code to indicate failure
# This part is only reached if all scripts succeed
print("\nAll scripts completed successfully.")
write_log(log_lines)
if __name__ == "__main__":
main()