API: ExecResult

class exec_helpers.ExecResult[source]

Command execution result.

__init__(cmd, stdin=None, stdout=None, stderr=None, exit_code=0xDEADBEEF, *, started=None)[source]
Parameters:
  • cmd (str) – command

  • stdin (bytes | str | bytearray | None) – STDIN

  • stdout (Iterable[bytes] | None) – binary STDOUT

  • stderr (Iterable[bytes] | None) – binary STDERR

  • exit_code (int | ExitCodes) – Exit code. If integer - try to convert to BASH enum.

  • started (datetime.datetime | None) – Timestamp of command start

stdout_lock

threading.RLock Lock object for thread-safe operation.

New in version 2.2.0.

stderr_lock

threading.RLock Lock object for thread-safe operation.

New in version 2.2.0.

timestamp

datetime.datetime | None Timestamp

set_timestamp()[source]

Set timestamp if empty.

This will block future object changes.

New in version 4.0.0.

cmd

str Command

stdin

str | None Stdin input as string.

stdout

tuple[bytes, ...] Stdout output as list of binaries.

stderr

tuple[bytes, ...] Stderr output as list of binaries.

stdout_bin

bytearray Stdout in binary format.

stderr_bin

bytearray Stderr in binary format.

stdout_str

str Stdout output as string.

stderr_str

str Stderr output as string.

stdout_brief

str Brief stdout output (mostly for exceptions).

stderr_brief

str Brief stderr output (mostly for exceptions).

stdout_lines

Get lines by indexes

Return type:

LinesAccessProxy

Usage example:

lines: str = res.stdout_lines[<line_number>, <index_start>:<index_end>, ...]
stderr_lines

Get lines by indexes

Return type:

LinesAccessProxy

exit_code

Return(exit) code of command.

Return type:

int | ExitCodes

ok

bool

Exit code is EX_OK

check_exit_code(expected_codes=(0,), raise_on_err=True, error_info=None, exception_class=CalledProcessError, logger=LOGGER, verbose=False)[source]

Check exit code and log/raise for unexpected code.

Parameters:
  • error_info (str | None) – optional additional error information

  • raise_on_err (bool) – raise exception_class in case of error

  • expected_codes (Iterable[int | ExitCodes]) – iterable expected exit codes

  • exception_class (type[exceptions.CalledProcessError]) – exception class for usage in case of errors (subclass of CalledProcessError)

  • logger (logging.Logger) – logger instance for error log

  • verbose (bool) – produce verbose log in case of failure

Raises:

exceptions.CalledProcessError – unexpected exit code and raise_on_err enabled

raise_for_status(expected_codes=(0,), exception_class=CalledProcessError)[source]

Requests-like exit code checker.

Parameters:
  • expected_codes (Iterable[int | ExitCodes]) – iterable expected exit codes

  • exception_class (type[exceptions.CalledProcessError]) – exception class for usage in case of errors (subclass of CalledProcessError)

Raises:

exceptions.CalledProcessError – unexpected exit code and raise_on_err enabled

started

datetime.datetime Timestamp of command start.

New in version 4.0.0.

stdout_json

JSON from stdout.

Return type:

Any

Raises:

DeserializeValueError – STDOUT can not be deserialized as JSON

stdout_yaml

YAML from stdout.

Return type:

Any

Raises:
  • DeserializeValueError – STDOUT can not be deserialized as YAML

  • AttributeError – no any yaml parser installed

stdout_xml

XML from stdout

Return type:

xml.etree.ElementTree.Element

Raises:
  • DeserializeValueError – STDOUT can not be deserialized as XML

  • AttributeError – defusedxml is not installed

stdout_lxml

XML from stdout using lxml.

Return type:

lxml.etree.Element

Raises:
  • DeserializeValueError – STDOUT can not be deserialized as XML

  • AttributeError – lxml is not installed

Note

Can be insecure.

read_stdout(src=None, log=None, verbose=False)[source]

Read stdout file-like object to stdout.

Parameters:
  • src (Iterable[bytes] | None) – source

  • log (logging.Logger | None) – logger

  • verbose (bool) – use log.info instead of log.debug

Changed in version 1.2.0: - src can be None

read_stderr(src=None, log=None, verbose=False)[source]

Read stderr file-like object to stderr.

Parameters:
  • src (Iterable[bytes] | None) – source

  • log (logging.Logger | None) – logger

  • verbose (bool) – use log.info instead of log.debug

Changed in version 1.2.0: - src can be None

class exec_helpers.LinesAccessProxy

Lines access proxy.

__init__(self, data)

Lines access proxy.

Parameters:

data (Sequence[bytes]) – data to work with.

__getitem__(self, item)

Access magic.

Parameters:

item (int | slice | Iterable[int | slice | ellipsis]) – index

Returns:

Joined selected lines

Return type:

str

Raises:

TypeError – Unexpected key

__str__(self)

Get string for debug purposes.

__len__(self)

Data len.