From a242d9a6799f60780489bbc208572fc866424ab3 Mon Sep 17 00:00:00 2001 From: Teacher_Dong Date: Sat, 18 May 2024 15:19:15 +0800 Subject: [PATCH 1/3] Signed-off-by:Teacher_Dong --- localCoverage/coverage_tools.py | 3 - .../interfaceCoverage_gcov_lcov.py | 2 +- .../restore_comment/after_lcov_branch.py | 92 +++++++++++-------- .../restore_comment/build_before_generate.py | 12 +-- .../restore_comment/restore_source_code.py | 46 ---------- src/core/driver/drivers.py | 4 + 6 files changed, 62 insertions(+), 97 deletions(-) delete mode 100644 localCoverage/restore_comment/restore_source_code.py diff --git a/localCoverage/coverage_tools.py b/localCoverage/coverage_tools.py index 83f17d0..609f1cc 100644 --- a/localCoverage/coverage_tools.py +++ b/localCoverage/coverage_tools.py @@ -193,9 +193,6 @@ if __name__ == '__main__': 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_test_path, "localCoverage/restore_comment/restore_source_code.py") - subprocess.run("python3 %s" % restore_source_code_path, shell=True) print(r"See the code coverage report in: " r"/test/testfwk/developer_test/localCoverage/codeCoverage/results/coverage/reports/cxx/html") diff --git a/localCoverage/interfaceCoverage/interfaceCoverage_gcov_lcov.py b/localCoverage/interfaceCoverage/interfaceCoverage_gcov_lcov.py index f9444bb..2c2e219 100644 --- a/localCoverage/interfaceCoverage/interfaceCoverage_gcov_lcov.py +++ b/localCoverage/interfaceCoverage/interfaceCoverage_gcov_lcov.py @@ -309,7 +309,7 @@ def get_covered_result_data(public_interface_func_list, covered_func_list): if para_list[index].strip() == "": continue curr_para = para_list[index] - new_list.appned(curr_para) + new_list.append(curr_para) para_string = ",".join(new_list) fun_string = f"{return_val}' '{func_name}({para_string.strip().strip(',')})" fun_string = fun_string.strip() diff --git a/localCoverage/restore_comment/after_lcov_branch.py b/localCoverage/restore_comment/after_lcov_branch.py index 609a43c..24fc63c 100644 --- a/localCoverage/restore_comment/after_lcov_branch.py +++ b/localCoverage/restore_comment/after_lcov_branch.py @@ -24,62 +24,76 @@ FLAGS = os.O_WRONLY | os.O_CREAT | os.O_EXCL MODES = stat.S_IWUSR | stat.S_IRUSR -def get_file_list(find_path, postfix=""): - file_names = os.listdir(find_path) - file_list = [] - if len(file_names) > 0: - for fn in file_names: - if postfix != "": - if fn.find(postfix) != -1 and fn[-len(postfix):] == postfix: - file_list.append(fn) - else: - file_list.append(fn) - return file_list - - -def get_file_list_by_postfix(path, postfix=""): - file_list = [] - for dirs in os.walk(path): - files = get_file_list(find_path=dirs[0], postfix=postfix) +def get_source_file_list(path): + """ + 获取path路径下源文件路径列表 + """ + file_path_list = [] + file_path_list_append = file_path_list.append + for root, dirs, files in os.walk(path): for file_name in files: - if "" != file_name and -1 == file_name.find(__file__): - file_name = os.path.join(dirs[0], file_name) - if os.path.isfile(file_name): - file_list.append(file_name) - return file_list + file_path = os.path.join(root, file_name) + _, suffix = os.path.splitext(file_name) + if suffix in [".c", ".cpp", ".html"]: + file_path_list_append(file_path) + return file_path_list -def recover_source_file(cpp_list, keys): - if not cpp_list: - print("no any .cpp file here") +def recover_source_file(source_path_list, keys): + if not source_path_list: + print("no any source file here") return - for path in cpp_list: + for path in source_path_list: + source_dir, suffix_name = os.path.splitext(path) if not os.path.exists(path): - return - for key in keys: - with open(path, "r") as read_fp: - code_lines = read_fp.readlines() - if os.path.exists(f"{path.split('.')[0]}_bk.html"): - os.remove(f"{path.split('.')[0]}_bk.html") - with os.fdopen(os.open(f"{path.split('.')[0]}_bk.html", FLAGS, MODES), 'w') as write_fp: - for line in code_lines: + continue + if suffix_name != ".html" and "test" in path: + continue + with open(path, "r", encoding="utf-8", errors="ignore") as read_fp: + code_lines = read_fp.readlines() + if os.path.exists(f"{source_dir}_bk{suffix_name}"): + os.remove(f"{source_dir}_bk{suffix_name}") + with os.fdopen(os.open(f"{source_dir}_bk{suffix_name}", FLAGS, MODES), 'w') as write_fp: + for line in code_lines: + for key in keys: if key in line: - write_fp.write(line.strip("\n").strip("\n\r").replace(" //LCOV_EXCL_BR_LINE", "")) - write_fp.write("\n") + write_fp.write(line.replace(key, "")) else: write_fp.write(line) os.remove(path) - subprocess.Popen("mv %s %s" % (f"{path.split('.')[0]}_bk.html", path), + subprocess.Popen("mv %s %s" % (f"{source_dir}_bk{suffix_name}", path), shell=True).communicate() +def recover_cpp_file(part_name_path): + try: + with open(part_name_path, "r", encoding="utf-8", errors="ignore") as fp: + data_dict = json.load(fp) + for key, value in data_dict.items(): + if "path" in value.keys(): + for path_str in value["path"]: + file_path = os.path.join(root_path, path_str) + if os.path.exists(file_path): + cpp_list = get_source_file_list(file_path) + recover_source_file(cpp_list, keys=[" //LCOV_EXCL_BR_LINE"]) + else: + print("The directory does not exist.", file_path) + except(FileNotFoundError, AttributeError, ValueError, KeyError): + print("recover LCOV_EXCL_BR_LINE Error") + + if __name__ == '__main__': current_path = os.getcwd() root_path = current_path.split("/test/testfwk/developer_test")[0] html_path = os.path.join( root_path, "test/testfwk/developer_test/localCoverage/codeCoverage/results/coverage/reports/cxx/html") - cpp_arr_list = get_file_list_by_postfix(html_path, ".html") - recover_source_file(cpp_arr_list, keys=[" //LCOV_EXCL_BR_LINE"]) + html_arr_list = get_source_file_list(html_path) + recover_source_file(html_arr_list, keys=[" //LCOV_EXCL_BR_LINE"]) + + part_info_path = os.path.join( + root_path, + "test/testfwk/developer_test/localCoverage/restore_comment/part_config.json") + recover_cpp_file(part_info_path) diff --git a/localCoverage/restore_comment/build_before_generate.py b/localCoverage/restore_comment/build_before_generate.py index 6a4cb34..8e1cbe7 100644 --- a/localCoverage/restore_comment/build_before_generate.py +++ b/localCoverage/restore_comment/build_before_generate.py @@ -56,9 +56,9 @@ def rewrite_source_file(source_path_list: list): with open(path, "r", encoding="utf-8", errors="ignore") as read_fp: code_lines = read_fp.readlines() source_dir, suffix_name = os.path.splitext(path) - if os.path.exists(f"{source_dir}_bk.{suffix_name}"): - os.remove(f"{source_dir}_bk.{suffix_name}") - with os.fdopen(os.open(f"{source_dir}_bk.{suffix_name}", FLAGS, MODES), 'w') as write_fp: + if os.path.exists(f"{source_dir}_bk{suffix_name}"): + os.remove(f"{source_dir}_bk{suffix_name}") + with os.fdopen(os.open(f"{source_dir}_bk{suffix_name}", FLAGS, MODES), 'w') as write_fp: for line in code_lines: sign_number = 0 for key in keys: @@ -76,7 +76,7 @@ def rewrite_source_file(source_path_list: list): break os.remove(path) - subprocess.Popen("mv %s %s" % (f"{source_dir}_bk.{suffix_name}", path), + subprocess.Popen("mv %s %s" % (f"{source_dir}_bk{suffix_name}", path), shell=True).communicate() print("[********** End Rewrite Source File **********]") @@ -89,11 +89,7 @@ def add_lcov(subsystem_config_path): if "path" in value.keys(): for path_str in value["path"]: file_path = os.path.join(root_path, path_str) - primal_path = f"{file_path}_primal" if os.path.exists(file_path): - if not os.path.exists(primal_path): - subprocess.Popen("cp -r %s %s" % ( - file_path, primal_path), shell=True).communicate() source_file_path = get_source_file_list(file_path) rewrite_source_file(source_file_path) else: diff --git a/localCoverage/restore_comment/restore_source_code.py b/localCoverage/restore_comment/restore_source_code.py deleted file mode 100644 index d6b543c..0000000 --- a/localCoverage/restore_comment/restore_source_code.py +++ /dev/null @@ -1,46 +0,0 @@ -#!/usr/bin/env python3 -# coding=utf-8 - -# -# Copyright (c) 2020-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 subprocess -import traceback -import json - - -if __name__ == '__main__': - try: - current_path = os.getcwd() - root_path = current_path.split("/test/testfwk/developer_test")[0] - subsystem_config_path = os.path.join( - root_path, "test/testfwk/developer_test/localCoverage/restore_comment/part_config.json") - if os.path.exists(subsystem_config_path): - with open(subsystem_config_path, "r", encoding="utf-8", errors="ignore") as fp: - data_dict = json.load(fp) - for key, value in data_dict.items(): - if "path" in value.keys(): - for path_str in value["path"]: - file_path = os.path.join(root_path, path_str) - if os.path.exists(file_path): - if os.path.exists(f"{file_path}_primal"): - subprocess.Popen("rm -rf %s" % file_path, shell=True).communicate() - subprocess.Popen("mv %s %s" % ( - f"{file_path}_primal", file_path), shell=True).communicate() - else: - print("The directory does not exist.", file_path) - except(FileNotFoundError, AttributeError, ValueError, KeyError): - print("restore source code Error") diff --git a/src/core/driver/drivers.py b/src/core/driver/drivers.py index f31e367..4abdb10 100644 --- a/src/core/driver/drivers.py +++ b/src/core/driver/drivers.py @@ -1220,6 +1220,10 @@ class OHRustTestDriver(IDriver): def _init_oh_rust(self): self.config.device.connector_command("target mount") + self.config.device.execute_shell_command( + "rm -rf %s" % self.config.target_test_path) + self.config.device.execute_shell_command( + "mkdir -p %s" % self.config.target_test_path) self.config.device.execute_shell_command( "mount -o rw,remount,rw /") -- Gitee From 3aad482725e6496c4218084218459387cc31b9d0 Mon Sep 17 00:00:00 2001 From: Teacher_Dong Date: Sat, 18 May 2024 15:26:35 +0800 Subject: [PATCH 2/3] Signed-off-by:Teacher_Dong --- localCoverage/restore_comment/after_lcov_branch.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/localCoverage/restore_comment/after_lcov_branch.py b/localCoverage/restore_comment/after_lcov_branch.py index 24fc63c..5e56dee 100644 --- a/localCoverage/restore_comment/after_lcov_branch.py +++ b/localCoverage/restore_comment/after_lcov_branch.py @@ -39,7 +39,7 @@ def get_source_file_list(path): return file_path_list -def recover_source_file(source_path_list, keys): +def recover_source_file(source_path_list, keys): if not source_path_list: print("no any source file here") return @@ -91,9 +91,13 @@ if __name__ == '__main__': root_path, "test/testfwk/developer_test/localCoverage/codeCoverage/results/coverage/reports/cxx/html") html_arr_list = get_source_file_list(html_path) - recover_source_file(html_arr_list, keys=[" //LCOV_EXCL_BR_LINE"]) + print("[************* start Recover Source File *************]") + recover_source_file(html_arr_list, keys=[" //LCOV_EXCL_BR_LINE"]) + part_info_path = os.path.join( root_path, "test/testfwk/developer_test/localCoverage/restore_comment/part_config.json") recover_cpp_file(part_info_path) + + print("[************** End Recover Source File **************]") -- Gitee From 7532c541e4e89ac6196ca856bf8b6c2a6fba9cac Mon Sep 17 00:00:00 2001 From: Teacher_Dong Date: Sat, 18 May 2024 16:52:05 +0800 Subject: [PATCH 3/3] Signed-off-by:Teacher_Dong --- .../restore_comment/after_lcov_branch.py | 26 ++++++++++--------- 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/localCoverage/restore_comment/after_lcov_branch.py b/localCoverage/restore_comment/after_lcov_branch.py index 5e56dee..b073617 100644 --- a/localCoverage/restore_comment/after_lcov_branch.py +++ b/localCoverage/restore_comment/after_lcov_branch.py @@ -15,7 +15,7 @@ # See the License for the specific language governing permissions and # limitations under the License. # - +import json import os import subprocess import stat @@ -69,17 +69,19 @@ def recover_source_file(source_path_list, keys): def recover_cpp_file(part_name_path): try: - with open(part_name_path, "r", encoding="utf-8", errors="ignore") as fp: - data_dict = json.load(fp) - for key, value in data_dict.items(): - if "path" in value.keys(): - for path_str in value["path"]: - file_path = os.path.join(root_path, path_str) - if os.path.exists(file_path): - cpp_list = get_source_file_list(file_path) - recover_source_file(cpp_list, keys=[" //LCOV_EXCL_BR_LINE"]) - else: - print("The directory does not exist.", file_path) + if os.path.exists(part_name_path): + with open(part_name_path, "r", encoding="utf-8", errors="ignore") as fp: + data_dict = json.load(fp) + for key, value in data_dict.items(): + if "path" in value.keys(): + for path_str in value["path"]: + file_path = os.path.join(root_path, path_str) + if os.path.exists(file_path): + cpp_list = get_source_file_list(file_path) + recover_source_file(cpp_list, keys=[" //LCOV_EXCL_BR_LINE"]) + else: + print("The directory does not exist.", file_path) + os.remove(path) except(FileNotFoundError, AttributeError, ValueError, KeyError): print("recover LCOV_EXCL_BR_LINE Error") -- Gitee