Skip to main content

Execution helpers for simplified usage of subprocess and ssh.

Project description

exec-helpers

https://travis-ci.org/penguinolog/exec-helpers.svg?branch=master https://coveralls.io/repos/github/penguinolog/exec-helpers/badge.svg?branch=master https://img.shields.io/pypi/v/exec-helpers.svg https://img.shields.io/pypi/pyversions/exec-helpers.svg https://img.shields.io/pypi/status/exec-helpers.svg https://img.shields.io/github/license/penguinolog/exec-helpers.svg

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:

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:

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:

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.

result = helper.execute(
    command,  # type: str
    verbose=False,  # type: bool
    timeout=None,  # type: typing.Optional[int]
    **kwargs
)
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
)
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: is used for checking: PEP8, pylint, bandit, installation possibility and unit tests. Also it’s publishes coverage on coveralls.

  2. coveralls: 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.7.2.zip (368.5 kB view details)

Uploaded Source

Built Distributions

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

Uploaded CPython 3.6m

exec_helpers-0.7.2-cp36-cp36m-manylinux1_i686.whl (966.3 kB view details)

Uploaded CPython 3.6m

exec_helpers-0.7.2-cp35-cp35m-manylinux1_x86_64.whl (989.1 kB view details)

Uploaded CPython 3.5m

exec_helpers-0.7.2-cp35-cp35m-manylinux1_i686.whl (929.7 kB view details)

Uploaded CPython 3.5m

exec_helpers-0.7.2-cp34-cp34m-manylinux1_x86_64.whl (1.0 MB view details)

Uploaded CPython 3.4m

exec_helpers-0.7.2-cp34-cp34m-manylinux1_i686.whl (951.0 kB view details)

Uploaded CPython 3.4m

exec_helpers-0.7.2-cp27-cp27mu-manylinux1_x86_64.whl (918.4 kB view details)

Uploaded CPython 2.7mu

exec_helpers-0.7.2-cp27-cp27mu-manylinux1_i686.whl (834.8 kB view details)

Uploaded CPython 2.7mu

File details

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

File metadata

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

File hashes

Hashes for exec-helpers-0.7.2.zip
Algorithm Hash digest
SHA256 d079782a24e778275da6ccbbd2e9ba104f027d837ea836a2be0b58cab78ad5e1
MD5 b858a2b9ac17ef3d645e3ff48e48acbf
BLAKE2b-256 fe2ec351e6d8c21629b97c2030b7edb5a72fe6a518b46bab03ae63e095cd57ac

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for exec_helpers-0.7.2-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 a703af76491d839af3c97ac2d7e12f780cd050a04c0b21ef79c6e5116648ed85
MD5 23152e1cd55dd75be732444455ae33db
BLAKE2b-256 19bb7601b94ca4a8a116d8f74bb1d2c87532683dc0d84daf0d52c3863c99c2ae

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for exec_helpers-0.7.2-cp36-cp36m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 8181c2c5556a2c808567407df9d912d1007a25ee311b7735ee8018087245b585
MD5 cd6b5cb9b783f4770dc3be66bc63a8f8
BLAKE2b-256 3700c3d8cd66f1a6f5aa15c3f96b8e0ec58498452931bd0ca6e5537f4e15894d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for exec_helpers-0.7.2-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 9550d37429d8586e1753c2f4aee7a539a9a037888d3d432ec3a4c6b9ae27ebc6
MD5 1be3bb4c1aeefacf004aaf06e710d780
BLAKE2b-256 b87f8519c68b22f4323b636603bdb6422a462abe0a8c20db65c004770ab636dd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for exec_helpers-0.7.2-cp35-cp35m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 d2eca7230a723a1835f5fa81d85133f60d1e0aee1ff80f93b5c708932de9175f
MD5 8d82bcf46eb8163cc7fa22c0376062dd
BLAKE2b-256 6146f9fe58b2ada221d419d08e311b28049e8ebba3f332d190c659414faa9620

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for exec_helpers-0.7.2-cp34-cp34m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 03c736d16ed68429fa97ca551ef93bca0c204f5e1ad09c107b29376982c09d78
MD5 abcb044cdef9a11b0160817d22e24df8
BLAKE2b-256 f84dcbcc1fbf7813b7fc8970f75be6df49b61b9a75287ba22a2fbbe478d0c176

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for exec_helpers-0.7.2-cp34-cp34m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 c2d1a0e4e6db4e1211f17c9032f1df5c7acbbb466d1320da3a8f7530053e78eb
MD5 39e7205ae1a3cad5f4296125c6d21227
BLAKE2b-256 ceb7b5ed47e576cff650db1d5a5188127cc9d43a8a135b0ea047a092c790e2ab

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for exec_helpers-0.7.2-cp27-cp27mu-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 93186309757420ff47d59e83c3e6ae3f4b050ed6a1c8df8a90141cd84708efea
MD5 09217d048b6a4c222822a7ddce4d01bc
BLAKE2b-256 1ce00e4c737a66b4cdda61b8f3b7f038ba3ba7f22f8fd9595534eed0b48d89f8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for exec_helpers-0.7.2-cp27-cp27mu-manylinux1_i686.whl
Algorithm Hash digest
SHA256 0375031cb7e7460911e77db22c85f21d04ee9ec02ffdab3cb96bf2c5f178dc79
MD5 17c4343cba5e04cf0e829e7e9235a6d0
BLAKE2b-256 49a3b62b105bdd10070572e697f48080ba04a1d9c32db15a1a5ab9f5ebec6ca1

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