def cli_call(arg_list, expect_success=True, env=os.environ.copy()):
"""Executes a CLI command in a subprocess and return the results.
Args:
arg_list: a list command arguments
expect_success: use False to return even if an error occurred
when executing the command
env:
Returns: (string, string, int) output message, error message, return code
"""
p = subprocess.Popen(arg_list, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, env=env)
output, error = p.communicate()
if p.returncode != 0:
if output:
print("Output:\n" + str(output))
if error:
print("Error Message:\n" + str(error))
if expect_success:
raise subprocess.CalledProcessError(
p.returncode, arg_list, output)
return output, error, p.returncode
评论列表
文章目录