-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
123 lines (103 loc) · 3.68 KB
/
main.py
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/env python3
"""
Script runner that executes multiple Python scripts in sequence.
This module provides functionality to run multiple Python scripts in order,
with comprehensive logging of execution results.
"""
import subprocess
import sys
import os
import logging
from datetime import datetime
from typing import List
# Define the directory for logs and scripts
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
LOG_DIR = os.path.join(SCRIPT_DIR, 'logs')
# Ensure the log directory exists
os.makedirs(LOG_DIR, exist_ok=True)
def setup_logging() -> logging.Logger:
"""
Configure logging with both file and console handlers.
Returns:
logging.Logger: Configured logger instance
"""
# Create logger
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
# Create formatters
file_formatter = logging.Formatter(
'%(asctime)s - %(levelname)s - [%(filename)s:%(lineno)d] - %(message)s'
)
console_formatter = logging.Formatter(
'%(asctime)s - %(levelname)s - %(message)s'
)
# Create file handlers
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
debug_handler = logging.FileHandler(
os.path.join(LOG_DIR, f'script_execution_debug_{timestamp}.log')
)
debug_handler.setLevel(logging.DEBUG)
debug_handler.setFormatter(file_formatter)
error_handler = logging.FileHandler(
os.path.join(LOG_DIR, f'script_execution_error_{timestamp}.log')
)
error_handler.setLevel(logging.ERROR)
error_handler.setFormatter(file_formatter)
# Create console handler
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.INFO)
console_handler.setFormatter(console_formatter)
# Add handlers to logger
logger.addHandler(debug_handler)
logger.addHandler(error_handler)
logger.addHandler(console_handler)
return logger
def run_script(script_name: str, logger: logging.Logger) -> bool:
"""
Executes a Python script using the current Python interpreter.
Args:
script_name (str): The name of the script to run.
logger (logging.Logger): The logger instance for logging messages.
Returns:
bool: True if the script runs successfully, False otherwise.
"""
logger.info(f"Running {script_name}...")
try:
# Run the script using the current Python interpreter
result = subprocess.run(
[sys.executable, script_name],
capture_output=True,
text=True,
check=True
)
logger.info(f"{script_name} completed successfully.")
logger.debug(f"Output:\n{result.stdout}")
return True
except subprocess.CalledProcessError as e:
logger.error(f"Error running {script_name}:")
logger.error(e.stderr) # Log the error message if the script fails
return False
def main():
"""
Main function to run a list of Python scripts sequentially.
The function will stop execution if any script fails, preventing subsequent
scripts from running if an error is encountered.
"""
logger = setup_logging()
# List of scripts to execute in order
scripts: List[str] = [
"netbox_export.py",
"network_scan.py",
"scan_processor.py",
"netbox_import.py"
]
# Iterate over the list of scripts and run each one
for script in scripts:
if not run_script(script, logger):
logger.error(f"Execution stopped due to an error in {script}")
break # Stop execution if a script fails
else:
logger.info("All scripts executed successfully.")
if __name__ == "__main__":
# Run the main function if the script is executed directly
main()