Coverage for /opt/cloud/slavespace/usr1/096471637100f3de0fcfc01072822a80/ut/src/asys/params/param_dict.py: 98%

134 statements  

« prev     ^ index     » next       coverage.py v7.15.4, created at 2026-08-19 17:46 +0800

1#!/usr/bin/env python3 

2# -*- coding: utf-8 -*- 

3# ---------------------------------------------------------------------------- 

4# Copyright (c) 2025 Huawei Technologies Co., Ltd. 

5# 

6# Licensed under the Apache License, Version 2.0 (the "License"); 

7# you may not use this file except in compliance with the License. 

8# You may obtain a copy of the License at 

9# 

10# http://www.apache.org/licenses/LICENSE-2.0 

11# 

12# Unless required by applicable law or agreed to in writing, software 

13# distributed under the License is distributed on an "AS IS" BASIS, 

14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

15# See the License for the specific language governing permissions and 

16# limitations under the License. 

17# ---------------------------------------------------------------------------- 

18 

19import os 

20from pathlib import Path 

21 

22from common import log_debug 

23from common import consts 

24from common import Singleton 

25from drv import LoadSoType 

26 

27__all__ = ["ParamDict"] 

28 

29 

30class ParamDict(metaclass=Singleton): 

31 

32 def __init__(self): 

33 self.__command = None 

34 self.__asys_output_timestamp_dir = None 

35 self.__args = dict() 

36 self.__deps = dict() 

37 self.__ini = dict() 

38 self.__env_type = None 

39 self.__task_pid = None 

40 

41 @property 

42 def tools_path(self): 

43 return Path(os.path.dirname(__file__)).parent 

44 

45 def get_command(self): 

46 return self.__command 

47 

48 @property 

49 def asys_output_timestamp_dir(self) -> str: 

50 return self.__asys_output_timestamp_dir 

51 

52 @asys_output_timestamp_dir.setter 

53 def asys_output_timestamp_dir(self, path): 

54 self.__asys_output_timestamp_dir = path 

55 

56 def get_ini(self, key): 

57 return self.__ini.get(key, False) 

58 

59 def set_ini(self, ini_name, ini_value): 

60 self.__ini[ini_name] = ini_value 

61 

62 def get_arg(self, key, default=False): 

63 if key in self.__args.keys(): 

64 return self.__args.get(key, default) 

65 return default 

66 

67 def get_deps(self): 

68 return self.__deps 

69 

70 def get_env_type(self): 

71 if self.__env_type is None: 

72 self.set_env_type() 

73 return self.__env_type 

74 

75 def set_env_type(self, env_type=None): 

76 if env_type is None: 

77 self.__env_type = LoadSoType().get_env_type() 

78 else: 

79 self.__env_type = env_type 

80 

81 def set_task_pid(self, pid): 

82 self.__task_pid = pid 

83 

84 def get_task_pid(self): 

85 return self.__task_pid 

86 

87 def _set_arg_d(self, args): 

88 if getattr(args, "d") is not None: 

89 self.__add_arg("device_id", args.d) 

90 log_debug("set arg: -d=\"{0}\" success.".format(self.__args.get("d"))) 

91 

92 def _set_arg_r(self, args): 

93 if getattr(args, "r") is not None: 

94 self.__add_arg("run_mode", args.r) 

95 log_debug("set arg: -r=\"{0}\" success.".format(self.__args.get("r"))) 

96 

97 def _set_arg_tar(self, args): 

98 arg_name = "tar" 

99 if getattr(args, arg_name) is not None: 

100 self.__add_arg(arg_name, args.tar.upper()) 

101 log_debug("set arg: --output=\"{0}\" success.".format(self.__args.get(arg_name))) 

102 

103 def _set_arg_symbol_path(self, args): 

104 arg_name = "symbol_path" 

105 if getattr(args, arg_name) is not None: 

106 self.__add_arg(arg_name, args.symbol_path.split(",")) 

107 log_debug("set arg: --symbol_path=\"{0}\" success.".format(self.__args.get(arg_name))) 

108 

109 def _set_arg_common(self, args, arg_name): 

110 if getattr(args, arg_name) is not None: 

111 self.__add_arg(arg_name, eval(f"args.{arg_name}")) 

112 log_debug(f"set arg: --{arg_name}=\"{self.__args.get(arg_name)}\" success.") 

113 

114 def set_args(self, args): 

115 self.__command = args.subparser_name 

116 

117 if self.__command == consts.diagnose_cmd: 

118 self._set_arg_d(args) 

119 self._set_arg_r(args) 

120 self._set_arg_common(args, "timeout") 

121 

122 if self.__command == consts.health_cmd: 

123 self._set_arg_d(args) 

124 return 

125 

126 if self.__command == consts.info_cmd: 

127 self._set_arg_d(args) 

128 self._set_arg_r(args) 

129 return 

130 

131 if self.__command == consts.analyze_cmd: 

132 self._set_arg_d(args) 

133 self._set_arg_r(args) 

134 self._set_arg_common(args, "file") 

135 self._set_arg_common(args, "path") 

136 self._set_arg_common(args, "core_file") 

137 self._set_arg_common(args, "exe_file") 

138 self._set_arg_common(args, "symbol") 

139 self._set_arg_symbol_path(args) 

140 self._set_arg_common(args, "reg") 

141 

142 if self.__command == consts.config_cmd: 

143 self._set_arg_d(args) 

144 self._set_arg_common(args, "get") 

145 self._set_arg_common(args, "restore") 

146 self._set_arg_common(args, "stress_detect") 

147 return 

148 

149 if self.__command == consts.collect_cmd: 

150 self._set_arg_common(args, "task_dir") 

151 self._set_arg_r(args) 

152 self._set_arg_common(args, "remote") 

153 self._set_arg_common(args, "all") 

154 self._set_arg_common(args, "quiet") 

155 self._set_arg_common(args, "timeout") 

156 self._set_arg_tar(args) 

157 

158 if self.__command == consts.launch_cmd: 

159 self._set_arg_common(args, "task") 

160 self._set_arg_tar(args) 

161 

162 if self.__command == consts.profiling_cmd: 

163 self._set_arg_d(args) 

164 self._set_arg_r(args) 

165 self._set_arg_p(args) 

166 self._set_arg_common(args, "aic_metrics") 

167 

168 self._set_arg_common(args, "output") 

169 

170 def set_deps(self, deps): 

171 for item in deps: 

172 self.__add_dep(item[0], item[1]) 

173 

174 def __add_arg(self, key, value): 

175 if key in self.__args.keys(): 

176 return False 

177 else: 

178 self.__args[key] = value 

179 return True 

180 

181 def __add_dep(self, key, value): 

182 if key in self.__deps.keys(): 

183 return False 

184 else: 

185 self.__deps[key] = value 

186 return True 

187 

188 def _set_arg_p(self, args): 

189 if getattr(args, "p") is not None: 

190 self.__add_arg("period", args.p) 

191 log_debug("set arg: -p=\"{0}\" success.".format(self.__args.get("p")))