• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# coding=utf-8
3
4#
5# Copyright (c) 2020 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
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
53def get_file_list_by_postfix(path, postfix=""):
54    file_list = []
55    for dirs in os.walk(path):
56        files = get_file_list(find_path=dirs[0], postfix=postfix)
57        for file_name in files:
58            if "" != file_name and -1 == file_name.find(__file__):
59                file_name = os.path.join(dirs[0], file_name)
60                if os.path.isfile(file_name):
61                    file_list.append(file_name)
62    return file_list
63
64
65def get_device_log_file(report_path, serial=None, log_name="device_log"):
66    log_path = os.path.join(report_path, "log")
67    os.makedirs(log_path, exist_ok=True)
68
69    serial = serial or time.time_ns()
70    device_file_name = "{}_{}.log".format(log_name, serial)
71    device_log_file = os.path.join(log_path, device_file_name)
72    return device_log_file
73
74
75def get_build_output_path(product_form):
76    if sys.source_code_root_path == "":
77        return ""
78
79    standard_large_system_list = scan_support_product()
80    if product_form in standard_large_system_list:
81        device_name = parse_device_name(product_form)
82        if device_name is not None:
83            build_output_name = device_name
84        else:
85            return ""
86    else:
87        board_info_list = product_form.split("_")
88        if len(board_info_list) < 3:
89            return ""
90
91        first_build_output = board_info_list[1] + "_" + board_info_list[2]
92        second_build_output = product_form
93        build_output_name = os.path.join(first_build_output,
94                                         second_build_output)
95
96    build_output_path = os.path.join(sys.source_code_root_path,
97                                     "out",
98                                     build_output_name)
99    return build_output_path
100
101
102def scan_support_product():
103    # scan standard and large system info
104    product_form_dir = os.path.join(sys.source_code_root_path,
105                                    "productdefine",
106                                    "common",
107                                    "products")
108    productform_list = []
109    if os.path.exists(product_form_dir):
110        for product_form_file in os.listdir(product_form_dir):
111            if os.path.isdir(os.path.join(product_form_dir,
112                                          product_form_file)):
113                continue
114            product_file = os.path.basename(product_form_file)
115            if product_file.endswith(".build") or "parts" in product_file or \
116                    "x86_64" in product_file or "32" in product_file:
117                continue
118            product_name, _ = os.path.splitext(product_file)
119            productform_list.append(product_name)
120    return productform_list
121
122
123def parse_device_name(product_form):
124    device_json_file = os.path.join(sys.source_code_root_path,
125                                    "productdefine", "common", "products",
126                                    "{}.json".format(product_form))
127    if not os.path.exists(device_json_file):
128        return None
129
130    with open(device_json_file, 'r') as json_file:
131        json_info = json.load(json_file)
132    if not json_info:
133        return None
134    device_name = json_info.get('product_device')
135    return device_name
136
137
138def parse_product_info(product_form):
139    build_prop = os.path.join(sys.source_code_root_path,
140                              "out",
141                              "preloader",
142                              product_form,
143                              "build.prop")
144    if not os.path.exists(build_prop):
145        return {}
146
147    with open(build_prop, 'r') as pro_file:
148        properties = {}
149        for line in pro_file:
150            if line.find('=') > 0:
151                strs = line.replace('\n', '').split('=')
152                properties[strs[0]] = strs[1]
153    return properties
154
155
156def is_32_bit_test():
157    manager = UserConfigManager()
158    para_dic = manager.get_user_config("build", "parameter")
159    target_cpu = para_dic.get("target_cpu", "")
160    if target_cpu == "arm":
161        return True
162    return False
163
164
165def get_decode(stream):
166    if not isinstance(stream, str) and not isinstance(stream, bytes):
167        ret = str(stream)
168    else:
169        try:
170            ret = stream.decode("utf-8", errors="ignore")
171        except (ValueError, AttributeError, TypeError):
172            ret = str(stream)
173    return ret
174
175
176def parse_fuzzer_info():
177    path_list = []
178    bin_list = []
179    list_path = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(
180        os.path.realpath(__file__)))), "libs", "fuzzlib", "fuzzer_list.txt")
181    with open(list_path, 'r') as list_file:
182        for line in list_file.readlines():
183            striped_str = line.strip()
184            if platform.system() == "Windows":
185                path_list.append(striped_str.split(" ")[0])
186                bin_list.append(striped_str.split(" ")[1])
187            else:
188                path_list.append(striped_str.split(":")[0][3:])
189                bin_list.append(striped_str.split(":")[1].split("(")[0])
190    return path_list, bin_list
191
192
193def get_fuzzer_path(filename):
194    path_list, bin_list = parse_fuzzer_info()
195    for i, name in enumerate(bin_list):
196        if name == filename:
197            return os.path.join(sys.source_code_root_path, path_list[i])
198    return ""
199
200
201def is_lite_product(product_form, code_root_path):
202    if code_root_path is None or code_root_path == "":
203        return True if len(product_form.split("_")) >= 3 else False
204    else:
205        return True if product_form not in scan_support_product() else False
206