1#!/usr/bin/env python3 2# coding: utf-8 3 4""" 5Copyright (c) 2024 Huawei Device Co., Ltd. 6Licensed under the Apache License, Version 2.0 (the "License"); 7you may not use this file except in compliance with the License. 8You may obtain a copy of the License at 9 10 http://www.apache.org/licenses/LICENSE-2.0 11 12Unless required by applicable law or agreed to in awriting, software 13distributed under the License is distributed on an "AS IS" BASIS, 14WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15See the License for the specific language governing permissions and 16limitations under the License. 17""" 18 19import os 20import stat 21import subprocess 22import time 23import zipfile 24 25import requests 26import yaml 27from pywinauto import Application 28from tqdm import tqdm 29 30 31def job(cmd): 32 subprocess.run(cmd, shell=False) 33 34 35def get_tool(): 36 config_file_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), '../config.yaml') 37 with open(config_file_path, 'r') as config_file: 38 data = yaml.safe_load(config_file.read()) 39 url = data['url_tools'] 40 print(f"Getting RKDevTool from {url}") 41 r = requests.get(url, stream=True) 42 total = int(r.headers.get('content-length'), 0) 43 flags = os.O_WRONLY | os.O_CREAT 44 modes = stat.S_IWUSR | stat.S_IRUSR 45 46 with os.fdopen(os.open(r".\RKDevTool.zip", flags, modes), "wb") as f, tqdm( 47 desc="RKDevTool.zip", 48 total=total, 49 unit='iB', 50 unit_scale=True, 51 unit_divisor=1024, 52 ) as bar: 53 for byte in r.iter_content(chunk_size=1024): 54 size = f.write(byte) 55 bar.update(size) 56 with zipfile.ZipFile(".\\RKDevTool.zip", 'r') as zfile: 57 zfile.extractall(path=".\\RKDevTool") 58 59 os.remove('./RKDevtool.zip') 60 61 62def end_burn(dlg): 63 timeout = 300 64 while True: 65 if timeout < 0: 66 return 67 mode = dlg.window(control_type="Tab").window_text() 68 if mode == 'Found One MASKROM Device': 69 dlg.Button16.click() 70 print("image burnning finished") 71 return 72 else: 73 print("please wait for a while...") 74 time.sleep(5) 75 timeout -= 5 76 77 78def auto_burn(): 79 rk_tool_exe_path = os.path.join(os.path.abspath(__file__), r'..\RKDevTool') 80 if not os.path.exists(rk_tool_exe_path): 81 get_tool() 82 os.chdir(rk_tool_exe_path) 83 job('hdc shell reboot bootloader') 84 app = Application(backend='uia').start('RKDevTool.exe') 85 dlg = app.top_window() 86 87 while True: 88 mode = dlg.window(control_type="Tab").window_text() 89 if mode == 'Found One LOADER Device': 90 print('start burning') 91 dlg.window(title="Run").click() 92 time.sleep(100) 93 end_burn(dlg) 94 return 95 else: 96 time.sleep(1) 97 98 99if __name__ == '__main__': 100 auto_burn()