util_debloat_thread_handler

Path: /utilities/util_debloat_thread_handler.py

Overview

The util_debloat_thread_handler module provides functionality to execute multiple debloat scripts concurrently with configurable worker limits and error cancellation support.

Purpose & Use Cases

Run a collection of system debloat scripts in parallel to reduce total execution time.

Optionally stop remaining scripts upon encountering the first failure to prevent cascading errors.

Dependencies

utilities.util_logger.logger

utilities.util_error_popup.show_error_popup

Configuration & Parameters

Parameter Type Description Default
max_workers int | None Maximum number of concurrent worker threads for script execution. None (uses number of scripts)
stop_on_error bool Whether to cancel all remaining scripts upon the first script failure. True

Usage Example

from utilities.util_debloat_thread_handler import ScriptProcessHandler
        
        handler = ScriptProcessHandler(max_workers=4, stop_on_error=True)
        handler.add_script('debloat_network.py')
        handler.add_script('debloat_services.py')
        handler.run_all()
        

Behavior & Implementation

The module defines a ScriptProcessHandler class that collects script paths and executes them using a ThreadPoolExecutor. Each script runs as its own subprocess, with stdout and stderr streamed to dedicated logging threads. A shared cancellation event is monitored to terminate any in-progress scripts if stop_on_error is enabled and an error occurs.

Error Handling

Failures in launching or executing a script are logged via logger.error or logger.exception. If a script exits with a non-zero code, show_error_popup is invoked to inform the user. When stop_on_error is True, remaining scripts are cancelled and a RuntimeError is raised.

Security Considerations

Scripts are executed with the same privileges as the calling process. Only trusted script paths should be provided to avoid arbitrary code execution risks. No additional input sanitization is performed.

Logging

Uses the Raven Development Team’s util_logger at INFO, WARNING, and ERROR levels. Logs include script launch notifications, real-time stdout/stderr content, cancellation events, and uncaught exceptions.

Testing & Validation

To validate behavior, create test scripts that exit with code 0 and non-zero codes. Confirm concurrent execution, real-time logging, and cancellation on failure. Unit tests can mock subprocess.Popen and the cancellation event.

Author

Author: Raven Development Team