-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
76 lines (61 loc) · 2.22 KB
/
main.py
File metadata and controls
76 lines (61 loc) · 2.22 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
import subprocess
import sys
from typing import List
from pathlib import Path
import datetime
def run_script(script_name: str, logs_dir: Path) -> bool:
"""
Executes a Python script using the current Python interpreter and logs output to a file.
Args:
script_name (str): The name of the script to run.
logs_dir (Path): Path to the directory where logs should be saved.
Returns:
bool: True if the script runs successfully, False otherwise.
"""
print(f"Running {script_name}...")
# Prepare log file path (one log file per script run, timestamped)
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
log_file = logs_dir / f"{script_name}_{timestamp}.log"
try:
# Base command
cmd = [sys.executable, script_name]
# Special case: master_cleanup.py must always be run with --yes
if script_name == "master_cleanup.py":
cmd.append("--yes")
# Open the log file and direct both stdout and stderr into it
with open(log_file, "w", encoding="utf-8") as f:
process = subprocess.run(
cmd,
stdout=f,
stderr=subprocess.STDOUT, # Merge stderr into the same log file
text=True,
check=True
)
print(f"{script_name} completed successfully. Logs: {log_file}")
return True
except subprocess.CalledProcessError:
print(f"Error running {script_name}. Check logs: {log_file}")
return False
def main():
"""
Main function to run a list of Python scripts sequentially.
"""
# Ensure logs directory exists
logs_dir = Path("LOGS")
logs_dir.mkdir(exist_ok=True)
scripts: List[str] = [
"master_cleanup.py",
"netbox_manufacturer.py",
"netbox_rack.py",
"netbox_device.py",
"netbox_module.py",
"netbox_images.py"
]
for script in scripts:
if not run_script(script, logs_dir):
print(f"Execution stopped due to an error in {script}")
break
else:
print("All scripts executed successfully.")
if __name__ == "__main__":
main()