1# -*- coding:utf-8 -*- 2import uuid 3import sys 4import subprocess 5import os 6import time 7import re 8import shutil 9import random 10 11 12from core.base import BaseApp, dec_stepmsg 13from util.file_locker import FileLock 14from util.log_info import logger 15from util.time_info import get_now_time_str_info, get_now_time_info, Timeout, timeout 16from aw.Download.Download import * 17from aw.Common.Constant import CONSTANT 18from aw.Common.Common import getFileName 19from aw.ExtractFile.ExtractFile import * 20from aw.Common.Common import getHostIp, copyFile, copyDirectory 21 22lock_suffix = CONSTANT.File.LOCK_SUFFIX 23suc_file = CONSTANT.File.SUC_FILE 24failed_file = CONSTANT.File.FAILED_FILE 25REBOOT_TIMEOUT = 20000000 26 27 28class liteOsUpgrade_RK3568(BaseApp): 29 ''' 30 @author: cwx1076044 31 ''' 32 33 def __init__(self, param_file): 34 super().__init__(param_file) 35 self.param_List = ["upgrade_upgradeLocation", "sn"] 36 37 @dec_stepmsg("hongmeng RK3568 flash") 38 def excute(self): 39 ''' 40 #=================================================================================== 41 # @Method: excute(self) 42 # @Precondition: none 43 # @Func: 升级执行入口 44 # @PostStatus: none 45 # @eg: excute() 46 # @return: True or Flase 47 #=================================================================================== 48 ''' 49 step_index = self.params_dict.get("step_list").index("liteOsUpgrade_RK3568_app") 50 51 # # 执行下载 52 # try: 53 # if not self.download(): 54 # CONSTANT.ENVERRMESSAGE = "image download fail" 55 # logger.printLog(CONSTANT.ENVERRMESSAGE) 56 # return 98 57 # except Exception as e: 58 # logger.error(e) 59 # #raise e 60 # return 98 61 62 # 执行升级 63 try: 64 return_code = self.upgrade() 65 if not return_code: 66 CONSTANT.ENVERRMESSAGE = "board upgrade fail" 67 logger.printLog(CONSTANT.ENVERRMESSAGE) 68 return False 69 if return_code == 98: 70 return 98 71 if return_code == 99: 72 return 99 73 return True 74 except Exception as e: 75 logger.error(e) 76 raise e 77 78 79 @dec_stepmsg("upgrade") 80 @timeout(3600) 81 def upgrade(self): 82 ''' 83 #=================================================================================== 84 # @Method: upgrade(self) 85 # @Precondition: none 86 # @Func: 升级相关业务逻辑 87 # @PostStatus: none 88 # @eg: upgrade() 89 # @return: True or Flase 90 #=================================================================================== 91 ''' 92 global local_image_path, loader_tool_path, sn, LocationID ,test_num 93 version_savepath = self.params_dict.get("img_path") 94 upgrade_test_type = self.params_dict.get("UpgradeTestType") 95 sn = self.params_dict.get("sn") 96 LocationID = self.params_dict.get("LocationID") 97 test_num = self.params_dict.get("test_num") 98 pr_url = self.params_dict.get("pr_url") 99 logFilePath = self.logFilePath 100 logger.info(logFilePath) 101 r = logFilePath.rfind("\\") 102 report_path = logFilePath[:r] 103 logger.info(report_path) 104 scriptpath = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(os.path.dirname(__file__))))) 105 logger.info(scriptpath) 106 local_image_path = os.path.join(version_savepath) 107 logger.info(local_image_path) 108 loader_tool_path = os.path.join(scriptpath, "resource", "RK3568_tool", "upgrade_tool.exe") 109 logger.info(loader_tool_path) 110 mini_path = os.path.join(local_image_path, "mini_system_test", "L2_mini_system_test.py") 111 archive_path = os.path.join(version_savepath) 112 if not self.check_devices_mode(): 113 check_devices_cmd = "hdc_std list targets" 114 f = send_times(check_devices_cmd) 115 logger.info(f) 116 if not f or "Empty" in f: 117 logger.error("No devices found,please check the device.") 118 return False 119 else: 120 logger.info("3568 board is connected.") 121 return self.check_devices_mode() 122 else: 123 # 下載鏡像 124 upgrde_loader_cmd = "%s -s %s UL %s/MiniLoaderAll.bin -noreset" % (loader_tool_path, LocationID, local_image_path) 125 h = sendCmd(upgrde_loader_cmd) 126 logger.info(h) 127 if "Upgrade loader ok" not in h: 128 logger.error("Download MiniLoaderAll.bin Fail!") 129 return False 130 else: 131 logger.printLog("Download MiniLoaderAll.bin Success!") 132 # time.sleep(3) 133 write_gpt_cmd = "%s -s %s DI -p %s/parameter.txt" % (loader_tool_path, LocationID, local_image_path) 134 j = sendCmd(write_gpt_cmd) 135 logger.info(j) 136 if "Write gpt ok" not in j: 137 logger.error("Failed to execute the parameter.txt") 138 return False 139 else: 140 logger.printLog("Successfully executed parameter.txt.") 141 # time.sleep(5) 142 download_uboot_cmd = "%s -s %s DI -uboot %s/uboot.img %s/parameter.txt" % ( 143 loader_tool_path, LocationID, local_image_path, local_image_path) 144 k = sendCmd(download_uboot_cmd) 145 logger.info(k) 146 if "Download image ok" not in k: 147 logger.error("Failed to download the uboot.image!") 148 if self.check_devices_mode(): 149 return 98 150 return False 151 else: 152 logger.printLog("The uboot.image downloaded successfully!") 153 # time.sleep(5) 154 if not self.flash_version(): 155 return False 156 reboot_devices_cmd = "%s -s %s RD" % (loader_tool_path, LocationID) 157 reboot_result = sendCmd(reboot_devices_cmd) 158 logger.info(reboot_result) 159 time.sleep(30) 160 try: 161 if upgrade_test_type != "mini_system_test": 162 if not start_cmd(sn): 163 if self.check_devices_mode(): 164 return 98 165 return False 166 except Exception as t: 167 logger.info(t) 168 if self.check_devices_mode(): 169 return 98 170 return False 171 time.sleep(10) 172 if "Reset Device OK" not in reboot_result: 173 logger.error("Failed to reboot the board!") 174 return False 175 else: 176 logger.info("Reboot successfully!") 177 logger.printLog("******下载完成,升级成功,开始进行冒烟测试******") 178 if upgrade_test_type == "null": 179 return True 180 screenshot_path = os.path.join(local_image_path, "screenshot") 181 resource_path = os.path.join(screenshot_path, "resource") 182 logger.info(resource_path) 183 py_path = os.path.join(resource_path, "capturescreentest.py") 184 new_report_path = os.path.join(report_path, "result") 185 logger.info(new_report_path) 186 time_sleep = random.randint(1, 5) 187 time.sleep(time_sleep) 188 try: 189 if not os.path.exists(new_report_path): 190 os.mkdir(new_report_path) 191 except Exception as e: 192 logger.error(e) 193 return 98 194 if upgrade_test_type == "mini_system_test": 195 save_path = os.path.join(new_report_path) 196 if exec_cmd(mini_path, sn, save_path, archive_path) == 98: 197 return 98 198 return True 199 if not upgrade_test_type or upgrade_test_type == "smoke_test": 200 test_return = cmd_test(screenshot_path, py_path, new_report_path, resource_path, sn, test_num, pr_url) 201 if test_return == 1: 202 return True 203 if test_return == 98: 204 return 98 205 if test_return == 99: 206 return 99 207 else: 208 return False 209 210 @timeout(1000) 211 def flash_version(self): 212 partList = ["boot_linux", "system", "vendor", "userdata", "resource", "ramdisk", "chipset", "sys-prod", "chip-prod"] 213 for i in partList: 214 if not os.path.exists("%s/%s.img" % (local_image_path, i)): 215 logger.printLog("%s.img is not exist, ignore" % i) 216 continue 217 loadcmd = "%s -s %s DI -%s %s/%s.img" % (loader_tool_path, LocationID, i, local_image_path, i) 218 p = sendCmd(loadcmd) 219 logger.info(p) 220 # time.sleep(5) 221 if "Download image ok" not in p: 222 logger.info("try download %s again!" % i) 223 time.sleep(1) 224 second_cmd = "%s -s %s DI -%s %s/%s.img" % (loader_tool_path, LocationID, i, local_image_path, i) 225 f = sendCmd(second_cmd) 226 logger.info(f) 227 if "Download image ok" not in f: 228 logger.printLog("Failed to download the %s.img!" % i) 229 if self.check_devices_mode(): 230 return 98 231 else: 232 return False 233 return True 234 else: 235 logger.printLog("The %s.img downloaded successfully!" % i) 236 return True 237 238 @timeout(120) 239 def check_devices_mode(self): 240 check_times = 0 241 while check_times < 5: 242 check_mode_cmd = "%s LD" % loader_tool_path 243 g = sendCmd(check_mode_cmd) 244 logger.info(g) 245 #time.sleep(40) 246 if "LocationID=%s Mode=Loader" % LocationID in g: 247 logger.info("3568 board has entered the Loader mode successfully!") 248 return True 249 else: 250 #if test_num != "2/2": 251 # hdc_kill() 252 os.system("hdc_std -t %s shell reboot loader" % sn) 253 time.sleep(5) 254 check_times += 1 255 logger.error("Failed to enter the loader mode!") 256 return False 257 258 # @dec_stepmsg("download") 259 # @timeout(360) 260 # def download(self): 261 # ''' 262 # #=================================================================================== 263 # # @Method: download(self) 264 # # @Precondition: none 265 # # @Func: 构建下载到本地的路径,执行相应包的下载 266 # # @PostStatus: none 267 # # @eg: download() 268 # # @return: True or Flase 269 # #=================================================================================== 270 # ''' 271 # global version_savepath, version_name 272 # dir_path = CONSTANT.Path.getDirPath() 273 # if self.params_dict.get("pbiid"): 274 # version_path = self.params_dict.get("pbiid") 275 # version_name = str(uuid.uuid5(uuid.NAMESPACE_URL, str(self.params_dict.get("pbiid")) + "FASTBOOT")) 276 # version_savepath = os.path.join(dir_path, self.params_dict.get("flash_type"), version_name) 277 # else: 278 # version_path = self.params_dict.get("upgrade_upgradeLocation") 279 # version_name = str(uuid.uuid5(uuid.NAMESPACE_URL, (self.params_dict.get("upgrade_upgradeLocation")))) 280 # version_savepath = os.path.join(dir_path, version_name, "img") 281 # # 执行img下载 282 # 283 # if self.params_dict.get("isDownload") == "True": 284 # logger.printLog("不需要做下载,直接返回") 285 # return True 286 # 287 # import hashlib 288 # save_file_str = version_path.replace("/", "").replace("\\", "") 289 # save_file_name = hashlib.sha1(save_file_str.encode("utf-8")).hexdigest() 290 # logger.info("download hash string:%s, hash value:%s" % (save_file_str, save_file_name)) 291 # save_path_file = os.path.join(dir_path, "record", "%s%s" % (save_file_name, ".txt")) 292 # logger.info(version_savepath) 293 # logger.info(save_path_file) 294 # t = version_savepath[:-4] 295 # logger.info(t) 296 # if not self.excutedown(version_path, version_savepath, save_path_file, False): 297 # logger.info("download again!") 298 # try: 299 # if os.path.exists(t): 300 # shutil.rmtree(t) 301 # logger.info("remove dir succeed") 302 # if os.path.exists(save_path_file): 303 # logger.info("remove file succeed") 304 # os.remove(save_path_file) 305 # except Exception as p: 306 # logger.error(p) 307 # raise Exception(p) 308 # time.sleep(15) 309 # if not self.excutedown(version_path, version_savepath, save_path_file, False): 310 # logger.error("download img fail") 311 # return False 312 # 313 # # 保存本地版本路径给devicetest去版本路径下取用例 314 # saveVersion(save_path_file, version_savepath) 315 # return True 316 # 317 # def excutedown(self, source_path, download_dir, suc_mark, is_file): 318 # ''' 319 # #=================================================================================== 320 # # @Method: excutedown(source_path, download_dir, suc_mark, is_file) 321 # # @Precondition: none 322 # # @Func: 执行下载动作 323 # # @PostStatus: none 324 # # @Param: source_path:资源文件路径 325 # # download_dir:文件下载到本地的文件夹路径 326 # # is_file:是否是文件 327 # # @eg: excutedown("xxxx", "D:\\local\\image", suc_mark, Flase) 328 # # @return: True or Flase 329 # #=================================================================================== 330 # ''' 331 # failed_mark = os.path.join(download_dir, failed_file) 332 # lock_path = os.path.join(download_dir, lock_suffix) 333 # file_lock = FileLock() 334 # 335 # if isDownLoadSuccess(download_dir, suc_mark, failed_mark): 336 # return True 337 # try: 338 # nowtime = get_now_time_str_info() 339 # logger.printLog("%s Downloading, please wait" % nowtime) 340 # file_lock.lockFile(lock_path) 341 # ret = "" 342 # logger.info("Get lock. Start to ") 343 # try: 344 # if self.params_dict.get("bt_enable") and self.params_dict.get("bt_enable") == "True": 345 # ret = downloadByBitComet(source_path, download_dir, os_method) 346 # elif source_path.startswith('\\\\'): 347 # ret = downloadByCopy(source_path, download_dir, is_file) 348 # elif self.params_dict.get("pbiid"): 349 # ret = downlaodByDownloadTool(version_savepath, self.params_dict.get("version_type"), "FASTBOOT", 350 # self.params_dict.get("pbiid")) 351 # elif source_path.startswith("http"): 352 # ret = run_download(source_path, download_dir) 353 # except Exception as f: 354 # logger.error(f) 355 # 356 # if source_path.endswith(".zip"): 357 # zip_name = os.path.basename(source_path) 358 # ret = extractZipFile(os.path.join(download_dir, zip_name), download_dir) 359 # if source_path.endswith(".tar.gz") or (source_path.startswith("http") and ("file_id=" in source_path)): 360 # if source_path.startswith("http") and ("file_id=" in source_path): 361 # if source_path.endswith(".tar.gz"): 362 # zip_name = source_path.split('=')[-1] 363 # else: 364 # zip_name = "out.tar.gz" 365 # else: 366 # zip_name = os.path.basename(source_path) 367 # ret = unTarFile(os.path.join(download_dir, zip_name), download_dir) 368 # nowtime = get_now_time_str_info() 369 # logger.printLog("%s download to %s end" % (nowtime, download_dir)) 370 # 371 # if not ret: 372 # with open(failed_mark, "a+") as fp: 373 # fp.write("") 374 # return ret 375 # except Exception as e: 376 # logger.printLog(e) 377 # #raise Exception(e) 378 # finally: 379 # file_lock.releaseFile() 380 381 382@timeout(30) 383def hdc_kill(): 384 logger.info("kill the process") 385 os.system("hdc_std kill") 386 time.sleep(2) 387 logger.info("start the process") 388 os.system("hdc_std -l5 start") 389 # time.sleep(10) 390 391 392def sendCmd(mycmd): 393 result = "".join(os.popen(mycmd).readlines()) 394 return result 395 396 397def send_times(mycmd): 398 times = 0 399 outcome = sendCmd(mycmd) 400 while times < 3: 401 if not outcome or "Empty" in outcome: 402 times += 1 403 time.sleep(3) 404 else: 405 time.sleep(3) 406 return outcome 407 return outcome 408 409 410@timeout(180) 411def start_cmd(sn): 412 try: 413 os.system("hdc_std -l5 start") 414 power_cmd = "hdc_std -t %s shell \"power-shell setmode 602\"" % sn 415 hilog_cmd = "hdc_std -t %s shell \"hilog -w start -l 400000000 -m none\"" % sn 416 logger.info(power_cmd) 417 logger.info(hilog_cmd) 418 power_result = sendCmd(power_cmd) 419 logger.info(power_result) 420 if not power_result: 421 return False 422 number = 0 423 while "Set Mode Success" not in power_result and number < 30: 424 time.sleep(4) 425 power_result = sendCmd(power_cmd) 426 logger.info(power_result) 427 number += 1 428 if number >= 20: 429 logger.error("Set mode failed") 430 return False 431 hilog_result = sendCmd(hilog_cmd) 432 logger.info(hilog_result) 433 return True 434 except Exception as e: 435 logger.error(e) 436 return False 437 438 439@timeout(3600) 440def cmd_test(screenshot_path, py_path, new_report_path, resource_path, sn, test_num, pr_url): 441 save_screenshot_path = os.path.join(new_report_path, "screenshot_result") 442 logger.info(save_screenshot_path) 443 time_sleep = random.randint(1, 5) 444 time.sleep(time_sleep) 445 try: 446 if not os.path.exists(save_screenshot_path): 447 os.mkdir(save_screenshot_path) 448 logger.info(save_screenshot_path) 449 base_screenshot_path = os.path.join(new_report_path, "screenshot_base") 450 if not os.path.exists(base_screenshot_path): 451 os.mkdir(base_screenshot_path) 452 logger.info(base_screenshot_path) 453 except Exception as e: 454 logger.error(e) 455 return 98 456 config_path = os.path.join(screenshot_path, "resource", "app_capture_screen_test_config.json") 457 py_cmd = "python %s --config %s --anwser_path %s --save_path %s --device_num %s --test_num %s --tools_path %s --pr_url %s" \ 458 % (py_path, config_path, resource_path, save_screenshot_path, sn, test_num, screenshot_path, pr_url) 459 result = outCmd(py_cmd, save_screenshot_path, base_screenshot_path, resource_path) 460 if result == 1: 461 return True 462 if result == 98: 463 return 98 464 if result == 99: 465 return 99 466 else: 467 return False 468 469 470@timeout(3600) 471def outCmd(cmd, save_screenshot_path, base_screenshot_path, resource_path): 472 logger.info("cmd is: %s" % cmd) 473 p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, encoding="gbk") 474 curline = p.stdout.readline() 475 list_png_name = [] 476 try: 477 while "End of check" not in curline: 478 curline = p.stdout.readline() 479 logger.info(curline) 480 if "abnarmal" in curline: 481 png_name = curline.split(" ")[3].split(".")[0] 482 list_png_name.append(png_name) 483 if "SmokeTest find some fatal problems" in curline: 484 logger.error("SmokeTest find some fatal problems!") 485 return 99 486 except Exception as e: 487 logger.error(e) 488 logger.error("execute smoke_test.py failed!") 489 return 99 490 l = list(set(list_png_name)) 491 if l: 492 logger.error(l) 493 try: 494 for i in l: 495 result = os.path.join(resource_path, "%s.png" % i) 496 base = os.path.join(base_screenshot_path, "%s.png" % i) 497 shutil.copy(result, base) 498 except Exception as t: 499 logger.info(t) 500 p.wait() 501 logger.info("p.returncode %s" % p.returncode) 502 if p.returncode == 0: 503 logger.info("screenshot check is ok!") 504 return True 505 if p.returncode == 101: 506 logger.error("device disconnection, please check the device!") 507 return False 508 logger.error("screenshot test failed, check the %s" % save_screenshot_path) 509 return 98 510 511 512@timeout(1000) 513def exec_cmd(mini_path, sn, save_path, archive_path): 514 cmd = "python %s --device_num %s --save_path %s --archive_path %s" % (mini_path, sn, save_path, archive_path) 515 p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, encoding="gbk") 516 curline = p.stdout.readline() 517 try: 518 while "End of check" not in curline: 519 curline = p.stdout.readline() 520 logger.info(curline) 521 except Exception as e: 522 logger.error(e) 523 p.wait() 524 logger.info("p.returncode %s" % p.returncode) 525 if p.returncode == 0: 526 logger.info("mini_system_test is ok!") 527 return True 528 logger.error("mini_system_test failed!") 529 return 98