|
3 | 3 | import os |
4 | 4 | import shutil |
5 | 5 | import subprocess |
6 | | -import threading |
7 | 6 | import zipfile |
| 7 | +from concurrent.futures import ThreadPoolExecutor, as_completed |
8 | 8 | from datetime import datetime |
9 | 9 | from typing import Any |
10 | 10 |
|
@@ -225,9 +225,6 @@ def generate_execution_list() -> list | list[str] | list[str | Any]: |
225 | 225 | - 'depth': Comprehensive script execution with data mining and logging scripts |
226 | 226 | - 'vulnscan_ai': Vulnerability scanning script only |
227 | 227 | |
228 | | - Parameters: |
229 | | - None |
230 | | - |
231 | 228 | Returns: |
232 | 229 | list[str]: A list of script file paths to be executed, filtered and modified based on the current action. |
233 | 230 | |
@@ -293,34 +290,42 @@ def execute_scripts(): |
293 | 290 | """Executes the scripts in the execution list based on the action.""" |
294 | 291 | # Check weather to use threading or not, as well as execute code |
295 | 292 | log.info("Starting Logicytics...") |
| 293 | + |
296 | 294 | if ACTION == "threaded" or ACTION == "depth": |
297 | | - def threaded_execution(execution_list_thread, index_thread): |
298 | | - log.debug(f"Thread {index_thread} started") |
| 295 | + |
| 296 | + def execute_single_script(script: str) -> tuple[str, Exception | None]: |
| 297 | + """ |
| 298 | + Executes a single script and logs the result. |
| 299 | +
|
| 300 | + This function executes a single script and logs the result, |
| 301 | + capturing any exceptions that occur during execution |
| 302 | +
|
| 303 | + Parameters: |
| 304 | + script (str): The path to the script to be executed |
| 305 | + """ |
| 306 | + log.debug(f"Executing {script}") |
299 | 307 | try: |
300 | | - log.parse_execution(Execute.script(execution_list_thread[index_thread])) |
301 | | - log.info(f"{execution_list_thread[index_thread]} executed") |
302 | | - except UnicodeDecodeError as err: |
303 | | - log.error(f"Error in thread: {err}") |
| 308 | + log.parse_execution(Execute.script(script)) |
| 309 | + log.info(f"{script} executed") |
| 310 | + return script, None |
304 | 311 | except Exception as err: |
305 | | - log.error(f"Error in thread: {err}") |
306 | | - log.debug(f"Thread {index_thread} finished") |
| 312 | + log.error(f"Error executing {script}: {err}") |
| 313 | + return script, err |
307 | 314 |
|
308 | 315 | log.debug("Using threading") |
309 | | - threads = [] |
310 | 316 | execution_list = generate_execution_list() |
311 | | - for index, _ in enumerate(execution_list): |
312 | | - thread = threading.Thread( |
313 | | - target=threaded_execution, |
314 | | - args=( |
315 | | - execution_list, |
316 | | - index, |
317 | | - ), |
318 | | - ) |
319 | | - threads.append(thread) |
320 | | - thread.start() |
321 | | - |
322 | | - for thread in threads: |
323 | | - thread.join() |
| 317 | + with ThreadPoolExecutor() as executor: |
| 318 | + futures = {executor.submit(execute_single_script, script): script |
| 319 | + for script in execution_list} |
| 320 | + |
| 321 | + for future in as_completed(futures): |
| 322 | + script = futures[future] |
| 323 | + result, error = future.result() |
| 324 | + if error: |
| 325 | + log.error(f"Failed to execute {script}") |
| 326 | + else: |
| 327 | + log.debug(f"Completed {script}") |
| 328 | + |
324 | 329 | elif ACTION == "performance_check": |
325 | 330 | execution_times = [] |
326 | 331 | execution_list = generate_execution_list() |
|
0 commit comments