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 time 23import platform 24import stat 25 26FLAGS = os.O_WRONLY | os.O_CREAT | os.O_EXCL 27MODES = stat.S_IWUSR | stat.S_IRUSR 28 29# insert src path for loading xdevice modules 30 31sys.framework_src_dir = os.path.abspath(os.path.dirname( 32 os.path.dirname(__file__))) 33sys.path.insert(1, sys.framework_src_dir) 34sys.framework_root_dir = os.path.abspath(os.path.dirname( 35 os.path.dirname(os.path.dirname(os.path.dirname( 36 os.path.dirname(__file__)))))) 37sys.xdevice_dir = os.path.abspath(os.path.join( 38 sys.framework_root_dir, 39 "src")) 40sys.path.insert(2, sys.xdevice_dir) 41sys.xdevice_dir = os.path.abspath(os.path.join( 42 sys.framework_root_dir, 43 "..", 44 "xdevice", 45 "src")) 46sys.path.insert(3, sys.xdevice_dir) 47sys.adapter_dir = os.path.abspath(os.path.join( 48 sys.framework_root_dir, 49 "adapter", 50 "aw", 51 "python")) 52sys.path.insert(4, sys.adapter_dir) 53 54 55from distributed.common.common import create_empty_result_file 56from distributed.common.common import get_resource_dir 57from distributed.common.drivers import CppTestDriver 58 59 60DEVICE_INFO_TEMPLATE = "agentlist:%s\nagentport:%s,\ndevicesuuid:%s" 61 62 63############################################################################## 64############################################################################## 65 66 67def get_current_driver(device, target_name, hdc_tools): 68 driver = None 69 _, suffix_name = os.path.splitext(target_name) 70 if suffix_name == "": 71 driver = CppTestDriver(device, hdc_tools) 72 elif suffix_name == ".bin": 73 driver = CppTestDriver(device, hdc_tools) 74 return driver 75 76 77############################################################################## 78############################################################################## 79 80 81class Distribute: 82 def __init__(self, suite_dir, major, agent_list, hdc_tools): 83 self.suite_dir = suite_dir 84 self.major = major 85 self.agent_list = agent_list 86 self.hdc_tools = hdc_tools 87 88 def exec_agent(self, device, target_name, result_path, options): 89 driver = get_current_driver(device, target_name, self.hdc_tools) 90 if driver is None: 91 print("Error: driver is None.") 92 return False 93 94 resource_dir = get_resource_dir(self.suite_dir, device.name) 95 96 self._make_agent_desc_file(device) 97 device.push_file(os.path.join(self.suite_dir, "agent.desc"), 98 device.test_path) 99 device.push_file(os.path.join(resource_dir, target_name), 100 device.test_path) 101 102 suite_path = os.path.join(self.suite_dir, target_name) 103 driver.execute(suite_path, result_path, options, background=True) 104 return self._check_thread(device, target_name) 105 106 def exec_major(self, device, target_name, result_path, options): 107 driver = get_current_driver(device, target_name, self.hdc_tools) 108 if driver is None: 109 print("Error: driver is None.") 110 return False 111 112 resource_dir = get_resource_dir(self.suite_dir, device.name) 113 self._make_major_desc_file() 114 device.push_file(os.path.join(self.suite_dir, "major.desc"), 115 device.test_path) 116 device.push_file(os.path.join(resource_dir, target_name), 117 device.test_path) 118 119 suite_path = os.path.join(self.suite_dir, target_name) 120 return driver.execute(suite_path, result_path, options, background=False) 121 122 @staticmethod 123 def pull_result(device, source_path, result_save_path): 124 _, file_name = os.path.split(source_path) 125 device.pull_file(source_path, result_save_path) 126 if not os.path.exists(os.path.join(result_save_path, file_name)): 127 create_empty_result_file(result_save_path, file_name) 128 129 @staticmethod 130 def _check_thread(device, thread_name): 131 check_command = "ps -A | grep %s" % thread_name 132 checksum = 0 133 while checksum < 10: 134 checksum += 1 135 print("check thread:%s %s times" % (thread_name, checksum)) 136 output = device.shell_with_output(check_command) 137 if output == "": 138 time.sleep(0.1) 139 else: 140 print("thread info: %s" % output) 141 break 142 return True if checksum < 100 else False 143 144 def _make_agent_desc_file(self, device): 145 agent_ip_list = "" 146 device_uuid_list = "" 147 148 if self.hdc_tools != "hdc": 149 if self._query_device_uuid(self.major) != '': 150 device_uuid_list += self._query_device_uuid(self.major) + "," 151 152 if self._query_device_ip(device) != "": 153 agent_ip_list += self._query_device_ip(device) + "," 154 155 for agent in self.agent_list: 156 if self._query_device_uuid(agent): 157 device_uuid_list += self._query_device_uuid(agent) + "," 158 159 config_info = DEVICE_INFO_TEMPLATE % (agent_ip_list, "8888", 160 device_uuid_list) 161 162 config_agent_file = os.path.join(self.suite_dir, "agent.desc") 163 self._write_device_config(config_info, config_agent_file) 164 else: 165 if self._query_device_agent_uuid(self.major): 166 device_uuid_list += self._query_device_agent_uuid(self.major) + "," 167 168 if self._query_device_hdc_ip(device): 169 agent_ip_list += self._query_device_hdc_ip(device) + "," 170 171 for agent in self.agent_list: 172 if self._query_device_agent_uuid(agent): 173 device_uuid_list += self._query_device_agent_uuid(agent) + "," 174 175 config_info = DEVICE_INFO_TEMPLATE % (agent_ip_list, "8888", 176 device_uuid_list) 177 178 config_agent_file = os.path.join(self.suite_dir, "agent.desc") 179 self._write_device_config(config_info, config_agent_file) 180 181 def _make_major_desc_file(self): 182 agent_ip_list = "" 183 device_uuid_list = "" 184 185 if self.hdc_tools != "hdc": 186 if self._query_device_uuid(self.major) != "NoneTyple": 187 device_uuid_list += self._query_device_uuid(self.major) + "," 188 189 for agent in self.agent_list: 190 if self._query_device_ip(agent) != "" and self._query_device_uuid(agent) != "": 191 agent_ip_list += self._query_device_ip(agent) + "," 192 device_uuid_list += self._query_device_uuid(agent) + "," 193 194 config_info = DEVICE_INFO_TEMPLATE % (agent_ip_list, "8888", 195 device_uuid_list) 196 197 config_major_file = os.path.join(self.suite_dir, "major.desc") 198 self._write_device_config(config_info, config_major_file) 199 else: 200 if self._query_device_major_uuid(self.major): 201 device_uuid_list += self._query_device_major_uuid(self.major) + "," 202 203 for agent in self.agent_list: 204 if self._query_device_major_uuid(agent): 205 agent_ip_list += self._query_device_hdc_ip(agent) + "," 206 device_uuid_list += self._query_device_major_uuid(agent) + "," 207 config_info = DEVICE_INFO_TEMPLATE % (agent_ip_list, "8888", 208 device_uuid_list) 209 210 config_major_file = os.path.join(self.suite_dir, "major.desc") 211 self._write_device_config(config_info, config_major_file) 212 213 @staticmethod 214 def _query_device_ip(device): 215 output = device.shell_with_output("getprop ro.hardware") 216 if output == "": 217 return "" 218 219 isemulator = re.findall(r"ranchu", output) 220 output = device.shell_with_output("ifconfig") 221 if output == "": 222 return "" 223 224 if len(isemulator) != 0: 225 ipaddress = re.findall(r"\b10\.0\.2\.[0-9]{1,3}\b", output) 226 else: 227 ip_template = r"\b1[0-9]{1,2}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\b" 228 ipaddress = re.findall(ip_template, output) 229 230 if len(ipaddress) == 0: 231 return "" 232 233 return ipaddress[0] 234 235 @staticmethod 236 def _query_device_hdc_ip(device): 237 output = device.shell_with_output("param get ohos.boot.hardware") 238 if output == "": 239 return "" 240 241 isemulator = re.findall('readonly', str(output)) 242 output = device.shell_with_output("ifconfig waln0") 243 if output == "": 244 return "" 245 246 if 0 != len(isemulator): 247 ipaddress = re.findall(r"\b10\.0\.2\.[0-9]{1,3}\b", output) 248 else: 249 ip_template = r"\b1[0-9]{1,2}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\b" 250 ipaddress = re.findall(ip_template, output) 251 if len(ipaddress) == 0: 252 return "" 253 return ipaddress[0] 254 255 @staticmethod 256 def _query_device_uuid(device): 257 """ 258 1. Run the dumpsys DdmpDeviceMonitorService command to query the value 259 of dev_nodeid. 260 2. The dump information reported by the soft bus. Local device info, 261 should be placed first. 262 Note: The dump information may not comply with the JSON format. 263 """ 264 dumpsys_command = "dumpsys DdmpDeviceMonitorService" 265 device_info = device.shell_with_output(dumpsys_command) 266 if device_info == "": 267 return "" 268 269 begin = device_info.find("dev_nodeid") 270 if begin == -1: 271 return "" 272 273 end = device_info.find(",", begin) 274 if end == -1: 275 return "" 276 277 dev_nodeid_info = device_info[begin:end].replace('"', "") 278 return dev_nodeid_info.split(":")[1] 279 280 @staticmethod 281 def _query_device_major_uuid(device): 282 device_sn = device.device_sn 283 command = "hdc -t %s shell hidumper -s 4700 -a 'buscenter -l local_device_info'" % device_sn 284 device_info = device.execute_command_with_output(command) 285 286 if device_info == "": 287 return "" 288 dev_nodeid_info = re.findall(r"Uuid = (.*?\r\n)", device_info) 289 if dev_nodeid_info: 290 return str(dev_nodeid_info[0]) 291 else: 292 return "" 293 294 @staticmethod 295 def _query_device_agent_uuid(device): 296 device_sn = device.device_sn 297 command = "hdc -t %s shell hidumper -s 4700 -a 'buscenter -l remote_device_info'" % device_sn 298 device_info = device.execute_command_with_output(command) 299 300 if device_info == "": 301 return "" 302 dev_nodeid_info = re.findall(r"Uuid = (.*?\r\n)", device_info) 303 if dev_nodeid_info: 304 return str(dev_nodeid_info[0]) 305 else: 306 return "" 307 308 @staticmethod 309 def _write_device_config(device_info, file_path): 310 file_dir, file_name = os.path.split(file_path) 311 final_file = os.path.join(file_dir, file_name.split('.')[0] + ".desc") 312 if os.path.exists(final_file): 313 os.remove(final_file) 314 if os.path.exists(file_path): 315 os.remove(file_path) 316 with os.fdopen(os.open(file_path, FLAGS, MODES), 'w') as file_desc: 317 file_desc.write(device_info) 318 os.rename(file_path, final_file) 319 320 321############################################################################## 322############################################################################## 323 324