From dbe462db45b27a30e3318c35bd1d4c933642b97b Mon Sep 17 00:00:00 2001 From: Teacher_Dong Date: Mon, 27 May 2024 17:02:10 +0800 Subject: [PATCH 1/4] Signed-off-by:Teacher_Dong --- localCoverage/automate_execute/build_part.py | 119 ++++++++++++++++++ localCoverage/automate_execute/coverage.sh | 25 ++++ .../install_coverage_tools.py | 64 ++++++++++ .../restore_comment/after_lcov_branch.py | 4 +- .../restore_comment/build_before_generate.py | 31 +++-- 5 files changed, 228 insertions(+), 15 deletions(-) create mode 100644 localCoverage/automate_execute/build_part.py create mode 100644 localCoverage/automate_execute/coverage.sh create mode 100644 localCoverage/automate_execute/install_coverage_tools.py diff --git a/localCoverage/automate_execute/build_part.py b/localCoverage/automate_execute/build_part.py new file mode 100644 index 0000000..e0d1c46 --- /dev/null +++ b/localCoverage/automate_execute/build_part.py @@ -0,0 +1,119 @@ +#!/usr/bin/env python3 +# coding=utf-8 + +# +# Copyright (c) 2024 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 json +import os +import subprocess +import sys + + +def get_subsystem_config(part_str, developer_path): + all_system_info_path = os.path.join( + developer_path, "localCoverage/all_subsystem_config.json" + ) + if os.path.exists(all_system_info_path): + new_json_text = {} + with open(all_system_info_path, "r", encoding="utf-8") as system_text: + system_text_json = json.load(system_text) + if part_str in system_text_json: + new_json_text[part_str] = system_text_json[part_str] + else: + print(f"Error: {part_str} not in all_subsystem_config.json") + return new_json_text + else: + print(f"{all_system_info_path} not exists!") + + +def get_system_or_vendor(code_path): + repo_config_path = os.path.join(code_path, ".repo/manifests.git/.repo_config.json") + if os.path.exists(repo_config_path): + with open(repo_config_path, "r", encoding="utf-8") as fp: + text_json = json.load(fp) + if "manifest.filename" in text_json: + text = text_json["manifest.filename"] + if text.startswith("system"): + return "system" + if text.startswith("vendor"): + return "vendor" + else: + print(f"Error: {repo_config_path} not exist!") + + +def get_bundle_json(part_str, developer_path, code_path): + part_json = get_subsystem_config(part_str, developer_path) + system_or_vendor = get_system_or_vendor(code_path) + if system_or_vendor == "system": + command = ["./build_system.sh", "--abi-type", "generic_generic_arm_64only", "--device-type", + "hisi_all_phone_standard", "--ccache", "--build-variant", "root"] + else: + command = ["./build_vendor.sh", "--abi-type", "generic_generic_arm_64only", "--device-type", + "general_8425L_phone_standard", "--ccache", "--build-variant", "root", + "--gn-args", "uefi_enable=true"] + if part_json.get(part_str): + bundle_json_path = os.path.join(code_path, part_json[part_str]["path"][0], "bundle.json") + if os.path.exists(bundle_json_path): + with open(bundle_json_path, "r", encoding="utf-8") as bundle_json_text: + bundle_json = json.load(bundle_json_text) + os.chdir(code_path) + part_name = bundle_json["component"]["name"] + command.append("--build-target") + command.append(part_name) + + if bundle_json["component"]["build"].get["test"]: + test_path = bundle_json["component"]["build"]["test"] + test_str = " ".join([i.strip("//") for i in test_path]) + command.append("--build-target") + command.append(test_str) + + command.append("--gn-args") + command.append("use_clang_coverage=true") + print(command) + + if subprocess.call(command) == 0: + build_result = True + else: + build_result = False + os.chdir(developer_path) + return build_result + + else: + print(f"{bundle_json_path}不存在,不能获取编译参数,请检查该部件的bundle.json文件!") + + +def execute_case(developer_test, part_name): + start_path = os.path.join(developer_test, "start.sh") + run_cmd = f"run -t UT -tp {part_name} -cov coverage \n" + print(run_cmd) + with os.popen(start_path, "w") as finput: + finput.write("1\n") + finput.write(run_cmd) + finput.write("quit\n") + finput.write("exit(0)\n") + + +if __name__ == '__main__': + test_part_str = sys.argv[1] + current_path = os.getcwd() + root_path = current_path.split("/test/testfwk/developer_test")[0] + developer_test_path = os.path.join(root_path, "test/testfwk/developer_test") + build_before_path = os.path.join( + developer_test_path, "localCoverage/restore_comment/build_before_generate.py") + subprocess.run("python3 %s %s" % (build_before_path, test_part_str), shell=True) + build_success = get_bundle_json(test_part_str, developer_test_path, root_path) + if build_success: + execute_case(developer_test_path, test_part_str) diff --git a/localCoverage/automate_execute/coverage.sh b/localCoverage/automate_execute/coverage.sh new file mode 100644 index 0000000..af3848f --- /dev/null +++ b/localCoverage/automate_execute/coverage.sh @@ -0,0 +1,25 @@ +#!/bin/bash +# +# Copyright (c) 2024 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. +# + +echo "++++++++++++++++++++start install package+++++++++++++++++++++++" + +sudo apt-get install lcov +sudo apt-get install dos2unix +pip install lxml -i http://mirrors.tools.huawei.com/pypi/simple/ --trusted-host mirrors.tools.huawei.com +pip install selectolax -i http://mirrors.tools.huawei.com/pypi/simple/ --trusted-host mirrors.tools.huawei.com +pip install CppHeaderParser -i http://mirrors.tools.huawei.com/pypi/simple/ --trusted-host mirrors.tools.huawei.com + +echo "++++++++++++++++++++end install package+++++++++++++++++++++++" \ No newline at end of file diff --git a/localCoverage/automate_execute/install_coverage_tools.py b/localCoverage/automate_execute/install_coverage_tools.py new file mode 100644 index 0000000..59c6d96 --- /dev/null +++ b/localCoverage/automate_execute/install_coverage_tools.py @@ -0,0 +1,64 @@ +#!/usr/bin/env python3 +# coding=utf-8 + +# +# Copyright (c) 2024 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 + + +def sub_command(command): + proc = subprocess.Popen(command, shell=True) + try: + proc.communicate() + except subprocess.TimeoutExpired: + proc.kill() + proc.terminate() + + +def install_tool(home_path): + coverage_sh = os.path.join( + home_path, "test/testfwk/developer_test/localCoverage/automate_execute/coverage.sh") + sub_command("chmod +x %s" % coverage_sh) + sub_command(coverage_sh) + + +def update_lcovrc(): + print("修改/etc/lcovrc文件中:lcov_branch_coverage=0为lcov_branch_coverage=1") + subprocess.call(["sudo", "echo", ""]) + file_path = "/etc/lcovrc" + os.chmod(file_path, 0o777) + with open(file_path, "r") as f: + txt = f.read() + txt = txt.replace("lcov_branch_coverage = 0", "lcov_branch_coverage = 1") + with open(file_path, "w") as f1: + f1.write(txt) + + +if __name__ == '__main__': + current_path = os.getcwd() + root_path = current_path.split("/test/testfwk/developer_test")[0] + if os.geteuid() == 0: + install_tool(root_path) + update_lcovrc() + else: + print("当前用户不是root用户,请在root用户下执行该脚本,因为lcov只能在root下安装," + "且需要修改/etc/lcovrc文件,其他用户没有权限修改,会导致分支覆盖率无法生成!") + + + + + diff --git a/localCoverage/restore_comment/after_lcov_branch.py b/localCoverage/restore_comment/after_lcov_branch.py index b073617..03560c9 100644 --- a/localCoverage/restore_comment/after_lcov_branch.py +++ b/localCoverage/restore_comment/after_lcov_branch.py @@ -81,9 +81,9 @@ def recover_cpp_file(part_name_path): recover_source_file(cpp_list, keys=[" //LCOV_EXCL_BR_LINE"]) else: print("The directory does not exist.", file_path) - os.remove(path) + os.remove(part_name_path) except(FileNotFoundError, AttributeError, ValueError, KeyError): - print("recover LCOV_EXCL_BR_LINE Error") + print("recover LCOV_EXCL_BR_LINE Error") if __name__ == '__main__': diff --git a/localCoverage/restore_comment/build_before_generate.py b/localCoverage/restore_comment/build_before_generate.py index 8e1cbe7..2c59141 100644 --- a/localCoverage/restore_comment/build_before_generate.py +++ b/localCoverage/restore_comment/build_before_generate.py @@ -20,6 +20,7 @@ import os import subprocess import json import stat +import sys FLAGS = os.O_WRONLY | os.O_CREAT | os.O_EXCL MODES = stat.S_IWUSR | stat.S_IRUSR @@ -119,19 +120,23 @@ def get_part_config_json(part_list, system_info_path, part_path): if __name__ == '__main__': part_name_list = [] - while True: - print("For example: run -tp partname\n" - " run -tp partname1 partname2") - - # 获取用户输入命令 - part_name = input("Please enter your command: ") - if part_name == "": - continue - if " -tp " in part_name: - part_name_list = part_name.strip().split(" -tp ")[1].split() - break - else: - continue + if sys.argv[1]: + part_name_list.append(sys.argv[1]) + print(part_name_list) + else: + while True: + print("For example: run -tp partname\n" + " run -tp partname1 partname2") + + # 获取用户输入命令 + part_name = input("Please enter your command: ") + if part_name == "": + continue + if " -tp " in part_name: + part_name_list = part_name.strip().split(" -tp ")[1].split() + break + else: + continue current_path = os.getcwd() root_path = current_path.split("/test/testfwk/developer_test")[0] -- Gitee From f1b453bf00e2294b289151d3452d5116628f3ca7 Mon Sep 17 00:00:00 2001 From: Teacher_Dong Date: Mon, 27 May 2024 17:58:35 +0800 Subject: [PATCH 2/4] Signed-off-by:Teacher_Dong --- localCoverage/automate_execute/build_part.py | 11 ++++++----- localCoverage/restore_comment/after_lcov_branch.py | 2 +- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/localCoverage/automate_execute/build_part.py b/localCoverage/automate_execute/build_part.py index e0d1c46..218c673 100644 --- a/localCoverage/automate_execute/build_part.py +++ b/localCoverage/automate_execute/build_part.py @@ -34,7 +34,7 @@ def get_subsystem_config(part_str, developer_path): new_json_text[part_str] = system_text_json[part_str] else: print(f"Error: {part_str} not in all_subsystem_config.json") - return new_json_text + return new_json_text else: print(f"{all_system_info_path} not exists!") @@ -45,7 +45,7 @@ def get_system_or_vendor(code_path): with open(repo_config_path, "r", encoding="utf-8") as fp: text_json = json.load(fp) if "manifest.filename" in text_json: - text = text_json["manifest.filename"] + text = text_json["manifest.filename"][0] if text.startswith("system"): return "system" if text.startswith("vendor"): @@ -74,7 +74,7 @@ def get_bundle_json(part_str, developer_path, code_path): command.append("--build-target") command.append(part_name) - if bundle_json["component"]["build"].get["test"]: + if bundle_json["component"]["build"].get("test"): test_path = bundle_json["component"]["build"]["test"] test_str = " ".join([i.strip("//") for i in test_path]) command.append("--build-target") @@ -83,7 +83,6 @@ def get_bundle_json(part_str, developer_path, code_path): command.append("--gn-args") command.append("use_clang_coverage=true") print(command) - if subprocess.call(command) == 0: build_result = True else: @@ -112,7 +111,9 @@ if __name__ == '__main__': root_path = current_path.split("/test/testfwk/developer_test")[0] developer_test_path = os.path.join(root_path, "test/testfwk/developer_test") build_before_path = os.path.join( - developer_test_path, "localCoverage/restore_comment/build_before_generate.py") + developer_test_path, + "localCoverage/restore_comment/build_before_generate.py" + ) subprocess.run("python3 %s %s" % (build_before_path, test_part_str), shell=True) build_success = get_bundle_json(test_part_str, developer_test_path, root_path) if build_success: diff --git a/localCoverage/restore_comment/after_lcov_branch.py b/localCoverage/restore_comment/after_lcov_branch.py index 03560c9..793a88f 100644 --- a/localCoverage/restore_comment/after_lcov_branch.py +++ b/localCoverage/restore_comment/after_lcov_branch.py @@ -81,7 +81,7 @@ def recover_cpp_file(part_name_path): recover_source_file(cpp_list, keys=[" //LCOV_EXCL_BR_LINE"]) else: print("The directory does not exist.", file_path) - os.remove(part_name_path) + os.remove(part_name_path) except(FileNotFoundError, AttributeError, ValueError, KeyError): print("recover LCOV_EXCL_BR_LINE Error") -- Gitee From 1221503c0d2f551671806e86c9d5a2fcc7db39a0 Mon Sep 17 00:00:00 2001 From: Teacher_Dong Date: Tue, 28 May 2024 15:42:28 +0800 Subject: [PATCH 3/4] Signed-off-by:Teacher_Dong --- localCoverage/automate_execute/build_part.py | 3 +++ localCoverage/automate_execute/install_coverage_tools.py | 4 ++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/localCoverage/automate_execute/build_part.py b/localCoverage/automate_execute/build_part.py index 218c673..084bacf 100644 --- a/localCoverage/automate_execute/build_part.py +++ b/localCoverage/automate_execute/build_part.py @@ -37,6 +37,7 @@ def get_subsystem_config(part_str, developer_path): return new_json_text else: print(f"{all_system_info_path} not exists!") + return {} def get_system_or_vendor(code_path): @@ -52,6 +53,7 @@ def get_system_or_vendor(code_path): return "vendor" else: print(f"Error: {repo_config_path} not exist!") + return "" def get_bundle_json(part_str, developer_path, code_path): @@ -92,6 +94,7 @@ def get_bundle_json(part_str, developer_path, code_path): else: print(f"{bundle_json_path}不存在,不能获取编译参数,请检查该部件的bundle.json文件!") + return False def execute_case(developer_test, part_name): diff --git a/localCoverage/automate_execute/install_coverage_tools.py b/localCoverage/automate_execute/install_coverage_tools.py index 59c6d96..c93f5ae 100644 --- a/localCoverage/automate_execute/install_coverage_tools.py +++ b/localCoverage/automate_execute/install_coverage_tools.py @@ -41,10 +41,10 @@ def update_lcovrc(): subprocess.call(["sudo", "echo", ""]) file_path = "/etc/lcovrc" os.chmod(file_path, 0o777) - with open(file_path, "r") as f: + with open(file_path, "r", encoding="utf-8") as f: txt = f.read() txt = txt.replace("lcov_branch_coverage = 0", "lcov_branch_coverage = 1") - with open(file_path, "w") as f1: + with open(file_path, "w", encoding="utf-8") as f1: f1.write(txt) -- Gitee From e1704b1dd312ce99b02c9830d6c3c898fcd2962b Mon Sep 17 00:00:00 2001 From: Teacher_Dong Date: Tue, 28 May 2024 19:28:23 +0800 Subject: [PATCH 4/4] Signed-off-by:Teacher_Dong --- localCoverage/automate_execute/build_part.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/localCoverage/automate_execute/build_part.py b/localCoverage/automate_execute/build_part.py index 084bacf..4fc4215 100644 --- a/localCoverage/automate_execute/build_part.py +++ b/localCoverage/automate_execute/build_part.py @@ -51,9 +51,11 @@ def get_system_or_vendor(code_path): return "system" if text.startswith("vendor"): return "vendor" + else: + return "blue" else: print(f"Error: {repo_config_path} not exist!") - return "" + return "Error" def get_bundle_json(part_str, developer_path, code_path): @@ -62,10 +64,13 @@ def get_bundle_json(part_str, developer_path, code_path): if system_or_vendor == "system": command = ["./build_system.sh", "--abi-type", "generic_generic_arm_64only", "--device-type", "hisi_all_phone_standard", "--ccache", "--build-variant", "root"] - else: + elif system_or_vendor == "system": command = ["./build_vendor.sh", "--abi-type", "generic_generic_arm_64only", "--device-type", "general_8425L_phone_standard", "--ccache", "--build-variant", "root", "--gn-args", "uefi_enable=true"] + else: + command = ["./build.sh", "--product-name", "rk3568", "--ccache"] + if part_json.get(part_str): bundle_json_path = os.path.join(code_path, part_json[part_str]["path"][0], "bundle.json") if os.path.exists(bundle_json_path): -- Gitee