1#!/usr/bin/env python3 2# coding=utf-8 3 4# 5# Copyright (c) 2022 Huawei Device Co., Ltd. 6# Licensed under the Apache License, Version 2.0 (the "License"); 7# you may not use this file except in compliance with the License. 8# You may obtain a copy of the License at 9# 10# http://www.apache.org/licenses/LICENSE-2.0 11# 12# Unless required by applicable law or agreed to in writing, software 13# distributed under the License is distributed on an "AS IS" BASIS, 14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15# See the License for the specific language governing permissions and 16# limitations under the License. 17# 18 19import sys 20import os 21import platform 22import time 23import json 24from core.config.config_manager import UserConfigManager, FrameworkConfigManager 25 26 27def get_filename_extension(file): 28 _, fullname = os.path.split(file) 29 filename, ext = os.path.splitext(fullname) 30 return filename, ext 31 32 33def create_dir(path): 34 full_path = os.path.abspath(os.path.expanduser(path)) 35 if not os.path.exists(full_path): 36 os.makedirs(full_path, exist_ok=True) 37 38 39def get_file_list(find_path, postfix=""): 40 file_names = os.listdir(find_path) 41 file_list = [] 42 if len(file_names) > 0: 43 for file_name in file_names: 44 if postfix != "": 45 if file_name.find(postfix) != -1 \ 46 and file_name[-len(postfix):] == postfix: 47 file_list.append(file_name) 48 else: 49 file_list.append(file_name) 50 return file_list 51 52 53# 获取目录下每一个文件,并放到一个列表里 54def get_file_list_by_postfix(path, postfix=""): 55 file_list = [] 56 for dirs in os.walk(path): 57 files = get_file_list(find_path=dirs[0], postfix=postfix) 58 for file_name in files: 59 if "" != file_name and -1 == file_name.find(__file__): 60 file_name = os.path.join(dirs[0], file_name) 61 if os.path.isfile(file_name): 62 file_list.append(file_name) 63 return file_list 64 65 66def get_device_log_file(report_path, serial=None, log_name="device_log"): 67 log_path = os.path.join(report_path, "log") 68 os.makedirs(log_path, exist_ok=True) 69 70 serial = serial or time.time_ns() 71 device_file_name = "{}_{}.log".format(log_name, serial) 72 device_log_file = os.path.join(log_path, device_file_name) 73 return device_log_file 74 75 76def get_build_output_path(product_form): 77 if sys.source_code_root_path == "": 78 return "" 79 80 standard_large_system_list = scan_support_product() 81 product_list = FrameworkConfigManager().get_framework_config("productform") 82 if product_form in standard_large_system_list: 83 device_name = parse_device_name(product_form) 84 if device_name is not None: 85 build_output_name = device_name 86 else: 87 return "" 88 elif product_form in product_list and (product_form not in standard_large_system_list): 89 build_output_name = product_form 90 else: 91 board_info_list = product_form.split("_") 92 if len(board_info_list) < 3: 93 return "" 94 95 first_build_output = board_info_list[1] + "_" + board_info_list[2] 96 second_build_output = product_form 97 build_output_name = os.path.join(first_build_output, 98 second_build_output) 99 100 build_output_path = os.path.join(sys.source_code_root_path, 101 "out", 102 build_output_name) 103 # 返回编译结果输出目录:~/OpenHarmony/out/rk3568(以rk3568举例) 104 return build_output_path 105 106 107def scan_support_product(): 108 # scan standard and large system info 109 # 路径注释 product_form_dir = OpenHarmony/productdefine/common/products/ 110 product_form_dir = os.path.join(sys.source_code_root_path, 111 "productdefine", 112 "common", 113 "products") 114 productform_list = [] 115 if os.path.exists(product_form_dir): 116 for product_form_file in os.listdir(product_form_dir): 117 if os.path.isdir(os.path.join(product_form_dir, 118 product_form_file)): 119 continue 120 product_file = os.path.basename(product_form_file) 121 if product_file.endswith(".build") or "parts" in product_file or \ 122 "x86_64" in product_file or "32" in product_file: 123 continue 124 product_name, _ = os.path.splitext(product_file) 125 productform_list.append(product_name) 126 return productform_list 127 128 129def parse_device_name(product_form): 130 device_json_file = os.path.join(sys.source_code_root_path, 131 "productdefine", "common", "products", 132 "{}.json".format(product_form)) 133 if not os.path.exists(device_json_file): 134 return 135 136 with open(device_json_file, 'r') as json_file: 137 json_info = json.load(json_file) 138 if not json_info: 139 return 140 device_name = json_info.get('product_device') 141 return device_name 142 143 144def parse_product_info(product_form): 145 build_prop = os.path.join(sys.source_code_root_path, 146 "out", 147 "preloader", 148 product_form, 149 "build.prop") 150 if not os.path.exists(build_prop): 151 return {} 152 153 with open(build_prop, 'r') as pro_file: 154 properties = {} 155 for line in pro_file: 156 if line.find('=') > 0: 157 strs = line.replace('\n', '').split('=') 158 properties[strs[0]] = strs[1] 159 return properties 160 161 162def is_32_bit_test(): 163 manager = UserConfigManager() 164 para_dic = manager.get_user_config("build", "parameter") 165 target_cpu = para_dic.get("target_cpu", "") 166 if target_cpu == "arm": 167 return True 168 return False 169 170 171def get_decode(stream): 172 if not isinstance(stream, str) and not isinstance(stream, bytes): 173 ret = str(stream) 174 else: 175 try: 176 ret = stream.decode("utf-8", errors="ignore") 177 except (ValueError, AttributeError, TypeError): 178 ret = str(stream) 179 return ret 180 181 182def get_fuzzer_path(suite_file): 183 filename = os.path.basename(suite_file) 184 suitename = filename.split("FuzzTest")[0] 185 current_dir = os.path.dirname(suite_file) 186 while True: 187 if os.path.exists(os.path.join(current_dir, "tests")): 188 res_path = os.path.join(os.path.join(current_dir, "tests"), "res") 189 break 190 current_dir = os.path.dirname(current_dir) 191 fuzzer_path = os.path.join(res_path, "%s_fuzzer" % suitename.lower()) 192 return fuzzer_path 193 194 195def is_lite_product(product_form, code_root_path): 196 if code_root_path is None or code_root_path == "": 197 return True if len(product_form.split("_")) >= 3 else False 198 else: 199 product_list = FrameworkConfigManager().get_framework_config("productform") 200 if (product_form in scan_support_product() or product_form in product_list) \ 201 and product_form.find("wifiiot") == -1: 202 return False 203 else: 204 return True 205