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