A library for easily running shell commands, whether standalone or piped.
Project description
bombshell
A library for easily running subprocesses in Python, whether single or piped.
Why?
Python's subprocess library is capable of running whatever you need it to, but isn't always the most friendly or readable option, even when running a single process:
res = subprocess.run(("echo", "1"), capture_output=True, text=True)
print(res.stdout) # "1\n"
Needing to pass capture_output=True, text=True all the time is annoying when those are probably the most common default. Plus, the command has to be passed as a tuple/list, rather than just the arguments themselves.
res = bombshell.Process("echo", "1").exec()
print(res.stdout) # "1\n"
print(type(res.stdout)) # <class 'str'>
But if you want bytes, then you can have bytes:
res = bombshell.Process("echo", "1").exec(mode=bytes)
print(res.stdout) # b"1\n"
print(type(res.stdout)) # <class 'bytes'>
subprocess is also really picky about the types of arguments you pass in:
res = subprocess.run(("echo", 1))
TypeError: "expected str, bytes or os.PathLike object, not int"
Why, though? bombshell automatically calls str() on every argument passed to it.
res = bombshell.Process("echo", 1).exec()
print(res.stdout) # "1\n"
print(res.exit_code) # 0
subprocess also makes piping commands way more difficult than it needs to be. What's easy in Bash...
res=$(echo "hello\nworld\ngoodbye" | grep "l")
echo "$res" # "hello\nworld"
...is way more complicated with subprocess since you have to individually manage both sides of the pipe.
parent = subprocess.Popen(("echo", "hello\nworld\ngoodbye"), stdout=subprocess.PIPE)
child = subprocess.Popen(("grep", "l"), stdin=parent.stdout, capture_output=True, text=True)
stdout, _ = child.communicate()
print(stdout) # "hello\nworld"
There must be a better way.
res = bombshell.Process("echo", "hello\nworld\ngoodbye").pipe_into("grep", "l")
print(res.stdout) # "hello\nworld"
# Process supports .__or__, so we can also do
p1 = bombshell.Process("echo", "hello\nworld\ngoodbye")
p2 = bombshell.Process("grep", "l")
res = (p1 | p2).exec()
print(res.stdout) # "hello\nworld"
We can also pass environment variables to individual commands:
res = subprocess.run(("printenv", "FOO"), capture_output=True, text=True, env={"FOO": "bar"})
print(res.stdout) # "bar\n"
res = bombshell.Process("printenv", "FOO").with_env(FOO="bar").exec()
print(res.stdout) # "bar\n"
subprocess also makes it somewhat difficult to chain commands (command1 && command2), preferring:
# only "echo 1" and "echo 2" will successfully run; "echo 3" will not
procs = [("echo", "1"), ("echo", "2"), ("false",), ("echo", "3")]
for proc in procs:
res = subprocess.run(proc, capture_output=True, text=True)
if res.returncode:
break
whereas we can do
res = bombshell.Process("echo", 1).then("echo", 2).then("false").then("echo", "3")
print(res.command) # echo 1 && echo 2 && false && echo 3
print(res.stdout) # "1\n2\n"
print(res.exit_code) # 1
print(res.exit_codes) # [0, 0, 1] <-- indicating that the first two echo commands exited with 0, then false exited with 1
Installation
bombshell is supported on Python 3.10 and newer and can be easily installed with a package manager such as:
# using pip
$ pip install bombshell
# using uv
$ uv add bombshell
bombshell has no other external dependencies (except typing_extensions, only on Python 3.10).
Documentation
PipelineError
An error that is thrown by CompletedProcess.check() when the pipeline has errored. It stores the calling process under its .process attribute.
try:
bombshell.Process("false").exec().check()
except bombshell.PipelineError as err:
# err.process == bombshell.Process("false").exec()
print(err.process.command) # "false"
print(err.process.exit_codes) # [1]
CompletedProcess[S]
An object that stores the state of a completed process. In particular, its attributes are:
args: tuple[tuple[str, ...], ...]: the arguments that were passed to the process(es) that gave this resultcommand: str: a string representation of the command as would be run on the command lineexit_codes: list[int]: all of the exit codes for the various processes in the pipelineexit_code: int: the exit code of the last executed part of the pipeline (and thus the exit code for the entire pipeline)stdout: str | bytes: the contents of the stdout pipes, if captured..exec(mode=str)(the default) means that this will be a string;.exec(mode=bytes)means this will be a byte string. Note:(p1 | p2).exec().stdoutwill contain only the stdout forp2;p1.then(p2).exec().stdoutwill contain both.stderr: str | bytes: the contents of the stderr pipes, if captured..exec(mode=str)(the default) means that this will be a string;.exec(mode=bytes)means this will be a byte string. This will always include the combination of all stderr pipes, if captured.
res = (
bombshell.Process("echo", 1)
.pipe_into("echo", 2)
.pipe_into("false")
.pipe_into("echo", 3)
.exec()
)
print(res.args) # (("echo", "1"), ("echo", "2"), ("false",), ("echo", "3"))
print(res.command) # "echo 1 | echo 2 | false | echo 3"
print(res.exit_codes) # [0, 0, 1, 0]
print(res.exit_code) # 0
print(res.stdout) # "3\n"
print(res.stderr) # ""
This class also defines a .check method:
res = (
bombshell.Process("echo", 1)
.pipe_into("echo", 2)
.pipe_into("false")
.pipe_into("echo", 3)
.exec()
)
res.check() # passes since the final exit code was zero
res.check(strict=True) # raises PipelineError since there was a failure along the pipeline
Process
A Process object takes a command to run as arguments, along with (optionally) an env mapping to use for it. The object defines:
-
exec(self, stdin: S | None = None, *, capture: bool = True, mode: type[S] = str, merge_stderr: bool = False) -> CompletedProcess[S]: Run the given command.Sis eitherstrorbytes(but must match in all cases).stdinis a str/bytes value (not a pipe/file) to pass as stdin to this command.capture=True(default) means that stdout and stderr will be captured in the resulting CompletedProcess object.modedetermines whether the output is of typestrorbytes. Ifmerge_stderris True, then stderr is redirected to stdout (meaning thatexec().stdoutwill contain both streams and.stderrwill be empty). -
__call__(...): an alias for.exec(...). -
with_env(self, **kwargs) -> Self: return a new Process object with the updated environment variables. Note that this updates the current environment, rather than replacing it. -
pipe_into(self, *args: Any, env: Mapping[str, str] = None | None) -> Pipeline: return a new Pipeline object that representscommand1 | command2. The givenargscan eithe ra series of values to use as a command (such asProcess("echo", 1).pipe_into("echo", 2), equivalent toecho 1 | echo 2), or it can be a singleProcessobject (such asProcess("echo", 1).pipe_into(Process("echo", 2)).) -
__or__(self, other: Self) -> Pipeline: an alias for.pipe_into, but requires that the other object is aProcessobject. -
then(self, *args: Any) -> CommandChain: return a CommandChain object that representscommand1 && command2. The givenargscan be either a series of values to use as a command (such asProcess("echo", 1).then("echo", 2), equivalent toecho 1 && echo 2), or it can be a single Process/Pipeline/CommandChain object (such as `Process("echo", 1).then(Process("echo", 2)).)
Pipeline
A Pipeline is an object that represents a piped series of commands. It provides the same methods to provide parity with Process, though Pipeline.pipe_into and Pipeline.__or__ both support Pipeline as an object.
In practice, it is unlikely that you would create Pipeline objects directly, but rather as Process(...).pipe_into(...).
CommandChain
Like Pipeline, this is an object that represents a chained series of commands. It also provides the same methods to provide parity with Process.
It is unlikely that you would create CommandChain objects directly, but rather as Process(...).then(...).
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 bombshell-0.1.0.tar.gz.
File metadata
- Download URL: bombshell-0.1.0.tar.gz
- Upload date:
- Size: 7.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.18 {"installer":{"name":"uv","version":"0.9.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Manjaro Linux","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2b04be89aa53332e5e253351d0b4b905f28092e37a4533246809a2d58a20af25
|
|
| MD5 |
c83e5fe48daf24debd72c6e7a2e64d3b
|
|
| BLAKE2b-256 |
b0dc928e387e3196d9a6581dae90d1a8d8390f01e653ba470802caef6f671487
|
File details
Details for the file bombshell-0.1.0-py3-none-any.whl.
File metadata
- Download URL: bombshell-0.1.0-py3-none-any.whl
- Upload date:
- Size: 8.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.18 {"installer":{"name":"uv","version":"0.9.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Manjaro Linux","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6075f6920a51f6a3eb23227a9a5f4e6888c9e190c01dc066cb1a9241ded26de5
|
|
| MD5 |
0dcc943dfdd5b6006293705fe4ce4c64
|
|
| BLAKE2b-256 |
67bbfae3739f3faed0efdaa656a577d2ea48b8a291c992e6431b89d258c190f2
|