import inspect
import os
import re
import subprocess
from ansible.module_utils.check_output_manager import set_error_msg
from ansible.module_utils.common_info import get_npu_info
class CallCmdException(Exception):
pass
FRAME_INFO_ERR = "[ASCEND ERROR] Failed to obtain frame index."
class CheckUtil:
GREP_RETURN_CODE = [0, 1]
@classmethod
def get_card(cls):
npu_info = get_npu_info()
scene = npu_info.get("scene")
if scene == "a300i" or scene == "a300iduo":
return "310p"
elif scene == "train":
return "910"
elif scene == "a910b":
return "910b"
elif scene == "a910_93":
return "910_93"
else:
return "--"
@classmethod
def run_cmd(cls, cmd, success_code=None):
"""
arguments:
cmd: str, the linux command line
success_code: list, refer to GREP_RETURN_CODE, which is [0, 1]
"""
sp = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
out, err = sp.communicate()
if success_code:
if sp.returncode not in success_code:
raise CallCmdException("call cmd {} failed, reason: {}".format(cmd, out + err))
return out
if sp.returncode != 0:
raise CallCmdException("call cmd {} failed, reason: {}".format(cmd, out + err))
return out
@classmethod
def record_error(cls, msg, error_messages):
top_decorated_func = cls.get_decorated_func_name()
if top_decorated_func == FRAME_INFO_ERR:
error_messages.append(FRAME_INFO_ERR)
if msg and msg not in error_messages:
error_messages.append(msg)
set_error_msg(msg, top_decorated_func)
@classmethod
def get_decorated_func_name(cls):
stack = inspect.stack()[1:]
top_decorated_func = None
for frame_info in stack:
if len(frame_info) < 4:
return FRAME_INFO_ERR
frame = frame_info[0]
func_name = frame_info[3]
func_obj = None
if 'self' in frame.f_locals:
self_obj = frame.f_locals['self']
func_obj = getattr(self_obj, func_name, None)
else:
func_obj = frame.f_globals.get(func_name, None)
if func_obj and hasattr(func_obj, 'decorated_by_check_event'):
top_decorated_func = func_obj.__name__
break
return top_decorated_func
@classmethod
def find_file(cls, resources, file_name):
for _, _, files in os.walk(resources):
for file in files:
if re.match(file_name, file):
return file
return None