From b1d169376ee9312fd1e9cf5aebd0fa369c785ad5 Mon Sep 17 00:00:00 2001 From: zhanghan2021 Date: Thu, 23 Nov 2023 15:01:05 +0800 Subject: [PATCH] Capture error output and return execution results --- common/command.py | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/common/command.py b/common/command.py index a5767b2..6e2e433 100644 --- a/common/command.py +++ b/common/command.py @@ -98,5 +98,26 @@ class Command: except Exception as err: Logger().error("An exception occurred when executing [{}]: {}".format(cmd, err)) return command_result - - \ No newline at end of file + + + @staticmethod + def cmd_exec_err(cmd, caller = ''): + ''' + Encapsulate the general RUN function, get the result + ''' + command_result = '' + try: + env_c = os.environ + env_c['LANG'] = 'en_US.UTF-8' + # ret = subprocess.run(cmd, shell = True, stdout = subprocess.PIPE , stderr = subprocess.PIPE, env = env_c) + ret = subprocess.Popen(cmd, shell = True, stdout = subprocess.PIPE , stderr = subprocess.PIPE, env = env_c) + stdout,stderr = ret.communicate() + if Command.cmd_check(stdout, stderr, ret.returncode, cmd): + command_result = stderr.decode('utf8') + return command_result + + except Exception as err: + Logger().error("An exception occurred when executing [{}]: {}".format(cmd, err)) + return command_result + + \ No newline at end of file -- Gitee