Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 21 additions & 25 deletions esrally/utils/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,23 @@ def run_subprocess(command_line: str) -> int:
:param command_line: The command line of the subprocess to launch.
:return: The process' return code
"""
return subprocess.call(command_line, shell=True)
return subprocess.run(command_line, check=False, shell=True).returncode


def run_subprocess_with_output(command_line: str, env: Optional[Mapping[str, str]] = None) -> list[str]:
logger = logging.getLogger(__name__)
logger.debug("Running subprocess [%s] with output.", command_line)
command_line_args = shlex.split(command_line)
with subprocess.Popen(command_line_args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env=env) as command_line_process:
has_output = True
lines = []
while has_output:
assert command_line_process.stdout is not None, "stdout is None"
line = command_line_process.stdout.readline()
if line:
lines.append(line.decode("UTF-8").strip())
else:
has_output = False
return lines
result = subprocess.run(
command_line_args,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
env=env,
text=True,
encoding="utf-8",
check=False,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: check=False is the default in subprocess.run(), so not required; same in other places.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I wanted to be explicit, since it's not obvious to me that check=False is the default.

)
return [line.rstrip("\n") for line in result.stdout.splitlines()]
Comment thread
pquentin marked this conversation as resolved.


def exit_status_as_bool(runnable: Callable[[], int], quiet: bool = False) -> bool:
Expand Down Expand Up @@ -95,26 +94,24 @@ def run_subprocess_with_logging(
logger = logging.getLogger(__name__)
logger.debug("Running subprocess [%s] with logging.", command_line)
command_line_args = shlex.split(command_line)
pre_exec = os.setpgrp if detach else None
if header is not None:
logger.info(header)

# pylint: disable=subprocess-popen-preexec-fn
with subprocess.Popen(
completed = subprocess.run(
command_line_args,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
universal_newlines=True,
text=True,
env=env,
stdin=stdin if stdin else None,
preexec_fn=pre_exec,
) as command_line_process:
stdout, _ = command_line_process.communicate()
if stdout:
logger.log(level=level, msg=stdout)
start_new_session=detach,
check=False,
Comment thread
pquentin marked this conversation as resolved.
)
if completed.stdout:
logger.log(level=level, msg=completed.stdout)

logger.debug("Subprocess [%s] finished with return code [%s].", command_line, str(command_line_process.returncode))
return command_line_process.returncode
logger.debug("Subprocess [%s] finished with return code [%s].", command_line, str(completed.returncode))
return completed.returncode


def run_subprocess_with_logging_and_output(
Expand All @@ -140,7 +137,6 @@ def run_subprocess_with_logging_and_output(
logger = logging.getLogger(__name__)
logger.debug("Running subprocess [%s] with logging.", command_line)
command_line_args = shlex.split(command_line)
pre_exec = os.setpgrp if detach else None
if header is not None:
logger.info(header)

Expand All @@ -152,7 +148,7 @@ def run_subprocess_with_logging_and_output(
env=env,
check=False,
stdin=stdin if stdin else None,
preexec_fn=pre_exec,
start_new_session=detach,
)

for stdout in completed.stdout.splitlines():
Expand Down
Loading