Skip to main content

A Python wrapper library for subprocess module.

Project description

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

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 retry

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

subprocrunner-2.0.0.tar.gz (14.4 kB view details)

Uploaded Source

Built Distribution

subprocrunner-2.0.0-py3-none-any.whl (10.9 kB view details)

Uploaded Python 3

File details

Details for the file subprocrunner-2.0.0.tar.gz.

File metadata

  • Download URL: subprocrunner-2.0.0.tar.gz
  • Upload date:
  • Size: 14.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.11

File hashes

Hashes for subprocrunner-2.0.0.tar.gz
Algorithm Hash digest
SHA256 fd22aad8befd610461536463955e205122fed80303b814cc926c3a4a8d9aaf3c
MD5 77f5ad3bbba338dcc6875d29eba2c95d
BLAKE2b-256 dac8ec55163826e1d6fe091c2bad0bb281255da6197f96e1162e938be0b27fba

See more details on using hashes here.

File details

Details for the file subprocrunner-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: subprocrunner-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 10.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.11

File hashes

Hashes for subprocrunner-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 34f66b28448e1fb9cb29c29ff0b8e6f1a749acd7ac08cc27956d1dde9059debf
MD5 def045efdf93977a336bb9265bb17469
BLAKE2b-256 b42ebf402424f59ac76355b02117f187f6e39987c8caf427485711e059aa746b

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page