Simplified and configurable wrapper for Python's subprocess.Popen
Project description
>_ comrun
comrun (shorthand for command runner) is a simplified and configurable wrapper for
Python's subprocess.Popen, focused on making
it easy to run external commands from Python scripts.
Features
- Easy-to-use interface for executing commands directly.
- Captures command output and exit code for further processing.
- Simplifies error handling and logging of external commands.
Installation
Install comrun using poetry:
poetry add comrun
Usage
Just create a CommandRunner instance and call it with the command you want to run:
from comrun import CommandRunner
comrun = CommandRunner()
# Run your command – instance is callable
result = comrun('echo "The cake is a lie."')
# (prints "The cake is a lie." to the console)
The same comrun instance can be reused indefinitely to call other commands.
Exit status
Calling CommandRunner returns a CommandResult object, which contains exit code of the command:
if result.success: # <- Shorthand for result.exit_code == 0
print(f"Command succeeded with exit code 0")
if result.failure: # <- Shorthand for result.exit_code != 0
print(f"Command failed with exit code {result.exit_code}")
You can also check for success just by using the CommandResult object as a boolean (it is truthy only if the command
was successful):
if result:
print(f"Command succeeded with exit code 0")
else:
print(f"Command failed with exit code {result.exit_code}")
Output
By default, comrun prints the command's output to the console as it appears. You can disable this behavior by
passing quiet=True:
comrun('echo "Potato."')
# (prints "Potato." to the console)
comrun('echo "POTATO!"', quiet=True)
# (prints nothing, no matter how loud the command is)
Regardless of the quiet parameter, CommandResult always contains the command's output (both stdout and stderr):
# Each output can be accessed as a single string...
print(result.stdout)
# ...or as a list of lines, depending on what you need.
for line in result.stdout.lines:
print(line)
You can also access the combined output of both stdout and stderr in the correct chronological order using output
property:
print(result.output) # <- Prints all output lines of the command
Advanced usage
Exceptions
By default, comrun will quietly execute the command even if it fails and returns a non-zero exit code. If you want
to catch it as an exception instead, you can pass check=True:
result = comrun('exit 1')
# (executed normally, result.exit_code == 1)
result = comrun('exit 1', check=True)
# (raises a CommandError because the command fails)
Live output
By default, comrun uses rich's Console for printing stdout and stderr lines as they come in from the command
subprocess (unless quiet=True is passed). This output handling can be changed by passing a
custom on_line callback to the CommandRunner
constructor:
from comrun import CommandContext, CommandRunner
def print_line_with_stream_name(line: str, stream: str, ctx: CommandContext):
# Note: line argument does not have a trailing newline character
print(f"{stream.upper()} | {ctx.command} | {line}")
comrun = CommandRunner(on_line=print_line_with_stream_name)
comrun('echo "For science."')
# (prints "STDOUT | For science." to the console)
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file comrun-0.3.0.tar.gz.
File metadata
- Download URL: comrun-0.3.0.tar.gz
- Upload date:
- Size: 6.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.17
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
437c4b96aee7d604312782597574038fc97bd80697076d53bf3a80681dea828c
|
|
| MD5 |
897c44dd4bdb3a3e04a18351d54f142f
|
|
| BLAKE2b-256 |
bc693f3f21b38a572321822ab33b3ba8da61e34fe4badf107e55f677e1654fda
|
File details
Details for the file comrun-0.3.0-py3-none-any.whl.
File metadata
- Download URL: comrun-0.3.0-py3-none-any.whl
- Upload date:
- Size: 7.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.17
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2a803ac4f58a217b036d42b4b41acb97701ee09ce45c60d161e34fe9333d1344
|
|
| MD5 |
1874a2f2d6c05baf29ff3f60494205f0
|
|
| BLAKE2b-256 |
87b8c1db6fd1024f0edad8ea63a45d330369e288dfde956b49a8f1a2b36e06ca
|