1# -*- coding: utf-8 -*- 2# Copyright (c) 2022 Huawei Device Co., Ltd. 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15from ast import parse 16import json 17import sys 18import os 19import time 20import argparse 21import re 22import subprocess 23import shlex 24import datetime 25import serial 26import threading 27 28def GetDirSize(dir_path): 29 if not os.path.exists(dir_path): 30 PrintToLog("\n\nERROR: %s, dir are not exist!!!\n" % dir_path) 31 PrintToLog("End of check, test failed!") 32 sys.exit(99) 33 size = 0 34 for root, dirs, files in os.walk(dir_path): 35 for name in files: 36 sz = os.path.getsize(os.path.join(root, name)) 37 print('{} : {}byte'.format(os.path.join(root, name), sz)) 38 size += sz 39 PrintToLog('total size: {:.2f}M'.format(size/1024/1024)) 40 return size 41 42def PrintToLog(str): 43 time = datetime.datetime.now() 44 str = "[{}] {}".format(time, str) 45 print(str) 46 with open(os.path.join(args.save_path, 'L1_mini_test.log'), mode='a', encoding='utf-8') as log_file: 47 console = sys.stdout 48 sys.stdout = log_file 49 print(str) 50 sys.stdout = console 51 log_file.close() 52 53def WriteToComPort(com_port, cmd): 54 len = com_port.write(cmd.encode('utf-8')) 55 #print('{}'.format(len)) 56 return 57 58def ReadFromComPort(com_port, timeout): 59 time_start = datetime.datetime.now() 60 time_end = time_start 61 #print((time_end - time_start).seconds) 62 global com_output 63 com_output = '' 64 while (time_end - time_start).seconds < timeout: 65 com_output_once = '' 66 while com_port.inWaiting() > 0: 67 com_output_once += com_port.read(com_port.inWaiting()).decode() 68 if com_output_once != '': 69 com_output += com_output_once 70 print('{}'.format(com_output_once), end='') 71 time_end = datetime.datetime.now() 72 return com_output 73 74if __name__ == "__main__": 75 parser = argparse.ArgumentParser(description='manual to this script') 76 parser.add_argument('--com_port', type=str, default = 'COM8') 77 parser.add_argument('--com_baudrate', type=int, default = 115200) 78 parser.add_argument('--save_path', type=str, default = 'D:\\DeviceTestTools\\screenshot') 79 parser.add_argument('--archive_path', type=str, default = 'D:\DeviceTestTools') 80 args = parser.parse_args() 81 82 com_port = serial.Serial(args.com_port, args.com_baudrate) 83 if com_port.isOpen(): 84 PrintToLog("{} is open successed".format(com_port)) 85 else: 86 PrintToLog("{} is open failed".format(com_port)) 87 PrintToLog("End of check, test failed!") 88 sys.exit(99) 89 90 res = com_port.write("free".encode('utf-8')); 91 read_com_thread = threading.Thread(target=ReadFromComPort, args=(com_port, 10)) 92 read_com_thread.setDaemon(True) 93 print('read wait:') 94 read_com_thread.start() 95 time.sleep(1) 96 WriteToComPort(com_port, '\r\n\r\n\r\n') 97 WriteToComPort(com_port, 'task\r\n') 98 print('enter task') 99 time.sleep(3) 100 print(com_output) 101 foundation_proc_find = re.findall('\d{1,4}\s*\d{1,4}\s*\d{1,4}\s*\d{1,4}.\d{1,4}\s*foundation', com_output) 102 print(foundation_proc_find) 103 if type(foundation_proc_find) == list and len(foundation_proc_find) > 0: 104 PrintToLog('found foundation process') 105 else: 106 PrintToLog('not found foundation process') 107 PrintToLog("End of check, test failed!") 108 sys.exit(99) 109 shell_proc_find = re.findall('\d{1,4}\s*\d{1,4}\s*\d{1,4}\s*\d{1,4}.\d{1,4}\s*shell', com_output) 110 print(shell_proc_find) 111 if type(shell_proc_find) == list and len(shell_proc_find) > 0: 112 PrintToLog('found shell process') 113 else: 114 PrintToLog('not found shell process') 115 PrintToLog("End of check, test failed!") 116 sys.exit(99) 117 apphilogcat_proc_find = re.findall('\d{1,4}\s*\d{1,4}\s*\d{1,4}\s*\d{1,4}.\d{1,4}\s*apphilogcat', com_output) 118 print(apphilogcat_proc_find) 119 if type(apphilogcat_proc_find) == list and len(apphilogcat_proc_find) > 0: 120 PrintToLog('found apphilogcat process') 121 else: 122 PrintToLog('not found apphilogcat process') 123 PrintToLog("End of check, test failed!") 124 sys.exit(99) 125 deviceauth_service_proc_find = re.findall('\d{1,4}\s*\d{1,4}\s*\d{1,4}\s*\d{1,4}.\d{1,4}\s*deviceauth_service', com_output) 126 print(deviceauth_service_proc_find) 127 if type(deviceauth_service_proc_find) == list and len(deviceauth_service_proc_find) > 0: 128 PrintToLog('found deviceauth_service process') 129 else: 130 PrintToLog('not found deviceauth_service process') 131 PrintToLog("End of check, test failed!") 132 sys.exit(99) 133 softbus_server_proc_find = re.findall('\d{1,4}\s*\d{1,4}\s*\d{1,4}\s*\d{1,4}.\d{1,4}\s*softbus_server', com_output) 134 print(softbus_server_proc_find) 135 if type(softbus_server_proc_find) == list and len(softbus_server_proc_find) > 0: 136 PrintToLog('found softbus_server process') 137 else: 138 PrintToLog('not found softbus_server process') 139 PrintToLog("End of check, test failed!") 140 sys.exit(99) 141 mem_find = re.findall('Mem:\s*\d*\s*\d*\s*\d*\s*\d*', com_output) 142 print(mem_find) 143 if len(mem_find) > 0: 144 mem_size = int(mem_find[0].split()[2]) / 1024 / 1024 145 else: 146 PrintToLog('Error:can find memory usage info!') 147 sys.exit(99) 148 if mem_size > 25: 149 PrintToLog( 150 'Error:memory usage is over the upper limit(25M),now is {:.2f}'.format(mem_size)) 151 sys.exit(99) 152 153 target_dir = os.path.normpath(os.path.join(args.archive_path, "rootfs")) 154 ret_size = GetDirSize(target_dir)/1024/1024 155 PrintToLog('Size of rootfs is ({:.2f}M)'.format(ret_size)) 156 if ret_size > 15: 157 PrintToLog('ERROR: Size of rootfs({:.2f}M) is over the upper limit(15M)'.format(ret_size)) 158 PrintToLog("End of check, test failed!") 159 sys.exit(99) 160 161 target_dir = os.path.normpath(os.path.join(args.archive_path, "userfs")) 162 ret_size = GetDirSize(target_dir)/1024/1024 163 PrintToLog('Size of userfs is ({:.2f}M)'.format(ret_size)) 164 if ret_size > 15: 165 PrintToLog('ERROR: Size of userfs({:.2f}M) is over the upper limit(15M)'.format(ret_size)) 166 PrintToLog("End of check, test failed!") 167 sys.exit(99) 168 169 PrintToLog("End of check, test succeeded!") 170 sys.exit(0) 171