utils.py 6.3 KB
Newer Older
W
wenjun 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#!/usr/bin/env python3
# coding=utf-8

#
# Copyright (c) 2020 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.
#

M
mamingshuai 已提交
19
import sys
W
wenjun 已提交
20
import os
M
mamingshuai 已提交
21 22
import time
from core.config.config_manager import UserConfigManager
W
wenjun 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60


def get_filename_extension(file):
    _, fullname = os.path.split(file)
    filename, ext = os.path.splitext(fullname)
    return filename, ext


def create_dir(path):
    full_path = os.path.abspath(os.path.expanduser(path))
    if not os.path.exists(full_path):
        os.makedirs(full_path, exist_ok=True)


def get_file_list(find_path, postfix=""):
    file_names = os.listdir(find_path)
    file_list = []
    if len(file_names) > 0:
        for file_name in file_names:
            if postfix != "":
                if file_name.find(postfix) != -1 \
                        and file_name[-len(postfix):] == postfix:
                    file_list.append(file_name)
            else:
                file_list.append(file_name)
    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)
        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
M
mamingshuai 已提交
61 62 63 64 65 66 67 68 69 70 71 72


def get_device_log_file(report_path, serial=None, log_name="device_log"):
    log_path = os.path.join(report_path, "log")
    os.makedirs(log_path, exist_ok=True)

    serial = serial or time.time_ns()
    device_file_name = "{}_{}.log".format(log_name, serial)
    device_log_file = os.path.join(log_path, device_file_name)
    return device_log_file


M
mamingshuai 已提交
73
def get_build_output_path(product_form):
M
mamingshuai 已提交
74 75
    if sys.source_code_root_path == "":
        return ""
M
mamingshuai 已提交
76 77 78 79 80 81 82 83 84
    standard_large_system_list = scan_support_product()
    property_info = parse_product_info(product_form)
    if product_form in standard_large_system_list:
        if property_info is not None:
            target_os = property_info.get("target_os")
            target_cpu = property_info.get("target_cpu")
            build_output_name = "%s-%s-%s" % (target_os, target_cpu, "release")
        else:
            return ""
85
    else:
M
mamingshuai 已提交
86
        para_dic = UserConfigManager().get_user_config("build", "board_info")
87 88 89
        board_series = para_dic.get("board_series", "")
        board_type = para_dic.get("board_type", "")
        board_product = para_dic.get("board_product", "")
M
mamingshuai 已提交
90 91 92
        fist_build_output = "%s_%s" % (board_series, board_type)
        second_build_output = "%s_%s" % (board_product, fist_build_output)
        build_output_name = os.path.join(fist_build_output,
H
hwb0703 已提交
93
                                         second_build_output)
M
mamingshuai 已提交
94 95 96
    build_output_path = os.path.join(sys.source_code_root_path,
                                     "out",
                                     build_output_name)
M
mamingshuai 已提交
97 98 99
    return build_output_path


M
mamingshuai 已提交
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
def scan_support_product():
    # scan standard and large system info
    product_form_dir = os.path.join(sys.source_code_root_path,
                                    "productdefine",
                                    "common",
                                    "products")
    productform_list = []
    if os.path.exists(product_form_dir):
        for product_form_file in os.listdir(product_form_dir):
            if os.path.isdir(os.path.join(product_form_dir,
                                          product_form_file)):
                continue
            product_file = os.path.basename(product_form_file)
            if product_file.endswith(".build") or "parts" in product_file or \
                    "x86_64" in product_file or "32" in product_file:
                continue
            product_name, _ = os.path.splitext(product_file)
            productform_list.append(product_name)
    return productform_list


def parse_product_info(product_form):
    build_prop = os.path.join(sys.source_code_root_path,
                              "out",
                              "build_configs",
                              product_form,
                              "preloader",
                              "build.prop")
    with open(build_prop, 'r') as pro_file:
        properties = {}
        for line in pro_file:
            if line.find('=') > 0:
                strs = line.replace('\n', '').split('=')
                properties[strs[0]] = strs[1]
    return properties


M
mamingshuai 已提交
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
def is_32_bit_test():
    manager = UserConfigManager()
    para_dic = manager.get_user_config("build", "paramter")
    target_cpu = para_dic.get("target_cpu", "")
    if target_cpu == "arm":
        return True
    return False


def get_decode(stream):
    if not isinstance(stream, str) and not isinstance(stream, bytes):
        ret = str(stream)
    else:
        try:
            ret = stream.decode("utf-8", errors="ignore")
        except (ValueError, AttributeError, TypeError):
            ret = str(stream)
    return ret
R
renxiang 已提交
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173

def parse_fuzzer_info():
    path_list = []
    bin_list = []
    list_path = os.path.join(sys.source_code_root_path, "test",
        "developertest", "libs", "fuzzlib", "fuzzer_list.txt")
    with open(list_path, 'r') as list_file:
        for line in list_file.readlines():
            striped_str = line.strip()
            path_list.append(striped_str.split(":")[0][3:])
            bin_list.append(striped_str.split(":")[1].split("(")[0])
    return path_list, bin_list

def get_fuzzer_path(filename):
    path_list, bin_list = parse_fuzzer_info()
    for i, name in enumerate(bin_list):
        if name == filename:
            return os.path.join(sys.source_code_root_path, path_list[i])
    return ""