1# encoding=utf-8 2 3''' 4Created on 20181019 5 6@author: wangqi10 7@modify data:20200424 8@modify:update for BlueArea 9''' 10import platform 11import sys 12import threading 13import time 14import os 15 16from util.time_info import get_now_time_str_info, get_now_time_info, Timeout, timeout 17from aw.Param.UpgradeParam import getAllParam 18from aw.Common.Constant import CONSTANT 19from util.log_info import logger 20 21def start(param): 22 ''' 23 #=================================================================================== 24 # @Method: start(param) 25 # @Precondition: none 26 # @Func: 全流程执行模板:1.组合app执行顺序 27 # 2.初始化app,调用app.excute方法 28 # 3.返回app执行结果 29 # @PostStatus: none 30 # @Param: param:用户传递的参数列表 31 # @eg: start() 32 # @return: None 33 #=================================================================================== 34 ''' 35 if isinstance(param, dict): 36 logger.switchFilePath(param.get("logFilePath")) 37 runstage = param.get("runstage") 38 if runstage == "": 39 CONSTANT.ENVERRMESSAGE = "no runstage" 40 return -1 41 step_list = runstage.split(",") 42 param["step_list"] = step_list 43 for step in step_list: 44 app_name = step 45 app_pyFile = 'func.' + app_name[0: app_name.find('_')] + '.' + app_name 46 #app_class_name = app_name[0: app_name.rfind('_')].capitalize() 47 app_class_name = app_name[0: app_name.rfind('_')] 48 improt_cmd_str = 'from %s import %s as APP' % (app_pyFile, app_class_name) 49 print(improt_cmd_str) 50 logger.info("import_cmd_str:%s" % improt_cmd_str) 51 scope = {} 52 exec(improt_cmd_str, scope) 53 APP = scope.get('APP', 'none') 54 logger.info("APP:%s" % APP) 55 app = APP(param) 56 errorcode = app._excuteApp('upgrade') 57 time.sleep(5) 58 scriptpath =os.path.dirname(os.path.dirname(os.path.abspath(os.path.dirname(__file__)))) 59 loader_tool_path = os.path.join(scriptpath, "resource", "RK3568_tool", "upgrade_tool.exe") 60 logger.info(loader_tool_path) 61 mycmd = "%s LD" % loader_tool_path 62 try: 63 num = send_cmd(mycmd) 64 if num != 2: 65 logger.info("try again!") 66 time.sleep(40) 67 num = send_cmd(mycmd) 68 if num != 2: 69 logger.error("有设备断连,全部处置为真隔离状态!") 70 errorcode = 200 71 except Exception as f: 72 logger.error(f) 73 logger.info(errorcode) 74 if errorcode == 99: 75 logger.error("upgrade success ,but Smoke failed, the fatal problem occurs.") 76 return 99 77 if errorcode == 98: 78 logger.error("upgrade success ,but Smoke failed, the key problem occurs.") 79 return 98 80 if errorcode == -1: 81 logger.error("upgrade failed.") 82 return 101 83 if errorcode == 200: 84 return -1 85 return 0 86 return 0 87 CONSTANT.ENVERRMESSAGE = "unknown error,please contact the engineer" 88 return -1 89 90 91def send_cmd(mycmd): 92 result = "".join(os.popen(mycmd).readlines()) 93 logger.info(result) 94 num = int(result.split("(")[1].split(")")[0]) 95 logger.info(num) 96 return num 97 98 99def parseProperties(param_file): 100 ''' 101 #=================================================================================== 102 # @Method: parseProperties(param_file) 103 # @Precondition: none 104 # @Func: 将xxx.properties文件解析成start(param)方法中param参数需要的格式 105 # @PostStatus: none 106 # @Param: param:用户传递的参数列表 107 # @eg: parseProperties() 108 # @return: None 109 #=================================================================================== 110 ''' 111 param_dict = getAllParam(param_file) 112 113 #获取taskid 114 taskid = "NoTaskidJob" 115 task_path = param_file.split('.')[0] 116 if task_path.rfind("/") > -1: 117 r = task_path.rfind("/") 118 else: 119 r = task_path.rfind("\\") 120 taskid = task_path[r + 1:] 121 122 param_dict["taskId"] = taskid 123 param_dict["logFilePath"] = ''.join([task_path, ".log"]) 124 125 return param_dict 126 127 128def saveSuccessVersion(success_version): 129 ''' 130 #=================================================================================== 131 # @Method: saveSuccessVersion(param_file) 132 # @Precondition: none 133 # @Func: 存储成功的版本 134 # @PostStatus: none 135 # @Param: param:用户传递的参数列表 136 # @return: None 137 #=================================================================================== 138 ''' 139 save_path = CONSTANT.Path.getSavePath() 140 try: 141 with open(save_path, 'w') as f: 142 f.write(f'upgrade_upgradeLocation={success_version}') 143 except: 144 logger.info('save success version fail') 145 logger.info('saving success') 146 147 148def send_times(mycmd): 149 times = 0 150 outcome = sendCmd(mycmd) 151 while times < 3: 152 if not outcome or "Empty" in outcome: 153 times += 1 154 time.sleep(3) 155 else: 156 return outcome 157 return outcome 158 159 160def sendCmd(mycmd): 161 result = "".join(os.popen(mycmd).readlines()) 162 return result 163 164 165@timeout(30) 166def hdc_kill(): 167 logger.info("kill the process") 168 os.system("hdc_std kill") 169 time.sleep(5) 170 logger.info("start the process") 171 os.system("hdc_std start") 172 time.sleep(10)