Commands¶
commands
¶
Safe subprocess execution with logging.
Replaces the PowerShell Invoke-AndLog function from UmeAiRTUtils.psm1. Uses subprocess.run() with explicit argument lists — no shell injection possible.
CommandError(command, return_code, stderr='')
¶
Bases: Exception
Raised when an external command fails.
Source code in src/utils/commands.py
run_and_log(command, args=None, *, cwd=None, ignore_errors=False, timeout=600, env=None, log=None)
¶
Execute an external command, logging both the command and its output.
This is a secure replacement for the PowerShell Invoke-AndLog function. It uses subprocess.run() with an explicit argument list, preventing any shell injection attacks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
command
|
str | Path
|
Path to the executable. |
required |
args
|
list[str] | None
|
List of arguments (NOT a single string — safe by design). |
None
|
cwd
|
Path | None
|
Working directory for the command. |
None
|
ignore_errors
|
bool
|
If True, don't raise on non-zero exit codes. |
False
|
timeout
|
int
|
Command timeout in seconds (default: 10 minutes). |
600
|
env
|
dict[str, str] | None
|
Optional environment variables to set. |
None
|
Returns:
| Type | Description |
|---|---|
CompletedProcess[str]
|
The completed process result. |
Raises:
| Type | Description |
|---|---|
CommandError
|
If command fails and ignore_errors is False. |
Source code in src/utils/commands.py
check_command_exists(command)
¶
Check if a command is available in the system PATH.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
command
|
str
|
The executable name to look for. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the command exists and is executable. |
Source code in src/utils/commands.py
get_command_version(command, version_flag='--version')
¶
Get the version string of a command.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
command
|
str
|
The executable to query. |
required |
version_flag
|
str
|
The flag to get version (default: --version). |
'--version'
|
Returns:
| Type | Description |
|---|---|
str | None
|
The version output string, or None if command fails. |