subprocrunner

A Python wrapper library for subprocess module.


Keywords
library, subprocess, python-library, wrapper-library
License
MIT
Install
pip install subprocrunner==1.2.3

Documentation

subprocrunner

Summary

A Python wrapper library for subprocess module.

PyPI package version Supported Python versions Supported Python implementations CI status of Linux/macOS/Windows Test coverage CodeQL

Usage

Execute a command

Sample Code
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

Raise an exception when a command execution failed

Sample Code
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
Output
SubprocessRunner(command='echo test', returncode='not yet executed', dryrun=True)
return code: 0
stdout: 

Get execution command history

Sample Code
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