A light-weight module to run shell / commander commands and get output with Python, compatible with Windows, Linux and MacOS.
Project description
PyExecutor - A light-weight module to run shell / commander commands and get output with Python.
Work with both Linux, Windows and MacOS, compatible with executable files like
executor,executor.exeandexecutor.bat.
Requirement
Python >= 3.5
Compatible with Python 3.5 3.6 3.7 3.8
Installation
Install with pip
pip install pyexecutor
QuickStart
Import and run with python
from pyexecutor import Executor
python = Executor('python')
version = python.run('--version')
print(version)
"""
Python 3.8.0
"""
Usage Examples
Run with executor
Get command execution result in string
from pyexecutor import Executor
kubectl = Executor('kubectl')
# Run 'kubectl version --client --short'
result = kubectl.run('version --client --short')
# Set trailer string of executor
kubectl.set_trailer('-o=json')
# Run 'kubectl version --client --short -o=json'
result = kubectl.run('version --client --short')
# Get command output in JSON object (<type 'dict'>), exception will be raised if result string is not JSON serializable.
result = kubectl.run('version --client --short', json_output=True)
Run with commander
from pyexecutor import Commander
commander = Commander()
# Run 'kubectl version --short --client'
result = commander.run('kubectl', 'version --short --client')
# Get command output message.
# {
# "clientVersion": {
# "major": "1",
# "minor": "19",
# "gitVersion": "v1.19.7",
# "gitCommit": "1dd5338295409edcfff11505e7bb246f0d325d15",
# "gitTreeState": "clean",
# "buildDate": "2021-01-13T13:23:52Z",
# "goVersion": "go1.15.5",
# "compiler": "gc",
# "platform": "darwin/amd64"
# }
# }
result.output()
# Get command output in JSON object (<type 'dict'>), exception will be raised if result string is not JSON serializable.
# {'clientVersion': {'major': '1', 'minor': '19', 'gitVersion': 'v1.19.7', 'gitCommit': '1dd5338295409edcfff11505e7bb246f0d325d15', 'gitTreeState': 'clean', 'buildDate': '2021-01-13T13:23:52Z', 'goVersion': 'go1.15.5', 'compiler': 'gc', 'platform': 'darwin/amd64'}}
result.json()
# Get command error message.
# ""
result.error()
# If command exit with zero.
result.ok() == True
# If command exit with non zero.
result.fail() == True
Logging
Loging in executor
import logging
from pyexecutor import Executor
kubectl = Executor('kubectl', logger=logging.getLogger())
Logging in commander
import logging
from pyexecutor import Commander
Commander(logger=logging.getLogger())
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
pyexecutor-1.0.9.tar.gz
(3.9 kB
view details)
File details
Details for the file pyexecutor-1.0.9.tar.gz.
File metadata
- Download URL: pyexecutor-1.0.9.tar.gz
- Upload date:
- Size: 3.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.7.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6c7d843f1db7f386ed5353aa489a4fc479605668d0d997d1c11f36a6f60533f0
|
|
| MD5 |
0f7c3b39855afd08c0f6d9eaef6c58e8
|
|
| BLAKE2b-256 |
5001c1b5f4091b69e40a9d479ad3a7cd7e5329bc93f20274f81f9848d8bcfe1d
|