13f085823Sopenharmony_ci#!/usr/bin/env python3
23f085823Sopenharmony_ci# coding=utf-8
33f085823Sopenharmony_ci
43f085823Sopenharmony_ci#
53f085823Sopenharmony_ci# Copyright (c) 2020-2023 Huawei Device Co., Ltd.
63f085823Sopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License");
73f085823Sopenharmony_ci# you may not use this file except in compliance with the License.
83f085823Sopenharmony_ci# You may obtain a copy of the License at
93f085823Sopenharmony_ci#
103f085823Sopenharmony_ci#     http://www.apache.org/licenses/LICENSE-2.0
113f085823Sopenharmony_ci#
123f085823Sopenharmony_ci# Unless required by applicable law or agreed to in writing, software
133f085823Sopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS,
143f085823Sopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
153f085823Sopenharmony_ci# See the License for the specific language governing permissions and
163f085823Sopenharmony_ci# limitations under the License.
173f085823Sopenharmony_ci#
183f085823Sopenharmony_ci
193f085823Sopenharmony_ciimport os
203f085823Sopenharmony_ciimport subprocess
213f085823Sopenharmony_ciimport json
223f085823Sopenharmony_ciimport xml.etree.ElementTree as ET
233f085823Sopenharmony_ci
243f085823Sopenharmony_ci
253f085823Sopenharmony_cidef get_config_ip(filepath):
263f085823Sopenharmony_ci    ip_config = ""
273f085823Sopenharmony_ci    sn = ""
283f085823Sopenharmony_ci    port = ""
293f085823Sopenharmony_ci    try:
303f085823Sopenharmony_ci        data_dic = {}
313f085823Sopenharmony_ci        if os.path.exists(filepath):
323f085823Sopenharmony_ci            tree = ET.parse(filepath)
333f085823Sopenharmony_ci            root = tree.getroot()
343f085823Sopenharmony_ci            for node in root.findall("environment/device"):
353f085823Sopenharmony_ci                if node.attrib["type"] != "usb-hdc":
363f085823Sopenharmony_ci                    continue
373f085823Sopenharmony_ci                for sub in node:
383f085823Sopenharmony_ci                    data_dic[sub.tag] = sub.text if sub.text else ""
393f085823Sopenharmony_ci                ip_config = data_dic.get("ip", "")
403f085823Sopenharmony_ci                sn = data_dic.get("sn", "")
413f085823Sopenharmony_ci                port = data_dic.get("port", "")
423f085823Sopenharmony_ci    except ET.ParseError as xml_exception:
433f085823Sopenharmony_ci        print("occurs exception:{}".format(xml_exception.args))
443f085823Sopenharmony_ci
453f085823Sopenharmony_ci    return ip_config, port, sn
463f085823Sopenharmony_ci
473f085823Sopenharmony_ci
483f085823Sopenharmony_cidef get_sn_list(command):
493f085823Sopenharmony_ci    device_sn_list = []
503f085823Sopenharmony_ci    # 执行查询设备sn号命令并获取执行结果
513f085823Sopenharmony_ci    proc = subprocess.Popen(command, shell=True, stdin=subprocess.PIPE,
523f085823Sopenharmony_ci                            stdout=subprocess.PIPE, stderr=subprocess.PIPE)
533f085823Sopenharmony_ci    strout = proc.stdout.read()
543f085823Sopenharmony_ci    if isinstance(strout, bytes):
553f085823Sopenharmony_ci        strout = strout.decode("utf-8", "ignore")
563f085823Sopenharmony_ci    for line in strout.split("\n"):
573f085823Sopenharmony_ci        line = line.strip().replace('\t', ' ')
583f085823Sopenharmony_ci        if line != "":
593f085823Sopenharmony_ci            device_sn_list.append(line)
603f085823Sopenharmony_ci
613f085823Sopenharmony_ci    return device_sn_list
623f085823Sopenharmony_ci
633f085823Sopenharmony_ci
643f085823Sopenharmony_cidef get_all_part_service():
653f085823Sopenharmony_ci    current_path = os.getcwd()
663f085823Sopenharmony_ci    root_path = current_path.split("/test/testfwk/developer_test")[0]
673f085823Sopenharmony_ci    developer_path = os.path.join(root_path, "test/testfwk/developer_test")
683f085823Sopenharmony_ci    system_part_service_path = os.path.join(
693f085823Sopenharmony_ci        developer_path, "local_coverage/resident_service/system_part_service.json")
703f085823Sopenharmony_ci    if os.path.exists(system_part_service_path):
713f085823Sopenharmony_ci        with open(system_part_service_path, "r") as system_text:
723f085823Sopenharmony_ci            system_text_json = json.load(system_text)
733f085823Sopenharmony_ci            system_info_dict = system_text_json["system_info_dict"]
743f085823Sopenharmony_ci            services_component_dict = system_text_json["services_component_dict"]
753f085823Sopenharmony_ci            component_gcda_dict = system_text_json["component_gcda_dict"]
763f085823Sopenharmony_ci            return system_info_dict, services_component_dict, component_gcda_dict
773f085823Sopenharmony_ci    print("%s not exists.", system_part_service_path)
783f085823Sopenharmony_ci    return {}, {}, {}
793f085823Sopenharmony_ci
803f085823Sopenharmony_ci
813f085823Sopenharmony_cidef get_system_dict_to_server_name(server_name: str, system_info_dict):
823f085823Sopenharmony_ci    for system, server_list in system_info_dict.items():
833f085823Sopenharmony_ci        if server_name in server_list:
843f085823Sopenharmony_ci            system_info_dict_after = {
853f085823Sopenharmony_ci                system: [server_name]
863f085823Sopenharmony_ci            }
873f085823Sopenharmony_ci            return system_info_dict_after
883f085823Sopenharmony_ci    return {}
893f085823Sopenharmony_ci
903f085823Sopenharmony_ci
913f085823Sopenharmony_cidef get_server_dict(command):
923f085823Sopenharmony_ci    system_info_dict, services_component_dict, component_gcda_dict = get_all_part_service()
933f085823Sopenharmony_ci    system_info_dict_after = {}
943f085823Sopenharmony_ci    services_component_dict_after = {}
953f085823Sopenharmony_ci    component_gcda_dict_after = {}
963f085823Sopenharmony_ci    server_name = None
973f085823Sopenharmony_ci    if " -ts " in command:
983f085823Sopenharmony_ci        _, testsuite = command.split(" -ts ")
993f085823Sopenharmony_ci        if testsuite in services_component_dict.get("dinput"):
1003f085823Sopenharmony_ci            services_component_dict_after = {
1013f085823Sopenharmony_ci                "dinput": [testsuite]
1023f085823Sopenharmony_ci            }
1033f085823Sopenharmony_ci            server_name = "dinput"
1043f085823Sopenharmony_ci        elif testsuite in services_component_dict.get("softbus_server"):
1053f085823Sopenharmony_ci            services_component_dict_after = {
1063f085823Sopenharmony_ci                "softbus_server": [testsuite]
1073f085823Sopenharmony_ci            }
1083f085823Sopenharmony_ci            server_name = "softbus_server"
1093f085823Sopenharmony_ci        if server_name:
1103f085823Sopenharmony_ci            system_info_dict_after = get_system_dict_to_server_name(server_name, system_info_dict)
1113f085823Sopenharmony_ci            component_gcda_dict_after = {
1123f085823Sopenharmony_ci                server_name: component_gcda_dict.get(server_name)
1133f085823Sopenharmony_ci            }
1143f085823Sopenharmony_ci    elif " -tp " in command:
1153f085823Sopenharmony_ci        component_name = command.split(" -tp ")[-1].split(" ")[0]
1163f085823Sopenharmony_ci        for server, component_list in services_component_dict.items():
1173f085823Sopenharmony_ci            if component_name in component_list:
1183f085823Sopenharmony_ci                if server in ["dinput", "softbus_server"]:
1193f085823Sopenharmony_ci                    break
1203f085823Sopenharmony_ci                services_component_dict_after = {
1213f085823Sopenharmony_ci                    server: [component_name]
1223f085823Sopenharmony_ci                }
1233f085823Sopenharmony_ci                server_name = server
1243f085823Sopenharmony_ci                break
1253f085823Sopenharmony_ci        if server_name:
1263f085823Sopenharmony_ci            system_info_dict_after = get_system_dict_to_server_name(server_name, system_info_dict)
1273f085823Sopenharmony_ci            component_gcda_dict_after = {
1283f085823Sopenharmony_ci                server_name: component_gcda_dict.get(server_name)
1293f085823Sopenharmony_ci            }
1303f085823Sopenharmony_ci    elif " -ss " in command:
1313f085823Sopenharmony_ci        system_name = command.split(" -ss ")[-1].split(" ")[0]
1323f085823Sopenharmony_ci        server_list = system_info_dict.get(system_name) if system_info_dict.get(system_name) else []
1333f085823Sopenharmony_ci        system_info_dict_after = {
1343f085823Sopenharmony_ci            system_name: server_list
1353f085823Sopenharmony_ci        }
1363f085823Sopenharmony_ci        for server_name in server_list:
1373f085823Sopenharmony_ci            services_component_dict_after.update({
1383f085823Sopenharmony_ci                server_name: services_component_dict.get(server_name)
1393f085823Sopenharmony_ci            })
1403f085823Sopenharmony_ci            component_gcda_dict_after.update({
1413f085823Sopenharmony_ci                server_name: component_gcda_dict.get(server_name)
1423f085823Sopenharmony_ci            })
1433f085823Sopenharmony_ci    return system_info_dict_after, services_component_dict_after, component_gcda_dict_after
144