prefect_shell.commands
Tasks for interacting with shell commands
Classes
ShellOperation
Bases: JobBlock
A block representing a shell operation, containing multiple commands.
For long-lasting operations, use the trigger method and utilize the block as a context manager for automatic closure of processes when context is exited. If not, manually call the close method to close processes.
For short-lasting operations, use the run method. Context is automatically managed with this method.
Attributes:
Name | Type | Description |
---|---|---|
commands |
List[str]
|
A list of commands to execute sequentially. |
stream_output |
bool
|
Whether to stream output. |
env |
Dict[str, str]
|
A dictionary of environment variables to set for the shell operation. |
working_dir |
DirectoryPath
|
The working directory context the commands will be executed within. |
shell |
str
|
The shell to use to execute the commands. |
extension |
Optional[str]
|
The extension to use for the temporary file.
if unset defaults to |
Examples:
Load a configured block:
from prefect_shell import ShellOperation
shell_operation = ShellOperation.load("BLOCK_NAME")
Source code in prefect_shell/commands.py
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 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 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 |
|
Functions
__aenter__
async
Asynchronous version of the enter method.
Source code in prefect_shell/commands.py
420 421 422 423 424 |
|
__aexit__
async
Asynchronous version of the exit method.
Source code in prefect_shell/commands.py
426 427 428 429 430 |
|
__enter__
Enter the context of the job block.
Source code in prefect_shell/commands.py
432 433 434 435 436 |
|
__exit__
Exit the context of the job block.
Source code in prefect_shell/commands.py
438 439 440 441 442 |
|
aclose
async
Asynchronous version of the close method.
Source code in prefect_shell/commands.py
414 415 416 417 418 |
|
close
async
Close the job block.
Source code in prefect_shell/commands.py
406 407 408 409 410 411 412 |
|
run
async
Runs a shell command, but unlike the trigger method,
additionally waits and fetches the result directly, automatically managing
the context. This method is ideal for short-lasting shell commands;
for long-lasting shell commands, it is
recommended to use the trigger
method instead.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
**open_kwargs |
Dict[str, Any]
|
Additional keyword arguments to pass to |
{}
|
Returns:
Type | Description |
---|---|
List[str]
|
The lines output from the shell command as a list. |
Examples:
Sleep for 5 seconds and then print "Hello, world!":
from prefect_shell import ShellOperation
shell_output = ShellOperation(
commands=["sleep 5", "echo 'Hello, world!'"]
).run()
Source code in prefect_shell/commands.py
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 |
|
trigger
async
Triggers a shell command and returns the shell command run object
to track the execution of the run. This method is ideal for long-lasting
shell commands; for short-lasting shell commands, it is recommended
to use the run
method instead.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
**open_kwargs |
Dict[str, Any]
|
Additional keyword arguments to pass to |
{}
|
Returns:
Type | Description |
---|---|
ShellProcess
|
A |
Examples:
Sleep for 5 seconds and then print "Hello, world!":
from prefect_shell import ShellOperation
with ShellOperation(
commands=["sleep 5", "echo 'Hello, world!'"],
) as shell_operation:
shell_process = shell_operation.trigger()
shell_process.wait_for_completion()
shell_output = shell_process.fetch_result()
Source code in prefect_shell/commands.py
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 |
|
ShellProcess
Bases: JobRun
A class representing a shell process.
Source code in prefect_shell/commands.py
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 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
|
Attributes
pid: int
property
The PID of the process.
Returns:
Type | Description |
---|---|
int
|
The PID of the process. |
return_code: Optional[int]
property
The return code of the process.
Returns:
Type | Description |
---|---|
Optional[int]
|
The return code of the process, or |
Functions
fetch_result
async
Retrieve the output of the shell operation.
Returns:
Type | Description |
---|---|
List[str]
|
The lines output from the shell operation as a list. |
Source code in prefect_shell/commands.py
189 190 191 192 193 194 195 196 197 198 199 |
|
wait_for_completion
async
Wait for the shell command to complete after a process is triggered.
Source code in prefect_shell/commands.py
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
|
Functions
shell_run_command
async
Runs arbitrary shell commands.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
command |
str
|
Shell command to be executed; can also be provided post-initialization by calling this task instance. |
required |
env |
Optional[dict]
|
Dictionary of environment variables to use for the subprocess; can also be provided at runtime. |
None
|
helper_command |
Optional[str]
|
String representing a shell command, which
will be executed prior to the |
None
|
shell |
Optional[str]
|
Shell to run the command with. |
None
|
extension |
Optional[str]
|
File extension to be appended to the command to be executed. |
None
|
return_all |
bool
|
Whether this task should return all lines of stdout as a list, or just the last line as a string. |
False
|
stream_level |
int
|
The logging level of the stream;
defaults to 20 equivalent to |
INFO
|
cwd |
Union[str, bytes, PathLike, None]
|
The working directory context the command will be executed within |
None
|
Returns:
Type | Description |
---|---|
Union[List, str]
|
If return all, returns all lines as a list; else the last line as a string. |
Example
List contents in the current directory.
from prefect import flow
from prefect_shell import shell_run_command
@flow
def example_shell_run_command_flow():
return shell_run_command(command="ls .", return_all=True)
example_shell_run_command_flow()
Source code in prefect_shell/commands.py
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 124 125 |
|