Removing Popen object return from utils.run_command_and_log

Argument 'retcode_only' of the function and associated logic was removed
as it was not beneficial to the purpose, or use of the function.

Before this patch, only call of the function setting 'retcode_only'
to 'True' was made in tests of the validations-libs. All other calls
worked with assumption that Bool, not object will be returned,
and didn't supply the argument explicitly. Instead relying on
the default value.

In addition, it is not clear how would the Popen[0] object be
used, if it was returned.

The function has limited impact across Openstack[1],
only calls to it are made within validations-libs, although there is
a homonymous function withing tripleoclient intended
for the same task.

Docstring and tests were adjusted accordingly.

[0]https://docs.python.org/3/library/subprocess.html#popen-objects
[1]https://codesearch.openstack.org/?q=run_command_and_log&i=nope&literal=nope&files=&excludeFiles=&repos=

Signed-off-by: Jiri Podivin <jpodivin@redhat.com>
Change-Id: I3f9c2ef364de645a99d28c45c96560b78862c848
This commit is contained in:
Jiri Podivin 2022-07-25 10:40:50 +02:00
parent 5076004733
commit 8a69f95e0a
2 changed files with 14 additions and 31 deletions

View File

@ -649,14 +649,3 @@ class TestRunCommandAndLog(TestCase):
self.assertEqual(retcode, 0)
self.mock_logger.debug.assert_has_calls(self.log_calls,
any_order=False)
def test_success_no_retcode(self):
run = utils.run_command_and_log(self.mock_logger, self.cmd,
retcode_only=False)
self.mock_popen.assert_called_once_with(self.cmd,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
shell=False,
cwd=None, env=None)
self.assertEqual(run, self.mock_process)
self.mock_logger.debug.assert_not_called()

View File

@ -604,8 +604,7 @@ def find_config_file(config_file_name='validation.cfg'):
return current_path
def run_command_and_log(log, cmd, cwd=None,
env=None, retcode_only=True):
def run_command_and_log(log, cmd, cwd=None, env=None):
"""Run command and log output
:param log: Logger instance for logging
@ -619,28 +618,23 @@ def run_command_and_log(log, cmd, cwd=None,
:param env: Modified environment for command run
:type env: ``List``
:param retcode_only: Returns only retcode instead or proc object
:type retcdode_only: ``Boolean``
"""
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, shell=False,
cwd=cwd, env=env)
if retcode_only:
while True:
try:
line = proc.stdout.readline()
except StopIteration:
break
if line != b'':
if isinstance(line, bytes):
line = line.decode('utf-8')
log.debug(line.rstrip())
else:
break
proc.stdout.close()
return proc.wait()
return proc
while True:
try:
line = proc.stdout.readline()
except StopIteration:
break
if line != b'':
if isinstance(line, bytes):
line = line.decode('utf-8')
log.debug(line.rstrip())
else:
break
proc.stdout.close()
return proc.wait()
def check_community_validations_dir(