Skip to content

Commit

Permalink
Add ability to execute commands in tee mode (#75)
Browse files Browse the repository at this point in the history
This is a required feature for use within molecule as the executed
commands may run for a long period of time and users want to see
live progress.

Fixes: #40
  • Loading branch information
ssbarnea authored Sep 7, 2021
1 parent 68e45eb commit 4f84c41
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 4 deletions.
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ repos:
- packaging
- pytest
- pytest-mock
- subprocess-tee>=0.3.3
- types-PyYAML
- repo: https://github.com/pre-commit/mirrors-pylint
rev: v3.0.0a4
Expand Down
2 changes: 2 additions & 0 deletions constraints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ pytest-plus==0.2
# via ansible-compat (setup.cfg)
pyyaml==5.4.1
# via ansible-compat (setup.cfg)
subprocess-tee==0.3.3
# via ansible-compat (setup.cfg)
toml==0.10.2
# via
# pep517
Expand Down
2 changes: 2 additions & 0 deletions docs/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ sphinxcontrib-qthelp==1.0.3
# via sphinx
sphinxcontrib-serializinghtml==1.1.5
# via sphinx
subprocess-tee==0.3.3
# via ansible-compat (setup.cfg)
tornado==6.1
# via
# livereload
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ setup_requires =
install_requires =
cached_property ~= 1.5; python_version<="3.7"
PyYAML
subprocess-tee >= 0.3.3

[options.extras_require]
test =
Expand Down
13 changes: 10 additions & 3 deletions src/ansible_compat/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
import shutil
import subprocess
import tempfile
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union
from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Union

import packaging
import subprocess_tee

from ansible_compat.config import (
AnsibleConfig,
Expand Down Expand Up @@ -116,14 +117,20 @@ def clean(self) -> None:
shutil.rmtree(self.cache_dir, ignore_errors=True)

def exec(
self, args: Union[str, List[str]], retry: bool = False
self, args: Union[str, List[str]], retry: bool = False, tee: bool = False
) -> CompletedProcess:
"""Execute a command inside an Ansible environment.
:param retry: Retry network operations on failures.
:param tee: Also pass captured stdout/stderr to system while running.
"""
if tee:
run_func: Callable[..., CompletedProcess] = subprocess_tee.run
else:
run_func = subprocess.run

for _ in range(self.max_retries + 1 if retry else 1):
result = subprocess.run(
result = run_func(
args,
universal_newlines=True,
check=False,
Expand Down
9 changes: 9 additions & 0 deletions test/test_runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -616,3 +616,12 @@ def test_install_collection_from_disk_fail() -> None:
with pytest.raises(exception, match=msg):
# this should call install_collection_from_disk(".")
runtime.prepare_environment(install_local=True)


def test_runtime_run(runtime: Runtime) -> None:
"""."""
result1 = runtime.exec(["seq", "10"])
result2 = runtime.exec(["seq", "10"], tee=True)
assert result1.returncode == result2.returncode
assert result1.stderr == result2.stderr
assert result1.stdout == result2.stdout
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ setenv =
PIP_DISABLE_PIP_VERSION_CHECK = 1
PIP_CONSTRAINT = {toxinidir}/constraints.txt
PRE_COMMIT_COLOR = always
PYTEST_REQPASS = 70
PYTEST_REQPASS = 71
FORCE_COLOR = 1
allowlist_externals =
sh
Expand Down

0 comments on commit 4f84c41

Please sign in to comment.