1# encoding=utf-8 2 3 4""" 5====================================================================================== 6copyright (C) 2018-2019, Huawei Technologies Co. 7======================================================================================== 8@文件名称: upgrade_param.py 9@描述: 参数处理相关动作 10@作者: lwx587017 11@生成日期: 20181015 12====================================================================================== 13""" 14import re 15 16 17from util.log_info import logger 18 19 20def validateParams(cust_param): 21 ''' 22 #=================================================================================== 23 # @Method: validateParams(cust_param) 24 # @Precondition: none 25 # @Func: 校验参数 26 # @PostStatus: none 27 # @Param: cust_param:升级需要的参数字典 28 # @eg: validateParams({“upgrade_upgradeLocation”:“”}) 29 # @return: params_dict:所有的参数 30 #=================================================================================== 31 ''' 32 for key in cust_param: 33 if checkHz(cust_param.get(key)): 34 logger.error("versionpath can't use Chinese") 35 raise UpgradeException(ERROR_VERSION_USE_CHINAESE) 36 if "Location" in key or "param1" in key: 37 if not cust_param.get(key): 38 logger.error("versionpath can't none") 39 raise UpgradeException(ERROR_VERSION_IS_NONE) 40 41 if len(cust_param.get(key)) > 255 : 42 logger.error("versionpath name is too long") 43 raise UpgradeException(ERROR_VERSION_PATH_TOO_LONG) 44 return True 45 46def checkHz(check_str): 47 ''' 48 #=================================================================================== 49 # @Method: checkHz(check_str) 50 # @Precondition: none 51 # @Func: 判断是否有中文 52 # @PostStatus: none 53 # @Param: check_str:判断的字符串 54 # @eg: checkHz("xxxx") 55 # @return: params_dict:所有的参数 56 #=================================================================================== 57 ''' 58 for ch in check_str: 59 if u'\u4e00' <= ch <= u'\u9fff': 60 return True 61 return False 62 63def getAllParam(param_file): 64 ''' 65 #=================================================================================== 66 # @Method: getAllParam(param_file) 67 # @Precondition: none 68 # @Func: 解析参数文件获取所有参数 69 # @PostStatus: none 70 # @Param: param_file:参数文件 71 # @eg: getAllParam(xxx.properties) 72 # @return: params_dict:所有的参数 73 #=================================================================================== 74 ''' 75 params_dict = {} 76 with open(param_file, 'Ur', encoding="utf-8") as file: 77 for line in file.readlines(): 78 line = line.strip() 79 if len(line) != 0: 80 if line.find("=") != -1: 81 str_list = line.split('=') 82 if str_list[0].find("#") != -1: 83 continue 84 else: 85 key = str_list[0].strip() 86 value = line[len(str_list[0]) + 1:].strip() 87 if len(key) != 0 and len(value) != 0: 88 params_dict[key] = value 89 90 #20200423 删除对参数进行特殊处理的代码 91 92 return params_dict 93 94 95def getCustParam(param_List, params_dict): 96 ''' 97 #=================================================================================== 98 # @Method: getCustParam(param_List, params_dict) 99 # @Precondition: none 100 # @Func: 获取用户定制参数 101 # @PostStatus: none 102 # @Param: param_List:用户给出的参数 103 # params_dict: 参数字典 104 # @eg: getCustParam([upgrade_upgradeLocation], {upgrade_upgradeLocation:“xxx”}) 105 # @return: cust_param:需要的参数 106 #=================================================================================== 107 ''' 108 if not param_List: 109 cust_param = params_dict 110 return cust_param 111 else: 112 cust_param = {k: params_dict.get(k) for k in params_dict if k in param_List} 113 return cust_param 114 115