13f085823Sopenharmony_ci#!/usr/bin/env python3 23f085823Sopenharmony_ci# coding=utf-8 33f085823Sopenharmony_ci 43f085823Sopenharmony_ci# 53f085823Sopenharmony_ci# Copyright (c) 2022 Huawei Device Co., Ltd. 63f085823Sopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License"); 73f085823Sopenharmony_ci# you may not use this file except in compliance with the License. 83f085823Sopenharmony_ci# You may obtain a copy of the License at 93f085823Sopenharmony_ci# 103f085823Sopenharmony_ci# http://www.apache.org/licenses/LICENSE-2.0 113f085823Sopenharmony_ci# 123f085823Sopenharmony_ci# Unless required by applicable law or agreed to in writing, software 133f085823Sopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS, 143f085823Sopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 153f085823Sopenharmony_ci# See the License for the specific language governing permissions and 163f085823Sopenharmony_ci# limitations under the License. 173f085823Sopenharmony_ci# 183f085823Sopenharmony_ci 193f085823Sopenharmony_ciimport sys 203f085823Sopenharmony_ciimport os 213f085823Sopenharmony_ciimport platform 223f085823Sopenharmony_ciimport time 233f085823Sopenharmony_ciimport json 243f085823Sopenharmony_cifrom core.config.config_manager import UserConfigManager, FrameworkConfigManager 253f085823Sopenharmony_ci 263f085823Sopenharmony_ci 273f085823Sopenharmony_cidef get_filename_extension(file): 283f085823Sopenharmony_ci _, fullname = os.path.split(file) 293f085823Sopenharmony_ci filename, ext = os.path.splitext(fullname) 303f085823Sopenharmony_ci return filename, ext 313f085823Sopenharmony_ci 323f085823Sopenharmony_ci 333f085823Sopenharmony_cidef create_dir(path): 343f085823Sopenharmony_ci full_path = os.path.abspath(os.path.expanduser(path)) 353f085823Sopenharmony_ci if not os.path.exists(full_path): 363f085823Sopenharmony_ci os.makedirs(full_path, exist_ok=True) 373f085823Sopenharmony_ci 383f085823Sopenharmony_ci 393f085823Sopenharmony_cidef get_file_list(find_path, postfix=""): 403f085823Sopenharmony_ci file_names = os.listdir(find_path) 413f085823Sopenharmony_ci file_list = [] 423f085823Sopenharmony_ci if len(file_names) > 0: 433f085823Sopenharmony_ci for file_name in file_names: 443f085823Sopenharmony_ci if postfix != "": 453f085823Sopenharmony_ci if file_name.find(postfix) != -1 \ 463f085823Sopenharmony_ci and file_name[-len(postfix):] == postfix: 473f085823Sopenharmony_ci file_list.append(file_name) 483f085823Sopenharmony_ci else: 493f085823Sopenharmony_ci file_list.append(file_name) 503f085823Sopenharmony_ci return file_list 513f085823Sopenharmony_ci 523f085823Sopenharmony_ci 533f085823Sopenharmony_ci# 获取目录下每一个文件,并放到一个列表里 543f085823Sopenharmony_cidef get_file_list_by_postfix(path, postfix=""): 553f085823Sopenharmony_ci file_list = [] 563f085823Sopenharmony_ci for dirs in os.walk(path): 573f085823Sopenharmony_ci files = get_file_list(find_path=dirs[0], postfix=postfix) 583f085823Sopenharmony_ci for file_name in files: 593f085823Sopenharmony_ci if "" != file_name and -1 == file_name.find(__file__): 603f085823Sopenharmony_ci file_name = os.path.join(dirs[0], file_name) 613f085823Sopenharmony_ci if os.path.isfile(file_name): 623f085823Sopenharmony_ci file_list.append(file_name) 633f085823Sopenharmony_ci return file_list 643f085823Sopenharmony_ci 653f085823Sopenharmony_ci 663f085823Sopenharmony_cidef get_device_log_file(report_path, serial=None, log_name="device_log"): 673f085823Sopenharmony_ci log_path = os.path.join(report_path, "log") 683f085823Sopenharmony_ci os.makedirs(log_path, exist_ok=True) 693f085823Sopenharmony_ci 703f085823Sopenharmony_ci serial = serial or time.time_ns() 713f085823Sopenharmony_ci device_file_name = "{}_{}.log".format(log_name, serial) 723f085823Sopenharmony_ci device_log_file = os.path.join(log_path, device_file_name) 733f085823Sopenharmony_ci return device_log_file 743f085823Sopenharmony_ci 753f085823Sopenharmony_ci 763f085823Sopenharmony_cidef get_build_output_path(product_form): 773f085823Sopenharmony_ci if sys.source_code_root_path == "": 783f085823Sopenharmony_ci return "" 793f085823Sopenharmony_ci 803f085823Sopenharmony_ci standard_large_system_list = scan_support_product() 813f085823Sopenharmony_ci product_list = FrameworkConfigManager().get_framework_config("productform") 823f085823Sopenharmony_ci if product_form in standard_large_system_list: 833f085823Sopenharmony_ci device_name = parse_device_name(product_form) 843f085823Sopenharmony_ci if device_name is not None: 853f085823Sopenharmony_ci build_output_name = device_name 863f085823Sopenharmony_ci else: 873f085823Sopenharmony_ci return "" 883f085823Sopenharmony_ci elif product_form in product_list and (product_form not in standard_large_system_list): 893f085823Sopenharmony_ci build_output_name = product_form 903f085823Sopenharmony_ci else: 913f085823Sopenharmony_ci board_info_list = product_form.split("_") 923f085823Sopenharmony_ci if len(board_info_list) < 3: 933f085823Sopenharmony_ci return "" 943f085823Sopenharmony_ci 953f085823Sopenharmony_ci first_build_output = board_info_list[1] + "_" + board_info_list[2] 963f085823Sopenharmony_ci second_build_output = product_form 973f085823Sopenharmony_ci build_output_name = os.path.join(first_build_output, 983f085823Sopenharmony_ci second_build_output) 993f085823Sopenharmony_ci 1003f085823Sopenharmony_ci build_output_path = os.path.join(sys.source_code_root_path, 1013f085823Sopenharmony_ci "out", 1023f085823Sopenharmony_ci build_output_name) 1033f085823Sopenharmony_ci # 返回编译结果输出目录:~/OpenHarmony/out/rk3568(以rk3568举例) 1043f085823Sopenharmony_ci return build_output_path 1053f085823Sopenharmony_ci 1063f085823Sopenharmony_ci 1073f085823Sopenharmony_cidef scan_support_product(): 1083f085823Sopenharmony_ci # scan standard and large system info 1093f085823Sopenharmony_ci # 路径注释 product_form_dir = OpenHarmony/productdefine/common/products/ 1103f085823Sopenharmony_ci product_form_dir = os.path.join(sys.source_code_root_path, 1113f085823Sopenharmony_ci "productdefine", 1123f085823Sopenharmony_ci "common", 1133f085823Sopenharmony_ci "products") 1143f085823Sopenharmony_ci productform_list = [] 1153f085823Sopenharmony_ci if os.path.exists(product_form_dir): 1163f085823Sopenharmony_ci for product_form_file in os.listdir(product_form_dir): 1173f085823Sopenharmony_ci if os.path.isdir(os.path.join(product_form_dir, 1183f085823Sopenharmony_ci product_form_file)): 1193f085823Sopenharmony_ci continue 1203f085823Sopenharmony_ci product_file = os.path.basename(product_form_file) 1213f085823Sopenharmony_ci if product_file.endswith(".build") or "parts" in product_file or \ 1223f085823Sopenharmony_ci "x86_64" in product_file or "32" in product_file: 1233f085823Sopenharmony_ci continue 1243f085823Sopenharmony_ci product_name, _ = os.path.splitext(product_file) 1253f085823Sopenharmony_ci productform_list.append(product_name) 1263f085823Sopenharmony_ci return productform_list 1273f085823Sopenharmony_ci 1283f085823Sopenharmony_ci 1293f085823Sopenharmony_cidef get_output_path(): 1303f085823Sopenharmony_ci # 获取输出路径 1313f085823Sopenharmony_ci ohos_config_path = os.path.join(sys.source_code_root_path, "out", "ohos_config.json") 1323f085823Sopenharmony_ci with open(ohos_config_path, 'r') as json_file: 1333f085823Sopenharmony_ci json_info = json.load(json_file) 1343f085823Sopenharmony_ci out_name = json_info.get("out_path").split("out")[1].strip("/") 1353f085823Sopenharmony_ci return out_name 1363f085823Sopenharmony_ci 1373f085823Sopenharmony_ci 1383f085823Sopenharmony_cidef parse_device_name(product_form): 1393f085823Sopenharmony_ci device_json_file = os.path.join(sys.source_code_root_path, 1403f085823Sopenharmony_ci "productdefine", "common", "products", 1413f085823Sopenharmony_ci "{}.json".format(product_form)) 1423f085823Sopenharmony_ci device_name = "" 1433f085823Sopenharmony_ci if not os.path.exists(device_json_file): 1443f085823Sopenharmony_ci return device_name 1453f085823Sopenharmony_ci 1463f085823Sopenharmony_ci with open(device_json_file, 'r') as json_file: 1473f085823Sopenharmony_ci json_info = json.load(json_file) 1483f085823Sopenharmony_ci if not json_info: 1493f085823Sopenharmony_ci return device_name 1503f085823Sopenharmony_ci device_name = json_info.get('product_device') 1513f085823Sopenharmony_ci if not device_name: 1523f085823Sopenharmony_ci device_name = get_output_path() 1533f085823Sopenharmony_ci return device_name 1543f085823Sopenharmony_ci 1553f085823Sopenharmony_ci 1563f085823Sopenharmony_cidef parse_product_info(product_form): 1573f085823Sopenharmony_ci build_prop = os.path.join(sys.source_code_root_path, 1583f085823Sopenharmony_ci "out", 1593f085823Sopenharmony_ci "preloader", 1603f085823Sopenharmony_ci product_form, 1613f085823Sopenharmony_ci "build.prop") 1623f085823Sopenharmony_ci if not os.path.exists(build_prop): 1633f085823Sopenharmony_ci return {} 1643f085823Sopenharmony_ci 1653f085823Sopenharmony_ci with open(build_prop, 'r') as pro_file: 1663f085823Sopenharmony_ci properties = {} 1673f085823Sopenharmony_ci for line in pro_file: 1683f085823Sopenharmony_ci if line.find('=') > 0: 1693f085823Sopenharmony_ci strs = line.replace('\n', '').split('=') 1703f085823Sopenharmony_ci properties[strs[0]] = strs[1] 1713f085823Sopenharmony_ci return properties 1723f085823Sopenharmony_ci 1733f085823Sopenharmony_ci 1743f085823Sopenharmony_cidef is_32_bit_test(): 1753f085823Sopenharmony_ci manager = UserConfigManager() 1763f085823Sopenharmony_ci para_dic = manager.get_user_config("build", "parameter") 1773f085823Sopenharmony_ci target_cpu = para_dic.get("target_cpu", "") 1783f085823Sopenharmony_ci if target_cpu == "arm": 1793f085823Sopenharmony_ci return True 1803f085823Sopenharmony_ci return False 1813f085823Sopenharmony_ci 1823f085823Sopenharmony_ci 1833f085823Sopenharmony_cidef get_decode(stream): 1843f085823Sopenharmony_ci if not isinstance(stream, str) and not isinstance(stream, bytes): 1853f085823Sopenharmony_ci ret = str(stream) 1863f085823Sopenharmony_ci else: 1873f085823Sopenharmony_ci try: 1883f085823Sopenharmony_ci ret = stream.decode("utf-8", errors="ignore") 1893f085823Sopenharmony_ci except (ValueError, AttributeError, TypeError): 1903f085823Sopenharmony_ci ret = str(stream) 1913f085823Sopenharmony_ci return ret 1923f085823Sopenharmony_ci 1933f085823Sopenharmony_ci 1943f085823Sopenharmony_cidef get_fuzzer_path(suite_file): 1953f085823Sopenharmony_ci filename = os.path.basename(suite_file) 1963f085823Sopenharmony_ci suitename = filename.split("FuzzTest")[0] 1973f085823Sopenharmony_ci current_dir = os.path.dirname(suite_file) 1983f085823Sopenharmony_ci while True: 1993f085823Sopenharmony_ci if os.path.exists(os.path.join(current_dir, "tests")): 2003f085823Sopenharmony_ci res_path = os.path.join(os.path.join(current_dir, "tests"), "res") 2013f085823Sopenharmony_ci break 2023f085823Sopenharmony_ci current_dir = os.path.dirname(current_dir) 2033f085823Sopenharmony_ci fuzzer_path = os.path.join(res_path, "%s_fuzzer" % suitename.lower()) 2043f085823Sopenharmony_ci return fuzzer_path 2053f085823Sopenharmony_ci 2063f085823Sopenharmony_ci 2073f085823Sopenharmony_cidef is_lite_product(product_form, code_root_path): 2083f085823Sopenharmony_ci if code_root_path is None or code_root_path == "": 2093f085823Sopenharmony_ci return True if len(product_form.split("_")) >= 3 else False 2103f085823Sopenharmony_ci else: 2113f085823Sopenharmony_ci product_list = FrameworkConfigManager().get_framework_config("productform") 2123f085823Sopenharmony_ci if (product_form in scan_support_product() or product_form in product_list) \ 2133f085823Sopenharmony_ci and product_form.find("wifiiot") == -1: 2143f085823Sopenharmony_ci return False 2153f085823Sopenharmony_ci else: 2163f085823Sopenharmony_ci return True 217