From ecec8f2e741e5d68ae65b8677d04e6d81af5cbef Mon Sep 17 00:00:00 2001 From: caojiale1 Date: Thu, 20 Apr 2023 12:03:53 +0800 Subject: [PATCH] Signed-off-by caojiale1 caojiale1@huawei.com --- .../codeCoverage/codeCoverage_gcov_lcov.py | 15 ++- .../codeCoverage/mutilProcess_CodeCoverage.py | 37 +++++--- localCoverage/coverage_tools.py | 51 +++++------ .../interfaceCoverage/get_innerkits_json.py | 20 ++-- .../interfaceCoverage_gcov_lcov.py | 52 ++++++----- localCoverage/interfaceCoverage/makeReport.py | 18 ++-- .../keyword_registration/keyword_filter.py | 13 ++- localCoverage/resident_service/init_gcov.py | 90 +++++++++--------- .../resident_service/public_method.py | 3 +- .../resident_service/pull_service_gcda.py | 91 ++++++++++--------- localCoverage/utils.py | 10 ++ 11 files changed, 212 insertions(+), 188 deletions(-) diff --git a/localCoverage/codeCoverage/codeCoverage_gcov_lcov.py b/localCoverage/codeCoverage/codeCoverage_gcov_lcov.py index ed6ab11..3443080 100644 --- a/localCoverage/codeCoverage/codeCoverage_gcov_lcov.py +++ b/localCoverage/codeCoverage/codeCoverage_gcov_lcov.py @@ -23,8 +23,6 @@ import shutil import shlex import subprocess import sys -sys.path.append("..") -from localCoverage.coverage_tools import generate_product_name # 代码根目录 root_path = os.getcwd() @@ -37,8 +35,11 @@ COVERAGE_GCDA_RESULTS = "test/localCoverage/codeCoverage/results/coverage/data/c REPORT_PATH = "test/localCoverage/codeCoverage/results/coverage/reports/cxx" # llvm-gcov.sh LLVM_GCOV = "test/localCoverage/codeCoverage/llvm-gcov.sh" -# 编译生成的out路径 -OUTPUT = "out/{}".format(generate_product_name(CODEPATH)) + + +def _init_sys_config(): + sys.localcoverage_path = os.path.join(current_path, "..") + sys.path.insert(0, sys.localcoverage_path) def call(cmd_list, is_show_cmd=False, out=None, err=None): @@ -284,6 +285,12 @@ def gen_final_report(cov_path): if __name__ == "__main__": + current_path = os.path.abspath(os.path.dirname(__name__)) + _init_sys_config() + from localCoverage.utils import get_product_name + # 编译生成的out路径 + OUTPUT = "out/{}".format(get_product_name(CODEPATH)) + gen_all_test_info(subsystem_list=get_subsystem_name_list()) merge_all_test_subsystem_info(subsystem_list=get_subsystem_name_list()) merge_all_subsystem_info() diff --git a/localCoverage/codeCoverage/mutilProcess_CodeCoverage.py b/localCoverage/codeCoverage/mutilProcess_CodeCoverage.py index 4ece28c..b456fcb 100644 --- a/localCoverage/codeCoverage/mutilProcess_CodeCoverage.py +++ b/localCoverage/codeCoverage/mutilProcess_CodeCoverage.py @@ -24,9 +24,6 @@ import subprocess import multiprocessing import sys from multiprocessing import Process -sys.path.append("..") -from localCoverage.coverage_tools import generate_product_name - # 根代码目录 root_path = os.getcwd() CODEPATH = root_path.split("/test/testfwk/developer_test")[0] @@ -38,14 +35,18 @@ COVERAGE_GCDA_RESULTS = "test/testfwk/developer_test/localCoverage/codeCoverage/ REPORT_PATH = "test/testfwk/developer_test/localCoverage/codeCoverage/results/coverage/reports/cxx" # llvm-gcov.sh LLVM_GCOV = "test/testfwk/developer_test/localCoverage/codeCoverage/llvm-gcov.sh" -# 编译生成的out路径 -OUTPUT = "out/{}".format(generate_product_name(CODEPATH)) + # 测试套划分步长 STEP_SIZE = 10 # lcovrc配置文件集合 LCOVRC_SET = f"{CODEPATH}/test/testfwk/developer_test/localCoverage/codeCoverage/coverage_rc" +def _init_sys_config(): + sys.localcoverage_path = os.path.join(current_path, "..") + sys.path.insert(0, sys.localcoverage_path) + + def call(cmd_list, is_show_cmd=False, out=None, err=None): return_flag = False try: @@ -53,7 +54,7 @@ def call(cmd_list, is_show_cmd=False, out=None, err=None): print("execute command: {}".format(" ".join(cmd_list))) if 0 == subprocess.call(cmd_list, shell=False, stdout=out, stderr=err): return_flag = True - except: + except IOError: print("Error : command {} execute faild!".format(cmd_list)) return_flag = False @@ -348,28 +349,34 @@ def gen_final_report(cov_path): if __name__ == '__main__': - caseLst = gen_all_test_info(subsystem_list=get_subsystem_name_list()) + current_path = os.path.abspath(os.path.dirname(__name__)) + _init_sys_config() + from localCoverage.utils import get_product_name + # 编译生成的out路径 + OUTPUT = "out/{}".format(get_product_name(CODEPATH)) + + case_list = gen_all_test_info(subsystem_list=get_subsystem_name_list()) multiprocessing.set_start_method("fork") # fork spawn forkserver start = end = 0 Tag = False process_list = [] - for i in range(len(caseLst)): - lcovrc_path = f"{LCOVRC_SET}/lcovrc_cov_{str(i)}" - print(lcovrc_path) - if os.path.exists(lcovrc_path): - print(f"{lcovrc_path}{'@' * 20}yes") + for i in range(len(case_list)): + lcov_path = f"{LCOVRC_SET}/lcovrc_cov_{str(i)}" + print(lcov_path) + if os.path.exists(lcov_path): + print(f"{lcov_path}{'@' * 20}yes") else: raise Exception("mutilProcess have error -rc path not existed. " "please fix add run") start = end end += STEP_SIZE - if end >= len(caseLst): - end = len(caseLst) + if end >= len(case_list): + end = len(case_list) Tag = True p = Process(target=generate_coverage_info, - args=(caseLst[start:end], lcovrc_path, + args=(case_list[start:end], lcov_path, get_subsystem_name_list())) p.daemon = True p.start() diff --git a/localCoverage/coverage_tools.py b/localCoverage/coverage_tools.py index 0fb2335..bc633eb 100644 --- a/localCoverage/coverage_tools.py +++ b/localCoverage/coverage_tools.py @@ -24,8 +24,10 @@ import shutil import subprocess from shutil import copyfile +from utils import get_product_name, coverage_command -def get_subsystem_config(test_part_list, developer_path): + +def get_subsystem_config(part_list, developer_path): all_system_info_path = os.path.join( developer_path, "localCoverage/all_subsystem_config.json" ) @@ -34,7 +36,7 @@ def get_subsystem_config(test_part_list, developer_path): ) if os.path.exists(all_system_info_path): new_json_text = {} - for part in test_part_list: + for part in part_list: with open(all_system_info_path, "r", encoding="utf-8") as system_text: system_text_json = json.load(system_text) if part in system_text_json: @@ -85,16 +87,15 @@ def execute_code_cov_tools(developer_path): llvm_gcov_path = os.path.join( developer_path, "localCoverage/codeCoverage/llvm-gcov.sh" ) - subprocess.Popen("dos2unix %s" % llvm_gcov_path, shell=True).communicate() - subprocess.Popen("chmod 777 %s" % llvm_gcov_path, shell=True).communicate() + coverage_command("dos2unix %s" % llvm_gcov_path) + coverage_command("chmod 777 %s" % llvm_gcov_path) tools_path = os.path.join( developer_path, "localCoverage/codeCoverage/mutilProcess_CodeCoverage.py" ) - code_coverage_process = subprocess.Popen("python3 %s" % tools_path, shell=True) - code_coverage_process.communicate() + coverage_command("python3 %s" % tools_path) -def get_subsystem_name(test_part_list, product_name): +def get_subsystem_name(part_list, product_name): if product_name: testfwk_json_path = os.path.join( root_path, "out", product_name, "build_configs/infos_for_testfwk.json" @@ -104,7 +105,7 @@ def get_subsystem_name(test_part_list, product_name): system_json = json.load(json_text) subsystem_info = system_json.get("phone").get("subsystem_infos") subsystem_list = [] - for part in test_part_list: + for part in part_list: for key in subsystem_info.keys(): if part in subsystem_info.get(key) and key not in subsystem_list: subsystem_list.append(key) @@ -112,17 +113,10 @@ def get_subsystem_name(test_part_list, product_name): return subsystem_str else: print("%s not exists.", testfwk_json_path) + return None else: print("product_name is not null") - - -def generate_product_name(root_path): - # 获取输出路径 - ohos_config_path = os.path.join(root_path, "ohos_config.json") - with open(ohos_config_path, 'r') as json_file: - json_info = json.load(json_file) - product_name = json_info.get("out_path").split("out")[1].strip("/") - return product_name + return None def execute_interface_cov_tools(subsystem_str, developer_path): @@ -131,10 +125,7 @@ def execute_interface_cov_tools(subsystem_str, developer_path): developer_path, "localCoverage/interfaceCoverage/get_innerkits_json.py" ) - interface_coverage_process = subprocess.Popen( - "python3 %s" % innerkits_json_path, shell=True - ) - interface_coverage_process.communicate() + coverage_command("python3 %s" % innerkits_json_path) interface_path = os.path.join( developer_path, @@ -151,33 +142,33 @@ if __name__ == '__main__': current_path = os.getcwd() root_path = current_path.split("/test/testfwk/developer_test")[0] - developer_path = os.path.join(root_path, "test/testfwk/developer_test") + developer_test_path = os.path.join(root_path, "test/testfwk/developer_test") # 获取产品形态 - product_name = generate_product_name(root_path) + product_name = get_product_name(root_path) # copy gcda数据到覆盖率工具指定位置 - copy_coverage(developer_path) - generate_coverage_rc(developer_path) + copy_coverage(developer_test_path) + generate_coverage_rc(developer_test_path) # 获取部件位置信息config if len(test_part_list) > 0: - get_subsystem_config(test_part_list, developer_path) + get_subsystem_config(test_part_list, developer_test_path) # 执行代码覆盖率 - execute_code_cov_tools(developer_path) + execute_code_cov_tools(developer_test_path) # 源代码还原 after_lcov_branch_path = os.path.join( - developer_path, "localCoverage/restore_comment/after_lcov_branch.py") + developer_test_path, "localCoverage/restore_comment/after_lcov_branch.py") if os.path.exists(after_lcov_branch_path): subprocess.run("python3 %s " % after_lcov_branch_path, shell=True) restore_source_code_path = os.path.join( - developer_path, "localCoverage/restore_comment/restore_source_code.py") + developer_test_path, "localCoverage/restore_comment/restore_source_code.py") subprocess.run("python3 %s" % restore_source_code_path, shell=True) keyword_path = os.path.join( - developer_path, "localCoverage/keyword_registration/keyword_filter.py") + developer_test_path, "localCoverage/keyword_registration/keyword_filter.py") subprocess.run("python3 %s" % keyword_path, shell=True) print(r"See the code coverage report in: " diff --git a/localCoverage/interfaceCoverage/get_innerkits_json.py b/localCoverage/interfaceCoverage/get_innerkits_json.py index 1458854..b85ddaa 100644 --- a/localCoverage/interfaceCoverage/get_innerkits_json.py +++ b/localCoverage/interfaceCoverage/get_innerkits_json.py @@ -19,11 +19,12 @@ import os import json -import subprocess import sys -sys.path.append("..") -from localCoverage.coverage_tools import generate_product_name + +def _init_sys_config(): + sys.localcoverage_path = os.path.join(current_path, "..") + sys.path.insert(0, sys.localcoverage_path) def gen_parts_info_json(folder_list, output_json_path): @@ -56,14 +57,17 @@ def get_parts_list(path): if __name__ == "__main__": current_path = os.getcwd() + _init_sys_config() + from localCoverage.utils import get_product_name, coverage_command + root_path = current_path.split("/test/testfwk/developer_test")[0] - product_name = generate_product_name(root_path) + product_name = get_product_name(root_path) part_info_path = os.path.join( root_path, "out", product_name, "innerkits/ohos-arm64" ) - output_json_path = os.path.join( + json_path = os.path.join( root_path, "out", product_name, "packages/phone/innerkits/ohos-arm64" ) - subprocess.Popen("mkdir -p %s" % output_json_path, shell=True) - folder_list = get_parts_list(part_info_path) - gen_parts_info_json(folder_list, output_json_path) + coverage_command("mkdir -p %s" % json_path) + part_list = get_parts_list(part_info_path) + gen_parts_info_json(part_list, json_path) diff --git a/localCoverage/interfaceCoverage/interfaceCoverage_gcov_lcov.py b/localCoverage/interfaceCoverage/interfaceCoverage_gcov_lcov.py index 0c0f9f3..34e0072 100644 --- a/localCoverage/interfaceCoverage/interfaceCoverage_gcov_lcov.py +++ b/localCoverage/interfaceCoverage/interfaceCoverage_gcov_lcov.py @@ -24,29 +24,26 @@ import shutil import subprocess import CppHeaderParser import get_innerkits_json -import makeReport -sys.path.append("..") -from localCoverage.coverage_tools import generate_product_name - +import make_report root_path = os.getcwd() CODEPATH = root_path.split("/test/testfwk/developer_test")[0] -product_name = generate_product_name(CODEPATH) -PATH_INFO_PATH = "out/{}/innerkits/ohos-arm64".format(product_name) -OUTPUT_JSON_PATH = "out/{}/packages/phone/innerkits/ohos-arm64".format(product_name) -KIT_MODULES_INFO = "out/{}/packages/phone/innerkits/ohos-arm64/kits_modules_info.json".format(product_name) SUB_SYSTEM_INFO_PATH = os.path.join( CODEPATH, "test/testfwk/developer_test/localCoverage/codeCoverage/results/coverage/reports/cxx") OUTPUT_REPORT_PATH = os.path.join( CODEPATH, "test/testfwk/developer_test/localCoverage/interfaceCoverage/results/coverage/interface_kits" ) - filter_file_name_list = [ "appexecfwk/libjnikit/include/jni.h", ] FILTER_CLASS_ATTRIBUTE_LIST = ["ACE_EXPORT", "OHOS_NWEB_EXPORT"] +def _init_sys_config(): + sys.localcoverage_path = os.path.join(current_path, "..") + sys.path.insert(0, sys.localcoverage_path) + + def create_coverage_result_outpath(filepath): if os.path.exists(filepath): shutil.rmtree(filepath) @@ -71,6 +68,7 @@ def get_subsystem_part_list(project_rootpath): return subsystme_part_dict else: print("subsystem_part_config_filepath not exists.") + return None def load_json_data(): @@ -107,7 +105,7 @@ def get_file_list_by_postfix(path, postfix, filter_jar=""): for file_path in files: if "" != file_path and -1 == file_path.find(__file__): pos = file_path.rfind(os.sep) - file_name = file_path[pos+1:] + file_name = file_path[pos + 1:] file_path = os.path.join(dirs[0], file_path) if filter_jar != "" and file_name == filter_jar: print("Skipped %s" % file_path) @@ -203,7 +201,7 @@ def get_sdk_interface_func_list(part_name): try: if is_need_to_be_parsed(file): interface_func_list += get_pubilc_func_list_from_headfile(file) - except: + except Exception: print("get interface error ", sdk_path) else: print("Error: %s is not exist." % sdk_path) @@ -237,7 +235,7 @@ def get_covered_function_list(subsystem_name): func_info = get_function_info_string(temp_list[1]) if "" == func_info: continue - func_info = func_info.replace("\n", "") + func_info = func_info.replace("\n", "") if func_info == temp_list[1] and func_info.startswith("_"): continue covered_function_list.append(func_info) @@ -270,7 +268,7 @@ def get_para_sub_string(content): substring = content else: if -1 != ended_index: - substring = content[start_index:ended_index+1] + substring = content[start_index:ended_index + 1] else: substring = content[start_index:] @@ -293,7 +291,7 @@ def filter_para_sub_string(source): def get_function_para_count(func_info): pos_start = func_info.find("(") pos_end = func_info.rfind(")") - content = func_info[pos_start+1: pos_end] + content = func_info[pos_start + 1: pos_end] if "" == content: return 0 content = filter_para_sub_string(content) @@ -315,7 +313,7 @@ def get_covered_result_data(public_interface_func_list, covered_func_list, subsy continue curr_para = para_list[index] para_string += curr_para - if index < len(para_list)-1: + if index < len(para_list) - 1: para_string += ", " fun_string = f"{return_val}' '{func_name}({para_string.strip().strip(',')})" fun_string = fun_string.strip() @@ -359,7 +357,7 @@ def get_interface_coverage_result_list(subsystem_name, subsystem_part_dict): try: interface_func_list = get_sdk_interface_func_list(part_str) public_interface_func_list.extend(interface_func_list) - except: + except Exception: print("####") covered_func_list = get_covered_function_list(subsystem_name) interface_coverage_result_list = get_covered_result_data( @@ -410,24 +408,24 @@ def make_summary_file(summary_list, output_path): "function_count=\"%s\" coverage_value=\"%s\" />\n" % ( item[0], str(item[1]), item[3])) fd.write('\n') - except(IOError, ValueError) as err_msg: - print("Error for make coverage result: ", err_msg) + except(IOError, ValueError): + print("Error for make coverage result:",) def make_result_file(interface_data_list, summary_list, output_path, title_name): report_path = os.path.join(output_path, "ohos_interfaceCoverage.html") - makeReport.create_html_start(report_path) - makeReport.create_title(report_path, title_name, summary_list) - makeReport.create_summary(report_path, summary_list) + make_report.create_html_start(report_path) + make_report.create_title(report_path, title_name, summary_list) + make_report.create_summary(report_path, summary_list) for item in interface_data_list: subsystem_name = item[0] data_list = item[1] if 0 == len(data_list): continue count, coverage = get_coverage_data(data_list) - makeReport.create_table_test( + make_report.create_table_test( report_path, subsystem_name, data_list, len(data_list), count) - makeReport.create_html_ended(report_path) + make_report.create_html_ended(report_path) def make_coverage_result_file(interface_data_list, output_path, title_name): @@ -449,6 +447,14 @@ def make_interface_coverage_result(): if __name__ == "__main__": + current_path = os.getcwd() + _init_sys_config() + from localCoverage.utils import get_product_name + product_name = get_product_name(CODEPATH) + PATH_INFO_PATH = "out/{}/innerkits/ohos-arm64".format(product_name) + OUTPUT_JSON_PATH = "out/{}/packages/phone/innerkits/ohos-arm64".format(product_name) + KIT_MODULES_INFO = "out/{}/packages/phone/innerkits/ohos-arm64/kits_modules_info.json".format(product_name) + system_args = sys.argv[1] system_name_list = system_args.split(",") get_innerkits_json.gen_parts_info_json( diff --git a/localCoverage/interfaceCoverage/makeReport.py b/localCoverage/interfaceCoverage/makeReport.py index 6285a2a..9743414 100644 --- a/localCoverage/interfaceCoverage/makeReport.py +++ b/localCoverage/interfaceCoverage/makeReport.py @@ -23,7 +23,7 @@ from importlib import reload reload(sys) -g_html_head = """ +html_head = """ @@ -69,12 +69,12 @@ g_html_head = """ """ -g_html_body_start = """ +html_body_start = """ """ -g_html_body_ended = """ +html_body_ended = """ """ -g_html_ended = """ +html_ended = """ """ @@ -89,8 +89,8 @@ def sort_by_field_element_data(elem): def create_html_start(reportpath): try: with open(reportpath, "w") as report: - report.write(g_html_head) - report.write(g_html_body_start) + report.write(html_head) + report.write(html_body_start) except(IOError, ValueError): print("Error for create html start ",) @@ -237,7 +237,7 @@ def create_table_test(reportpath, subsystem_name, datalist, total_count, covered def create_html_ended(reportpath): try: with open(reportpath, "a") as report: - report.write(g_html_body_ended) - report.write(g_html_ended) + report.write(html_body_ended) + report.write(html_ended) except(IOError, ValueError): - print("Error for create html end",) + print("Error for create html end") diff --git a/localCoverage/keyword_registration/keyword_filter.py b/localCoverage/keyword_registration/keyword_filter.py index c23d3c8..1dab507 100644 --- a/localCoverage/keyword_registration/keyword_filter.py +++ b/localCoverage/keyword_registration/keyword_filter.py @@ -525,8 +525,7 @@ class KeywordRegistration: no_change_file_tag += linecache.getline(index_path, tag_line + i) file_rate_tag = linecache.getline(index_path, tag_line + 8) file_covered_tag = linecache.getline(index_path, tag_line + 9) - file_tag = no_change_file_tag + file_rate_tag + \ - file_covered_tag + file_tag = no_change_file_tag + file_rate_tag + file_covered_tag origin_file_covered_num, origin_file_total_num = map( int, re.findall(r"(\d+)", file_covered_tag)) if origin_file_total_num: @@ -601,7 +600,7 @@ class KeywordRegistration: break else: break - except: + except Exception: print("获取关键字if分支行报错", traceback.format_exc()) return if_branch_line @@ -642,7 +641,7 @@ class KeywordRegistration: update_branch_tag = update_branch_tag.replace("> - <", "> <") update_branch_tag = update_branch_tag.replace("> # <", "> <") update_branch_tag = branch_tag + update_branch_tag - except: + except Exception: return else: line_feed_index = branch_html.find(line_item) @@ -671,7 +670,7 @@ class KeywordRegistration: update_branch_tag = update_branch_tag.replace("> # <", "> <") branch_tag = branch_tag[line_feed_index + len(line_item) + 1:] line_feed_index = branch_tag.find(line_item) - except: + except Exception: return branch_tag = branch_tag.replace("> - <", "> <") @@ -705,7 +704,7 @@ class KeywordRegistration: branch_tag_after = branch_html[:line_feed_index] branch_tag_after = branch_tag_after.replace("> - <", "> <") branch_tag = branch_tag_before + branch_tag_after - except: + except Exception: return else: branch_tag = branch_html @@ -750,7 +749,7 @@ class KeywordRegistration: branch_tag = branch_tag.replace("> - <", "> * <") update_branch_tag += branch_tag branch_html = branch_html[end_index + 5:] - except: + except Exception: return update_branch_tag += branch_html return update_branch_tag diff --git a/localCoverage/resident_service/init_gcov.py b/localCoverage/resident_service/init_gcov.py index 7415d8e..8031316 100644 --- a/localCoverage/resident_service/init_gcov.py +++ b/localCoverage/resident_service/init_gcov.py @@ -25,21 +25,24 @@ import time from public_method import get_server_dict, get_config_ip, get_sn_list +def _init_sys_config(): + sys.localcoverage_path = os.path.join(current_path, "..") + sys.path.insert(0, sys.localcoverage_path) + + def modify_init_file(developer_path, hdc_str): """ /etc/init.cfg文件添加cmds """ recv_path = os.path.join(developer_path, "localCoverage/resident_service/resources") print("%s file recv /etc/init.cfg %s" % (hdc_str, recv_path)) - subprocess.Popen("%s file recv /etc/init.cfg %s" % (hdc_str, recv_path), - shell=True).communicate() + coverage_command("%s file recv /etc/init.cfg %s" % (hdc_str, recv_path)) recv_restores_path = os.path.join(recv_path, "restores_environment") if not os.path.exists(recv_restores_path): os.mkdir(recv_restores_path) recv_restores_name = os.path.join(recv_restores_path, "init.cfg") if not os.path.exists(recv_restores_name): - subprocess.Popen("%s file recv /etc/init.cfg %s" % (hdc_str, recv_restores_path), - shell=True).communicate() + coverage_command("%s file recv /etc/init.cfg %s" % (hdc_str, recv_restores_path)) else: print("INFO: file exit", recv_restores_name) @@ -65,30 +68,25 @@ def modify_init_file(developer_path, hdc_str): print("init.cfg file not exists") return print("%s shell mount -o rw,remount / > /dev/null 2>&1" % hdc_str) - subprocess.Popen("%s shell mount -o rw,remount / > /dev/null 2>&1" % hdc_str, - shell=True).communicate() + coverage_command("%s shell mount -o rw,remount / > /dev/null 2>&1" % hdc_str) print("%s file send %s %s" % (hdc_str, cfg_file_path, "/etc/")) - subprocess.Popen("%s file send %s %s" % (hdc_str, cfg_file_path, "/etc/"), - shell=True).communicate() - subprocess.Popen("%s shell param set persist.appspawn.client.timeout 120 > /dev/null 2>&1" % hdc_str, - shell=True).communicate() + coverage_command("%s file send %s %s" % (hdc_str, cfg_file_path, "/etc/")) + coverage_command("%s shell param set persist.appspawn.client.timeout 120 > /dev/null 2>&1" % hdc_str) return if_reboot def modify_faultloggerd_file(developer_path, hdc_str): _, enforce = subprocess.getstatusoutput("%s shell getenforce" % hdc_str) if_reboot = False - subprocess.Popen("%s shell mount -o rw,remount /" % hdc_str, shell=True).communicate() + coverage_command("%s shell mount -o rw,remount /" % hdc_str) print("%s shell mount -o rw,remount /" % hdc_str) if enforce != "Permissive": if_reboot = True - subprocess.Popen("%s shell sed -i 's/enforcing/permissive/g' /system/etc/selinux/config" % hdc_str, - shell=True).communicate() + coverage_command("%s shell sed -i 's/enforcing/permissive/g' /system/etc/selinux/config" % hdc_str) recv_path = os.path.join(developer_path, "localCoverage/resident_service/resources") print("%s file recv /system/etc/init/faultloggerd.cfg %s" % (hdc_str, recv_path)) - subprocess.Popen("%s file recv /system/etc/init/faultloggerd.cfg %s" % (hdc_str, recv_path), - shell=True).communicate() + coverage_command("%s file recv /system/etc/init/faultloggerd.cfg %s" % (hdc_str, recv_path)) cfg_file_path = os.path.join(recv_path, "faultloggerd.cfg") if os.path.exists(cfg_file_path): @@ -106,8 +104,7 @@ def modify_faultloggerd_file(developer_path, hdc_str): with open(cfg_file_path, "w") as json_file: json_file.write(json_str) print("%s file send %s %s" % (hdc_str, cfg_file_path, "/system/etc/init/")) - subprocess.Popen("%s file send %s %s" % (hdc_str, cfg_file_path, "/system/etc/init/"), - shell=True).communicate() + coverage_command("%s file send %s %s" % (hdc_str, cfg_file_path, "/system/etc/init/")) else: print("faultloggerd.cfg file not exists.") @@ -129,8 +126,7 @@ def split_foundation_services(developer_path, system_info_dict, home_path, hdc_s if not os.path.exists(xml_restores_path): print("%s file recv /system/profile/foundation.xml %s" % (hdc_str, restores_path)) - subprocess.Popen("%s file recv /system/profile/foundation.xml %s" % (hdc_str, restores_path), - shell=True).communicate() + coverage_command("%s file recv /system/profile/foundation.xml %s" % (hdc_str, restores_path)) # 推送xml数据 flag = False @@ -138,33 +134,33 @@ def split_foundation_services(developer_path, system_info_dict, home_path, hdc_s for process_str in value_list: foundation_xml_path = os.path.join(config_path, process_str, "foundation.xml") print("%s shell mount -o rw,remount /" % hdc_str) - subprocess.Popen("%s shell mount -o rw,remount /" % hdc_str, - shell=True).communicate() + coverage_command("%s shell mount -o rw,remount /" % hdc_str) if os.path.exists(foundation_xml_path): flag = True - subprocess.Popen("%s shell rm -rf /lost+found" % hdc_str, shell=True).communicate() - subprocess.Popen("%s shell rm -rf /log" % hdc_str, shell=True).communicate() - subprocess.Popen("%s shell rm -rf %s" % (hdc_str, home_path), shell=True).communicate() + coverage_command("%s shell rm -rf /lost+found" % hdc_str) + coverage_command("%s shell rm -rf /log" % hdc_str) + coverage_command("%s shell rm -rf %s" % (hdc_str, home_path)) print("%s file send %s %s" % (hdc_str, foundation_xml_path, "/system/profile/")) - subprocess.Popen("%s file send %s %s" % ( - hdc_str, foundation_xml_path, "/system/profile/"), shell=True).communicate() + coverage_command("%s file send %s %s" % ( + hdc_str, foundation_xml_path, "/system/profile/")) process_xml_path = os.path.join(config_path, process_str, f"{process_str}.xml") print("%s file send %s %s" % (hdc_str, process_xml_path, "/system/profile/")) - subprocess.Popen("%s file send %s %s" % ( - hdc_str, process_xml_path, "/system/profile/"), shell=True).communicate() + coverage_command("%s file send %s %s" % ( + hdc_str, process_xml_path, "/system/profile/")) process_cfg_path = os.path.join(config_path, process_str, f"{process_str}.cfg") print("%s file send %s %s" % (hdc_str, process_cfg_path, "/etc/init/")) - subprocess.Popen("%s file send %s %s" % ( - hdc_str, process_cfg_path, "/etc/init/"), shell=True).communicate() + coverage_command("%s file send %s %s" % ( + hdc_str, process_cfg_path, "/etc/init/")) return flag -def modify_cfg_xml_file(developer_path, device_ip, device_sn_list, system_info_dict, home_path): +def modify_cfg_xml_file(developer_path, device_ip, device_sn_list, + system_info_dict, home_path, device_port): if device_ip and len(device_sn_list) >= 1: for device_sn_str in device_sn_list: - hdc_str = "hdc -s %s:8710 -t %s" % (device_ip, device_sn_str) + hdc_str = "hdc -s %s:%s -t %s" % (device_ip, device_port, device_sn_str) init_if_reboot = modify_init_file(developer_path, hdc_str) log_if_reboot = modify_faultloggerd_file( developer_path, hdc_str) @@ -173,14 +169,13 @@ def modify_cfg_xml_file(developer_path, device_ip, device_sn_list, system_info_d developer_path, system_info_dict, home_path, hdc_str) if init_if_reboot or log_if_reboot or xml_if_reboot: print("%s shell reboot" % hdc_str) - subprocess.Popen("%s shell reboot > /dev/null 2>&1" % hdc_str, - shell=True).communicate() + coverage_command("%s shell reboot > /dev/null 2>&1" % hdc_str) while True: - after_sn_list = get_sn_list("hdc -s %s:8710 list targets" % device_ip) + after_sn_list = get_sn_list("hdc -s %s:%s list targets" % (device_ip, device_port)) time.sleep(10) if device_sn_str in after_sn_list: break - subprocess.Popen("%s shell getenforce" % hdc_str, shell=True).communicate() + coverage_command("%s shell getenforce" % hdc_str) else: print("user_config.xml device ip not config") @@ -189,21 +184,26 @@ if __name__ == '__main__': command_args = sys.argv[1] command_str = command_args.split("command_str=")[1].replace(",", " ") current_path = os.getcwd() + _init_sys_config() + from localCoverage.utils import coverage_command + root_path = current_path.split("/test/testfwk/developer_test")[0] - developer_path = os.path.join(root_path, "test/testfwk/developer_test") - home_path = '/'.join(root_path.split("/")[:3]) + developer_test_path = os.path.join(root_path, "test/testfwk/developer_test") + home_paths = '/'.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_sn_list = [] + ip, port, sn = get_config_ip(os.path.join(developer_test_path, "config/user_config.xml")) + if not port: + port = "8710" + sn_list = [] if sn: - device_sn_list.extend(sn.replace(" ", "").split(";")) + sn_list.extend(sn.replace(" ", "").split(";")) else: - device_sn_list = get_sn_list("hdc -s %s:8710 list targets" % device_ip) + sn_list = get_sn_list("hdc -s %s:%s list targets" % (ip, port)) # 获取子系统部件与服务的关系 - system_info_dict, services_component_dict, component_gcda_dict = get_server_dict(command_str) + system_dict, _, _ = get_server_dict(command_str) # 修改设备init.cfg, faultloggerd.cfg等文件 - modify_cfg_xml_file(developer_path, device_ip, device_sn_list, - system_info_dict, home_path) + modify_cfg_xml_file(developer_test_path, ip, sn_list, + system_dict, home_paths, port) diff --git a/localCoverage/resident_service/public_method.py b/localCoverage/resident_service/public_method.py index 338a0c4..1f7f79a 100644 --- a/localCoverage/resident_service/public_method.py +++ b/localCoverage/resident_service/public_method.py @@ -74,8 +74,7 @@ def get_all_part_service(): services_component_dict = system_text_json["services_component_dict"] component_gcda_dict = system_text_json["component_gcda_dict"] return system_info_dict, services_component_dict, component_gcda_dict - else: - print("%s not exists.", system_part_service_path) + print("%s not exists.", system_part_service_path) def get_system_dict_to_server_name(server_name: str, system_info_dict): diff --git a/localCoverage/resident_service/pull_service_gcda.py b/localCoverage/resident_service/pull_service_gcda.py index 8dc7ad9..e0e9dd0 100644 --- a/localCoverage/resident_service/pull_service_gcda.py +++ b/localCoverage/resident_service/pull_service_gcda.py @@ -17,46 +17,44 @@ # import os -import subprocess import sys from public_method import get_server_dict, get_config_ip, get_sn_list -sys.path.append("..") -from localCoverage.coverage_tools import generate_product_name -def attach_pid(device_ip, device_sn, process_str, component_gcda_dict, - developer_path, resident_service_path, services_str): +def _init_sys_config(): + sys.localcoverage_path = os.path.join(current_path, "..") + sys.path.insert(0, sys.localcoverage_path) + + +def attach_pid(device_ip, device_sn, process_str, component_gcda_dict, developer_path, + resident_service_path, services_str, root_path, device_port): """ 1. 在设备里ps -ef | grep SERVICE 获取进程号 2. kill - '信号' pid """ - hdc_str = "hdc -s %s:8710 -t %s" % (device_ip, device_sn) + hdc_str = "hdc -s %s:%s -t %s" % (device_ip, device_port, device_sn) print("%s shell chmod 777 /data/gcov -R" % hdc_str) - subprocess.Popen("%s shell chmod 777 /data/gcov -R" % hdc_str, shell=True).communicate() - subprocess.Popen("%s shell mount -o rw,remount /" % hdc_str, shell=True).communicate() + coverage_command("%s shell chmod 777 /data/gcov -R" % hdc_str) + coverage_command("%s shell mount -o rw,remount /" % hdc_str) local_sh_path = os.path.join(resident_service_path, "resources", "gcov_flush.sh") - subprocess.Popen("dos2unix %s" % local_sh_path, shell=True).communicate() + coverage_command("dos2unix %s" % local_sh_path) print("%s file send %s %s" % (hdc_str, local_sh_path, "/data/")) - subprocess.Popen("%s file send %s %s" % (hdc_str, local_sh_path, "/data/"), - shell=True).communicate() - subprocess.Popen("%s shell chmod 777 /data/gcov_flush.sh" % hdc_str, - shell=True).communicate() + coverage_command("%s file send %s %s" % (hdc_str, local_sh_path, "/data/")) + coverage_command("%s shell chmod 777 /data/gcov_flush.sh" % hdc_str) print("%s shell sh /data/gcov_flush.sh %s" % (hdc_str, services_str)) - subprocess.Popen("%s shell sh /data/gcov_flush.sh %s" % (hdc_str, services_str), - shell=True).communicate() + coverage_command("%s shell sh /data/gcov_flush.sh %s" % (hdc_str, services_str)) # 拉取gcda文件 get_gcda_file(device_ip, device_sn, process_str, component_gcda_dict, - developer_path, services_str) + developer_path, services_str, root_path, device_port) def get_gcda_file(device_ip, device_sn, process_str, component_gcda_dict, - developertest_path, services_str): - hdc_str = "hdc -s %s:8710 -t %s" % (device_ip, device_sn) - root_path = current_path.split("/test/testfwk/developer_test")[0] - home_path = '/'.join(root_path.split("/")[:3]) - gcda_path = f"/data/gcov{root_path}" + developertest_path, services_str, roots_path, device_port): + hdc_str = "hdc -s %s:%s -t %s" % (device_ip, device_port, device_sn) + home_path = '/'.join(roots_path.split("/")[:3]) + gcda_path = f"/data/gcov{roots_path}" for component_gcda_path in component_gcda_dict[process_str]: gcov_root = os.path.join(gcda_path, 'out', product_name, component_gcda_path) @@ -64,9 +62,9 @@ def get_gcda_file(device_ip, device_sn, process_str, component_gcda_dict, gcda_file_path = os.path.dirname(gcov_root) print("%s shell 'cd %s; tar -czf %s.tar.gz %s'" % ( hdc_str, gcda_file_path, gcda_file_name, gcda_file_name)) - subprocess.Popen("%s shell 'cd %s; tar -czf %s.tar.gz %s'" % ( + coverage_command("%s shell 'cd %s; tar -czf %s.tar.gz %s'" % ( hdc_str, gcda_file_path, gcda_file_name, gcda_file_name - ), shell=True).communicate() + )) local_gcda_path = os.path.dirname( os.path.join(developertest_path, "reports/coverage/data/cxx", @@ -76,21 +74,20 @@ def get_gcda_file(device_ip, device_sn, process_str, component_gcda_dict, os.makedirs(local_gcda_path) tar_path = os.path.join(gcda_file_path, "%s.tar.gz" % gcda_file_name) print("%s file recv %s %s" % (hdc_str, tar_path, local_gcda_path)) - subprocess.Popen("%s file recv %s %s" % ( - hdc_str, tar_path, local_gcda_path), shell=True).communicate() + coverage_command("%s file recv %s %s" % ( + hdc_str, tar_path, local_gcda_path)) local_tar = os.path.join(local_gcda_path, "%s.tar.gz" % gcda_file_name) print("tar -zxf %s -C %s > /dev/null 2>&1" % (local_tar, local_gcda_path)) - subprocess.Popen("tar -zxf %s -C %s > /dev/null 2>&1" % ( - local_tar, local_gcda_path), shell=True).communicate() - subprocess.Popen("rm -rf %s" % local_tar, shell=True).communicate() + coverage_command("tar -zxf %s -C %s > /dev/null 2>&1" % ( + local_tar, local_gcda_path)) + coverage_command("rm -rf %s" % local_tar) print("%s shell rm -fr %s" % (hdc_str, f"/data/gcov{home_path}")) - subprocess.Popen("%s shell rm -fr %s" % (hdc_str, f"/data/gcov{home_path}"), - shell=True).communicate() + coverage_command("%s shell rm -fr %s" % (hdc_str, f"/data/gcov{home_path}")) def get_service_list(device_ip, device_sn, system_info_dict, services_component_dict, - component_gcda_dict, developer_path, resident_service_path): + component_gcda_dict, developer_path, resident_service_path, root_path, port): service_list = [] for key, value_list in system_info_dict.items(): @@ -104,7 +101,7 @@ def get_service_list(device_ip, device_sn, system_info_dict, services_component_ services_list = process_str.split("|") for services_str in services_list: attach_pid(device_ip, device_sn, process_str, component_gcda_dict, - developer_path, resident_service_path, services_str) + developer_path, resident_service_path, services_str, root_path, port) return @@ -112,25 +109,29 @@ if __name__ == '__main__': command_args = sys.argv[1] command_str = command_args.split("command_str=")[1].replace(",", " ") current_path = os.getcwd() + _init_sys_config() + from localCoverage.utils import get_product_name, coverage_command + root_path = current_path.split("/test/testfwk/developer_test")[0] - developer_path = os.path.join(root_path, "test/testfwk/developer_test") - resident_service_path = os.path.join( - developer_path, "localCoverage/resident_service") - config_path = os.path.join(resident_service_path, "config") - product_name = generate_product_name(root_path) + developer_test_path = os.path.join(root_path, "test/testfwk/developer_test") + service_path = os.path.join( + developer_test_path, "localCoverage/resident_service") + config_path = os.path.join(service_path, "config") + product_name = get_product_name(root_path) # 获取子系统部件与服务的关系 - system_info_dict, services_component_dict, component_gcda_dict = get_server_dict(command_str) + system_dict, services_dict, component_dict = get_server_dict(command_str) - device_ip, _, sn = get_config_ip(os.path.join(developer_path, "config/user_config.xml")) + ip, port, sn = get_config_ip(os.path.join(developer_test_path, "config/user_config.xml")) + if not port: + port = "8710" device_sn_list = [] if sn: device_sn_list.extend(sn.replace(" ", "").split(";")) else: - device_sn_list = get_sn_list("hdc -s %s:8710 list targets" % device_ip) - - if device_ip and len(device_sn_list) >= 1 and len(system_info_dict.keys()) >= 1: - for device_sn_str in device_sn_list: - get_service_list(device_ip, device_sn_str, system_info_dict, services_component_dict, - component_gcda_dict, developer_path, resident_service_path) + device_sn_list = get_sn_list("hdc -s %s:%s list targets" % (ip, port)) + if ip and len(device_sn_list) >= 1 and len(system_dict.keys()) >= 1: + for sn_str in device_sn_list: + get_service_list(ip, sn_str, system_dict, services_dict, component_dict, + developer_test_path, service_path, root_path, port) diff --git a/localCoverage/utils.py b/localCoverage/utils.py index 287b79c..c03b3fc 100644 --- a/localCoverage/utils.py +++ b/localCoverage/utils.py @@ -18,6 +18,7 @@ import os import json +import subprocess import time from subprocess import Popen, PIPE, STDOUT @@ -65,6 +66,15 @@ def hdc_command(device_ip, device_port, device_sn, command): return exitcode +def coverage_command(command): + proc = subprocess.Popen(command, shell=True) + try: + proc.communicate() + except subprocess.TimeoutExpired: + proc.kill() + proc.terminate() + + def tree_find_file_endswith(path, suffix, file_list=None): for f in os.listdir(path): full_path = os.path.join(path, f) -- Gitee