forked from csml-rpi/Foam-Agent
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit_database.py
More file actions
90 lines (76 loc) · 3.29 KB
/
init_database.py
File metadata and controls
90 lines (76 loc) · 3.29 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
import os
import subprocess
import sys
import argparse
import shlex
def parse_args():
parser = argparse.ArgumentParser(description="Initialize database for Foam-Agent project")
parser.add_argument(
'--openfoam_path',
type=str,
default=os.getenv("WM_PROJECT_DIR"),
help="Path to OpenFOAM installation (WM_PROJECT_DIR)"
)
parser.add_argument(
'--force',
action='store_true',
help="Force re-generate raw tutorial dumps + FAISS indices even if files exist"
)
return parser.parse_args()
def run_command(command_str):
"""
Execute a command string using the current terminal's input/output,
with the working directory set to the directory of the current file.
Parameters:
command_str (str): The command to execute, e.g. "python main.py --output_dir xxxx"
or "bash xxxxx.sh".
"""
# Split the command string into a list of arguments
args = shlex.split(command_str)
# Set the working directory to the directory of the current file
cwd = os.path.dirname(os.path.abspath(__file__))
try:
result = subprocess.run(
args,
cwd=cwd,
check=True,
stdout=sys.stdout,
stderr=sys.stderr,
stdin=sys.stdin
)
print(f"Finished command: Return Code {result.returncode}")
except subprocess.CalledProcessError as e:
print(f"Error running command: {e}")
sys.exit(e.returncode)
def main():
args = parse_args()
print(args)
# Set environment variables
WM_PROJECT_DIR = args.openfoam_path
# Get the directory where this script is located
script_dir = os.path.dirname(os.path.abspath(__file__))
print(f"script_dir: {script_dir}")
SCRIPTS = []
# Preprocess the OpenFOAM tutorials
if args.force or not os.path.exists(f"{script_dir}/database/raw/openfoam_tutorials_details.txt"):
SCRIPTS.append(f"python database/script/tutorial_parser.py --output_dir=./database/raw --wm_project_dir={WM_PROJECT_DIR}")
# (Re)build FAISS indices
if args.force or not os.path.exists(f"{script_dir}/database/faiss/openfoam_command_help"):
SCRIPTS.append(f"python database/script/faiss_command_help.py --database_path=./database")
if args.force or not os.path.exists(f"{script_dir}/database/faiss/openfoam_allrun_scripts"):
SCRIPTS.append(f"python database/script/faiss_allrun_scripts.py --database_path=./database")
if args.force or not os.path.exists(f"{script_dir}/database/faiss/openfoam_tutorials_structure"):
SCRIPTS.append(f"python database/script/faiss_tutorials_structure.py --database_path=./database")
if args.force or not os.path.exists(f"{script_dir}/database/faiss/openfoam_tutorials_details"):
SCRIPTS.append(f"python database/script/faiss_tutorials_details.py --database_path=./database")
if not SCRIPTS:
print("All database files already exist. No initialization needed.")
print("Tip: pass --force to rebuild.")
return
print("Starting database initialization...")
for script in SCRIPTS:
run_command(script)
print("Database initialization completed successfully.")
if __name__ == "__main__":
## python init_database.py --openfoam_path $WM_PROJECT_DIR
main()