From 5cadbd73102901bec9f68e3d6bf4d4b1022ce27a Mon Sep 17 00:00:00 2001 From: Teacher_Dong Date: Mon, 26 Feb 2024 17:19:04 +0800 Subject: [PATCH] Signed-off-by:Teacher_Dong --- aw/cxx/distributed/distributed.h | 2 +- aw/cxx/distributed/distributed_major.cpp | 2 +- aw/python/distributed/common/common.py | 5 +- aw/python/distributed/common/devices.py | 8 +-- aw/python/distributed/common/drivers.py | 11 +++- .../distributed/distribute/distribute.py | 13 ++++- examples/sleep/src/sleep_ex.cpp | 8 +-- .../performance/common/spent_time_test.cpp | 2 +- libs/benchmark/report/generate_report.py | 31 ++++++----- libs/fuzzlib/fuzzer_helper.py | 35 ++++++++++--- libs/fuzzlib/tools/colored.py | 6 ++- libs/fuzzlib/tools/run_result.py | 9 +++- .../codeCoverage/codeCoverage_gcov_lcov.py | 6 ++- .../codeCoverage/mutilProcess_CodeCoverage.py | 7 ++- localCoverage/coverage_tools.py | 8 ++- .../interfaceCoverage/get_innerkits_json.py | 8 ++- .../interfaceCoverage_gcov_lcov.py | 8 ++- .../interfaceCoverage/make_report.py | 17 ++++-- .../keyword_registration/keyword_filter.py | 52 ++++++++++++------- localCoverage/resident_service/init_gcov.py | 24 +++++++-- .../resident_service/public_method.py | 2 + .../resident_service/pull_service_gcda.py | 8 +-- .../restore_comment/after_lcov_branch.py | 8 ++- .../restore_comment/build_before_generate.py | 37 ++++--------- src/core/build/build_manager.py | 9 +++- src/core/build/build_testcases.py | 4 +- src/core/build/pretreat_targets.py | 19 ++++--- src/core/command/console.py | 3 +- src/core/command/display.py | 2 +- src/core/command/distribute_utils.py | 9 +++- src/core/command/gen.py | 8 ++- src/core/command/run.py | 6 +-- src/core/common.py | 4 +- src/core/config/config_manager.py | 4 +- src/core/driver/drivers.py | 45 ++++++++++------ src/core/testcase/testcase_manager.py | 19 +++++-- src/core/testkit/kit_lite.py | 4 +- src/core/utils.py | 5 +- src/main/_init_global_config.py | 4 +- 39 files changed, 312 insertions(+), 150 deletions(-) diff --git a/aw/cxx/distributed/distributed.h b/aw/cxx/distributed/distributed.h index 44c712f..a9fb144 100644 --- a/aw/cxx/distributed/distributed.h +++ b/aw/cxx/distributed/distributed.h @@ -39,7 +39,7 @@ struct DistributedCmd { }; using DistributedMsg = DistributedCmd; -#define DST_COMMAND_HEAD_LEN (sizeof(DistributedCmd)- sizeof(int)) +#define DST_COMMAND_HEAD_LEN (sizeof(DistributedCmd) - sizeof(int)) struct DistDevInfo { int devNo; diff --git a/aw/cxx/distributed/distributed_major.cpp b/aw/cxx/distributed/distributed_major.cpp index afc79f4..54690b3 100644 --- a/aw/cxx/distributed/distributed_major.cpp +++ b/aw/cxx/distributed/distributed_major.cpp @@ -255,7 +255,7 @@ bool DistributeTestEnvironment::RunTestCmd(size_t devNo, const std::string &strC return breturn; } // add 2 '\0' - size_t rlen = cmdLen + expectValueLen + DST_COMMAND_HEAD_LEN + sizeof(int)*HALF_BUF_LEN + HALF_BUF_LEN; + size_t rlen = cmdLen + expectValueLen + DST_COMMAND_HEAD_LEN + sizeof(int) * HALF_BUF_LEN + HALF_BUF_LEN; if (rlen <= MAX_BUFF_LEN) { auto pCmdTest = reinterpret_cast(szbuf); pCmdTest->cmdTestType = DST_COMMAND_CALL; diff --git a/aw/python/distributed/common/common.py b/aw/python/distributed/common/common.py index e0c087b..8581ae8 100755 --- a/aw/python/distributed/common/common.py +++ b/aw/python/distributed/common/common.py @@ -18,7 +18,10 @@ import os import time +import stat +FLAGS = os.O_WRONLY | os.O_CREAT | os.O_EXCL +MODES = stat.S_IWUSR | stat.S_IRUSR ############################################################################## ############################################################################## @@ -81,7 +84,7 @@ def create_empty_result_file(savepath, filename, message=""): if filename.endswith(".hap"): filename = filename.split(".")[0] if not os.path.exists(savepath): - with open(savepath, "w", encoding='utf-8') as file_desc: + with os.fdopen(os.open(savepath, FLAGS, MODES), 'w') as file_desc: time_stamp = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) file_desc.write('\n') diff --git a/aw/python/distributed/common/devices.py b/aw/python/distributed/common/devices.py index 057d9c9..913de00 100755 --- a/aw/python/distributed/common/devices.py +++ b/aw/python/distributed/common/devices.py @@ -54,7 +54,7 @@ def get_package_name(hap_filepath): try: zf_desc.extractall(path=hap_bak_path) except RuntimeError as error: - print(error) + print("Unzip error", hap_bak_path) zf_desc.close() # verify config.json file @@ -215,15 +215,15 @@ class DeviceShell: stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) - + result = "" try: data, _ = proc.communicate() if isinstance(data, bytes): - data = data.decode('utf-8', 'ignore') + result = data.decode("utf-8", "ignore") finally: proc.stdout.close() proc.stderr.close() - return data + return data if result else data @classmethod def check_path_legal(cls, path): diff --git a/aw/python/distributed/common/drivers.py b/aw/python/distributed/common/drivers.py index c09ae37..2585ac7 100755 --- a/aw/python/distributed/common/drivers.py +++ b/aw/python/distributed/common/drivers.py @@ -20,6 +20,7 @@ import os import platform import subprocess import tempfile +import stat from abc import ABCMeta from abc import abstractmethod @@ -29,6 +30,9 @@ from core.config.resource_manager import ResourceManager ############################################################################## ############################################################################## +FLAGS = os.O_WRONLY | os.O_APPEND | os.O_CREAT +MODES = stat.S_IWUSR | stat.S_IRUSR + DEVICE_TEST_PATH = "/%s/%s/" % ("data", "test") @@ -48,10 +52,10 @@ def make_long_command_file(command, longcommand_path, filename): sh_file_name = '%s.sh' % filename file_path = os.path.join(longcommand_path, sh_file_name) try: - with open(file_path, "a") as file_desc: + with os.fdopen(os.open(file_path, FLAGS, MODES), 'a') as file_desc: file_desc.write(command) except(IOError, ValueError) as err_msg: - print("Error for make long command file: ", err_msg) + print(f"Error for make long command file: {err_msg}") return sh_file_name, file_path @@ -162,6 +166,9 @@ class CppTestDriver(ITestDriver): if options.coverage: receive_coverage_data(self.device, result_path, suite_file, file_name) + if os.path.exists(long_command_path): + os.remove(long_command_path) + ############################################################################## ############################################################################## diff --git a/aw/python/distributed/distribute/distribute.py b/aw/python/distributed/distribute/distribute.py index a4083f0..ad3e24c 100755 --- a/aw/python/distributed/distribute/distribute.py +++ b/aw/python/distributed/distribute/distribute.py @@ -21,6 +21,10 @@ import sys import re import time import platform +import stat + +FLAGS = os.O_WRONLY | os.O_CREAT | os.O_EXCL +MODES = stat.S_IWUSR | stat.S_IRUSR # insert src path for loading xdevice modules @@ -284,6 +288,8 @@ class Distribute: dev_nodeid_info = re.findall(r"Uuid = (.*?\r\n)", device_info) if dev_nodeid_info: return str(dev_nodeid_info[0]) + else: + return "" @staticmethod def _query_device_agent_uuid(device): @@ -296,6 +302,8 @@ class Distribute: dev_nodeid_info = re.findall(r"Uuid = (.*?\r\n)", device_info) if dev_nodeid_info: return str(dev_nodeid_info[0]) + else: + return "" @staticmethod def _write_device_config(device_info, file_path): @@ -303,11 +311,12 @@ class Distribute: final_file = os.path.join(file_dir, file_name.split('.')[0] + ".desc") if os.path.exists(final_file): os.remove(final_file) - with open(file_path, 'w') as file_desc: + if os.path.exists(file_path): + os.remove(file_path) + with os.fdopen(os.open(file_path, FLAGS, MODES), 'w') as file_desc: file_desc.write(device_info) os.rename(file_path, final_file) - ############################################################################## ############################################################################## diff --git a/examples/sleep/src/sleep_ex.cpp b/examples/sleep/src/sleep_ex.cpp index 46444f1..5d57d29 100644 --- a/examples/sleep/src/sleep_ex.cpp +++ b/examples/sleep/src/sleep_ex.cpp @@ -32,10 +32,10 @@ static double TimeDiff(struct timeval *x , struct timeval *y) return 0; } - double xUs = reinterpret_cast(x->tv_sec * SleepTest::ID_MS_TO_NS_LEVEL) - + reinterpret_cast(x->tv_usec); - double yUs = reinterpret_cast(y->tv_sec * SleepTest::ID_MS_TO_NS_LEVEL) - + reinterpret_cast(y->tv_usec); + double xUs = reinterpret_cast(x->tv_sec * SleepTest::ID_MS_TO_NS_LEVEL) + + reinterpret_cast(x->tv_usec); + double yUs = reinterpret_cast(y->tv_sec * SleepTest::ID_MS_TO_NS_LEVEL) + + reinterpret_cast(y->tv_usec); return (yUs - xUs); } diff --git a/examples/sleep/test/performance/common/spent_time_test.cpp b/examples/sleep/test/performance/common/spent_time_test.cpp index 421ffde..fe3c568 100644 --- a/examples/sleep/test/performance/common/spent_time_test.cpp +++ b/examples/sleep/test/performance/common/spent_time_test.cpp @@ -36,7 +36,7 @@ static void LoopMsleep(void* pMsec) return; } - int msec = *reinterpret_cast (pMsec); + int msec = *reinterpret_cast(pMsec); for (int index = 0; index < msec; index++) { Msleep(1); } diff --git a/libs/benchmark/report/generate_report.py b/libs/benchmark/report/generate_report.py index 9cb5c23..45ae6bd 100644 --- a/libs/benchmark/report/generate_report.py +++ b/libs/benchmark/report/generate_report.py @@ -20,6 +20,10 @@ import json import os import shutil import sys +import stat + +FLAGS = os.O_WRONLY | os.O_CREAT | os.O_EXCL +MODES = stat.S_IWUSR | stat.S_IRUSR SETTING_RED_STYLE = """\033[33;31m%s\033[0m""" @@ -33,8 +37,7 @@ def load_json_data(json_file_path): print("Loading file \"%s\" error" % json_file_path) return {} except(IOError, ValueError) as err_msg: - print("Error for load_json_data: \"%s\"" % - json_file_path, err_msg) + print("Error for load_json_data: ", json_file_path) else: print("Info: \"%s\" not exist." % json_file_path) return json_data @@ -265,16 +268,16 @@ class BenchmarkReport(object): len(content_new)] try: - with open(os.path.abspath(out_report_file_path), "w") \ - as output_fd: + if os.path.exists(os.path.abspath(out_report_file_path)): + os.remove(os.path.abspath(out_report_file_path)) + with os.fdopen(os.open(os.path.abspath(out_report_file_path), + FLAGS, MODES), 'w') as output_fd: content_new = str(content_new) output_fd.write(content_new) except IOError as err_msg: - print("Error5 for open %s failed, with msg %s" % - (out_report_file_path, err_msg)) + print("Error5 for open %s failed:" % out_report_file_path) except IOError as err_msg: - print("Error6 for open %s failed, with msg %s" % - (tmpl_file_path, err_msg)) + print("Error6 for open %s failed:" % tmpl_file_path) def _generate_all_benchmark_detail(self, dest_dir_parh): for benchmark_info in self.benchmark_list: @@ -327,15 +330,15 @@ class BenchmarkReport(object): self._update_report_summary(content_new, detail_info) try: - with open(os.path.abspath(out_report_file_path), "w") \ - as output_fd: + if os.path.exists(os.path.abspath(out_report_file_path)): + os.remove(os.path.abspath(out_report_file_path)) + with os.fdopen(os.open(os.path.abspath(out_report_file_path), + FLAGS, MODES), 'w') as output_fd: output_fd.write(content_new) except IOError as err_msg: - print("Error5 for open %s failed, with msg %s" % - (out_report_file_path, err_msg)) + print("Error5 for open %s failed:" % out_report_file_path) except IOError as err_msg: - print("Error6 for open %s failed, with msg %s" % - (report_tmpl_file_path, err_msg)) + print("Error6 for open %s failed" % report_tmpl_file_path) def _get_detail_info(self, benchmark_info): detail_info = [] diff --git a/libs/fuzzlib/fuzzer_helper.py b/libs/fuzzlib/fuzzer_helper.py index df633f0..e451ee3 100644 --- a/libs/fuzzlib/fuzzer_helper.py +++ b/libs/fuzzlib/fuzzer_helper.py @@ -29,6 +29,7 @@ import time import copy import shutil from pprint import pprint +import stat from tools.colored import Colored from tools.templates import GN_ENTRY_TEMPLATE @@ -38,6 +39,9 @@ from tools.templates import PROJECT_HEADER_TEMPLATE from tools.templates import PROJECT_XML_TEMPLATE from tools.run_result import RunResult +FLAGS = os.O_WRONLY | os.O_CREAT | os.O_EXCL +MODES = stat.S_IWUSR | stat.S_IRUSR + CURRENT_DIR = os.path.dirname(os.path.realpath(__file__)) SOURCE_ROOT_DIR = os.path.dirname( os.path.dirname( @@ -93,7 +97,7 @@ def _get_fuzzer_yaml_config(fuzzer_name): fuzzer_name, "project.yaml") if not os.path.exists(project_yaml_path): - return + return {} #log run stdout to fuzzlog dir with open(project_yaml_path) as filehandle: yaml_config = yaml.safe_load(filehandle) @@ -129,25 +133,36 @@ def generate(args): color_logger.green('Writing new files to %s' % project_dir_path) file_path = os.path.join(project_dir_path, 'project.xml') - with open(file_path, 'w') as filehandle: + if os.path.exists(file_path): + os.remove(file_path) + with os.fdopen(os.open(file_path, FLAGS, MODES), 'w') as filehandle: filehandle.write(PROJECT_XML_TEMPLATE % template_args) file_path = os.path.join(project_dir_path, "%s.cpp" % args.project_name) - with open(file_path, 'w') as filehandle: + if os.path.exists(file_path): + os.remove(file_path) + with os.fdopen(os.open(file_path, FLAGS, MODES), 'w') as filehandle: filehandle.write(PROJECT_DEMO_TEMPLATE % template_args) file_path = os.path.join(project_dir_path, "%s.h" % args.project_name) - with open(file_path, 'w') as filehandle: + if os.path.exists(file_path): + os.remove(file_path) + with os.fdopen(os.open(file_path, FLAGS, MODES), 'w') as filehandle: filehandle.write(PROJECT_HEADER_TEMPLATE % template_args) file_path = os.path.join(project_dir_path, "BUILD.gn") - with open(file_path, 'w') as filehandle: + if os.path.exists(file_path): + os.remove(file_path) + with os.fdopen(os.open(file_path, FLAGS, MODES), 'w') as filehandle: filehandle.write(PROJECT_GN_TEMPLATE % template_args) corpus_dir = os.path.join(project_dir_path, 'corpus') if not os.path.exists(corpus_dir): os.mkdir(corpus_dir) - with open(os.path.join(corpus_dir, 'init'), 'w') as filehandle: + if os.path.exists(os.path.join(corpus_dir, 'init')): + os.remove(os.path.join(corpus_dir, 'init')) + with os.fdopen(os.open(os.path.join(corpus_dir, 'init'), FLAGS, MODES), 'w') as filehandle: filehandle.write("FUZZ") + return 0 #complie fuzzer project @@ -187,7 +202,9 @@ def make(args, stdout=None): ) if not os.path.exists(os.path.dirname(subsystem_src_flag_file_path)): os.makedirs(os.path.dirname(subsystem_src_flag_file_path)) - with open(subsystem_src_flag_file_path, "wb") as file_handle: + if os.path.exists(subsystem_src_flag_file_path): + os.remove(subsystem_src_flag_file_path) + with os.fdopen(os.open(subsystem_src_flag_file_path, FLAGS, MODES), 'wb') as file_handle: file_handle.write(args.project_name.encode()) try: @@ -260,6 +277,10 @@ def main(): elif args.command == 'report': report(args) + return 1 + else: + return 0 + if __name__ == "__main__": main() diff --git a/libs/fuzzlib/tools/colored.py b/libs/fuzzlib/tools/colored.py index 63a4887..865c1f3 100644 --- a/libs/fuzzlib/tools/colored.py +++ b/libs/fuzzlib/tools/colored.py @@ -17,6 +17,10 @@ import sys import os import time +import stat + +FLAGS = os.O_WRONLY | os.O_APPEND | os.O_CREAT +MODES = stat.S_IWUSR | stat.S_IRUSR class Colored(object): @@ -82,7 +86,7 @@ class Colored(object): self.get_fuzz_current_project_log_dir(), "run.log" ) - with open(run_log, 'ab') as f: + with os.fdopen(os.open(run_log, FLAGS, MODES), 'ab') as f: f.write(msg + "\n") diff --git a/libs/fuzzlib/tools/run_result.py b/libs/fuzzlib/tools/run_result.py index 5fb2b6e..8c2490b 100644 --- a/libs/fuzzlib/tools/run_result.py +++ b/libs/fuzzlib/tools/run_result.py @@ -16,6 +16,11 @@ # import re +import os +import stat + +FLAGS = os.O_WRONLY | os.O_CREAT | os.O_EXCL +MODES = stat.S_IWUSR | stat.S_IRUSR class RunResult(): @@ -75,7 +80,9 @@ class RunResult(): pass def write_analysis_result(self, analysis_ressult_path, html_format=True): - with open(analysis_ressult_path, "wb") as f: + if os.path.exists(analysis_ressult_path): + os.remove(analysis_ressult_path) + with os.fdopen(os.open(analysis_ressult_path, FLAGS, MODES), 'wb') as f: if html_format: f.write(RunResult.filter_log(render_detail(self.crash_info))) else: diff --git a/localCoverage/codeCoverage/codeCoverage_gcov_lcov.py b/localCoverage/codeCoverage/codeCoverage_gcov_lcov.py index 4edf5d4..ae82dbb 100644 --- a/localCoverage/codeCoverage/codeCoverage_gcov_lcov.py +++ b/localCoverage/codeCoverage/codeCoverage_gcov_lcov.py @@ -23,6 +23,10 @@ import shutil import shlex import subprocess import sys +import stat + +FLAGS = os.O_WRONLY | os.O_APPEND | os.O_CREAT +MODES = stat.S_IWUSR | stat.S_IRUSR # 子系统json目录 SYSTEM_JSON = "build/subsystem_config.json" @@ -57,7 +61,7 @@ def execute_command(command, printflag=False): cmd_list = shlex.split(command) coverage_log_path = os.path.join( CODEPATH, "test/testfwk/developer_test/localCoverage", "coverage.log") - with open(coverage_log_path, 'a') as fd: + with os.fdopen(os.open(coverage_log_path, FLAGS, MODES), 'a') as fd: call(cmd_list, printflag, fd, fd) except IOError: print("Error: Exception occur in open err") diff --git a/localCoverage/codeCoverage/mutilProcess_CodeCoverage.py b/localCoverage/codeCoverage/mutilProcess_CodeCoverage.py index 1acba90..753b43b 100644 --- a/localCoverage/codeCoverage/mutilProcess_CodeCoverage.py +++ b/localCoverage/codeCoverage/mutilProcess_CodeCoverage.py @@ -23,8 +23,13 @@ import shlex import subprocess import multiprocessing import sys +import stat + from multiprocessing import Process +FLAGS = os.O_WRONLY | os.O_APPEND | os.O_CREAT +MODES = stat.S_IWUSR | stat.S_IRUSR + # 子系统json目录 SYSTEM_JSON = "test/testfwk/developer_test/localCoverage/codeCoverage/subsystem_config.json" # 覆盖率gcda @@ -61,7 +66,7 @@ def execute_command(command, printflag=False): cmd_list = shlex.split(command) coverage_log_path = os.path.join( CODEPATH, "test/testfwk/developer_test/localCoverage", "coverage.log") - with open(coverage_log_path, 'a') as fd: + with os.fdopen(os.open(coverage_log_path, FLAGS, MODES), 'a') as fd: call(cmd_list, printflag, fd, fd) except IOError: print("Error: Exception occur in open error") diff --git a/localCoverage/coverage_tools.py b/localCoverage/coverage_tools.py index 1aee796..f1c5ea9 100644 --- a/localCoverage/coverage_tools.py +++ b/localCoverage/coverage_tools.py @@ -22,10 +22,14 @@ import sys import json import shutil import subprocess +import stat from shutil import copyfile from utils import get_product_name, coverage_command +FLAGS = os.O_WRONLY | os.O_CREAT | os.O_EXCL +MODES = stat.S_IWUSR | stat.S_IRUSR + def get_subsystem_config(part_list, developer_path): all_system_info_path = os.path.join( @@ -45,7 +49,9 @@ def get_subsystem_config(part_list, developer_path): print("part not in all_subsystem_config.json") new_json = json.dumps(new_json_text, indent=4) - with open(system_info_path, "w") as out_file: + if os.path.exists(system_info_path): + os.remove(system_info_path) + with os.fdopen(os.open(system_info_path, FLAGS, MODES), 'w') as out_file: out_file.write(new_json) else: print("%s not exists.", all_system_info_path) diff --git a/localCoverage/interfaceCoverage/get_innerkits_json.py b/localCoverage/interfaceCoverage/get_innerkits_json.py index 27a3621..e8484e3 100644 --- a/localCoverage/interfaceCoverage/get_innerkits_json.py +++ b/localCoverage/interfaceCoverage/get_innerkits_json.py @@ -20,6 +20,10 @@ import os import json import sys +import stat + +FLAGS = os.O_WRONLY | os.O_CREAT | os.O_EXCL +MODES = stat.S_IWUSR | stat.S_IRUSR def _init_sys_config(): @@ -37,7 +41,9 @@ def gen_parts_info_json(folder_list, output_json_path, target_cpu): data_dict[folder_str] = f"innerkits/ohos-{target_cpu}/{folder_str}" output_json_path = os.path.join(output_json_path, "kits_modules_info.json") json_str = json.dumps(data_dict, indent=2) - with open(output_json_path, "w") as json_file: + if os.path.exists(output_json_path): + os.remove(output_json_path) + with os.fdopen(os.open(output_json_path, FLAGS, MODES), 'w') as json_file: json_file.write(json_str) else: print("part_name list information is null") diff --git a/localCoverage/interfaceCoverage/interfaceCoverage_gcov_lcov.py b/localCoverage/interfaceCoverage/interfaceCoverage_gcov_lcov.py index b88b01b..47cc669 100644 --- a/localCoverage/interfaceCoverage/interfaceCoverage_gcov_lcov.py +++ b/localCoverage/interfaceCoverage/interfaceCoverage_gcov_lcov.py @@ -22,10 +22,14 @@ import sys import json import shutil import subprocess +import stat import CppHeaderParser import get_innerkits_json import make_report +FLAGS = os.O_WRONLY | os.O_CREAT | os.O_EXCL +MODES = stat.S_IWUSR | stat.S_IRUSR + filter_file_name_list = [ "appexecfwk/libjnikit/include/jni.h", ] @@ -389,7 +393,9 @@ def get_summary_data(interface_data_list): def make_summary_file(summary_list, output_path): report_path = os.path.join(output_path, "coverage_summary_file.xml") try: - with open(report_path, "w") as fd: + if os.path.exists(report_path): + os.remove(report_path) + with os.fdopen(os.open(report_path, FLAGS, MODES), 'w') as fd: fd.write('\n') fd.write('\n') for item in summary_list: diff --git a/localCoverage/interfaceCoverage/make_report.py b/localCoverage/interfaceCoverage/make_report.py index c9a6d5e..1a06513 100644 --- a/localCoverage/interfaceCoverage/make_report.py +++ b/localCoverage/interfaceCoverage/make_report.py @@ -19,9 +19,14 @@ import sys import datetime +import os +import stat from importlib import reload reload(sys) +FLAGS_WRITE = os.O_WRONLY | os.O_CREAT | os.O_EXCL +FLAGS_ADD = os.O_WRONLY | os.O_APPEND | os.O_CREAT +MODES = stat.S_IWUSR | stat.S_IRUSR html_head = """ @@ -88,7 +93,9 @@ def sort_by_field_element_data(elem): def create_html_start(reportpath): try: - with open(reportpath, "w") as report: + if os.path.exists(reportpath): + os.remove(reportpath) + with os.fdopen(os.open(reportpath, FLAGS_WRITE, MODES), 'w') as report: report.write(html_head) report.write(html_body_start) except(IOError, ValueError): @@ -123,7 +130,7 @@ def create_title(reportpath, title_name, summary_list): nocoverd = item[1] - item[2] report_title = report_title % (item[1], item[2], nocoverd) try: - with open(reportpath, "a") as report: + with os.fdopen(os.open(reportpath, FLAGS_ADD, MODES), 'a') as report: report.write(report_title) except(IOError, ValueError): print("Error for create html title") @@ -158,7 +165,7 @@ def create_summary(reportpath, summary_list): try: if len(summary_list) == 0: return - with open(reportpath, "a") as report: + with os.fdopen(os.open(reportpath, FLAGS_ADD, MODES), 'a') as report: report.write(table_title) report.write(table_start) report.write(table_head) @@ -198,7 +205,7 @@ def create_table_test(reportpath, subsystem_name, datalist, total_count, covered table_ended = """ """ try: - with open(reportpath, "a") as report: + with os.fdopen(os.open(reportpath, FLAGS_ADD, MODES), 'a') as report: print("part_name==" + subsystem_name) tabletitle = table_title % (subsystem_name) print("tabletitle==" + tabletitle) @@ -234,7 +241,7 @@ def create_table_test(reportpath, subsystem_name, datalist, total_count, covered def create_html_ended(reportpath): try: - with open(reportpath, "a") as report: + with os.fdopen(os.open(reportpath, FLAGS_ADD, MODES), 'a') as report: report.write(html_body_ended) report.write(html_ended) except(IOError, ValueError): diff --git a/localCoverage/keyword_registration/keyword_filter.py b/localCoverage/keyword_registration/keyword_filter.py index 0c1e886..ddb627e 100644 --- a/localCoverage/keyword_registration/keyword_filter.py +++ b/localCoverage/keyword_registration/keyword_filter.py @@ -20,6 +20,7 @@ import os import re import time import json +import stat import fcntl import platform import linecache @@ -28,6 +29,10 @@ from multiprocessing import Pool from lxml import html from selectolax.parser import HTMLParser +FLAGS_WRITE = os.O_WRONLY | os.O_CREAT | os.O_EXCL +FLAGS_ADD = os.O_WRONLY | os.O_APPEND | os.O_CREAT +MODES = stat.S_IWUSR | stat.S_IRUSR + class CoverageReportPath: def __init__(self, report_path): @@ -61,8 +66,9 @@ span.branchnocovupdate { background-color:#BBBBBB; }""" - with open(css_file_path, "a+", encoding="utf-8") as file: - file.write(text) + if os.path.exists(css_file_path): + with os.fdopen(os.open(css_file_path, FLAGS_ADD, MODES), 'a+') as file: + file.write(text) def get_statistic_path(self, gcov_file_path: str): """ @@ -106,7 +112,7 @@ class KeywordRegistration: 获取覆盖率文件内容 """ if not os.path.exists(file_path): - return + return "" with open(file_path, "r", encoding="utf-8") as file: content = file.read() return content @@ -153,7 +159,7 @@ class KeywordRegistration: if not content: content = self.get_coverage_content(file_path) if not content: - return + return [] _, file_name = os.path.split(file_path) branch_line_list = re.findall( r''' 200: - return + return "" previous_line -= 1 tag = get_tag(content, previous_line) tag_text = HTMLParser(tag).text() @@ -271,7 +279,7 @@ class KeywordRegistration: while previous_line: child_count += 1 if child_count > 200: - return + return "" previous_line -= 1 html_text = get_tag(content, previous_line) source_code = HTMLParser(html_text).text()[39:].strip() @@ -285,16 +293,18 @@ class KeywordRegistration: if class_name_list: function_name_str = class_name_list[0] if not function_name_str: - return + return "" + function_name = function_name_str.split()[-1] return function_name function_name_list = re.findall(r' (.*?)\(', source_code) if function_name_list: function_name_str = function_name_list[0] if not function_name_str: - return + return "" function_name = function_name_str.split()[-1] return function_name + return "" except (OSError, IndexError, TypeError) as error: print(f"覆盖率报告{branch_line}行获取函数名报错, error:{error}", traceback.format_exc()) @@ -325,7 +335,7 @@ class KeywordRegistration: keyword_index = keyword_source_code.find(keyword) if keyword_index == -1: - return + return "" try: keyword_code = keyword_source_code[:keyword_index + len(keyword)] @@ -338,6 +348,7 @@ class KeywordRegistration: return judge_key except (IndexError, ValueError): print("获取关键字替代字符失败") + return "" @staticmethod def get_branch_data_by_tag(tag_html: str, symbol_status=None): @@ -460,7 +471,8 @@ class KeywordRegistration: if replace_tag in content: content = content.replace(replace_tag, update_tag) - with open(file_path, "w", encoding="utf-8") as file: + os.remove(file_path) + with os.fdopen(os.open(file_path, FLAGS_WRITE, MODES), 'w') as file: file.write(content) # 修改数据统计页面 @@ -565,7 +577,8 @@ class KeywordRegistration: content = content.replace(replace_tag, update_tag) if file_tag in content: content = content.replace(file_tag, update_branch_tag) - with open(index_path, "w", encoding="utf-8") as file: + os.remove(index_path) + with os.fdopen(os.open(index_path, FLAGS_WRITE, MODES), 'w') as file: file.write(content) fcntl.flock(file.fileno(), fcntl.LOCK_UN) time.sleep(1) @@ -641,7 +654,7 @@ class KeywordRegistration: update_branch_tag = update_branch_tag.replace("> # <", "> <") update_branch_tag = branch_tag + update_branch_tag except ValueError: - return + return "" else: line_feed_index = branch_html.find(line_item) update_branch_tag = branch_html[:line_feed_index + len(line_item) + 1] @@ -670,7 +683,7 @@ class KeywordRegistration: branch_tag = branch_tag[line_feed_index + len(line_item) + 1:] line_feed_index = branch_tag.find(line_item) except ValueError: - return + return "" branch_tag = branch_tag.replace("> - <", "> <") update_branch_tag = update_branch_tag.replace("> # <", "> <") @@ -704,7 +717,7 @@ class KeywordRegistration: branch_tag_after = branch_tag_after.replace("> - <", "> <") branch_tag = branch_tag_before + branch_tag_after except ValueError: - return + return "" else: branch_tag = branch_html branch_tag = branch_tag.replace("> - <", "> <") @@ -749,7 +762,7 @@ class KeywordRegistration: update_branch_tag += branch_tag branch_html = branch_html[end_index + 5:] except (ValueError, TypeError): - return + return "" update_branch_tag += branch_html return update_branch_tag @@ -856,12 +869,15 @@ class KeywordRegistration: update_source_code_tag_list) content = content.replace( source_code_tag_html, update_source_code_tag_html) - with open(file_path, "w", encoding="utf-8") as new_html: + os.remove(file_path) + with os.fdopen(os.open(file_path, FLAGS_WRITE, MODES), 'w') as new_html: new_html.write(content) content = self.get_coverage_content(file_path) content = content.replace('> * ', '> ') - with open(file_path, "w", encoding="utf-8") as new_html: + os.remove(file_path) + + with os.fdopen(os.open(file_path, FLAGS_WRITE, MODES), 'w') as new_html: new_html.write(content) def keyword_registration(self, file_path, keyword_list): diff --git a/localCoverage/resident_service/init_gcov.py b/localCoverage/resident_service/init_gcov.py index b42d11c..a5848dc 100644 --- a/localCoverage/resident_service/init_gcov.py +++ b/localCoverage/resident_service/init_gcov.py @@ -22,9 +22,13 @@ import subprocess import json import sys import time +import stat import xml.etree.ElementTree as ET from public_method import get_server_dict, get_config_ip, get_sn_list +FLAGS = os.O_WRONLY | os.O_CREAT | os.O_EXCL +MODES = stat.S_IWUSR | stat.S_IRUSR + def _init_sys_config(): sys.localcoverage_path = os.path.join(current_path, "..") @@ -61,7 +65,9 @@ def modify_init_file(developer_path, hdc_str): else: return json_str = json.dumps(json_data, indent=2) - with open(cfg_file_path, "w") as json_file: + if os.path.exists(cfg_file_path): + os.remove(cfg_file_path) + with os.fdopen(os.open(cfg_file_path, FLAGS, MODES), 'w') as json_file: json_file.write(json_str) else: print("init.cfg file not exists") @@ -97,7 +103,9 @@ def modify_faultloggerd_file(developer_path, hdc_str): ] }) json_str = json.dumps(json_data, indent=4) - with open(cfg_file_path, "w") as json_file: + if os.path.exists(cfg_file_path): + os.remove(cfg_file_path) + with os.fdopen(os.open(cfg_file_path, FLAGS, MODES), 'w') as json_file: json_file.write(json_str) print("%s file send %s %s" % (hdc_str, cfg_file_path, "/system/etc/init/")) coverage_command("%s file send %s %s" % (hdc_str, cfg_file_path, "/system/etc/init/")) @@ -153,7 +161,9 @@ def modify_foundation_json(serv, config_path, origin_json) -> str: f_dict["systemability"] = tmp_list new_json = os.path.join(config_path, 'foundation.json') - with open(new_json, "w", encoding="utf-8") as f: + if os.path.exists(new_json): + os.remove(new_json) + with os.fdopen(os.open(new_json, FLAGS, MODES), 'w') as f: json.dump(f_dict, f, indent=4) return new_json @@ -180,7 +190,9 @@ def create_service_json(serv, config_path, origin_json) -> str: f_dict["process"] = "{}".format(serv) new_json = os.path.join(config_path, '{}.json'.format(serv)) - with open(new_json, "w", encoding="utf-8") as f: + if os.path.exists(new_json): + os.remove(new_json) + with os.fdopen(os.open(new_json, FLAGS, MODES), 'w') as f: json.dump(f_dict, f, indent=4) return new_json @@ -234,7 +246,9 @@ def create_service_cfg(serv, config_path, origin_cfg) -> str: json_obj["services"][0]["jobs"]["on-start"] = "services:{}".format(serv) cfg_path = os.path.join(config_path, "{}.cfg".format(serv)) - with open(cfg_path, 'w') as r: + if os.path.exists(cfg_path): + os.remove(cfg_path) + with os.fdopen(os.open(cfg_path, FLAGS, MODES), 'w') as r: json.dump(json_obj, r, indent=4) return cfg_path diff --git a/localCoverage/resident_service/public_method.py b/localCoverage/resident_service/public_method.py index f203c2c..7714459 100644 --- a/localCoverage/resident_service/public_method.py +++ b/localCoverage/resident_service/public_method.py @@ -75,6 +75,7 @@ def get_all_part_service(): component_gcda_dict = system_text_json["component_gcda_dict"] return system_info_dict, services_component_dict, component_gcda_dict print("%s not exists.", system_part_service_path) + return {}, {}, {} def get_system_dict_to_server_name(server_name: str, system_info_dict): @@ -84,6 +85,7 @@ def get_system_dict_to_server_name(server_name: str, system_info_dict): system: [server_name] } return system_info_dict_after + return {} def get_server_dict(command): diff --git a/localCoverage/resident_service/pull_service_gcda.py b/localCoverage/resident_service/pull_service_gcda.py index 1ea9227..e8b2ac7 100644 --- a/localCoverage/resident_service/pull_service_gcda.py +++ b/localCoverage/resident_service/pull_service_gcda.py @@ -50,7 +50,7 @@ def restore_config(device_ip, device_port, device_sn, cfg_path): def attach_pid(device_ip, device_sn, process_str, component_gcda_dict, developer_path, - resident_service_path, services_str, root_path, device_port): + resident_service_path, services_str, root, device_port): """ 1. 在设备里ps -ef | grep SERVICE 获取进程号 2. kill - '信号' pid @@ -69,7 +69,7 @@ def attach_pid(device_ip, device_sn, process_str, component_gcda_dict, developer # 拉取gcda文件 get_gcda_file(device_ip, device_sn, process_str, component_gcda_dict, - developer_path, services_str, root_path, device_port) + developer_path, services_str, root, device_port) def get_gcda_file(device_ip, device_sn, process_str, component_gcda_dict, @@ -110,7 +110,7 @@ def get_gcda_file(device_ip, device_sn, process_str, component_gcda_dict, def get_service_list(device_ip, device_sn, system_info_dict, services_component_dict, - component_gcda_dict, developer_path, resident_service_path, root_path, port): + component_gcda_dict, developer_path, resident_service_path, root, port_nu): service_list = [] for key, value_list in system_info_dict.items(): @@ -124,7 +124,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, root_path, port) + developer_path, resident_service_path, services_str, root, port_nu) return diff --git a/localCoverage/restore_comment/after_lcov_branch.py b/localCoverage/restore_comment/after_lcov_branch.py index 7c8198c..609a43c 100644 --- a/localCoverage/restore_comment/after_lcov_branch.py +++ b/localCoverage/restore_comment/after_lcov_branch.py @@ -18,6 +18,10 @@ import os import subprocess +import stat + +FLAGS = os.O_WRONLY | os.O_CREAT | os.O_EXCL +MODES = stat.S_IWUSR | stat.S_IRUSR def get_file_list(find_path, postfix=""): @@ -56,7 +60,9 @@ def recover_source_file(cpp_list, keys): for key in keys: with open(path, "r") as read_fp: code_lines = read_fp.readlines() - with open(f"{path.split('.')[0]}_bk.html", "w") as write_fp: + 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: if key in line: write_fp.write(line.strip("\n").strip("\n\r").replace(" //LCOV_EXCL_BR_LINE", "")) diff --git a/localCoverage/restore_comment/build_before_generate.py b/localCoverage/restore_comment/build_before_generate.py index 4f79506..9d5e00e 100644 --- a/localCoverage/restore_comment/build_before_generate.py +++ b/localCoverage/restore_comment/build_before_generate.py @@ -19,31 +19,10 @@ import os import subprocess import json +import stat - -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 - - -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) - 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 +FLAGS = os.O_WRONLY | os.O_CREAT | os.O_EXCL +MODES = stat.S_IWUSR | stat.S_IRUSR def get_source_file_list(path): @@ -77,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) - with open(f"{source_dir}_bk.{suffix_name}", "w", - encoding="utf-8", errors="ignore") 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: for key in keys: if key in line and line.strip().startswith(key): @@ -129,7 +108,9 @@ def get_part_config_json(part_list, system_info_path, part_path): else: print("part not in all_subsystem_config.json") new_json = json.dumps(new_json_text, indent=4) - with open(part_path, "w") as out_file: + if os.path.exists(part_path): + os.remove(part_path) + with os.fdopen(os.open(part_path, FLAGS, MODES), 'w') as out_file: out_file.write(new_json) else: print("%s not exists.", system_info_path) diff --git a/src/core/build/build_manager.py b/src/core/build/build_manager.py index b7ca0e2..3c8ad7a 100644 --- a/src/core/build/build_manager.py +++ b/src/core/build/build_manager.py @@ -17,6 +17,8 @@ # import os +import stat + from xdevice import platform_logger from core.utils import scan_support_product from core.config.config_manager import UserConfigManager, FrameworkConfigManager @@ -26,6 +28,9 @@ from core.build.build_testcases import BuildTestcases from core.command.gen import Gen from core.command.run import Run +FLAGS = os.O_WRONLY | os.O_APPEND | os.O_CREAT +MODES = stat.S_IWUSR | stat.S_IRUSR + LOG = platform_logger("BuildManager") @@ -37,7 +42,9 @@ class BuildManager(object): @classmethod def _make_gn_file(cls, filepath, target_list): LOG.info("The gn file path: %s" % filepath) - with open(filepath, "w") as gn_file: + if os.path.exists(filepath): + os.remove(filepath) + with os.fdopen(os.open(filepath, FLAGS, MODES), 'w') as gn_file: gn_file.write("group(\"make_temp_test\") {\n") gn_file.write(" testonly = true\n") gn_file.write(" deps = []\n") diff --git a/src/core/build/build_testcases.py b/src/core/build/build_testcases.py index 4973aec..24f9ff3 100644 --- a/src/core/build/build_testcases.py +++ b/src/core/build/build_testcases.py @@ -346,7 +346,7 @@ class BuildTestcases(object): self._delete_xts_testcase_dir(para) xts_build_command = [] if para.productform == "rk3568": - pass + False else: xts_build_test_command = ["--abi-type", "generic_generic_arm_64only", "--device-type", get_output_path().split("/")[-1], "--build-variant", "root", "--gn-args", @@ -357,7 +357,7 @@ class BuildTestcases(object): xts_build_test_command.append(input_subsystem) if para.testsuit != "" and len(para.subsystem) == 0: LOG.error("Please specify subsystem.") - return + return False xts_build_command.extend(xts_build_test_command) xts_build_command.append("--ccache") build_result = self._execute_build_xts_command(para, xts_build_command) diff --git a/src/core/build/pretreat_targets.py b/src/core/build/pretreat_targets.py index 7d30c9e..a5113df 100644 --- a/src/core/build/pretreat_targets.py +++ b/src/core/build/pretreat_targets.py @@ -19,11 +19,16 @@ import os import sys import json +import stat import shutil from core.constants import JsTestConst from xdevice import platform_logger +FLAGS_WRITE = os.O_WRONLY | os.O_CREAT | os.O_EXCL +FLAGS_ADD = os.O_WRONLY | os.O_APPEND | os.O_CREAT +MODES = stat.S_IWUSR | stat.S_IRUSR + LOG = platform_logger("PretreatTargets") @@ -84,14 +89,15 @@ class PretreatTargets(object): output_path = self._parse_output_path_in_gn(gn_path) if output_path == "": LOG.error(" BUILD.gn needs 'module_output_path'") - return + return False os.rename(gn_path, gn_bak_path) template_args = {'output_path': output_path, 'suite_name': name} - with open(gn_path, 'w') as filehandle: + os.remove(gn_path) + with os.fdopen(os.open(gn_path, FLAGS_WRITE, MODES), 'w') as filehandle: filehandle.write(JsTestConst.BUILD_GN_FILE_TEMPLATE % template_args) - #copy js hap template to target path + # copy js hap template to target path shutil.copytree(template_path, os.path.join(target_path, "src")) shutil.copy(config_path, os.path.join(target_path, "src", "main")) file_name = os.listdir(target_path) @@ -99,8 +105,8 @@ class PretreatTargets(object): if file.endswith(".js"): LOG.info("file: %s" % file) shutil.copy(os.path.join(target_path, file), test_path) - with open(os.path.join(test_path, "List.test.js"), 'a') \ - as list_data: + with os.fdopen(os.open(os.path.join(test_path, "List.test.js"), + FLAGS_ADD, MODES), 'a') as list_data: list_data.write("require('./%s')\n" % file) #modify i18n json file @@ -113,7 +119,8 @@ class PretreatTargets(object): if "TargetName" in line: line = line.replace("TargetName", name) json_data += line - with open(i18n_path, 'w') as i18n_file: + os.remove(i18n_path) + with os.fdopen(os.open(i18n_path, FLAGS_WRITE, MODES), 'w') as i18n_file: i18n_file.write(json_data) return True diff --git a/src/core/command/console.py b/src/core/command/console.py index 249defa..84624f7 100755 --- a/src/core/command/console.py +++ b/src/core/command/console.py @@ -363,8 +363,7 @@ class Console(object): else: print("The %s command is not supported." % command) except (AttributeError, IOError, IndexError, ImportError, NameError, - RuntimeError, SystemError, TypeError, ValueError, - UnicodeError) as exception: + RuntimeError, SystemError, TypeError, ValueError) as exception: LOG.exception(exception, exc_info=False) @classmethod diff --git a/src/core/command/display.py b/src/core/command/display.py index c0ce5db..a762f47 100644 --- a/src/core/command/display.py +++ b/src/core/command/display.py @@ -198,7 +198,7 @@ def select_user_input(data_list): exit the frame.") quit() sys.exit(0) - return select_item_value, select_item_index + return select_item_value, select_item_index # 选择productform diff --git a/src/core/command/distribute_utils.py b/src/core/command/distribute_utils.py index ffd1679..9073a5f 100755 --- a/src/core/command/distribute_utils.py +++ b/src/core/command/distribute_utils.py @@ -20,11 +20,16 @@ import os import subprocess import sys import re +import stat + from xdevice import platform_logger from xdevice import EnvironmentManager from xdevice import ResultReporter from xdevice import ExecInfo +FLAGS = os.O_WRONLY | os.O_CREAT | os.O_EXCL +MODES = stat.S_IWUSR | stat.S_IRUSR + LOG = platform_logger("distribute_utils") @@ -32,7 +37,9 @@ def make_device_info_file(tmp_path): env_manager = EnvironmentManager() device_info_file_path = os.path.join(tmp_path, "device_info_file.txt") - with open(device_info_file_path, "w") as file_handle: + if os.path.exists(device_info_file_path): + os.remove(device_info_file_path) + with os.fdopen(os.open(device_info_file_path, FLAGS, MODES), 'w') as file_handle: if not env_manager.managers: return if list(env_manager.managers.values())[0].devices_list: diff --git a/src/core/command/gen.py b/src/core/command/gen.py index 4bc0fee..332ed40 100644 --- a/src/core/command/gen.py +++ b/src/core/command/gen.py @@ -19,9 +19,13 @@ import os import sys import subprocess +import stat from xdevice import platform_logger +FLAGS = os.O_WRONLY | os.O_CREAT | os.O_EXCL +MODES = stat.S_IWUSR | stat.S_IRUSR + LOG = platform_logger("Gen") class Gen(object): @@ -40,7 +44,9 @@ class Gen(object): filepath = os.path.join(sys.source_code_root_path, "test", "developertest", "libs", "fuzzlib", "fuzzer_list.txt") LOG.info("The fuzzer list file path: %s" % filepath) - with open(filepath, "w") as gn_file: + if os.path.exists(filepath): + os.remove(filepath) + with os.fdopen(os.open(filepath, FLAGS, MODES), 'w') as gn_file: gn_file.truncate(0) if fuzzer_list: for target in fuzzer_list: diff --git a/src/core/command/run.py b/src/core/command/run.py index 20bb93f..1e33641 100644 --- a/src/core/command/run.py +++ b/src/core/command/run.py @@ -297,7 +297,7 @@ class Run(object): if len(self.history_cmd_list) >= 10: del self.history_cmd_list[0] self.history_cmd_list.append(cmd_record) - print("-------------run end: ", self.history_cmd_list) + if options.coverage and platform.system() != "Windows": pull_service_gcov_path = os.path.join( sys.framework_root_dir, "localCoverage/resident_service/pull_service_gcda.py") @@ -422,7 +422,7 @@ class Run(object): xts_test_case_path = self.get_xts_tests_out_path(options.productform, options.testtype) if not os.path.exists(xts_test_case_path): LOG.error("%s is not exist." % xts_test_case_path) - return + return {} xts_test_dict = TestCaseManager().get_xts_test_files(xts_test_case_path, options) return xts_test_dict @@ -431,7 +431,7 @@ class Run(object): test_case_path = self.get_tests_out_path(options.productform) if not os.path.exists(test_case_path): LOG.error("%s is not exist." % test_case_path) - return + return {} test_dict = TestCaseManager().get_test_files(test_case_path, options) return test_dict diff --git a/src/core/common.py b/src/core/common.py index e46dbc9..878aa10 100755 --- a/src/core/common.py +++ b/src/core/common.py @@ -49,6 +49,4 @@ def get_source_code_root_path(path): def is_open_source_product(product_name): open_source_products = ["Hi3516DV300"] return True - if product_name in open_source_products: - return True - return False + diff --git a/src/core/config/config_manager.py b/src/core/config/config_manager.py index 7f05543..b9d12fa 100755 --- a/src/core/config/config_manager.py +++ b/src/core/config/config_manager.py @@ -239,12 +239,12 @@ class UserConfigManager(object): node = root.find(target_name) if not node: - return + return data_dic if sub_target != "": node = node.find(sub_target) if not node: - return + return data_dic for sub in node: if sub.text is None: diff --git a/src/core/driver/drivers.py b/src/core/driver/drivers.py index f77f8b8..d203c33 100644 --- a/src/core/driver/drivers.py +++ b/src/core/driver/drivers.py @@ -61,6 +61,9 @@ TIME_OUT = 900 * 1000 JS_TIMEOUT = 10 CYCLE_TIMES = 30 +FLAGS = os.O_WRONLY | os.O_CREAT | os.O_EXCL +MODES = stat.S_IWUSR | stat.S_IRUSR + ############################################################################## ############################################################################## @@ -184,7 +187,7 @@ def _create_empty_result_file(filepath, filename, error_message): if filename.endswith(".hap"): filename = filename.split(".")[0] if not os.path.exists(filepath): - with open(filepath, "w", encoding='utf-8') as file_desc: + with os.fdopen(os.open(filepath, FLAGS, MODES), 'w') as file_desc: time_stamp = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) file_desc.write('\n') @@ -231,7 +234,7 @@ def _sleep_according_to_result(result): def _create_fuzz_crash_file(filepath, filename): if not os.path.exists(filepath): - with open(filepath, "w", encoding='utf-8') as file_desc: + with os.fdopen(os.open(filepath, FLAGS, MODES), 'w') as file_desc: time_stamp = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) file_desc.write('\n') @@ -257,7 +260,7 @@ def _create_fuzz_crash_file(filepath, filename): def _create_fuzz_pass_file(filepath, filename): if not os.path.exists(filepath): - with open(filepath, "w", encoding='utf-8') as file_desc: + with os.fdopen(os.open(filepath, FLAGS, MODES), 'w') as file_desc: time_stamp = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) file_desc.write('\n') @@ -685,16 +688,29 @@ class CppTestDriver(IDriver): @staticmethod def _alter_init(name): - lines = list() - with open(name, "r") as f: - for line in f: - line_strip = line.strip() - if not line_strip: - continue - if line_strip[0] != "#" and line_strip[0] != "/" and line_strip[0] != "*": - lines.append(line_strip) - with open(name, "w") as f: - f.writelines(lines) + with open(name, "rb") as f: + lines = f.read() + str_content = lines.decode("utf-8") + + pattern_sharp = '^\s*#.*$(\n|\r\n)' + pattern_star = '/\*.*?\*/(\n|\r\n)+' + pattern_xml = '(\n|\r\n)+' + + if re.match(pattern_sharp, str_content, flags=re.M): + striped_content = re.sub(pattern_sharp, '', str_content, flags=re.M) + elif re.findall(pattern_star, str_content, flags=re.S): + striped_content = re.sub(pattern_star, '', str_content, flags=re.S) + elif re.findall(pattern_xml, str_content, flags=re.S): + striped_content = re.sub(pattern_xml, '', str_content, flags=re.S) + else: + striped_content = str_content + + striped_bt = striped_content.encode("utf-8") + with open(name, "wb") as f: + if os.path.exists(name): + os.remove(name) + with os.fdopen(os.open(name, FLAGS, MODES), 'wb') as f: + f.write(striped_bt) def _push_corpus_cov_if_exist(self, suite_file): cov_file = suite_file + "_corpus.tar.gz" @@ -1091,7 +1107,7 @@ class JSUnitTestDriver(IDriver): try: zf_desc.extractall(path=hap_bak_path) except RuntimeError as error: - print(error) + print("Unzip error:", hap_bak_path) zf_desc.close() # verify config.json file @@ -1132,7 +1148,6 @@ class JSUnitTestDriver(IDriver): return package_name, ability_name - @Plugin(type=Plugin.DRIVER, id=DeviceTestType.oh_rust_test) class OHRustTestDriver(IDriver): def __init__(self): diff --git a/src/core/testcase/testcase_manager.py b/src/core/testcase/testcase_manager.py index 428b541..41cb2e1 100644 --- a/src/core/testcase/testcase_manager.py +++ b/src/core/testcase/testcase_manager.py @@ -296,7 +296,7 @@ class TestCaseManager(object): def get_hap_test_driver(cls, hap_file_path): data_dic = cls.get_hap_json(hap_file_path) if not data_dic: - return False + return "" else: if "driver" in data_dic.keys(): driver_dict = data_dic.get("driver") @@ -305,6 +305,9 @@ class TestCaseManager(object): return driver_type else: LOG.error("%s has not set driver." % hap_file_path) + return "" + else: + return "" @classmethod def get_hap_json(cls, hap_file_path): @@ -314,6 +317,10 @@ class TestCaseManager(object): with open(json_file_path, 'r') as json_file: data_dic = json.load(json_file) return data_dic + else: + return {} + else: + return {} @classmethod def get_hap_part_json(cls, hap_file_path): @@ -323,13 +330,19 @@ class TestCaseManager(object): with open(json_file_path, 'r') as json_file: data_dic = json.load(json_file) return data_dic + else: + return {} + else: + return {} @classmethod def get_part_name_test_file(cls, hap_file_path): data_dic = cls.get_hap_part_json(hap_file_path) if not data_dic: - return False + return "" else: if "part" in data_dic.keys(): part_name = data_dic["part"] - return part_name \ No newline at end of file + return part_name + else: + return "" diff --git a/src/core/testkit/kit_lite.py b/src/core/testkit/kit_lite.py index bb062d1..35fafde 100755 --- a/src/core/testkit/kit_lite.py +++ b/src/core/testkit/kit_lite.py @@ -142,4 +142,6 @@ class DeployKit(ITestKit): new_temp_tool_path = '{}_{}{}'.format( os.path.splitext(original_file)[0], "".join(random_str), os.path.splitext(original_file)[1]) - return shutil.copyfile(original_file, new_temp_tool_path) \ No newline at end of file + return shutil.copyfile(original_file, new_temp_tool_path) + else: + return "" diff --git a/src/core/utils.py b/src/core/utils.py index 7be7853..fdab9db 100755 --- a/src/core/utils.py +++ b/src/core/utils.py @@ -139,13 +139,14 @@ def parse_device_name(product_form): device_json_file = os.path.join(sys.source_code_root_path, "productdefine", "common", "products", "{}.json".format(product_form)) + device_name = "" if not os.path.exists(device_json_file): - return + return device_name with open(device_json_file, 'r') as json_file: json_info = json.load(json_file) if not json_info: - return + return device_name device_name = json_info.get('product_device') if not device_name: device_name = get_output_path() diff --git a/src/main/_init_global_config.py b/src/main/_init_global_config.py index 7dddbf5..e79ee0e 100755 --- a/src/main/_init_global_config.py +++ b/src/main/_init_global_config.py @@ -122,13 +122,13 @@ def _load_internal_plugins(): import benchmark.report.benchmark_reporter _iter_module_plugins([core.driver, benchmark.report.benchmark_reporter]) - except (ModuleNotFoundError, ImportError): + except ImportError: pass try: import script.report _iter_module_plugins([script.report]) - except (ModuleNotFoundError, ImportError): + except ImportError: pass -- Gitee