Execution
provide.foundation.process.aio.execution
¶
TODO: Add module docstring.
Classes¶
Functions¶
async_run
async
¶
async_run(
cmd: list[str] | str,
cwd: str | Path | None = None,
env: Mapping[str, str] | None = None,
capture_output: bool = True,
check: bool = True,
timeout: float | None = None,
input: bytes | None = None,
shell: bool = False,
**kwargs: Any
) -> CompletedProcess
Run a subprocess command asynchronously.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cmd
|
list[str] | str
|
Command and arguments as a list |
required |
cwd
|
str | Path | None
|
Working directory for the command |
None
|
env
|
Mapping[str, str] | None
|
Environment variables (if None, uses current environment) |
None
|
capture_output
|
bool
|
Whether to capture stdout/stderr |
True
|
check
|
bool
|
Whether to raise exception on non-zero exit |
True
|
timeout
|
float | None
|
Command timeout in seconds |
None
|
input
|
bytes | None
|
Input to send to the process |
None
|
shell
|
bool
|
Whether to execute via shell |
False
|
**kwargs
|
Any
|
Additional subprocess arguments |
{}
|
Returns:
| Type | Description |
|---|---|
CompletedProcess
|
CompletedProcess with results |
Raises:
| Type | Description |
|---|---|
ValidationError
|
If command type and shell parameter mismatch |
ProcessError
|
If command fails and check=True |
ProcessTimeoutError
|
If timeout is exceeded |
Source code in provide/foundation/process/aio/execution.py
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 | |
check_process_success
¶
check_process_success(
process: Process,
cmd_str: str,
capture_output: bool,
stdout_str: str,
stderr_str: str,
check: bool,
) -> None
Check if process succeeded and raise if needed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
process
|
Process
|
Completed subprocess |
required |
cmd_str
|
str
|
Command string for error messages |
required |
capture_output
|
bool
|
Whether output was captured |
required |
stdout_str
|
str
|
Standard output |
required |
stderr_str
|
str
|
Standard error |
required |
check
|
bool
|
Whether to raise on non-zero exit |
required |
Raises:
| Type | Description |
|---|---|
ProcessError
|
If check=True and process failed |
Source code in provide/foundation/process/aio/execution.py
cleanup_process
async
¶
Clean up process resources.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
process
|
Process | None
|
Subprocess to clean up |
required |
Source code in provide/foundation/process/aio/execution.py
communicate_with_timeout
async
¶
communicate_with_timeout(
process: Process,
input: bytes | None,
timeout: float | None,
cmd_str: str,
) -> tuple[bytes | None, bytes | None]
Communicate with process with optional timeout.
Uses background tasks to continuously read stdout/stderr to ensure no output is lost on timeout.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
process
|
Process
|
Subprocess to communicate with |
required |
input
|
bytes | None
|
Input bytes for stdin |
required |
timeout
|
float | None
|
Optional timeout in seconds |
required |
cmd_str
|
str
|
Command string for error messages |
required |
Returns:
| Type | Description |
|---|---|
tuple[bytes | None, bytes | None]
|
Tuple of (stdout, stderr) bytes |
Raises:
| Type | Description |
|---|---|
ProcessTimeoutError
|
If timeout exceeded |
Source code in provide/foundation/process/aio/execution.py
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 | |
create_completed_process_result
¶
create_completed_process_result(
cmd: list[str] | str,
process: Process,
stdout: bytes | None,
stderr: bytes | None,
cwd: str | None,
env: Mapping[str, str] | None,
run_env: dict[str, str],
) -> CompletedProcess
Create a CompletedProcess from subprocess results.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cmd
|
list[str] | str
|
Command that was executed |
required |
process
|
Process
|
Completed subprocess |
required |
stdout
|
bytes | None
|
Standard output bytes |
required |
stderr
|
bytes | None
|
Standard error bytes |
required |
cwd
|
str | None
|
Working directory |
required |
env
|
Mapping[str, str] | None
|
Original environment mapping |
required |
run_env
|
dict[str, str]
|
Actual environment used |
required |
Returns:
| Type | Description |
|---|---|
CompletedProcess
|
CompletedProcess with results |
Source code in provide/foundation/process/aio/execution.py
create_subprocess
async
¶
create_subprocess(
cmd: list[str] | str,
cmd_str: str,
shell: bool,
cwd: str | None,
run_env: dict[str, str],
capture_output: bool,
input: bytes | None,
kwargs: dict[str, Any],
) -> asyncio.subprocess.Process
Create an async subprocess.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cmd
|
list[str] | str
|
Command to execute |
required |
cmd_str
|
str
|
String representation of command |
required |
shell
|
bool
|
Whether to use shell execution |
required |
cwd
|
str | None
|
Working directory |
required |
run_env
|
dict[str, str]
|
Environment variables |
required |
capture_output
|
bool
|
Whether to capture stdout/stderr |
required |
input
|
bytes | None
|
Input bytes for stdin |
required |
kwargs
|
dict[str, Any]
|
Additional subprocess parameters |
required |
Returns:
| Type | Description |
|---|---|
Process
|
Created subprocess |
Source code in provide/foundation/process/aio/execution.py
read_stream_continuously
async
¶
Continuously read from a stream until EOF.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stream
|
StreamReader | None
|
Stream to read from |
required |
Returns:
| Type | Description |
|---|---|
bytes
|
All bytes read from stream |