Skip to content

Commit

Permalink
Dev: help: capture error messages when --help fails rather
Browse files Browse the repository at this point in the history
add an option `mix_stderr` to `ShellUtils.get_stdout_stderr` to allow
mixing stdout and stderr stream.
  • Loading branch information
nicholasyang2022 committed Sep 6, 2024
1 parent 906cf8e commit 4e8e1e2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
2 changes: 1 addition & 1 deletion crmsh/help.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def long(self):
args = ['crm']
args.extend(self._cmd_args)
args.append('--help-without-redirect')
rc, stdout = ShellUtils.get_stdout(args, shell=False)
rc, stdout, _ = ShellUtils.get_stdout_stderr(args, shell=False, mix_stderr=True)
return stdout

def paginate(self):
Expand Down
13 changes: 8 additions & 5 deletions crmsh/sh.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,9 +458,11 @@ def get_stdout(cls, cmd, input_s=None, stderr_on=True, shell=True, raw=False):
return proc.returncode, stdout_data.strip()

@classmethod
def get_stdout_stderr(cls, cmd, input_s=None, shell=True, raw=False, no_reg=False, timeout=None):
def get_stdout_stderr(cls, cmd, input_s=None, shell=True, raw=False, no_reg=False, timeout=None, mix_stderr=False):
'''
Run a cmd, return (rc, stdout, stderr)
Parameters:
mix_stderr: whether to mix stderr data into stdout
'''
if crmsh.options.regression_tests and not no_reg:
print(".EXT", cmd)
Expand All @@ -469,7 +471,7 @@ def get_stdout_stderr(cls, cmd, input_s=None, shell=True, raw=False, no_reg=Fals
shell=shell,
stdin=input_s and subprocess.PIPE or None,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
stderr=subprocess.STDOUT if mix_stderr else subprocess.PIPE,
env=os.environ, # bsc#1205925
)
# will raise subprocess.TimeoutExpired if set timeout
Expand All @@ -478,9 +480,10 @@ def get_stdout_stderr(cls, cmd, input_s=None, shell=True, raw=False, no_reg=Fals
return proc.returncode, stdout_data, stderr_data
else:
if isinstance(stdout_data, bytes):
stdout_data = Utils.decode_str(stdout_data)
stderr_data = Utils.decode_str(stderr_data)
return proc.returncode, stdout_data.strip(), stderr_data.strip()
stdout_data = Utils.decode_str(stdout_data).strip()
if isinstance(stderr_data, bytes):
stderr_data = Utils.decode_str(stderr_data).strip()
return proc.returncode, stdout_data, stderr_data


class ClusterShellAdaptorForLocalShell(ClusterShell):
Expand Down

0 comments on commit 4e8e1e2

Please sign in to comment.