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 Distribution

exec-helpers-0.8.0.zip (366.8 kB view details)

Uploaded Source

Built Distributions

exec_helpers-0.8.0-cp36-cp36m-manylinux1_x86_64.whl (1.0 MB view details)

Uploaded CPython 3.6m

exec_helpers-0.8.0-cp36-cp36m-manylinux1_i686.whl (953.7 kB view details)

Uploaded CPython 3.6m

exec_helpers-0.8.0-cp35-cp35m-manylinux1_x86_64.whl (974.3 kB view details)

Uploaded CPython 3.5m

exec_helpers-0.8.0-cp35-cp35m-manylinux1_i686.whl (917.5 kB view details)

Uploaded CPython 3.5m

exec_helpers-0.8.0-cp34-cp34m-manylinux1_x86_64.whl (997.3 kB view details)

Uploaded CPython 3.4m

exec_helpers-0.8.0-cp34-cp34m-manylinux1_i686.whl (937.3 kB view details)

Uploaded CPython 3.4m

exec_helpers-0.8.0-cp27-cp27mu-manylinux1_x86_64.whl (905.4 kB view details)

Uploaded CPython 2.7mu

exec_helpers-0.8.0-cp27-cp27mu-manylinux1_i686.whl (824.2 kB view details)

Uploaded CPython 2.7mu

File details

Details for the file exec-helpers-0.8.0.zip.

File metadata

  • Download URL: exec-helpers-0.8.0.zip
  • Upload date:
  • Size: 366.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for exec-helpers-0.8.0.zip
Algorithm Hash digest
SHA256 1743754e738653523e60cadd61d7281d12774e7ffff3302dc5a85cda61a87445
MD5 71508f75cb53506fe6f59676ecd145e8
BLAKE2b-256 d0dd5351ae1bb7ad34bbe3ec7801f24b00c2cfc56cbc2b5da29c98de1a0c6dfe

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for exec_helpers-0.8.0-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 ce1ec080c398a60bf252ef9b2aec8a1f94018fde2a9bee0e8a6c455b8c4da38c
MD5 e017d9a7882347cd0b8f4ba388d74c34
BLAKE2b-256 be16ab6aadef51bfd5e628a663ee2dfde107454ec6b8a59e69d42d80a8850c0f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for exec_helpers-0.8.0-cp36-cp36m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 c23fa9b2baa44532c899e02937d54d34519f66cf1c24fbe5dc10935a91148ff6
MD5 076b2e049d1fce0bfab9276e515ccd82
BLAKE2b-256 cd34f6f2bc1f83a1c790637ea4134b3626ad66ea118ddde1de74e5172dea597b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for exec_helpers-0.8.0-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 487ef71b1620b34c7e9234f7739814477cbc34dc7e3225741e5e1006f29f6e5a
MD5 796dbc1eb54f4022e76129b99603a3ee
BLAKE2b-256 4c22e780141975600d430210be2b2c668f55ccf0ba7dc98e13a7a3e527877954

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for exec_helpers-0.8.0-cp35-cp35m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 3c9f14a9b2a7a5c9d82886082ae32ab6a879f25f869a1144b05b7f673fb8a9ef
MD5 f4b4f8e72d6ce3ec9fb40222c2b2f3c2
BLAKE2b-256 168e434032b5a5dcba05f1c28c9606a092a1066b8273b5e148ceb761a7954810

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for exec_helpers-0.8.0-cp34-cp34m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 cab5d0b1fbb17373682616666d9360593d2b7502156aef086fb69d63d737d7ad
MD5 4471662e057d775ccb0c62995a008c58
BLAKE2b-256 1514b186d40354f77e0c718923e0d566d35604adac6cc9eb6f4b9712a3ba7abb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for exec_helpers-0.8.0-cp34-cp34m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 456315c27e2687d8e21d2521a75f4594eaa6ce80e429de8149343dacde8557e0
MD5 c8aaff5e10d45ac3dcdcb8fec7bbf047
BLAKE2b-256 4b222fa2bc5df0368a844d5ac7087165fd30b08ee5cf6753a87de7fa4a34342a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for exec_helpers-0.8.0-cp27-cp27mu-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 64ee6aaef1f2c8a2142db5ba86a744068e5e9c4f9ea0cfdbe9bbd65f810fd052
MD5 66feefe73151c671a60f62b6e39dc48f
BLAKE2b-256 22ac77e57b498837c1fa7bfc41d5c23271dd21de860d2cbbf4a7a47321a98003

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for exec_helpers-0.8.0-cp27-cp27mu-manylinux1_i686.whl
Algorithm Hash digest
SHA256 05965a08d2adfa575795c4b65813b0c52c87db600af022c43dd16cf278e03b44
MD5 fc8248ad8b2ecf47b1cc25093eab74c9
BLAKE2b-256 672d8856b20e2991a0dcaf264f77e006f8538f0e83f1aaaee8513a12e0ea0b77

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