Skip to main content

Execution helpers for simplified usage of subprocess and ssh.

Project description

exec-helpers
============

.. image:: https://travis-ci.org/penguinolog/exec-helpers.svg?branch=master
:target: https://travis-ci.org/penguinolog/exec-helpers
.. image:: https://img.shields.io/appveyor/ci/penguinolog/exec-helpers.svg
:alt: AppVeyor
:target: https://ci.appveyor.com/project/penguinolog/exec-helpers
.. image:: https://coveralls.io/repos/github/penguinolog/exec-helpers/badge.svg?branch=master
:target: https://coveralls.io/github/penguinolog/exec-helpers?branch=master
.. image:: https://img.shields.io/pypi/v/exec-helpers.svg
:target: https://pypi-hypernode.com/pypi/exec-helpers
.. image:: https://img.shields.io/pypi/pyversions/exec-helpers.svg
:target: https://pypi-hypernode.com/pypi/exec-helpers
.. image:: https://img.shields.io/pypi/status/exec-helpers.svg
:target: https://pypi-hypernode.com/pypi/exec-helpers
.. image:: https://img.shields.io/github/license/penguinolog/exec-helpers.svg
:target: https://raw.githubusercontent.com/penguinolog/exec-helpers/master/LICENSE

Execution helpers for simplified usage of subprocess and ssh.
Why another subprocess wrapper and why no clear `paramiko`?

Historically `paramiko` offers good ssh client, but with specific limitations:
you can call command with timeout, but without receiving return code,
or call command and wait for return code, but without timeout processing.

In the most cases, we are need just simple SSH client with comfortable API for calls, calls via SSH proxy and checking return code/stderr.
This library offers this functionality with connection memorizing, deadlock free polling and friendly result objects (with inline decoding of YAML, JSON, binary or just strings).
In addition this library offers the same API for subprocess calls, but with specific limitation: no parallel calls (for protection from race conditions).

Pros:

* STDOUT and STDERR polling during command execution - no deadlocks.
* The same API for subprocess and ssh.
* Connection memorize.
* Free software: Apache license
* Open Source: https://github.com/penguinolog/exec-helpers
* PyPI packaged: https://pypi-hypernode.com/pypi/exec-helpers
* Self-documented code: docstrings with types in comments
* Tested: see bages on top
* Support multiple Python versions:

::

Python 2.7
Python 3.4
Python 3.5
Python 3.6

This package includes:

* `SSHClient` - historically the first one helper, which used for SSH connections and requires memorization
due to impossibility of connection close prediction.
Several API calls for sFTP also presents.

* `SSHAuth` - class for credentials storage. `SSHClient` does not store credentials as-is, but uses `SSHAuth` for it.
Objects of this class can be copied between ssh connection objects, also it used for `execute_through_host`.

* `Subprocess` - `subprocess.Popen` wrapper with timeouts, polling and almost the same API, as `SSHClient`
(except specific flags, like `cwd` for subprocess and `get_tty` for ssh).

* `ExecResult` - class for execution results storage.
Contains exit code, stdout, stderr and getters for decoding as JSON, YAML, string, bytearray and brief strings (up to 7 lines).

* `ExitCodes` - enumerator for standard Linux exit codes. BASH return codes (broduced from signal codes) also available.

Usage
=====

SSHClient
---------

Basic initialization of `SSHClient` can be done without construction of specific objects:

.. code-block:: python

client = exec_helpers.SSHClient(host, username="username", password="password")

If ssh agent is running - keys will be collected by paramiko automatically, but if keys are in specific location
- it should be loaded manually and providen as iterable object of `paramiko.RSAKey`.

For advanced cases or re-use of credentials, `SSHAuth` object should be used.
It can be collected from connection object via property `auth`.

Creation from scratch:

.. code-block:: python

auth = exec_helpers.SSHAuth(
username='username', # type: typing.Optional[str]
password='password', # type: typing.Optional[str]
key=None, # type: typing.Optional[paramiko.RSAKey]
keys=None,
)

Key is a main connection key (always tried first) and keys are alternate keys.
If main key now correct for username - alternate keys tried, if correct key found - it became main.
If no working key - password is used and None is set as main key.

Subprocess
==========

No initialization required.

Base methods
------------
Main methods are `execute`, `check_call` and `check_stderr` for simple executing, executing and checking return code
and executing, checking return code and checking for empty stderr output.
This methods are almost the same for `SSHCleint` and `Subprocess`, except specific flags.

.. code-block:: python

result = helper.execute(
command, # type: str
verbose=False, # type: bool
timeout=None, # type: typing.Optional[int]
**kwargs
)


.. code-block:: python

result = helper.check_call(
command, # type: str
verbose=False, # type: bool
timeout=None, # type: typing.Optional[int]
error_info=None, # type: typing.Optional[str]
expected=None, # type: typing.Optional[typing.Iterable[int]]
raise_on_err=True, # type: bool
**kwargs
)

.. code-block:: python

result = helper.check_stderr(
command, # type: str
verbose=False, # type: bool
timeout=None, # type: typing.Optional[int]
error_info=None, # type: typing.Optional[str]
raise_on_err=True, # type: bool
)

The next command level uses lower level and kwargs are forwarded, so expected exit codes are forwarded from `check_stderr`.
Implementation specific flags are always set via kwargs.

ExecResult
----------

Execution result object has a set of useful properties:

* `cmd` - Command
* `exit_code` - Command return code. If possible to decode using enumerators for Linux -> it used.
* `stdout` -> `typing.Tuple[bytes]`. Raw stdout output.
* `stderr` -> `typing.Tuple[bytes]`. Raw stderr output.
* `stdout_bin` -> `bytearray`. Binary stdout output.
* `stderr_bin` -> `bytearray`. Binary stderr output.
* `stdout_str` -> `six.text_types`. Text representation of output.
* `stderr_str` -> `six.text_types`. Text representation of output.
* `stdout_brief` -> `six.text_types`. Up to 7 lines from stdout (3 first and 3 last if >7 lines).
* `stderr_brief` -> `six.text_types`. Up to 7 lines from stderr (3 first and 3 last if >7 lines).

* `stdout_json` - STDOUT decoded as JSON.

* `stdout_yaml` - STDOUT decoded as YAML

* `timestamp` -> `typing.Optional(datetime.datetime)`. Timestamp for received exit code.

Testing
=======
The main test mechanism for the package `exec-helpers` is using `tox`.
Test environments available:

::

pep8
py27
py34
py35
py36
pylint
pep257

CI systems
==========
For code checking several CI systems is used in parallel:

1. `Travis CI: <https://travis-ci.org/penguinolog/exec-helpers>`_ is used for checking: PEP8, pylint, bandit, installation possibility and unit tests. Also it's publishes coverage on coveralls.

2. `AppVeyor<https://ci.appveyor.com/project/penguinolog/exec-helpers>`_ is used for checking windows compatibility.

3. `coveralls: <https://coveralls.io/github/penguinolog/exec-helpers>`_ is used for coverage display.


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 Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

exec_helpers-0.8.1-cp36-cp36m-manylinux1_x86_64.whl (1.6 MB view details)

Uploaded CPython 3.6m

exec_helpers-0.8.1-cp36-cp36m-manylinux1_i686.whl (1.5 MB view details)

Uploaded CPython 3.6m

exec_helpers-0.8.1-cp35-cp35m-manylinux1_x86_64.whl (1.6 MB view details)

Uploaded CPython 3.5m

exec_helpers-0.8.1-cp35-cp35m-manylinux1_i686.whl (1.5 MB view details)

Uploaded CPython 3.5m

exec_helpers-0.8.1-cp34-cp34m-manylinux1_x86_64.whl (1.6 MB view details)

Uploaded CPython 3.4m

exec_helpers-0.8.1-cp34-cp34m-manylinux1_i686.whl (1.5 MB view details)

Uploaded CPython 3.4m

exec_helpers-0.8.1-cp27-cp27mu-manylinux1_x86_64.whl (1.4 MB view details)

Uploaded CPython 2.7mu

exec_helpers-0.8.1-cp27-cp27mu-manylinux1_i686.whl (1.3 MB view details)

Uploaded CPython 2.7mu

File details

Details for the file exec_helpers-0.8.1-cp36-cp36m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for exec_helpers-0.8.1-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 b6ef7f7a5dc8820b226d427b63ef6a8a731c538b223dfc9085c168e0352fbe0f
MD5 8fd835fc76e6096447a4b89a88e8b8d1
BLAKE2b-256 c7a91b8e19754e7d57864d18c4ad1132a5a8bb3f4c611cb4c999318cb2a7dc8a

See more details on using hashes here.

File details

Details for the file exec_helpers-0.8.1-cp36-cp36m-manylinux1_i686.whl.

File metadata

File hashes

Hashes for exec_helpers-0.8.1-cp36-cp36m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 3821a8b2f284b7a272e61cb4d568de94f707cd6e174b8b005133d9d0a0fc6494
MD5 af91b6198f56d34b1e3a368422e2bcd5
BLAKE2b-256 34eeb0a6d2f408394a3826871384051d988d8299627db7a3f7bb9e153d6e8df4

See more details on using hashes here.

File details

Details for the file exec_helpers-0.8.1-cp35-cp35m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for exec_helpers-0.8.1-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 78404aa0e4d6c503f4ac341cf8987a4201062f72e587af06d08c3de36288e24f
MD5 32559dd603d4945490588e52d33085e2
BLAKE2b-256 41fffbc13fbe4db6baec1c833ce26677fafb48cba3623300828cbaaa3cebc25f

See more details on using hashes here.

File details

Details for the file exec_helpers-0.8.1-cp35-cp35m-manylinux1_i686.whl.

File metadata

File hashes

Hashes for exec_helpers-0.8.1-cp35-cp35m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 415c996befbbb91826efb026781d370cf458c5416837151a84411320c35b74b5
MD5 ec2582ac8c297aa153c5d724db8d1f47
BLAKE2b-256 62f7ab99e62e2fbdfa7bca8b346a6b9742419973782ae8248324f4dde328007c

See more details on using hashes here.

File details

Details for the file exec_helpers-0.8.1-cp34-cp34m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for exec_helpers-0.8.1-cp34-cp34m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 e573ba4738ad1959e723071c1c2b8dc8a3f072562b0d4f4f524733ad0f5ff690
MD5 e89c06760e216e2c000c4f906d8c812b
BLAKE2b-256 2070f84ccdca29243a50284d1aa8165ef9b54245fe707d015788bdccf1c72704

See more details on using hashes here.

File details

Details for the file exec_helpers-0.8.1-cp34-cp34m-manylinux1_i686.whl.

File metadata

File hashes

Hashes for exec_helpers-0.8.1-cp34-cp34m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 a94ca1e5c09b0617c541819ba5333945875779f175b7ddf41ca0934da821a244
MD5 9646dd86269e139dcedd2afa05b1b8b8
BLAKE2b-256 9c3415b3ecb14c1ab84a8dacb6c0f8a8031a5ad84f3aa861ce21bce9f62567a4

See more details on using hashes here.

File details

Details for the file exec_helpers-0.8.1-cp27-cp27mu-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for exec_helpers-0.8.1-cp27-cp27mu-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 f1707b9163630bf12b1762de20dadd8bed7d8f419c01db8abd83f54bafffcdb9
MD5 2d5dfd5bafc492f267bb4787042ab6ab
BLAKE2b-256 8ddbbc0832a0cbc8847bbed4768f5616af72c105a784dc969e4da07a04bd93e2

See more details on using hashes here.

File details

Details for the file exec_helpers-0.8.1-cp27-cp27mu-manylinux1_i686.whl.

File metadata

File hashes

Hashes for exec_helpers-0.8.1-cp27-cp27mu-manylinux1_i686.whl
Algorithm Hash digest
SHA256 63882ecfc3e6242579b563da4d4cd32fbbb7c5066f3ec265b05cb05219cdeb51
MD5 4da0fa010c7751cf286495104fb3eb94
BLAKE2b-256 15a9cc13ff6a040bfbe76ac4d95b5922abb3bf3db916ea3c6d1dcad219472601

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