|
20 | 20 | import subprocess |
21 | 21 | import argparse |
22 | 22 | import shutil |
| 23 | +import time |
23 | 24 | from pathlib import Path |
24 | 25 | from langchain_openai import ChatOpenAI |
25 | 26 | from langchain.agents import create_agent |
@@ -52,6 +53,9 @@ def create_llm(model, temperature=0, base_url=None): |
52 | 53 | # Show prompts flag (set by command line) |
53 | 54 | SHOW_PROMPTS = False |
54 | 55 |
|
| 56 | +# Directory where existing generated_scripts runs are moved (create if missing) |
| 57 | +ARCHIVE_RUNS_DIR = "archive_runs" |
| 58 | + |
55 | 59 | # Files and directories to archive after each run |
56 | 60 | # Can include directory names and glob patterns (e.g., "*.npy", "ensemble/", "*.log") |
57 | 61 | ARCHIVE_ITEMS = [ |
@@ -131,6 +135,20 @@ def archive_run_outputs(output_dir, archive_name, error_msg=""): |
131 | 135 | if filepath.is_file(): |
132 | 136 | shutil.move(str(filepath), str(run_output_dir / filepath.name)) |
133 | 137 |
|
| 138 | +def archive_existing_output_dir(output_dir, archive_parent=None): |
| 139 | + """If output_dir exists, move it to archive_parent/output_dir_<unique>, then create fresh output_dir.""" |
| 140 | + output_dir = Path(output_dir) |
| 141 | + archive_dir = Path(archive_parent or ARCHIVE_RUNS_DIR) |
| 142 | + if not output_dir.exists(): |
| 143 | + output_dir.mkdir(parents=True, exist_ok=True) |
| 144 | + return |
| 145 | + archive_dir.mkdir(parents=True, exist_ok=True) |
| 146 | + dest = archive_dir / f"{output_dir.name}_{hex(time.time_ns())[2:10]}" |
| 147 | + shutil.move(str(output_dir), str(dest)) |
| 148 | + print(f"Moved existing {output_dir} to {dest}") |
| 149 | + output_dir.mkdir(parents=True, exist_ok=True) |
| 150 | + |
| 151 | + |
134 | 152 | def detect_run_script(directory): |
135 | 153 | """Find the run script in directory (first run_*.py file)""" |
136 | 154 | directory = Path(directory) |
@@ -225,7 +243,10 @@ async def main(): |
225 | 243 |
|
226 | 244 | SHOW_PROMPTS = args.show_prompts |
227 | 245 | output_dir = "generated_scripts" |
228 | | - |
| 246 | + |
| 247 | + # If output_dir already exists, move it to archive_runs/generated_scripts_<hash>, then create fresh |
| 248 | + archive_existing_output_dir(output_dir) |
| 249 | + |
229 | 250 | # Copy existing scripts |
230 | 251 | current_scripts = copy_existing_scripts(args.scripts, output_dir) |
231 | 252 | run_script_name = detect_run_script(output_dir) |
|
0 commit comments