• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 get_output_path():
130    # 获取输出路径
131    ohos_config_path = os.path.join(sys.source_code_root_path, "ohos_config.json")
132    with open(ohos_config_path, 'r') as json_file:
133        json_info = json.load(json_file)
134        out_name = json_info.get("out_path").split("out")[1].strip("/")
135    return out_name
136
137
138def parse_device_name(product_form):
139    device_json_file = os.path.join(sys.source_code_root_path,
140                                    "productdefine", "common", "products",
141                                    "{}.json".format(product_form))
142    if not os.path.exists(device_json_file):
143        return
144
145    with open(device_json_file, 'r') as json_file:
146        json_info = json.load(json_file)
147    if not json_info:
148        return
149    device_name = json_info.get('product_device')
150    if not device_name:
151        device_name = get_output_path()
152    return device_name
153
154
155def parse_product_info(product_form):
156    build_prop = os.path.join(sys.source_code_root_path,
157                              "out",
158                              "preloader",
159                              product_form,
160                              "build.prop")
161    if not os.path.exists(build_prop):
162        return {}
163
164    with open(build_prop, 'r') as pro_file:
165        properties = {}
166        for line in pro_file:
167            if line.find('=') > 0:
168                strs = line.replace('\n', '').split('=')
169                properties[strs[0]] = strs[1]
170    return properties
171
172
173def is_32_bit_test():
174    manager = UserConfigManager()
175    para_dic = manager.get_user_config("build", "parameter")
176    target_cpu = para_dic.get("target_cpu", "")
177    if target_cpu == "arm":
178        return True
179    return False
180
181
182def get_decode(stream):
183    if not isinstance(stream, str) and not isinstance(stream, bytes):
184        ret = str(stream)
185    else:
186        try:
187            ret = stream.decode("utf-8", errors="ignore")
188        except (ValueError, AttributeError, TypeError):
189            ret = str(stream)
190    return ret
191
192
193def get_fuzzer_path(suite_file):
194    filename = os.path.basename(suite_file)
195    suitename = filename.split("FuzzTest")[0]
196    current_dir = os.path.dirname(suite_file)
197    while True:
198        if os.path.exists(os.path.join(current_dir, "tests")):
199            res_path = os.path.join(os.path.join(current_dir, "tests"), "res")
200            break
201        current_dir = os.path.dirname(current_dir)
202    fuzzer_path = os.path.join(res_path, "%s_fuzzer" % suitename.lower())
203    return fuzzer_path
204
205
206def is_lite_product(product_form, code_root_path):
207    if code_root_path is None or code_root_path == "":
208        return True if len(product_form.split("_")) >= 3 else False
209    else:
210        product_list = FrameworkConfigManager().get_framework_config("productform")
211        if (product_form in scan_support_product() or product_form in product_list) \
212                and product_form.find("wifiiot") == -1:
213            return False
214        else:
215            return True
216