Giter Club home page Giter Club logo

subprocrunner's Introduction

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

subprocrunner's People

Contributors

dependabot[bot] avatar thombashi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

ekapujiw2002

subprocrunner's Issues

Add timeout option

Can someone please add explicit timeout option.

It can be easily implemented with

SubprocessRunner("ping ya.ru -t").run(timeout=2)

but IDE support for explicit option name is more beautiful and error-safe.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.