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