A library for executing shell commands in either a blocking or non-blocking way.
Project description
Overview
This library provides a utility to make executing shell commands much simpler than would be posisble using standard modules. It is built on top of the subprocess
module and uses it's subprocess.Popen()
function to execute a shell commands. The library allows shell commands to be executed synchronously (blocking) or asynchronously (non-blocking). It also has utility functions to collect information about the execution such as the ExitCode, the Stdout, and the Stderr.
How It Works
The Shell
module provides the execute_shell_command()
function as the main entry point. This fucntion accepts a number of parameters and allows the user to:
- specify environment variables
- specify the working directory
- specify the shell being used
- specify the number of retries to attempt in the event of a failure
- specify the delay between reties
- specify whether the execution should be blocking or non blocking
In the event of a success (zero exit code) the function will return a ShellCommandResults object. This object contains pointers to the command, the exit code, the Stdout, and the stderr.
Note: The Stdout and Stderr are strings which are decoded from the original byte stream. They will contain all the newlines that were written out to the shell.
In the event of a failure, the function will raise a ShellCommandException which extends the Exception base class. Like the ShellCommandResults object, this exception contains pointers to the command, the exit code, the Stdout, and the stderr.
Getting Started
Here is an example of the simple use case for this utility:
from ShellUtilities import Shell
shell_command_results = Shell.execute_shell_command("echo $MYVAR", env={"MYVAR": "Hello, World!"})
shell_command_results.ExitCode == 0
print(shell_command_results.StdOut) # Hello, World!
print(shell_command_results.Stderr)
And here is a more complex example of this utility running a long command asynchronously while piping the output to stdout in real time:
from ShellUtilities import Shell
# Define an async function to use when we execute the shell command
def stdout_func(stdout_line):
print(stdout_line)
# Run the command and also output while the command is running
shell_command_string = r"echo 'a'; sleep 2; echo 'b'; sleep 2; echo 'c'; sleep 2; echo 'd';"
shell_command_results = Shell.execute_shell_command(shell_command_string, blocking=False, async_buffer_funcs={"stdout": [stdout_func]})
time.sleep(1)
print("hello")
shell_command_results.wait()
# The output of this main thread will be:
# a
# hello
# b
# c
# d
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file ShellUtilities-2.1.14.tar.gz
.
File metadata
- Download URL: ShellUtilities-2.1.14.tar.gz
- Upload date:
- Size: 9.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.11.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 51e4ec507bd0e3a2eb08f19b604e7cf4c507997a8103d8ef0abff2f6980fd135 |
|
MD5 | 94a2de725c4c04d64e8cf3f751d6fca8 |
|
BLAKE2b-256 | 95784fd57a70e619248ddc3de81707b030ce627214ab965b473d84a181c47c39 |
File details
Details for the file ShellUtilities-2.1.14-py3-none-any.whl
.
File metadata
- Download URL: ShellUtilities-2.1.14-py3-none-any.whl
- Upload date:
- Size: 7.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.11.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7d2b373cba4d3423a43000917d22693ba5d997cd65af5a7497ccbfbd3defe70a |
|
MD5 | 4d76138a275a031b52c2e40201890d58 |
|
BLAKE2b-256 | 02b5aad48cf01c29db70d6bd1d720f51eac859c1e94cff57c02048eea6fc7ce3 |