# encoding=utf-8 """ ====================================================================================== copyright (C) 2018-2019, Huawei Technologies Co. ======================================================================================== @文件å称: upgrade_param.py @æè¿°: å‚数处ç†ç›¸å…³åŠ¨ä½œ @作者: lwx587017 @生æˆæ—¥æœŸ: 20181015 ====================================================================================== """ import re from util.log_info import logger def validateParams(cust_param): ''' #=================================================================================== # @Method: validateParams(cust_param) # @Precondition: none # @Func: æ ¡éªŒå‚æ•° # @PostStatus: none # @Param: cust_param:å‡çº§éœ€è¦çš„å‚æ•°å—å…¸ # @eg: validateParams({“upgrade_upgradeLocationâ€ï¼šâ€œâ€}) # @return: params_dict:所有的å‚æ•° #=================================================================================== ''' for key in cust_param: if checkHz(cust_param.get(key)): logger.error("versionpath can't use Chinese") raise UpgradeException(ERROR_VERSION_USE_CHINAESE) if "Location" in key or "param1" in key: if not cust_param.get(key): logger.error("versionpath can't none") raise UpgradeException(ERROR_VERSION_IS_NONE) if len(cust_param.get(key)) > 255 : logger.error("versionpath name is too long") raise UpgradeException(ERROR_VERSION_PATH_TOO_LONG) return True def checkHz(check_str): ''' #=================================================================================== # @Method: checkHz(check_str) # @Precondition: none # @Func: 判æ–是å¦æœ‰ä¸æ–‡ # @PostStatus: none # @Param: check_str:判æ–çš„å—符串 # @eg: checkHz("xxxx") # @return: params_dict:所有的å‚æ•° #=================================================================================== ''' for ch in check_str: if u'\u4e00' <= ch <= u'\u9fff': return True return False def getAllParam(param_file): ''' #=================================================================================== # @Method: getAllParam(param_file) # @Precondition: none # @Func: 解æžå‚数文件获å–所有å‚æ•° # @PostStatus: none # @Param: param_file:å‚数文件 # @eg: getAllParam(xxx.properties) # @return: params_dict:所有的å‚æ•° #=================================================================================== ''' params_dict = {} with open(param_file, 'Ur', encoding="utf-8") as file: for line in file.readlines(): line = line.strip() if len(line) != 0: if line.find("=") != -1: str_list = line.split('=') if str_list[0].find("#") != -1: continue else: key = str_list[0].strip() value = line[len(str_list[0]) + 1:].strip() if len(key) != 0 and len(value) != 0: params_dict[key] = value #20200423 åˆ é™¤å¯¹å‚数进行特殊处ç†çš„代ç return params_dict def getCustParam(param_List, params_dict): ''' #=================================================================================== # @Method: getCustParam(param_List, params_dict) # @Precondition: none # @Func: 获å–用户定制å‚æ•° # @PostStatus: none # @Param: param_List:用户给出的å‚æ•° # params_dict: å‚æ•°å—å…¸ # @eg: getCustParam([upgrade_upgradeLocation], {upgrade_upgradeLocation:“xxxâ€}) # @return: cust_param:需è¦çš„å‚æ•° #=================================================================================== ''' if not param_List: cust_param = params_dict return cust_param else: cust_param = {k: params_dict.get(k) for k in params_dict if k in param_List} return cust_param