diff --git a/localCoverage/push_coverage_so/__init__.py b/localCoverage/push_coverage_so/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/localCoverage/push_coverage_so/push_coverage.py b/localCoverage/push_coverage_so/push_coverage.py new file mode 100644 index 0000000000000000000000000000000000000000..f5ef3bc4cf38a33f4f7c9998aa2552c365c3fd60 --- /dev/null +++ b/localCoverage/push_coverage_so/push_coverage.py @@ -0,0 +1,118 @@ +import os +import sys + +current_path = os.path.abspath(os.path.dirname(__name__)) +sys.path.append(os.path.join(current_path, "..")) +from localCoverage.resident_service.public_method import get_config_ip, get_sn_list +from localCoverage.utils import get_product_name, hdc_command, tree_find_file_endswith, json_parse, logger + +root_path = current_path.split("/test/testfwk/developer_test")[0] +out_path = os.path.join(root_path, "out", get_product_name(root_path)) +developer_path = os.path.join(root_path, "test", "testfwk", "developer_test") +device_ip, device_port, device_sn_strs = get_config_ip(os.path.join(developer_path, "config", "user_config.xml")) +if not device_port: + device_port = "8710" +device_sn_list = device_sn_strs.split(";") +if not device_sn_list: + device_sn_list = get_sn_list("hdc -s {}:{} list targets".format(device_ip, device_port)) + + +def find_part_so_dest_path(testpart: str) -> str: + parts_info_json = os.path.join(out_path, "build_configs", "parts_info", "parts_path_info.json") + if not os.path.exists(parts_info_json): + logger("{} not exists.".format(parts_info_json), "ERROR") + return "" + json_obj = json_parse(parts_info_json) + if json_obj: + if testpart not in json_obj: + logger("{} part not exist in {}.".format(testpart, parts_info_json), "ERROR") + return "" + path = os.path.join(out_path, "obj", json_obj[testpart]) + return path + + return "" + +def find_subsystem_so_dest_path(subsystem: str) -> list: + subsystem_config_json = os.path.join(out_path, "build_configs", "subsystem_info", "subsystem_build_config.json") + if not os.path.exists(subsystem_config_json): + logger("{} not exists.".format(subsystem_config_json), "ERROR") + return [] + + json_obj = json_parse(subsystem_config_json) + if json_obj: + if subsystem not in json_obj["subsystem"]: + logger("{} not exist in subsystem_build_config.json".format(subsystem), "ERROR") + return [] + if "path" not in json_obj["subsystem"][subsystem]: + logger("{} no path in subsystem_build_config.json".format(subsystem), "ERROR") + return [] + + path = list() + for s in json_obj["subsystem"][subsystem]["path"]: + path.append(os.path.join(out_path, "obj", s)) + return path + + return [] + + +def find_so_source_dest(path: str) -> dict: + so_dict = dict() + json_list = list() + if not path: + return {} + + json_list = tree_find_file_endswith(path, "_module_info.json", json_list) + + for j in json_list: + json_obj = json_parse(j) + if "source" not in json_obj or "dest" not in json_obj: + logger("{} json file error.".format(j), "ERROR") + return {} + + if json_obj["source"].endswith(".so"): + so_dict[json_obj["source"]] = json_obj["dest"] + + return so_dict + + +def push_coverage_so(so_dict: dict): + if not so_dict: + logger("No coverage so to push.", "INFO") + return + for device in device_sn_list: + cmd = "shell mount -o rw,remount /" + hdc_command(device_ip, device_port, device, cmd) + for source_path, dest_paths in so_dict.items(): + full_source = os.path.join(out_path, source_path) + if os.path.exists(full_source): + for dest_path in dest_paths: + full_dest = os.path.join("/", dest_path) + command = "file send {} {}".format(full_source, full_dest) + + hdc_command(device_ip, device_port, device, command) + else: + logger("{} not exist.".format(full_source), "ERROR") + + +if __name__ == "__main__": + subsystem_list, testpart_list = [], [] + param = sys.argv[1] + if param.split("=")[0] == "testpart": + testpart_list = param.split("=")[1].lstrip("[").rstrip("]").split(",") + else: + subsystem_list = param.split("=")[1].lstrip("[").rstrip("]").split(",") + + if subsystem_list: + for subsystem in subsystem_list: + json_path_list = find_subsystem_so_dest_path(subsystem) + for json_path in json_path_list: + source_dest_dict = find_so_source_dest(json_path) + push_coverage_so(source_dest_dict) + elif testpart_list: + for testpart in testpart_list: + json_path_list = find_part_so_dest_path(testpart) + source_dest_dict = find_so_source_dest(json_path_list) + push_coverage_so(source_dest_dict) + else: + logger("No subsystem or partname input, no need to push coverage so.", "INFO") + exit(0) diff --git a/localCoverage/resident_service/init_gcov.py b/localCoverage/resident_service/init_gcov.py index 58d6fa13231ee03c102354f4fb4e9eecef85acd6..7415d8e671684b32f8d0f19c501795579846f1f2 100644 --- a/localCoverage/resident_service/init_gcov.py +++ b/localCoverage/resident_service/init_gcov.py @@ -194,7 +194,7 @@ if __name__ == '__main__': home_path = '/'.join(root_path.split("/")[:3]) # 获取user_config中的device ip - device_ip, sn = get_config_ip(os.path.join(developer_path, "config/user_config.xml")) + device_ip, _, sn = get_config_ip(os.path.join(developer_path, "config/user_config.xml")) device_sn_list = [] if sn: device_sn_list.extend(sn.replace(" ", "").split(";")) diff --git a/localCoverage/resident_service/public_method.py b/localCoverage/resident_service/public_method.py index 0587aafe59b5a12c7d433ae5da5e07ee4cf20399..338a0c44c8b5a88940209b62a2650f56e3ff6a4f 100644 --- a/localCoverage/resident_service/public_method.py +++ b/localCoverage/resident_service/public_method.py @@ -25,6 +25,7 @@ import xml.etree.ElementTree as ET def get_config_ip(filepath): ip_config = "" sn = "" + port = "" try: data_dic = {} if os.path.exists(filepath): @@ -37,10 +38,11 @@ def get_config_ip(filepath): data_dic[sub.tag] = sub.text if sub.text else "" ip_config = data_dic.get("ip", "") sn = data_dic.get("sn", "") + port = data_dic.get("port", "") except ET.ParseError as xml_exception: print("occurs exception:{}".format(xml_exception.args)) - return ip_config, sn + return ip_config, port, sn def get_sn_list(command): diff --git a/localCoverage/resident_service/pull_service_gcda.py b/localCoverage/resident_service/pull_service_gcda.py index 2394189d6bf49a243fb86af6579bb8872857700b..8dc7ad9b62bb23e66fca7dd348e63c68dfa1dbb7 100644 --- a/localCoverage/resident_service/pull_service_gcda.py +++ b/localCoverage/resident_service/pull_service_gcda.py @@ -122,7 +122,7 @@ if __name__ == '__main__': # 获取子系统部件与服务的关系 system_info_dict, services_component_dict, component_gcda_dict = get_server_dict(command_str) - device_ip, sn = get_config_ip(os.path.join(developer_path, "config/user_config.xml")) + device_ip, _, sn = get_config_ip(os.path.join(developer_path, "config/user_config.xml")) device_sn_list = [] if sn: device_sn_list.extend(sn.replace(" ", "").split(";")) diff --git a/localCoverage/utils.py b/localCoverage/utils.py new file mode 100644 index 0000000000000000000000000000000000000000..6778c9a66e771d4c43cad346180bbd584b0c9859 --- /dev/null +++ b/localCoverage/utils.py @@ -0,0 +1,72 @@ +#!/usr/bin/env python3 +# coding=utf-8 + +# +# Copyright (c) 2023 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +import os +import json +import time +from subprocess import Popen, PIPE, STDOUT + +def logger(info, level): + create_time = "{}".format(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())) + print("[{}] [{}] [{}]".format(create_time, level, info)) + +def json_parse(json_file): + if os.path.exists(json_file): + with open(json_file, "r") as jf: + return json.load(jf) + + logger("{} not exist.".format(json_file), "ERROR") + return + + +def get_product_name(root_path): + ohos_config = os.path.join(root_path, "ohos_config.json") + json_obj = json_parse(ohos_config) + if json_obj: + product_name = json_obj["out_path"].split("out")[1].strip("/") + return product_name + + logger("{} not exist.".format(ohos_config), "ERROR") + return + + +def shell_command(command): + process = Popen(command, stdout=PIPE, stderr=STDOUT, shell=True) + with process.stdout: + for line in iter(process.stdout.readline, b""): + logger(line.decode().strip(), "INFO") + exitcode = process.wait() + return process, exitcode + + +def hdc_command(device_ip, device_port, device_sn, command): + connect_cmd = "hdc -s {}:{} -t {} ".format(device_ip, device_port, device_sn) + cmd = connect_cmd + command + logger(cmd, "INFO") + _, exitcode = shell_command(cmd) + return exitcode + + +def tree_find_file_endswith(path, suffix, file_list=[]): + for f in os.listdir(path): + full_path = os.path.join(path, f) + if os.path.isfile(full_path) and full_path.endswith(suffix): + file_list.append(full_path) + if os.path.isdir(full_path): + tree_find_file_endswith(full_path, suffix, file_list) + return file_list diff --git a/src/core/command/run.py b/src/core/command/run.py index aec5a212ac0496c5ebf08c5a40091a7ee571dea0..0649931168abc3a65cce274094176ab2ceb82ae8 100644 --- a/src/core/command/run.py +++ b/src/core/command/run.py @@ -56,6 +56,20 @@ class Run(object): def process_command_run(self, command, options): current_raw_cmd = ",".join(list(map(str, options.current_raw_cmd.split(" ")))) if options.coverage and platform.system() != "Windows": + push_cov_path = os.path.join(sys.framework_root_dir, "localCoverage/push_coverage_so/push_coverage.py") + if os.path.exists(push_cov_path): + if str(options.testpart) == "[]" and str(options.subsystem) == "[]": + LOG.info("No subsystem or testpart input, no need push coverage so.") + else: + if str(options.testpart) != "[]": + param = "testpart=" + str(options.testpart) + else: + param = "subsystem=" + str(options.subsystem) + subprocess.run("python3 {} {}".format( + push_cov_path, param), shell=True) + else: + print(f"{push_cov_path} not exists.") + init_gcov_path = os.path.join(sys.framework_root_dir, "localCoverage/resident_service/init_gcov.py") if os.path.exists(init_gcov_path): subprocess.run("python3 %s command_str=%s" % ( @@ -344,14 +358,10 @@ class Run(object): if is_open_source_product(product_form): testcase_path = os.path.abspath(os.path.join( get_build_output_path(product_form), - "packages", - "phone", "tests")) else: testcase_path = os.path.abspath(os.path.join( get_build_output_path(product_form), - "packages", - product_form, "tests")) else: testcase_path = os.path.join(