Summary
A Python wrapper library for subprocess module.
Usage
Execute a command
- Sample Code:
from subprocrunner import SubprocessRunner runner = SubprocessRunner(["echo", "test"]) print(runner) print(f"return code: {runner.run()}") print(f"stdout: {runner.stdout}") runner = SubprocessRunner(["ls", "__not_exist_dir__"]) print(runner) print(f"return code: {runner.run()}") print(f"stderr: {runner.stderr}")- Output:
SubprocessRunner(command='echo test', returncode='not yet executed') return code: 0 stdout: test SubprocessRunner(command='ls __not_exist_dir__', returncode='not yet executed') return code: 2 stderr: ls: cannot access '__not_exist_dir__': No such file or directory
Execute a command with retries
- Sample Code:
from subprocrunner import Retry, SubprocessRunner SubprocessRunner(command).run(retry=Retry(total=3, backoff_factor=0.2, jitter=0.2))
Raise an exception when a command execution failed
- Sample Code:
import sys from subprocrunner import SubprocessRunner from subprocrunner.error import CalledProcessError runner = SubprocessRunner("ls not-exist-dir") # raise an exception at run try: runner.run(check=True) except CalledProcessError as e: print(f"run(check=True): {e}\n{e.stderr}", file=sys.stderr) # raise an exception after run runner.run() try: runner.raise_for_returncode() except CalledProcessError as e: print(f"raise_for_returncode(): {e}\n{e.stderr}", file=sys.stderr)- Output:
run(check=True): Command 'ls not-exist-dir' returned non-zero exit status 2. ls: cannot access 'not-exist-dir': No such file or directory raise_for_returncode(): Command 'ls not-exist-dir' returned non-zero exit status 2. ls: cannot access 'not-exist-dir': No such file or directory
dry run
Commands are not actually run when passing dry_run=True to SubprocessRunner class constructor.
- Sample Code:
from subprocrunner import SubprocessRunner runner = SubprocessRunner("echo test", dry_run=True) print(runner) print(f"return code: {runner.run()}") print(f"stdout: {runner.stdout}")- Output:
SubprocessRunner(command='echo test', returncode='not yet executed', dryrun=True) return code: 0 stdout:
Get execution command history
- Sample Code:
from subprocrunner import SubprocessRunner SubprocessRunner.clear_history() SubprocessRunner.is_save_history = True SubprocessRunner(["echo", "hoge"]).run() SubprocessRunner(["echo", "foo"]).run() print("\n".join(SubprocessRunner.get_history()))- Output:
echo hoge echo foo
Get a command information
>>> from subprocrunner import Which
>>> which = Which("ls")
>>> which.is_exist()
True
>>> which.abspath()
'/usr/bin/ls'
>>> which
command=ls, is_exist=True, abspath=/usr/bin/ls
Installation
Install from PyPI
pip install subprocrunner
Install from PPA (for Ubuntu)
sudo add-apt-repository ppa:thombashi/ppa sudo apt update sudo apt install python3-subprocrunner
Dependencies
Optional dependencies
- loguru
Used for logging if the package installed
Metadata
Release files for subprocrunner 2.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| subprocrunner-2.0.1.tar.gz | 15.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| subprocrunner-2.0.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 26.3 kB
Release files / subprocrunner-2.0.1.tar.gz
| Download URL | subprocrunner-2.0.1.tar.gz |
|---|---|
| Size | 15.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3723226a1bf6b51569fd8f82aa4f0588eef7adf129d16aac8b317555624e8f69
|
|
BLAKE2b-256 checksum How to use checksums |
57a6e3a704af7236df05f423fe8c6bc169b752ab0a2a9e266c27d608d3366152
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.4
|
Release files / subprocrunner-2.0.1-py3-none-any.whl
| Download URL | subprocrunner-2.0.1-py3-none-any.whl |
|---|---|
| Size | 11.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
778e1bcec011b3214d247bf65c42b26f3d3f0a25d12188def6b9574c6671e166
|
|
BLAKE2b-256 checksum How to use checksums |
26f2d8361d6b9ac84656122e01c2c0c677e2b64f94d799149020a4b1869b337e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.4
|