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