• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# coding=utf-8
3
4#
5# Copyright (c) 2020-2023 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
21
22from public_method import get_server_dict, get_config_ip_info, get_sn_list
23
24
25def _init_sys_config():
26    sys.localcoverage_path = os.path.join(current_path, "..")
27    sys.path.insert(0, sys.localcoverage_path)
28
29
30def restore_config(device_ip, device_port, device_sn, cfg_path):
31    """
32    恢复设备内配置文件
33    :param device_ip:
34    :param device_sn:
35    :param cfg_path:
36    :param device_port:
37    :return:
38    """
39    remount_cmd = "shell mount -o rw,remount /"
40    hdc_command(device_ip, device_port, device_sn, remount_cmd)
41    remount_cmd_mount = "target mount"
42    hdc_command(device_ip, device_port, device_sn, remount_cmd_mount)
43    origin_foundation = os.path.join(cfg_path, "foundation_origin.json")
44    restore_foundation_cmd = "file send {} /system/profile/foundation.json".format(origin_foundation)
45    hdc_command(device_ip, device_port, device_sn, restore_foundation_cmd)
46    serv_list = FoundationServer.lib_dict
47    for serv in serv_list:
48        rm_xml_cmd = "shell rm /system/profile/{}.json".format(serv)
49        hdc_command(device_ip, device_port, device_sn, rm_xml_cmd)
50        rm_cfg_cmd = "shell rm /etc/init/{}.cfg".format(serv)
51        hdc_command(device_ip, device_port, device_sn, rm_cfg_cmd)
52
53
54def attach_pid(device_ip, device_sn, process_str, component_gcda_dict, developer_path,
55               resident_service_path, services_str, root, device_port):
56    """
57    1. 在设备里ps -ef | grep SERVICE 获取进程号
58    2. kill - '信号' pid
59    """
60    hdc_str = "hdc -s %s:%s -t %s" % (device_ip, device_port, device_sn)
61    print("%s shell chmod 777 /data/gcov -R" % hdc_str)
62    coverage_command("%s shell chmod 777 /data/gcov -R" % hdc_str)
63    coverage_command("%s shell mount -o rw,remount /" % hdc_str)
64    coverage_command("%s target mount" % hdc_str)
65    local_sh_path = os.path.join(resident_service_path, "resources", "gcov_flush.sh")
66    coverage_command("dos2unix %s" % local_sh_path)
67    print("%s file send %s %s" % (hdc_str, local_sh_path, "/data/"))
68    coverage_command("%s file send %s %s" % (hdc_str, local_sh_path, "/data/"))
69    coverage_command("%s shell chmod 777 /data/gcov_flush.sh" % hdc_str)
70    print("%s shell sh /data/gcov_flush.sh %s" % (hdc_str, services_str))
71    coverage_command("%s shell sh /data/gcov_flush.sh %s" % (hdc_str, services_str))
72
73    # 拉取gcda文件
74    get_gcda_file(device_ip, device_sn, process_str, component_gcda_dict,
75                  developer_path, services_str, root, device_port)
76
77
78def get_gcda_file(device_ip, device_sn, process_str, component_gcda_dict,
79                  developertest_path, services_str, roots_path, device_port):
80    hdc_str = "hdc -s %s:%s -t %s" % (device_ip, device_port, device_sn)
81    home_path = '/'.join(roots_path.split("/")[:3])
82    gcda_path = f"/data/gcov{roots_path}"
83
84    component_gcda_paths = component_gcda_dict.get(process_str) if component_gcda_dict.get(process_str) else []
85    for component_gcda_path in component_gcda_paths:
86        gcov_root = os.path.join(gcda_path, 'out', product_name, component_gcda_path)
87        gcda_file_name = os.path.basename(gcov_root)
88        gcda_file_path = os.path.dirname(gcov_root)
89        print("%s shell 'cd %s; tar -czf %s.tar.gz %s'" % (
90            hdc_str, gcda_file_path, gcda_file_name, gcda_file_name))
91        coverage_command("%s shell 'cd %s; tar -czf %s.tar.gz %s'" % (
92            hdc_str, gcda_file_path, gcda_file_name, gcda_file_name
93        ))
94
95        local_gcda_path = os.path.dirname(
96            os.path.join(developertest_path, "reports/coverage/data/cxx",
97                         f"{services_str}_service", component_gcda_path))
98
99        if not os.path.exists(local_gcda_path):
100            os.makedirs(local_gcda_path)
101        tar_path = os.path.join(gcda_file_path, "%s.tar.gz" % gcda_file_name)
102        print("%s file recv %s %s" % (hdc_str, tar_path, local_gcda_path))
103        coverage_command("%s file recv %s %s" % (
104            hdc_str, tar_path, local_gcda_path))
105
106        local_tar = os.path.join(local_gcda_path, "%s.tar.gz" % gcda_file_name)
107        print("tar -zxf %s -C %s > /dev/null 2>&1" % (local_tar, local_gcda_path))
108        coverage_command("tar -zxf %s -C %s > /dev/null 2>&1" % (
109            local_tar, local_gcda_path))
110        coverage_command("rm -rf %s" % local_tar)
111    print("%s shell rm -fr %s" % (hdc_str, f"/data/gcov{home_path}"))
112    coverage_command("%s shell rm -fr %s" % (hdc_str, f"/data/gcov{home_path}"))
113
114
115def get_service_list(device_ip, device_sn, system_info_dict, services_component_dict,
116                     component_gcda_dict, developer_path, resident_service_path, root, port_nu):
117
118    service_list = []
119    for key, value_list in system_info_dict.items():
120        for process_str in value_list:
121            if process_str in services_component_dict.keys():
122                service_list.append(process_str)
123            else:
124                return
125    if len(service_list) > 0:
126        for process_str in service_list:
127            services_list = process_str.split("|")
128            for services_str in services_list:
129                attach_pid(device_ip, device_sn, process_str, component_gcda_dict,
130                           developer_path, resident_service_path, services_str, root, port_nu)
131    return
132
133
134if __name__ == '__main__':
135    command_args = sys.argv[1]
136    command_str = command_args.split("command_str=")[1].replace(",", " ")
137    current_path = os.getcwd()
138    _init_sys_config()
139    from local_coverage.utils import get_product_name, coverage_command, hdc_command, FoundationServer
140
141    root_path = current_path.split("/test/testfwk/developer_test")[0]
142    developer_test_path = os.path.join(root_path, "test/testfwk/developer_test")
143    service_path = os.path.join(
144        developer_test_path, "local_coverage/resident_service")
145    config_path = os.path.join(service_path, "config")
146    product_name = get_product_name(root_path)
147
148    # 获取子系统部件与服务的关系
149    system_dict, services_dict, component_dict = get_server_dict(command_str)
150
151    ip, port, sn = get_config_ip_info(os.path.join(developer_test_path, "config/user_config.xml"))
152    if not port:
153        port = "8710"
154    device_sn_list = []
155    if sn:
156        device_sn_list.extend(sn.replace(" ", "").split(";"))
157    else:
158        device_sn_list = get_sn_list("hdc -s %s:%s list targets" % (ip, port))
159
160    if ip and len(device_sn_list) >= 1 and len(system_dict.keys()) >= 1:
161        for sn_str in device_sn_list:
162            get_service_list(ip, sn_str, system_dict, services_dict, component_dict,
163                             developer_test_path, service_path, root_path, port)
164            restore_config(ip, port, sn_str, config_path)
165