1#!/usr/bin/env python3 2# coding=utf-8 3 4# 5# Copyright (c) 2021 Huawei Device Co., Ltd. 6# Licensed under the Apache License, Version 2.0 (the "License"); 7# you may not use this file except in compliance with the License. 8# You may obtain a copy of the License at 9# 10# http://www.apache.org/licenses/LICENSE-2.0 11# 12# Unless required by applicable law or agreed to in writing, software 13# distributed under the License is distributed on an "AS IS" BASIS, 14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15# See the License for the specific language governing permissions and 16# limitations under the License. 17# 18 19import os 20import sys 21import re 22import json 23import time 24 25# insert src path for loading xdevice modules 26sys.framework_src_dir = os.path.abspath(os.path.dirname( 27 os.path.dirname(__file__))) 28sys.path.insert(1, sys.framework_src_dir) 29sys.framework_root_dir = os.path.abspath(os.path.dirname( 30 os.path.dirname(os.path.dirname(os.path.dirname( 31 os.path.dirname(__file__)))))) 32sys.xdevice_dir = os.path.abspath(os.path.join( 33 sys.framework_root_dir, 34 "src")) 35sys.path.insert(2, sys.xdevice_dir) 36sys.xdevice_dir = os.path.abspath(os.path.join( 37 sys.framework_root_dir, 38 "..", 39 "xdevice", 40 "src")) 41sys.path.insert(3, sys.xdevice_dir) 42sys.adapter_dir = os.path.abspath(os.path.join( 43 sys.framework_root_dir, 44 "adapter", 45 "aw", 46 "python")) 47sys.path.insert(4, sys.adapter_dir) 48 49from distributed.common.common import create_empty_result_file 50from distributed.common.common import get_resource_dir 51from distributed.common.drivers import CppTestDriver 52from distributed.common.drivers import DexTestDriver 53from distributed.common.drivers import HapTestDriver 54 55DEVICE_INFO_TEMPLATE = "agentlist:%s\nagentport:%s,\ndevicesuuid:%s" 56 57 58############################################################################## 59############################################################################## 60 61 62def get_current_driver(device, target_name): 63 driver = None 64 _, suffix_name = os.path.splitext(target_name) 65 if suffix_name == "": 66 driver = CppTestDriver(device) 67 elif suffix_name == ".bin": 68 driver = CppTestDriver(device) 69 elif suffix_name == ".dex": 70 driver = DexTestDriver(device) 71 elif suffix_name == ".hap": 72 driver = HapTestDriver(device) 73 return driver 74 75 76############################################################################## 77############################################################################## 78 79 80class Distribute: 81 def __init__(self, suite_dir, major, agent_list): 82 self.suite_dir = suite_dir 83 self.major = major 84 self.agent_list = agent_list 85 86 def exec_agent(self, device, target_name): 87 driver = get_current_driver(device, target_name) 88 if driver is None: 89 print("Error: driver is None.") 90 return False 91 92 resource_dir = get_resource_dir(self.suite_dir, device.name) 93 self._make_agent_desc_file(device) 94 device.push_file(os.path.join(self.suite_dir, "agent.desc"), 95 device.test_path) 96 device.push_file(os.path.join(resource_dir, target_name), 97 device.test_path) 98 99 suite_path = os.path.join(self.suite_dir, target_name) 100 driver.execute(suite_path, background=True) 101 return self._check_thread(device, target_name) 102 103 def exec_major(self, device, target_name): 104 driver = get_current_driver(device, target_name) 105 if driver is None: 106 print("Error: driver is None.") 107 return False 108 109 resource_dir = get_resource_dir(self.suite_dir, device.name) 110 self._make_major_desc_file() 111 device.push_file(os.path.join(self.suite_dir, "major.desc"), 112 device.test_path) 113 device.push_file(os.path.join(resource_dir, target_name), 114 device.test_path) 115 116 suite_path = os.path.join(self.suite_dir, target_name) 117 return driver.execute(suite_path, background=False) 118 119 def pull_result(self, device, source_path, result_save_path): 120 _, file_name = os.path.split(source_path) 121 device.pull_file(source_path, result_save_path) 122 if not os.path.exists(os.path.join(result_save_path, file_name)): 123 create_empty_result_file(result_save_path, file_name) 124 return 125 126 def _check_thread(self, device, thread_name): 127 check_command = "ps -A | grep %s" % thread_name 128 checksum = 0 129 while checksum < 100: # check 100 times 130 checksum += 1 131 print("check thread:%s %s times" % (thread_name, checksum)) 132 output = device.shell_with_output(check_command) 133 if output == "": 134 time.sleep(0.1) 135 else: 136 print("thread info: %s" % output) 137 break 138 return True if checksum < 100 else False 139 140 def _make_agent_desc_file(self, device): 141 agent_ip_list = "" 142 device_uuid_list = "" 143 device_uuid_list += self._query_device_uuid(self.major) + "," 144 agent_ip_list += self._query_device_ip(device) + "," 145 for agent in self.agent_list: 146 device_uuid_list += self._query_device_uuid(agent) + "," 147 config_info = DEVICE_INFO_TEMPLATE % (agent_ip_list, "8888", 148 device_uuid_list) 149 150 config_agent_file = os.path.join(self.suite_dir, "agent.desc") 151 self._write_device_config(config_info, config_agent_file) 152 153 def _make_major_desc_file(self): 154 agent_ip_list = "" 155 device_uuid_list = "" 156 157 device_uuid_list += self._query_device_uuid(self.major) + "," 158 for agent in self.agent_list: 159 agent_ip_list += self._query_device_ip(agent) + "," 160 device_uuid_list += self._query_device_uuid(agent) + "," 161 config_info = DEVICE_INFO_TEMPLATE % (agent_ip_list, "8888", 162 device_uuid_list) 163 164 config_major_file = os.path.join(self.suite_dir, "major.desc") 165 self._write_device_config(config_info, config_major_file) 166 167 def _query_device_ip(self, device): 168 output = device.shell_with_output("getprop ro.hardware") 169 if output == "": 170 return "" 171 172 isemulator = re.findall(r"ranchu", output) 173 output = device.shell_with_output("ifconfig") 174 if output == "": 175 return "" 176 177 if len(isemulator) != 0: 178 ipaddress = re.findall(r"\b10\.0\.2\.[0-9]{1,3}\b", output) 179 else: 180 ip_template = r"\b192\.168\.(?:[0-9]{1,3}\.)[0-9]{1,3}\b" 181 ipaddress = re.findall(ip_template, output) 182 183 if len(ipaddress) == 0: 184 return "" 185 186 return ipaddress[0] 187 188 def _query_device_uuid(self, device): 189 """ 190 1. Run the dumpsys DdmpDeviceMonitorService command to query the value 191 of dev_nodeid. 192 2. The dump information reported by the soft bus. Local device info, 193 should be placed first. 194 Note: The dump information may not comply with the JSON format. 195 """ 196 dumpsys_command = "dumpsys DdmpDeviceMonitorService" 197 device_info = device.shell_with_output(dumpsys_command) 198 if device_info == "": 199 return "" 200 201 begin = device_info.find("dev_nodeid") 202 if (begin == -1): 203 return "" 204 205 end = device_info.find(",", begin) 206 if (end == -1): 207 return "" 208 209 dev_nodeid_info = device_info[begin:end].replace('"', "") 210 return dev_nodeid_info.split(":")[1] 211 212 def _write_device_config(self, device_info, file_path): 213 file_dir, file_name = os.path.split(file_path) 214 final_file = os.path.join(file_dir, file_name.split('.')[0] + ".desc") 215 if os.path.exists(final_file): 216 os.remove(final_file) 217 with open(file_path, 'w') as file_desc: 218 file_desc.write(device_info) 219 os.rename(file_path, final_file) 220 221 222############################################################################## 223############################################################################## 224 225