1#-*- coding:utf-8 -*- 2import uuid 3import sys 4import subprocess 5 6from core.base import BaseApp, dec_stepmsg 7from util.file_locker import FileLock 8from util.log_info import logger 9from util.time_info import get_now_time_str_info, get_now_time_info, Timeout, timeout 10from aw.Download.Download import * 11from aw.Common.Constant import CONSTANT 12from aw.Common.Common import getFileName 13from aw.ExtractFile.ExtractFile import * 14 15lock_suffix = CONSTANT.File.LOCK_SUFFIX 16suc_file = CONSTANT.File.SUC_FILE 17failed_file = CONSTANT.File.FAILED_FILE 18REBOOT_TIMEOUT = 20000000 19 20class liteOsUpgrade_L0(BaseApp): 21 ''' 22 @author: zwx877058 23 ''' 24 25 def __init__(self, param_file): 26 super().__init__(param_file) 27 self.param_List = ["upgrade_upgradeLocation", 28 "sn"] 29 30 @dec_stepmsg("hongmeng L0 flash") 31 def excute(self): 32 ''' 33 #=================================================================================== 34 # @Method: excute(self) 35 # @Precondition: none 36 # @Func: 升级执行入口 37 # @PostStatus: none 38 # @eg: excute() 39 # @return: True or Flase 40 #=================================================================================== 41 ''' 42 step_index = self.params_dict.get("step_list").index("liteOsUpgrade_L0_app") 43 44 # 执行下载 45 try: 46 if not self.download(): 47 nowtime = get_now_time_str_info() 48 logger.printLog("image download fail" ) 49 return False 50 except Exception as e: 51 logger.error(e) 52 raise e 53 54 if self.params_dict.get("isUpdate") == "False": 55 logger.printLog("no need upgrade board") 56 return True 57 58 # 执行升级 59 try: 60 if not self.upgrade(): 61 logger.printLog("%s upgrade fail") 62 return False 63 return True 64 except Exception as e: 65 logger.error(e) 66 raise e 67 68 def getupgradeport(self): 69 ''' 70 #=================================================================================== 71 # @Method: getupgradeport(self) 72 # @Precondition: none 73 # @Func: 获取升级的端口号 74 # @PostStatus: none 75 # @eg: upgrade() 76 # @return: True or Flase 77 #=================================================================================== 78 ''' 79 coms_info = list_ports.comports() 80 com_list = [] 81 cmd_keyword = "Ch A" 82 up_keyword = "Ch B" 83 logger.printLog(coms_info) 84 for com_info in coms_info: 85 if cmd_keyword in com_info[1]: 86 logger.printLog("get upgrade port is %s" % com_info[0]) 87 self.cmd_com = com_info[0] 88 if up_keyword in com_info[1]: 89 logger.printLog("get upgrade port is %s" % com_info[0]) 90 self.deploy_com = com_info[0] 91 return True 92 93 @dec_stepmsg("upgrade") 94 @timeout(1200) 95 def upgrade(self): 96 ''' 97 #=================================================================================== 98 # @Method: upgrade(self) 99 # @Precondition: none 100 # @Func: 升级相关业务逻辑 101 # @PostStatus: none 102 # @eg: upgrade() 103 # @return: True or Flase 104 #=================================================================================== 105 ''' 106 # 调用ES接口预留 107 logger.info("send_es_mark") 108 if self.params_dict.get("pbiid"): 109 local_image_path = os.path.join(version_savepath, "jarimg", "FASTBOOT", self.params_dict.get("version_type")) 110 logger.info("img path : %s" % local_image_path) 111 else: 112 local_image_path = os.path.join(version_savepath, "img") 113 current_tool_path = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(os.path.dirname(__file__))))) 114 local_tool_path = os.path.join(current_tool_path, "resource", "L0") 115 116 #方法一 117 upgrade_file_name = getFileName(local_tool_path, CONSTANT.OSType.getscripttype()) 118 upgrade_command = "%s %s %s %s >> %s 2>&1" % (upgrade_file_name, local_tool_path, self.cmd_com, self.deploy_com, local_image_path, logger.log_file_path) 119 return subprocess.call(upgrade_command, shell=True) 120 121 @dec_stepmsg("download") 122 @timeout(1800) 123 def download(self): 124 ''' 125 #=================================================================================== 126 # @Method: download(self) 127 # @Precondition: none 128 # @Func: 构建下载到本地的路径,执行相应包的下载 129 # @PostStatus: none 130 # @eg: download() 131 # @return: True or Flase 132 #=================================================================================== 133 ''' 134 global version_savepath, version_name 135 dir_path = CONSTANT.Path.getDirPath() 136 if self.params_dict.get("pbiid"): 137 version_path = self.params_dict.get("pbiid") 138 version_name = str(uuid.uuid5(uuid.NAMESPACE_URL, str(self.params_dict.get("pbiid")) + "FASTBOOT")) 139 version_savepath = os.path.join(dir_path, self.params_dict.get("flash_type"), version_name) 140 else: 141 version_path = self.params_dict.get("upgrade_upgradeLocation") 142 version_name = str(uuid.uuid5(uuid.NAMESPACE_URL, (self.params_dict.get("upgrade_upgradeLocation")))) 143 version_savepath = os.path.join(dir_path, version_name, "img") 144 #执行img下载 145 146 if self.params_dict.get("isDownload") == "True": 147 logger.printLog("不需要做下载,直接返回") 148 return True 149 150 import hashlib 151 save_file_str = version_path.replace("/", "").replace("\\", "") 152 save_file_name = hashlib.sha1(save_file_str.encode("utf-8")).hexdigest() 153 logger.info("download hash string:%s, hash value:%s" % (save_file_str, save_file_name)) 154 save_path_file = os.path.join(dir_path, "record", "%s%s" % (save_file_name, ".txt")) 155 if not self.excutedown(version_path, version_savepath, save_path_file, False): 156 logger.error("download img fail") 157 return False 158 159 #保存本地版本路径给devicetest去版本路径下取用例 160 saveVersion(save_path_file, version_savepath) 161 return True 162 163 def excutedown(self, source_path, download_dir, suc_mark, is_file): 164 ''' 165 #=================================================================================== 166 # @Method: excutedown(source_path, download_dir, suc_mark, is_file) 167 # @Precondition: none 168 # @Func: 执行下载动作 169 # @PostStatus: none 170 # @Param: source_path:资源文件路径 171 # download_dir:文件下载到本地的文件夹路径 172 # is_file:是否是文件 173 # @eg: excutedown("xxxx", "D:\\local\\image", suc_mark, Flase) 174 # @return: True or Flase 175 #=================================================================================== 176 ''' 177 failed_mark = os.path.join(download_dir, failed_file) 178 lock_path = os.path.join(download_dir, lock_suffix) 179 file_lock = FileLock() 180 181 if isDownLoadSuccess(download_dir, suc_mark, failed_mark): 182 return True 183 try: 184 nowtime = get_now_time_str_info() 185 logger.printLog("%s Downloading, please wait" % nowtime) 186 file_lock.lockFile(lock_path) 187 ret = "" 188 logger.info("Get lock. Start to ") 189 if self.params_dict.get("bt_enable") and self.params_dict.get("bt_enable") == "True": 190 ret = downloadByBitComet(source_path, download_dir, os_method) 191 elif source_path.startswith('\\\\'): 192 ret = downloadByCopy(source_path, download_dir, is_file) 193 elif self.params_dict.get("pbiid"): 194 ret = downlaodByDownloadTool(version_savepath, self.params_dict.get("version_type"), "FASTBOOT", self.params_dict.get("pbiid")) 195 elif source_path.startswith("http"): 196 ret = downloadFileFromDevCloud(source_path, "", "", download_dir) 197 198 if source_path.endswith(".zip"): 199 zip_name = os.path.basename(source_path) 200 ret = extractZipFile(os.path.join(download_dir, zip_name), download_dir) 201 if source_path.endswith(".tar.gz") or (source_path.startswith("http") and ("file_id=" in source_path)): 202 if source_path.startswith("http") and ("file_id=" in source_path): 203 if source_path.endswith(".tar.gz"): 204 zip_name = source_path.split('=')[-1] 205 else: 206 zip_name = "out.tar.gz" 207 else: 208 zip_name = os.path.basename(source_path) 209 ret = unTarFile(os.path.join(download_dir, zip_name), download_dir) 210 nowtime = get_now_time_str_info() 211 logger.printLog("%s download to %s end" % (nowtime, download_dir)) 212 213 if not ret: 214 with open(failed_mark, "a+") as fp: 215 fp.write("") 216 return ret 217 except Exception as e: 218 logger.printLog(e) 219 raise Exception(e) 220 finally: 221 file_lock.releaseFile() 222 223 224if __name__ == "__main__": 225 param_file = sys.argv[1] 226 if not param_file: 227 logger.printLog("Missing params file") 228 sys.exit(-1) 229 try: 230 uphandle = liteOsUpgrade_L0(param_file) 231 uphandle._excuteApp() 232 except Exception as e: 233 logger.printLog(e) 234 sys.exit(-1)