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 200 if not upgrade_test_type or upgrade_test_type == "smoke_test": 201 test_return = cmd_test(screenshot_path, py_path, new_report_path, resource_path, sn, test_num, pr_url) 202 if test_return == 1: 203 return True 204 if test_return == 98: 205 return 98 206 if test_return == 99: 207 return 99 208 else: 209 return False 210 211 @timeout(1000) 212 def flash_version(self): 213 partList = ["boot_linux", "system", "vendor", "userdata", "resource", "ramdisk", "chipset", "sys-prod", "chip-prod"] 214 for i in partList: 215 if not os.path.exists("%s/%s.img" % (local_image_path, i)): 216 logger.printLog("%s.img is not exist, ignore" % i) 217 continue 218 loadcmd = "%s -s %s DI -%s %s/%s.img" % (loader_tool_path, LocationID, i, local_image_path, i) 219 p = sendCmd(loadcmd) 220 logger.info(p) 221 # time.sleep(5) 222 if "Download image ok" not in p: 223 logger.info("try download %s again!" % i) 224 time.sleep(1) 225 second_cmd = "%s -s %s DI -%s %s/%s.img" % (loader_tool_path, LocationID, i, local_image_path, i) 226 f = sendCmd(second_cmd) 227 logger.info(f) 228 if "Download image ok" not in f: 229 logger.printLog("Failed to download the %s.img!" % i) 230 if self.check_devices_mode(): 231 return 98 232 else: 233 return False 234 return True 235 else: 236 logger.printLog("The %s.img downloaded successfully!" % i) 237 return True 238 239 @timeout(120) 240 def check_devices_mode(self): 241 check_times = 0 242 while check_times < 5: 243 check_mode_cmd = "%s LD" % loader_tool_path 244 g = sendCmd(check_mode_cmd) 245 logger.info(g) 246 #time.sleep(40) 247 if "LocationID=%s Mode=Loader" % LocationID in g: 248 logger.info("3568 board has entered the Loader mode successfully!") 249 return True 250 else: 251 #if test_num != "2/2": 252 # hdc_kill() 253 os.system("hdc_std -t %s shell reboot loader" % sn) 254 time.sleep(5) 255 check_times += 1 256 logger.error("Failed to enter the loader mode!") 257 return False 258 259 # @dec_stepmsg("download") 260 # @timeout(360) 261 # def download(self): 262 # ''' 263 # #=================================================================================== 264 # # @Method: download(self) 265 # # @Precondition: none 266 # # @Func: 构建下载到本地的路径,执行相应包的下载 267 # # @PostStatus: none 268 # # @eg: download() 269 # # @return: True or Flase 270 # #=================================================================================== 271 # ''' 272 # global version_savepath, version_name 273 # dir_path = CONSTANT.Path.getDirPath() 274 # if self.params_dict.get("pbiid"): 275 # version_path = self.params_dict.get("pbiid") 276 # version_name = str(uuid.uuid5(uuid.NAMESPACE_URL, str(self.params_dict.get("pbiid")) + "FASTBOOT")) 277 # version_savepath = os.path.join(dir_path, self.params_dict.get("flash_type"), version_name) 278 # else: 279 # version_path = self.params_dict.get("upgrade_upgradeLocation") 280 # version_name = str(uuid.uuid5(uuid.NAMESPACE_URL, (self.params_dict.get("upgrade_upgradeLocation")))) 281 # version_savepath = os.path.join(dir_path, version_name, "img") 282 # # 执行img下载 283 # 284 # if self.params_dict.get("isDownload") == "True": 285 # logger.printLog("不需要做下载,直接返回") 286 # return True 287 # 288 # import hashlib 289 # save_file_str = version_path.replace("/", "").replace("\\", "") 290 # save_file_name = hashlib.sha1(save_file_str.encode("utf-8")).hexdigest() 291 # logger.info("download hash string:%s, hash value:%s" % (save_file_str, save_file_name)) 292 # save_path_file = os.path.join(dir_path, "record", "%s%s" % (save_file_name, ".txt")) 293 # logger.info(version_savepath) 294 # logger.info(save_path_file) 295 # t = version_savepath[:-4] 296 # logger.info(t) 297 # if not self.excutedown(version_path, version_savepath, save_path_file, False): 298 # logger.info("download again!") 299 # try: 300 # if os.path.exists(t): 301 # shutil.rmtree(t) 302 # logger.info("remove dir succeed") 303 # if os.path.exists(save_path_file): 304 # logger.info("remove file succeed") 305 # os.remove(save_path_file) 306 # except Exception as p: 307 # logger.error(p) 308 # raise Exception(p) 309 # time.sleep(15) 310 # if not self.excutedown(version_path, version_savepath, save_path_file, False): 311 # logger.error("download img fail") 312 # return False 313 # 314 # # 保存本地版本路径给devicetest去版本路径下取用例 315 # saveVersion(save_path_file, version_savepath) 316 # return True 317 # 318 # def excutedown(self, source_path, download_dir, suc_mark, is_file): 319 # ''' 320 # #=================================================================================== 321 # # @Method: excutedown(source_path, download_dir, suc_mark, is_file) 322 # # @Precondition: none 323 # # @Func: 执行下载动作 324 # # @PostStatus: none 325 # # @Param: source_path:资源文件路径 326 # # download_dir:文件下载到本地的文件夹路径 327 # # is_file:是否是文件 328 # # @eg: excutedown("xxxx", "D:\\local\\image", suc_mark, Flase) 329 # # @return: True or Flase 330 # #=================================================================================== 331 # ''' 332 # failed_mark = os.path.join(download_dir, failed_file) 333 # lock_path = os.path.join(download_dir, lock_suffix) 334 # file_lock = FileLock() 335 # 336 # if isDownLoadSuccess(download_dir, suc_mark, failed_mark): 337 # return True 338 # try: 339 # nowtime = get_now_time_str_info() 340 # logger.printLog("%s Downloading, please wait" % nowtime) 341 # file_lock.lockFile(lock_path) 342 # ret = "" 343 # logger.info("Get lock. Start to ") 344 # try: 345 # if self.params_dict.get("bt_enable") and self.params_dict.get("bt_enable") == "True": 346 # ret = downloadByBitComet(source_path, download_dir, os_method) 347 # elif source_path.startswith('\\\\'): 348 # ret = downloadByCopy(source_path, download_dir, is_file) 349 # elif self.params_dict.get("pbiid"): 350 # ret = downlaodByDownloadTool(version_savepath, self.params_dict.get("version_type"), "FASTBOOT", 351 # self.params_dict.get("pbiid")) 352 # elif source_path.startswith("http"): 353 # ret = run_download(source_path, download_dir) 354 # except Exception as f: 355 # logger.error(f) 356 # 357 # if source_path.endswith(".zip"): 358 # zip_name = os.path.basename(source_path) 359 # ret = extractZipFile(os.path.join(download_dir, zip_name), download_dir) 360 # if source_path.endswith(".tar.gz") or (source_path.startswith("http") and ("file_id=" in source_path)): 361 # if source_path.startswith("http") and ("file_id=" in source_path): 362 # if source_path.endswith(".tar.gz"): 363 # zip_name = source_path.split('=')[-1] 364 # else: 365 # zip_name = "out.tar.gz" 366 # else: 367 # zip_name = os.path.basename(source_path) 368 # ret = unTarFile(os.path.join(download_dir, zip_name), download_dir) 369 # nowtime = get_now_time_str_info() 370 # logger.printLog("%s download to %s end" % (nowtime, download_dir)) 371 # 372 # if not ret: 373 # with open(failed_mark, "a+") as fp: 374 # fp.write("") 375 # return ret 376 # except Exception as e: 377 # logger.printLog(e) 378 # #raise Exception(e) 379 # finally: 380 # file_lock.releaseFile() 381 382 383@timeout(30) 384def hdc_kill(): 385 logger.info("kill the process") 386 os.system("hdc_std kill") 387 time.sleep(2) 388 logger.info("start the process") 389 os.system("hdc_std -l5 start") 390 # time.sleep(10) 391 392 393def sendCmd(mycmd): 394 result = "".join(os.popen(mycmd).readlines()) 395 return result 396 397 398def send_times(mycmd): 399 times = 0 400 outcome = sendCmd(mycmd) 401 while times < 3: 402 if not outcome or "Empty" in outcome: 403 times += 1 404 time.sleep(3) 405 else: 406 time.sleep(3) 407 return outcome 408 return outcome 409 410 411@timeout(180) 412def start_cmd(sn): 413 try: 414 os.system("hdc_std -l5 start") 415 power_cmd = "hdc_std -t %s shell \"power-shell setmode 602\"" % sn 416 hilog_cmd = "hdc_std -t %s shell \"hilog -w start -l 400000000 -m none\"" % sn 417 logger.info(power_cmd) 418 logger.info(hilog_cmd) 419 power_result = sendCmd(power_cmd) 420 logger.info(power_result) 421 if not power_result: 422 return False 423 number = 0 424 while "Set Mode Success" not in power_result and number < 30: 425 time.sleep(4) 426 power_result = sendCmd(power_cmd) 427 logger.info(power_result) 428 number += 1 429 if number >= 20: 430 logger.error("Set mode failed") 431 return False 432 hilog_result = sendCmd(hilog_cmd) 433 logger.info(hilog_result) 434 return True 435 except Exception as e: 436 logger.error(e) 437 return False 438 439 440@timeout(3600) 441def cmd_test(screenshot_path, py_path, new_report_path, resource_path, sn, test_num, pr_url): 442 save_screenshot_path = os.path.join(new_report_path, "screenshot_result") 443 logger.info(save_screenshot_path) 444 time_sleep = random.randint(1, 5) 445 time.sleep(time_sleep) 446 try: 447 if not os.path.exists(save_screenshot_path): 448 os.mkdir(save_screenshot_path) 449 logger.info(save_screenshot_path) 450 base_screenshot_path = os.path.join(new_report_path, "screenshot_base") 451 if not os.path.exists(base_screenshot_path): 452 os.mkdir(base_screenshot_path) 453 logger.info(base_screenshot_path) 454 except Exception as e: 455 logger.error(e) 456 return 98 457 config_path = os.path.join(screenshot_path, "resource", "app_capture_screen_test_config.json") 458 py_cmd = "python %s --config %s --anwser_path %s --save_path %s --device_num %s --test_num %s --tools_path %s --pr_url %s" \ 459 % (py_path, config_path, resource_path, save_screenshot_path, sn, test_num, screenshot_path, pr_url) 460 result = outCmd(py_cmd, save_screenshot_path, base_screenshot_path, resource_path) 461 if result == 1: 462 return True 463 if result == 98: 464 return 98 465 if result == 99: 466 return 99 467 else: 468 return False 469 470 471@timeout(3600) 472def outCmd(cmd, save_screenshot_path, base_screenshot_path, resource_path): 473 logger.info("cmd is: %s" % cmd) 474 p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, encoding="gbk") 475 curline = p.stdout.readline() 476 list_png_name = [] 477 try: 478 while "End of check" not in curline: 479 curline = p.stdout.readline() 480 logger.info(curline) 481 if "abnarmal" in curline: 482 png_name = curline.split(" ")[3].split(".")[0] 483 list_png_name.append(png_name) 484 if "SmokeTest find some fatal problems" in curline: 485 logger.error("SmokeTest find some fatal problems!") 486 return 99 487 except Exception as e: 488 logger.error(e) 489 logger.error("execute smoke_test.py failed!") 490 return 99 491 l = list(set(list_png_name)) 492 if l: 493 logger.error(l) 494 try: 495 for i in l: 496 result = os.path.join(resource_path, "%s.jpeg" % i) 497 base = os.path.join(base_screenshot_path, "%s.jpeg" % i) 498 shutil.copy(result, base) 499 except Exception as t: 500 logger.info(t) 501 p.wait() 502 logger.info("p.returncode %s" % p.returncode) 503 if p.returncode == 0: 504 logger.info("screenshot check is ok!") 505 return True 506 if p.returncode == 101: 507 logger.error("device disconnection, please check the device!") 508 return False 509 logger.error("screenshot test failed, check the %s" % save_screenshot_path) 510 return 98 511 512 513@timeout(1000) 514def exec_cmd(mini_path, sn, save_path, archive_path): 515 cmd = "python %s --device_num %s --save_path %s --archive_path %s" % (mini_path, sn, save_path, archive_path) 516 p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, encoding="gbk") 517 curline = p.stdout.readline() 518 try: 519 while "End of check" not in curline: 520 curline = p.stdout.readline() 521 logger.info(curline) 522 except Exception as e: 523 logger.error(e) 524 p.wait() 525 logger.info("p.returncode %s" % p.returncode) 526 if p.returncode == 0: 527 logger.info("mini_system_test is ok!") 528 return True 529 logger.error("mini_system_test failed!") 530 return 98