Coverage for /opt/cloud/slavespace/usr1/096471637100f3de0fcfc01072822a80/ut/src/asys/cmdline/arg_checker.py: 92%

139 statements  

« prev     ^ index     » next       coverage.py v7.15.3, created at 2026-08-11 18:39 +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 

20import enum 

21import re 

22from common.const import RetCode 

23from common import log_error 

24from common import FileOperate as f 

25from common import DeviceInfo 

26from common.const import DEVICE_ID_MIN, DEVICE_ID_MAX 

27 

28 

29__all__ = ["ArgChecker", "check_arg_exist_dir", "check_arg_executable", "check_arg_create_dir"] 

30 

31 

32def empty_str_check(arg_name, arg_val): 

33 if arg_val == "": 

34 log_error("Argument \"{0}\" can not be empty string, argument value: \"{1}\"".format(arg_name, arg_val)) 

35 return RetCode.ARG_EMPTY_STRING 

36 return RetCode.SUCCESS 

37 

38 

39def space_check(arg_name, arg_val): 

40 if " " in arg_val: 

41 log_error("Argument \"{0}\" can not have space character, argument value: \"{1}\"".format(arg_name, arg_val)) 

42 return RetCode.ARG_SAPCE_STRING 

43 return RetCode.SUCCESS 

44 

45 

46def ill_char_check(arg_name, arg_val): 

47 pattern = r'^[a-zA-Z0-9_\-/\.]+' 

48 rex = bool(re.fullmatch(pattern, arg_val)) 

49 if not rex: 

50 log_error(f"Argument {arg_name} is invalid, only characters in [a-zA-Z0-9_-.], input {arg_val}") 

51 return RetCode.ARG_ILLEGAL_STRING 

52 return RetCode.SUCCESS 

53 

54 

55def path_str_check(arg_name, arg_val): 

56 if empty_str_check(arg_name, arg_val) != RetCode.SUCCESS: 

57 return RetCode.ARG_EMPTY_STRING 

58 if space_check(arg_name, arg_val) != RetCode.SUCCESS: 

59 return RetCode.ARG_SAPCE_STRING 

60 if ill_char_check(arg_name, arg_val) != RetCode.SUCCESS: 

61 return RetCode.ARG_ILLEGAL_STRING 

62 return RetCode.SUCCESS 

63 

64 

65def check_arg_exist_dir(arg_name, arg_val): 

66 """ 

67 Check if arg_val is an exist dir. 

68 

69 Args: 

70 arg_val: The value of arg to check 

71 

72 Returns: 

73 RetCode: return code (SUCCESS:0, FAILED:1) 

74 """ 

75 ret = path_str_check(arg_name, arg_val) 

76 if ret != RetCode.SUCCESS: 

77 return ret 

78 if not os.path.isdir(arg_val): 

79 log_error("Argument \"{0}\" is not an exist directory, argument value: \"{1}\"".format(arg_name, arg_val)) 

80 return RetCode.ARG_NO_EXIST_DIR 

81 return RetCode.SUCCESS 

82 

83 

84def check_arg_executable(arg_name, arg_val): 

85 """ 

86 Check if arg_val is an executable command. 

87 

88 Args: 

89 arg_val: The value of arg to check 

90 

91 Returns: 

92 RetCode: return code (SUCCESS:0, FAILED:1) 

93 """ 

94 if empty_str_check(arg_name, arg_val.strip()) != RetCode.SUCCESS: 

95 return RetCode.ARG_EMPTY_STRING 

96 

97 check_exe = re.compile(r"sh|.*/sh|bash|.*/bash|python[0-9.]*|.*/python[0-9.]*") 

98 if check_exe.match(arg_val.strip()) and check_exe.match(arg_val.strip()).group() == arg_val.strip(): 

99 log_error(f"Argument \"{arg_name}\" no executable script, argument value: \"{arg_val}\"") 

100 return RetCode.ARG_NO_EXECUTABLE 

101 

102 check_exe = re.compile(r"sh |.*/sh |bash |.*/bash |python[0-9.]* |.*/python[0-9.]* ") 

103 if not check_exe.match(arg_val.lstrip()): 

104 return RetCode.SUCCESS 

105 

106 check_script = re.compile( 

107 r"sh .*?\.sh|.*?/sh .*?\.sh|bash .*?\.sh|.*?/bash .*?\.sh|" 

108 r"sh .*?\.bash|.*?/sh .*?\.bash|bash .*?\.bash|.*?/bash .*?\.bash|" 

109 r"python[0-9.]*? .*?\.py|.*?/python[0-9.]*? .*?\.py" 

110 ) 

111 if not check_script.match(arg_val.strip()): 

112 log_error(f"Argument \"{arg_name}\" no executable script, argument value: \"{arg_val}\"") 

113 return RetCode.ARG_NO_EXECUTABLE 

114 return RetCode.SUCCESS 

115 

116 

117def check_arg_create_dir(arg_name, arg_val): 

118 """ 

119 Check if arg_value is a directory string. 

120 If arg_val is not exist, it will find its parent dir and check. 

121 

122 Args: 

123 arg_val: The value of arg to check 

124 

125 Returns: 

126 RetCode: return code (SUCCESS:0, FAILED:1) 

127 """ 

128 def create_dir_reverse_scan(scan_path): 

129 while True: 

130 if os.access(scan_path, os.F_OK): 

131 if os.access(scan_path, os.W_OK): 

132 f.create_dir(arg_val) 

133 return RetCode.SUCCESS 

134 else: 

135 return RetCode.ARG_CREATE_DIR_FAILED 

136 if scan_path == "/": 

137 break 

138 scan_path = os.path.dirname(scan_path) 

139 return RetCode.ARG_CREATE_DIR_FAILED 

140 

141 ret = path_str_check(arg_name, arg_val) 

142 if ret != RetCode.SUCCESS: 

143 return ret 

144 

145 arg_val = os.path.abspath(arg_val) 

146 if os.path.exists(arg_val): 

147 if not os.path.isdir(arg_val): 

148 log_error("Argument \"{0}\" is existed but not a directory, argument value: \"{1}\"". 

149 format(arg_name, arg_val)) 

150 return RetCode.ARG_NO_EXIST_DIR 

151 if not os.access(arg_val, os.W_OK): 

152 log_error("Argument \"{0}\" is not permissible to write, argument value: \"{1}\"". 

153 format(arg_name, arg_val)) 

154 return RetCode.ARG_NO_WRITABLE_PERMISSION 

155 return RetCode.SUCCESS 

156 else: 

157 scan_path = os.path.dirname(arg_val) 

158 ret = create_dir_reverse_scan(scan_path) 

159 if ret != RetCode.SUCCESS: 

160 log_error("Argument \"{0}\" is not permissible to create, argument value: \"{1}\"". 

161 format(arg_name, arg_val)) 

162 return ret 

163 

164 

165def check_arg_tar(arg_name, arg_val): 

166 if arg_val.upper() not in ["F", "T", "FALSE", "TRUE"]: 

167 log_error("Argument \"{0}\" should be in [\"F\", \"T\", \"False\", \"True\"], input: \"{1}\"". 

168 format(arg_name, arg_val)) 

169 return RetCode.FAILED 

170 return RetCode.SUCCESS 

171 

172 

173def check_arg_device_id(arg_name, arg_val): 

174 if arg_val < DEVICE_ID_MIN or arg_val > DEVICE_ID_MAX: 

175 log_error("Argument \"{}\" value is range of [{}, {}], input: \"{}\"".format( 

176 arg_name, DEVICE_ID_MIN, DEVICE_ID_MAX, arg_val)) 

177 return RetCode.FAILED 

178 devices_num = DeviceInfo().get_device_count() 

179 if arg_val >= devices_num: 

180 log_error("'-d' value should be in [0, {}), input {}".format(devices_num, arg_val)) 

181 return RetCode.FAILED 

182 return RetCode.SUCCESS 

183 

184 

185def check_arg_exist_or_read_permissibale(arg_name, arg_val): 

186 """ 

187 Check if arg_value is a directory string. 

188 If arg_val is not exist, 

189 If arg_val is not read permissibale 

190 

191 Args: 

192 arg_val: The value of arg to check 

193 

194 Returns: 

195 RetCode: return code (SUCCESS:0, FAILED:1) 

196 """ 

197 

198 ret = path_str_check(arg_name, arg_val) 

199 if ret != RetCode.SUCCESS: 

200 return ret 

201 arg_val = os.path.abspath(arg_val) 

202 if f.check_exists(arg_val): 

203 if not f.check_access(arg_val, os.R_OK): 

204 log_error("Argument \"{0}\" is not permissibale to read, argument value: \"{1}\"". 

205 format(arg_name, arg_val)) 

206 return RetCode.ARG_NO_WRITABLE_PERMISSION 

207 if arg_name == "file" and not f.check_file(arg_val): 

208 log_error(f"{arg_val} is not a file.") 

209 return RetCode.FAILED 

210 if arg_name == "path" and f.check_emtpy(arg_val): 

211 log_error(f"{arg_val} is not a directory or is empty.") 

212 return RetCode.FAILED 

213 return RetCode.SUCCESS 

214 else: 

215 log_error(f"{arg_val}: no such file or directory.") 

216 return RetCode.FAILED 

217 

218 

219def check_core_file(arg_name, arg_val): 

220 if not (f.check_exists(arg_val) and f.check_file(arg_val)): 

221 log_error(f"{arg_val} is not a file or does not exist.") 

222 return RetCode.FAILED 

223 return RetCode.SUCCESS 

224 

225 

226def check_symbol_path(arg_name, arg_val): 

227 arg_list = arg_val.split(",") 

228 for dir in arg_list: 

229 if f.check_valid_dir(dir): 

230 return RetCode.SUCCESS 

231 log_error(f"Check whether the {arg_name} parameter is a directory and whether the user has the read permission.") 

232 return RetCode.FAILED 

233 

234 

235class ArgChecker(enum.Enum): 

236 '''The arg type for check.''' 

237 DIR_EXIST = check_arg_exist_dir 

238 DIR_CREATE = check_arg_create_dir 

239 EXECUTABLE = check_arg_executable 

240 TAR_CHECK = check_arg_tar 

241 DEVICE_ID = check_arg_device_id 

242 FILE_PATH_EXIST_R = check_arg_exist_or_read_permissibale 

243 CORE_FILE = check_core_file 

244 SYMBOL_PATH = check_symbol_path