A Python wrapper library for subprocess module.
Project Links
Meta
Author: Tsuyoshi Hombashi
Requires Python: >=3.7
Classifiers
Development Status
- 5 - Production/Stable
Intended Audience
- Developers
- Information Technology
License
- OSI Approved :: MIT License
Operating System
- MacOS
- Microsoft :: Windows
- POSIX
- POSIX :: Linux
Programming Language
- Python :: 3
- Python :: 3.7
- Python :: 3.8
- Python :: 3.9
- Python :: 3.10
- Python :: 3.11
- Python :: 3.12
- Python :: 3 :: Only
- Python :: Implementation :: CPython
- Python :: Implementation :: PyPy
Topic
- Software Development :: Libraries
- Software Development :: Libraries :: Python Modules
Typing
- Typed
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
2.0.1
Apr 06, 2024
2.0.0
Jan 15, 2022
1.6.0
Jun 05, 2021
1.5.0
May 30, 2021
1.4.2
May 30, 2021
1.4.1
May 30, 2021
1.4.0
May 26, 2021
1.3.1
May 23, 2021
1.3.0
May 23, 2021
1.2.3
May 16, 2021
1.2.2
May 05, 2021
1.2.1
Apr 04, 2020
1.2.0
Mar 20, 2020
1.1.0
Feb 15, 2020
1.0.1
Feb 14, 2020
1.0.0
Feb 11, 2020
0.17.2
Jan 04, 2020
0.17.1
May 11, 2019
0.17.0
May 02, 2019
0.16.2
Apr 30, 2019
0.16.1
Apr 30, 2019
0.16.0
Jan 12, 2019
0.15.6
Jan 03, 2019
0.15.5
Dec 30, 2018
0.15.4
Dec 24, 2018
0.15.3
Oct 08, 2018
0.15.2
Sep 02, 2018
0.15.1
Aug 18, 2018
0.15.0
Aug 18, 2018
0.14.1
Jul 13, 2018
0.14.0
May 13, 2018
0.13.0
Apr 29, 2018
0.12.2
Apr 29, 2018
0.12.1
Mar 11, 2018
0.12.0
Mar 10, 2018
0.11.0
Feb 04, 2018
0.10.0
Nov 05, 2017
0.9.0
Sep 02, 2017
0.8.7
Aug 16, 2017
0.8.6
Aug 16, 2017
0.8.5
Jul 31, 2017
0.8.4
Jul 16, 2017
0.8.3
Jul 09, 2017
0.8.2
Jun 06, 2017
0.8.1
May 14, 2017
0.8.0
May 14, 2017
0.7.0
Mar 25, 2017
0.6.0
Mar 18, 2017
0.5.0
Mar 17, 2017
0.4.11
Feb 26, 2017
0.4.10
Feb 24, 2017
0.4.9
Feb 15, 2017
0.4.8
Feb 15, 2017
0.4.7
Feb 11, 2017
0.4.6
Jan 22, 2017
0.4.5
Jan 19, 2017
0.4.4
Jan 14, 2017
0.4.3
Jan 14, 2017
0.4.2
Jan 14, 2017
0.4.1
Oct 02, 2016
0.4.0
Aug 27, 2016
0.3.0
Aug 20, 2016
0.2.0
Aug 12, 2016
0.1.0
Aug 12, 2016