15f9996aaSopenharmony_ci#!/usr/bin/env python
25f9996aaSopenharmony_ci# -*- coding: utf-8 -*-
35f9996aaSopenharmony_ci# Copyright (c) 2024 Huawei Device Co., Ltd.
45f9996aaSopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License");
55f9996aaSopenharmony_ci# you may not use this file except in compliance with the License.
65f9996aaSopenharmony_ci# You may obtain a copy of the License at
75f9996aaSopenharmony_ci#
85f9996aaSopenharmony_ci#     http://www.apache.org/licenses/LICENSE-2.0
95f9996aaSopenharmony_ci#
105f9996aaSopenharmony_ci# Unless required by applicable law or agreed to in writing, software
115f9996aaSopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS,
125f9996aaSopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
135f9996aaSopenharmony_ci# See the License for the specific language governing permissions and
145f9996aaSopenharmony_ci# limitations under the License.
155f9996aaSopenharmony_ciimport subprocess
165f9996aaSopenharmony_ciimport sys
175f9996aaSopenharmony_ciimport stat
185f9996aaSopenharmony_ciimport os
195f9996aaSopenharmony_ciimport argparse
205f9996aaSopenharmony_ciimport shutil
215f9996aaSopenharmony_ciimport json
225f9996aaSopenharmony_ciimport time
235f9996aaSopenharmony_ciimport re
245f9996aaSopenharmony_ciimport urllib.request
255f9996aaSopenharmony_ci
265f9996aaSopenharmony_ci
275f9996aaSopenharmony_cidef _get_args():
285f9996aaSopenharmony_ci    parser = argparse.ArgumentParser(add_help=True)
295f9996aaSopenharmony_ci    parser.add_argument("-op", "--out_path", default=r"./", type=str,
305f9996aaSopenharmony_ci                        help="path of out.", )
315f9996aaSopenharmony_ci    parser.add_argument("-rp", "--root_path", default=r"./", type=str,
325f9996aaSopenharmony_ci                        help="path of root. default: ./", )
335f9996aaSopenharmony_ci    parser.add_argument("-cl", "--components_list", default="", type=str,
345f9996aaSopenharmony_ci                        help="components_list , "
355f9996aaSopenharmony_ci                             "pass in the components' name, separated by commas , "
365f9996aaSopenharmony_ci                             "example: A,B,C . "
375f9996aaSopenharmony_ci                             "default: none", )
385f9996aaSopenharmony_ci    parser.add_argument("-bt", "--build_type", default=0, type=int,
395f9996aaSopenharmony_ci                        help="build_type ,default: 0", )
405f9996aaSopenharmony_ci    parser.add_argument("-on", "--organization_name", default='ohos', type=str,
415f9996aaSopenharmony_ci                        help="organization_name ,default: '' ", )
425f9996aaSopenharmony_ci    parser.add_argument("-os", "--os_arg", default=r"linux", type=str,
435f9996aaSopenharmony_ci                        help="path of output file. default: linux", )
445f9996aaSopenharmony_ci    parser.add_argument("-ba", "--build_arch", default=r"x86", type=str,
455f9996aaSopenharmony_ci                        help="build_arch_arg. default: x86", )
465f9996aaSopenharmony_ci    parser.add_argument("-lt", "--local_test", default=0, type=int,
475f9996aaSopenharmony_ci                        help="local test ,default: not local , 0", )
485f9996aaSopenharmony_ci    args = parser.parse_args()
495f9996aaSopenharmony_ci    return args
505f9996aaSopenharmony_ci
515f9996aaSopenharmony_ci
525f9996aaSopenharmony_cidef _check_label(public_deps, value):
535f9996aaSopenharmony_ci    innerapis = value["innerapis"]
545f9996aaSopenharmony_ci    for _innerapi in innerapis:
555f9996aaSopenharmony_ci        if _innerapi:
565f9996aaSopenharmony_ci            label = _innerapi.get("label")
575f9996aaSopenharmony_ci            if public_deps == label:
585f9996aaSopenharmony_ci                return label.split(':')[-1]
595f9996aaSopenharmony_ci            continue
605f9996aaSopenharmony_ci    return ""
615f9996aaSopenharmony_ci
625f9996aaSopenharmony_ci
635f9996aaSopenharmony_cidef _get_public_external_deps(data, public_deps):
645f9996aaSopenharmony_ci    if not isinstance(data, dict):
655f9996aaSopenharmony_ci        return ""
665f9996aaSopenharmony_ci    for key, value in data.items():
675f9996aaSopenharmony_ci        if not isinstance(value, dict):
685f9996aaSopenharmony_ci            continue
695f9996aaSopenharmony_ci        _data = _check_label(public_deps, value)
705f9996aaSopenharmony_ci        if _data:
715f9996aaSopenharmony_ci            return f"{key}:{_data}"
725f9996aaSopenharmony_ci        continue
735f9996aaSopenharmony_ci    return ""
745f9996aaSopenharmony_ci
755f9996aaSopenharmony_ci
765f9996aaSopenharmony_cidef _is_innerkit(data, part, module):
775f9996aaSopenharmony_ci    if not isinstance(data, dict):
785f9996aaSopenharmony_ci        return False
795f9996aaSopenharmony_ci
805f9996aaSopenharmony_ci    part_data = data.get(part)
815f9996aaSopenharmony_ci    if not isinstance(part_data, dict):
825f9996aaSopenharmony_ci        return False
835f9996aaSopenharmony_ci    module_list = []
845f9996aaSopenharmony_ci    for i in part_data["innerapis"]:
855f9996aaSopenharmony_ci        if i:
865f9996aaSopenharmony_ci            module_list.append(i["name"])
875f9996aaSopenharmony_ci    if module in module_list:
885f9996aaSopenharmony_ci        return True
895f9996aaSopenharmony_ci    return False
905f9996aaSopenharmony_ci
915f9996aaSopenharmony_ci
925f9996aaSopenharmony_cidef _get_components_json(out_path):
935f9996aaSopenharmony_ci    jsondata = ""
945f9996aaSopenharmony_ci    json_path = os.path.join(out_path + "/build_configs/parts_info/components.json")
955f9996aaSopenharmony_ci    with os.fdopen(os.open(json_path, os.O_RDWR | os.O_CREAT, stat.S_IWUSR | stat.S_IRUSR),
965f9996aaSopenharmony_ci                   'r', encoding='utf-8') as f:
975f9996aaSopenharmony_ci        try:
985f9996aaSopenharmony_ci            jsondata = json.load(f)
995f9996aaSopenharmony_ci        except Exception as e:
1005f9996aaSopenharmony_ci            print('--_get_components_json parse json error--')
1015f9996aaSopenharmony_ci    return jsondata
1025f9996aaSopenharmony_ci
1035f9996aaSopenharmony_ci
1045f9996aaSopenharmony_cidef _handle_one_layer_json(json_key, json_data, desc_list):
1055f9996aaSopenharmony_ci    data_list = json_data.get(json_key)
1065f9996aaSopenharmony_ci    if isinstance(data_list, list) and len(json_data.get(json_key)) >= 1:
1075f9996aaSopenharmony_ci        desc_list.extend(data_list)
1085f9996aaSopenharmony_ci    else:
1095f9996aaSopenharmony_ci        desc_list.append(json_data.get(json_key))
1105f9996aaSopenharmony_ci
1115f9996aaSopenharmony_ci
1125f9996aaSopenharmony_cidef _handle_two_layer_json(json_key, json_data, desc_list):
1135f9996aaSopenharmony_ci    value_depth = len(json_data.get(json_key))
1145f9996aaSopenharmony_ci    for i in range(value_depth):
1155f9996aaSopenharmony_ci        _include_dirs = json_data.get(json_key)[i].get('include_dirs')
1165f9996aaSopenharmony_ci        if _include_dirs:
1175f9996aaSopenharmony_ci            desc_list.extend(_include_dirs)
1185f9996aaSopenharmony_ci
1195f9996aaSopenharmony_ci
1205f9996aaSopenharmony_cidef _get_json_data(args, module):
1215f9996aaSopenharmony_ci    json_path = os.path.join(args.get("out_path"),
1225f9996aaSopenharmony_ci                             args.get("subsystem_name"), args.get("part_name"), "publicinfo", module + ".json")
1235f9996aaSopenharmony_ci    with os.fdopen(os.open(json_path, os.O_RDWR | os.O_CREAT, stat.S_IWUSR | stat.S_IRUSR),
1245f9996aaSopenharmony_ci                   'r', encoding='utf-8') as f:
1255f9996aaSopenharmony_ci        try:
1265f9996aaSopenharmony_ci            jsondata = json.load(f)
1275f9996aaSopenharmony_ci        except Exception as e:
1285f9996aaSopenharmony_ci            print(json_path)
1295f9996aaSopenharmony_ci            print('--_get_json_data parse json error--')
1305f9996aaSopenharmony_ci    return jsondata
1315f9996aaSopenharmony_ci
1325f9996aaSopenharmony_ci
1335f9996aaSopenharmony_cidef _handle_deps_data(json_data):
1345f9996aaSopenharmony_ci    dep_list = []
1355f9996aaSopenharmony_ci    if json_data.get('public_deps'):
1365f9996aaSopenharmony_ci        _handle_one_layer_json('public_deps', json_data, dep_list)
1375f9996aaSopenharmony_ci    return dep_list
1385f9996aaSopenharmony_ci
1395f9996aaSopenharmony_ci
1405f9996aaSopenharmony_cidef _handle_includes_data(json_data):
1415f9996aaSopenharmony_ci    include_list = []
1425f9996aaSopenharmony_ci    if json_data.get('public_configs'):
1435f9996aaSopenharmony_ci        _handle_two_layer_json('public_configs', json_data, include_list)
1445f9996aaSopenharmony_ci    if json_data.get('all_dependent_configs'):
1455f9996aaSopenharmony_ci        _handle_two_layer_json('all_dependent_configs', json_data, include_list)
1465f9996aaSopenharmony_ci    return include_list
1475f9996aaSopenharmony_ci
1485f9996aaSopenharmony_ci
1495f9996aaSopenharmony_cidef _get_static_lib_path(args, json_data):
1505f9996aaSopenharmony_ci    label = json_data.get('label')
1515f9996aaSopenharmony_ci    split_label = label.split("//")[1].split(":")[0]
1525f9996aaSopenharmony_ci    real_static_lib_path = os.path.join(args.get("out_path"), "obj",
1535f9996aaSopenharmony_ci                                        split_label, json_data.get('out_name'))
1545f9996aaSopenharmony_ci    return real_static_lib_path
1555f9996aaSopenharmony_ci
1565f9996aaSopenharmony_ci
1575f9996aaSopenharmony_cidef _copy_dir(src_path, target_path):
1585f9996aaSopenharmony_ci    if not os.path.isdir(src_path):
1595f9996aaSopenharmony_ci        return False
1605f9996aaSopenharmony_ci    filelist_src = os.listdir(src_path)
1615f9996aaSopenharmony_ci    suffix_list = [".h", ".hpp", ".in", ".inc", ".inl"]
1625f9996aaSopenharmony_ci    for file in filelist_src:
1635f9996aaSopenharmony_ci        path = os.path.join(os.path.abspath(src_path), file)
1645f9996aaSopenharmony_ci        if os.path.isdir(path):
1655f9996aaSopenharmony_ci            if file.startswith("."):
1665f9996aaSopenharmony_ci                continue
1675f9996aaSopenharmony_ci            path1 = os.path.join(target_path, file)
1685f9996aaSopenharmony_ci            _copy_dir(path, path1)
1695f9996aaSopenharmony_ci        else:
1705f9996aaSopenharmony_ci            if not (os.path.splitext(path)[-1] in suffix_list):
1715f9996aaSopenharmony_ci                continue
1725f9996aaSopenharmony_ci            with open(path, 'rb') as read_stream:
1735f9996aaSopenharmony_ci                contents = read_stream.read()
1745f9996aaSopenharmony_ci            if not os.path.exists(target_path):
1755f9996aaSopenharmony_ci                os.makedirs(target_path)
1765f9996aaSopenharmony_ci            path1 = os.path.join(target_path, file)
1775f9996aaSopenharmony_ci            with os.fdopen(os.open(path1, os.O_WRONLY | os.O_CREAT, mode=0o640), "wb") as write_stream:
1785f9996aaSopenharmony_ci                write_stream.write(contents)
1795f9996aaSopenharmony_ci    return True
1805f9996aaSopenharmony_ci
1815f9996aaSopenharmony_ci
1825f9996aaSopenharmony_cidef _copy_includes(args, module, includes: list):
1835f9996aaSopenharmony_ci    if module == 'ipc_single':
1845f9996aaSopenharmony_ci        includes = [
1855f9996aaSopenharmony_ci            "//foundation/communication/ipc/interfaces/innerkits/ipc_core/include",
1865f9996aaSopenharmony_ci            "//foundation/communication/ipc/ipc/native/src/core/include",
1875f9996aaSopenharmony_ci            "//foundation/communication/ipc/ipc/native/src/mock/include",
1885f9996aaSopenharmony_ci        ]
1895f9996aaSopenharmony_ci    includes_out_dir = os.path.join(args.get("out_path"), "component_package",
1905f9996aaSopenharmony_ci                                    args.get("part_path"), "innerapis", module, "includes")
1915f9996aaSopenharmony_ci    for i in args.get("toolchain_info").keys():
1925f9996aaSopenharmony_ci        toolchain_includes_out_dir = os.path.join(args.get("out_path"), "component_package",
1935f9996aaSopenharmony_ci                                                  args.get("part_path"), "innerapis", module, i, "includes")
1945f9996aaSopenharmony_ci        toolchain_lib_out_dir = os.path.join(args.get("out_path"), "component_package",
1955f9996aaSopenharmony_ci                                             args.get("part_path"), "innerapis", module, i, "libs")
1965f9996aaSopenharmony_ci        if not os.path.exists(toolchain_includes_out_dir) and os.path.exists(toolchain_lib_out_dir):
1975f9996aaSopenharmony_ci            os.makedirs(toolchain_includes_out_dir)
1985f9996aaSopenharmony_ci        else:
1995f9996aaSopenharmony_ci            continue
2005f9996aaSopenharmony_ci        for include in includes:
2015f9996aaSopenharmony_ci            part_path = args.get("part_path")
2025f9996aaSopenharmony_ci            _sub_include = include.split(f"{part_path}/")[-1]
2035f9996aaSopenharmony_ci            split_include = include.split("//")[1]
2045f9996aaSopenharmony_ci            real_include_path = os.path.join(args.get("root_path"), split_include)
2055f9996aaSopenharmony_ci            if args.get('part_name') == 'libunwind':
2065f9996aaSopenharmony_ci                _out_dir = os.path.join(toolchain_includes_out_dir, _sub_include)
2075f9996aaSopenharmony_ci                _copy_dir(real_include_path, _out_dir)
2085f9996aaSopenharmony_ci                continue
2095f9996aaSopenharmony_ci            _copy_dir(real_include_path, toolchain_includes_out_dir)
2105f9996aaSopenharmony_ci    if not os.path.exists(includes_out_dir):
2115f9996aaSopenharmony_ci        os.makedirs(includes_out_dir)
2125f9996aaSopenharmony_ci    for include in includes:
2135f9996aaSopenharmony_ci        part_path = args.get("part_path")
2145f9996aaSopenharmony_ci        _sub_include = include.split(f"{part_path}/")[-1]
2155f9996aaSopenharmony_ci        split_include = include.split("//")[1]
2165f9996aaSopenharmony_ci        real_include_path = os.path.join(args.get("root_path"), split_include)
2175f9996aaSopenharmony_ci        if args.get('part_name') == 'libunwind':
2185f9996aaSopenharmony_ci            _out_dir = os.path.join(includes_out_dir, _sub_include)
2195f9996aaSopenharmony_ci            _copy_dir(real_include_path, _out_dir)
2205f9996aaSopenharmony_ci            continue
2215f9996aaSopenharmony_ci        _copy_dir(real_include_path, includes_out_dir)
2225f9996aaSopenharmony_ci    print("_copy_includes has done ")
2235f9996aaSopenharmony_ci
2245f9996aaSopenharmony_ci
2255f9996aaSopenharmony_cidef _copy_toolchain_lib(file_name, root, _name, lib_out_dir):
2265f9996aaSopenharmony_ci    if not file_name.startswith('.') and file_name.startswith(_name):
2275f9996aaSopenharmony_ci        if not os.path.exists(lib_out_dir):
2285f9996aaSopenharmony_ci            os.makedirs(lib_out_dir)
2295f9996aaSopenharmony_ci        file = os.path.join(root, file_name)
2305f9996aaSopenharmony_ci        shutil.copy(file, lib_out_dir)
2315f9996aaSopenharmony_ci
2325f9996aaSopenharmony_ci
2335f9996aaSopenharmony_cidef _toolchain_lib_handler(args, toolchain_path, _name, module, toolchain_name):
2345f9996aaSopenharmony_ci    for root, dirs, files in os.walk(toolchain_path):
2355f9996aaSopenharmony_ci        for file_name in files:
2365f9996aaSopenharmony_ci            lib_out_dir = os.path.join(args.get("out_path"), "component_package",
2375f9996aaSopenharmony_ci                                       args.get("part_path"), "innerapis", module, toolchain_name, "libs")
2385f9996aaSopenharmony_ci            _copy_toolchain_lib(file_name, root, _name, lib_out_dir)
2395f9996aaSopenharmony_ci
2405f9996aaSopenharmony_ci
2415f9996aaSopenharmony_cidef _toolchain_static_file_path_mapping(subsystem_name, args, i):
2425f9996aaSopenharmony_ci    if subsystem_name == "thirdparty":
2435f9996aaSopenharmony_ci        subsystem_name = "third_party"
2445f9996aaSopenharmony_ci    toolchain_path = os.path.join(args.get("out_path"), i, 'obj', subsystem_name,
2455f9996aaSopenharmony_ci                                  args.get("part_name"))
2465f9996aaSopenharmony_ci    return toolchain_path
2475f9996aaSopenharmony_ci
2485f9996aaSopenharmony_ci
2495f9996aaSopenharmony_cidef _copy_lib(args, json_data, module):
2505f9996aaSopenharmony_ci    so_path = ""
2515f9996aaSopenharmony_ci    lib_status = False
2525f9996aaSopenharmony_ci    subsystem_name = args.get("subsystem_name")
2535f9996aaSopenharmony_ci    if json_data.get('type') == 'static_library':
2545f9996aaSopenharmony_ci        so_path = _get_static_lib_path(args, json_data)
2555f9996aaSopenharmony_ci    elif json_data.get('type') == 'shared_library':
2565f9996aaSopenharmony_ci        so_path = os.path.join(args.get("out_path"), subsystem_name,
2575f9996aaSopenharmony_ci                               args.get("part_name"), json_data.get('out_name'))
2585f9996aaSopenharmony_ci    elif json_data.get('type') == 'copy' and module == 'ipc_core':
2595f9996aaSopenharmony_ci        so_path = os.path.join(args.get("out_path"), subsystem_name,
2605f9996aaSopenharmony_ci                               args.get("part_name"), 'libipc_single.z.so')
2615f9996aaSopenharmony_ci    if args.get("toolchain_info").keys():
2625f9996aaSopenharmony_ci        for i in args.get("toolchain_info").keys():
2635f9996aaSopenharmony_ci            so_type = ''
2645f9996aaSopenharmony_ci            toolchain_path = os.path.join(args.get("out_path"), i, subsystem_name,
2655f9996aaSopenharmony_ci                                          args.get("part_name"))
2665f9996aaSopenharmony_ci            _name = json_data.get('out_name').split('.')[0]
2675f9996aaSopenharmony_ci            if json_data.get('type') == 'static_library':
2685f9996aaSopenharmony_ci                _name = json_data.get('out_name')
2695f9996aaSopenharmony_ci                toolchain_path = _toolchain_static_file_path_mapping(subsystem_name, args, i)
2705f9996aaSopenharmony_ci            _toolchain_lib_handler(args, toolchain_path, _name, module, i)
2715f9996aaSopenharmony_ci            lib_status = lib_status or True
2725f9996aaSopenharmony_ci    if os.path.isfile(so_path):
2735f9996aaSopenharmony_ci        lib_out_dir = os.path.join(args.get("out_path"), "component_package",
2745f9996aaSopenharmony_ci                                   args.get("part_path"), "innerapis", module, "libs")
2755f9996aaSopenharmony_ci        if not os.path.exists(lib_out_dir):
2765f9996aaSopenharmony_ci            os.makedirs(lib_out_dir)
2775f9996aaSopenharmony_ci        shutil.copy(so_path, lib_out_dir)
2785f9996aaSopenharmony_ci        lib_status = lib_status or True
2795f9996aaSopenharmony_ci    return lib_status
2805f9996aaSopenharmony_ci
2815f9996aaSopenharmony_ci
2825f9996aaSopenharmony_cidef _dirs_handler(bundlejson_out):
2835f9996aaSopenharmony_ci    dirs = dict()
2845f9996aaSopenharmony_ci    dirs['./'] = []
2855f9996aaSopenharmony_ci    directory = bundlejson_out
2865f9996aaSopenharmony_ci    for filename in os.listdir(directory):
2875f9996aaSopenharmony_ci        filepath = os.path.join(directory, filename)
2885f9996aaSopenharmony_ci        if os.path.isfile(filepath):
2895f9996aaSopenharmony_ci            dirs['./'].append(filename)
2905f9996aaSopenharmony_ci        else:
2915f9996aaSopenharmony_ci            dirs[filename] = [f"{filename}/*"]
2925f9996aaSopenharmony_ci    delete_list = ['LICENSE', 'README.md', 'README_zh.md', 'README_en.md', 'bundle.json']
2935f9996aaSopenharmony_ci    for delete_txt in delete_list:
2945f9996aaSopenharmony_ci        if delete_txt in dirs['./']:
2955f9996aaSopenharmony_ci            dirs['./'].remove(delete_txt)
2965f9996aaSopenharmony_ci    if dirs['./'] == []:
2975f9996aaSopenharmony_ci        del dirs['./']
2985f9996aaSopenharmony_ci    return dirs
2995f9996aaSopenharmony_ci
3005f9996aaSopenharmony_ci
3015f9996aaSopenharmony_cidef _copy_bundlejson(args, public_deps_list):
3025f9996aaSopenharmony_ci    bundlejson_out = os.path.join(args.get("out_path"), "component_package", args.get("part_path"))
3035f9996aaSopenharmony_ci    print("bundlejson_out : ", bundlejson_out)
3045f9996aaSopenharmony_ci    if not os.path.exists(bundlejson_out):
3055f9996aaSopenharmony_ci        os.makedirs(bundlejson_out)
3065f9996aaSopenharmony_ci    bundlejson = os.path.join(args.get("root_path"), args.get("part_path"), "bundle.json")
3075f9996aaSopenharmony_ci    dependencies_dict = {}
3085f9996aaSopenharmony_ci    for public_deps in public_deps_list:
3095f9996aaSopenharmony_ci        _public_dep_part_name = public_deps.split(':')[0]
3105f9996aaSopenharmony_ci        if _public_dep_part_name != args.get("part_name"):
3115f9996aaSopenharmony_ci            _public_dep = f"@{args.get('organization_name')}/{_public_dep_part_name}"
3125f9996aaSopenharmony_ci            dependencies_dict.update({_public_dep: "*"})
3135f9996aaSopenharmony_ci    if os.path.isfile(bundlejson):
3145f9996aaSopenharmony_ci        with open(bundlejson, 'r') as f:
3155f9996aaSopenharmony_ci            bundle_data = json.load(f)
3165f9996aaSopenharmony_ci            bundle_data['publishAs'] = 'binary'
3175f9996aaSopenharmony_ci            bundle_data.update({'os': args.get('os')})
3185f9996aaSopenharmony_ci            bundle_data.update({'buildArch': args.get('buildArch')})
3195f9996aaSopenharmony_ci            dirs = _dirs_handler(bundlejson_out)
3205f9996aaSopenharmony_ci            bundle_data['dirs'] = dirs
3215f9996aaSopenharmony_ci            bundle_data['version'] = str(bundle_data['version'])
3225f9996aaSopenharmony_ci            if bundle_data['version'] == '':
3235f9996aaSopenharmony_ci                bundle_data['version'] = '1.0.0'
3245f9996aaSopenharmony_ci            pattern = r'^(\d+)\.(\d+)(-[a-zA-Z]+)?$'  # 正则表达式匹配a.b[-后缀]格式的字符串
3255f9996aaSopenharmony_ci            match = re.match(pattern, bundle_data['version'])
3265f9996aaSopenharmony_ci            if match:
3275f9996aaSopenharmony_ci                a = match.group(1)
3285f9996aaSopenharmony_ci                b = match.group(2)
3295f9996aaSopenharmony_ci                suffix = match.group(3) if match.group(3) else ""
3305f9996aaSopenharmony_ci                bundle_data['version'] = f"{a}.{b}.0{suffix}"
3315f9996aaSopenharmony_ci            if args.get('build_type') in [0, 1]:
3325f9996aaSopenharmony_ci                bundle_data['version'] += '-snapshot'
3335f9996aaSopenharmony_ci            if args.get('organization_name'):
3345f9996aaSopenharmony_ci                _name_pattern = r'@(.*.)/'
3355f9996aaSopenharmony_ci                bundle_data['name'] = re.sub(_name_pattern, '@' + args.get('organization_name') + '/',
3365f9996aaSopenharmony_ci                                             bundle_data['name'])
3375f9996aaSopenharmony_ci            if bundle_data.get('scripts'):
3385f9996aaSopenharmony_ci                bundle_data.update({'scripts': {}})
3395f9996aaSopenharmony_ci            if bundle_data.get('licensePath'):
3405f9996aaSopenharmony_ci                del bundle_data['licensePath']
3415f9996aaSopenharmony_ci            if bundle_data.get('readmePath'):
3425f9996aaSopenharmony_ci                del bundle_data['readmePath']
3435f9996aaSopenharmony_ci            bundle_data['dependencies'] = dependencies_dict
3445f9996aaSopenharmony_ci            if os.path.isfile(os.path.join(bundlejson_out, "bundle.json")):
3455f9996aaSopenharmony_ci                os.remove(os.path.join(bundlejson_out, "bundle.json"))
3465f9996aaSopenharmony_ci            with os.fdopen(os.open(os.path.join(bundlejson_out, "bundle.json"), os.O_WRONLY | os.O_CREAT, mode=0o640),
3475f9996aaSopenharmony_ci                           "w",
3485f9996aaSopenharmony_ci                           encoding='utf-8') as fd:
3495f9996aaSopenharmony_ci                json.dump(bundle_data, fd, indent=4, ensure_ascii=False)
3505f9996aaSopenharmony_ci
3515f9996aaSopenharmony_ci
3525f9996aaSopenharmony_cidef _copy_license(args):
3535f9996aaSopenharmony_ci    license_out = os.path.join(args.get("out_path"), "component_package", args.get("part_path"))
3545f9996aaSopenharmony_ci    print("license_out : ", license_out)
3555f9996aaSopenharmony_ci    if not os.path.exists(license_out):
3565f9996aaSopenharmony_ci        os.makedirs(license_out)
3575f9996aaSopenharmony_ci    license_file = os.path.join(args.get("root_path"), args.get("part_path"), "LICENSE")
3585f9996aaSopenharmony_ci    if os.path.isfile(license_file):
3595f9996aaSopenharmony_ci        shutil.copy(license_file, license_out)
3605f9996aaSopenharmony_ci    else:
3615f9996aaSopenharmony_ci        license_default = os.path.join(args.get("root_path"), "build", "LICENSE")
3625f9996aaSopenharmony_ci        shutil.copy(license_default, license_out)
3635f9996aaSopenharmony_ci        bundlejson_out = os.path.join(args.get("out_path"), "component_package", args.get("part_path"), 'bundle.json')
3645f9996aaSopenharmony_ci        with open(bundlejson_out, 'r') as f:
3655f9996aaSopenharmony_ci            bundle_data = json.load(f)
3665f9996aaSopenharmony_ci            bundle_data.update({"license": "Apache License 2.0"})
3675f9996aaSopenharmony_ci        if os.path.isfile(bundlejson_out):
3685f9996aaSopenharmony_ci            os.remove(bundlejson_out)
3695f9996aaSopenharmony_ci        with os.fdopen(os.open(bundlejson_out, os.O_WRONLY | os.O_CREAT, mode=0o640), "w",
3705f9996aaSopenharmony_ci                       encoding='utf-8') as fd:
3715f9996aaSopenharmony_ci            json.dump(bundle_data, fd, indent=4, ensure_ascii=False)
3725f9996aaSopenharmony_ci
3735f9996aaSopenharmony_ci
3745f9996aaSopenharmony_cidef _copy_readme(args):
3755f9996aaSopenharmony_ci    readme_out = os.path.join(args.get("out_path"), "component_package", args.get("part_path"))
3765f9996aaSopenharmony_ci    print("readme_out : ", readme_out)
3775f9996aaSopenharmony_ci    if not os.path.exists(readme_out):
3785f9996aaSopenharmony_ci        os.makedirs(readme_out)
3795f9996aaSopenharmony_ci    readme = os.path.join(args.get("root_path"), args.get("part_path"), "README.md")
3805f9996aaSopenharmony_ci    readme_zh = os.path.join(args.get("root_path"), args.get("part_path"), "README_zh.md")
3815f9996aaSopenharmony_ci    readme_en = os.path.join(args.get("root_path"), args.get("part_path"), "README_en.md")
3825f9996aaSopenharmony_ci    readme_out_file = os.path.join(readme_out, "README.md")
3835f9996aaSopenharmony_ci    if os.path.isfile(readme):
3845f9996aaSopenharmony_ci        shutil.copy(readme, readme_out)
3855f9996aaSopenharmony_ci    elif os.path.isfile(readme_zh):
3865f9996aaSopenharmony_ci        shutil.copy(readme_zh, readme_out_file)
3875f9996aaSopenharmony_ci    elif os.path.isfile(readme_en):
3885f9996aaSopenharmony_ci        shutil.copy(readme_en, readme_out_file)
3895f9996aaSopenharmony_ci    else:
3905f9996aaSopenharmony_ci        try:
3915f9996aaSopenharmony_ci            with os.fdopen(os.open(readme_out_file, os.O_WRONLY | os.O_CREAT, mode=0o640), 'w') as fp:
3925f9996aaSopenharmony_ci                fp.write('READ.ME')
3935f9996aaSopenharmony_ci        except FileExistsError:
3945f9996aaSopenharmony_ci            pass
3955f9996aaSopenharmony_ci
3965f9996aaSopenharmony_ci
3975f9996aaSopenharmony_cidef _generate_import(fp):
3985f9996aaSopenharmony_ci    fp.write('import("//build/ohos.gni")\n')
3995f9996aaSopenharmony_ci
4005f9996aaSopenharmony_ci
4015f9996aaSopenharmony_cidef _generate_configs(fp, module):
4025f9996aaSopenharmony_ci    fp.write('\nconfig("' + module + '_configs") {\n')
4035f9996aaSopenharmony_ci    fp.write('  visibility = [ ":*" ]\n')
4045f9996aaSopenharmony_ci    fp.write('  include_dirs = [\n')
4055f9996aaSopenharmony_ci    fp.write('    "includes",\n')
4065f9996aaSopenharmony_ci    if module == 'libunwind':
4075f9996aaSopenharmony_ci        fp.write('    "includes/libunwind/src",\n')
4085f9996aaSopenharmony_ci        fp.write('    "includes/libunwind/include",\n')
4095f9996aaSopenharmony_ci        fp.write('    "includes/libunwind/include/tdep-arm",\n')
4105f9996aaSopenharmony_ci    if module == 'ability_runtime':
4115f9996aaSopenharmony_ci        fp.write('    "includes/context",\n')
4125f9996aaSopenharmony_ci        fp.write('    "includes/app",\n')
4135f9996aaSopenharmony_ci    fp.write('  ]\n')
4145f9996aaSopenharmony_ci    if module == 'libunwind':
4155f9996aaSopenharmony_ci        fp.write('  cflags = [\n')
4165f9996aaSopenharmony_ci        fp.write("""    "-D_GNU_SOURCE",
4175f9996aaSopenharmony_ci    "-DHAVE_CONFIG_H",
4185f9996aaSopenharmony_ci    "-DNDEBUG",
4195f9996aaSopenharmony_ci    "-DCC_IS_CLANG",
4205f9996aaSopenharmony_ci    "-fcommon",
4215f9996aaSopenharmony_ci    "-Werror",
4225f9996aaSopenharmony_ci    "-Wno-absolute-value",
4235f9996aaSopenharmony_ci    "-Wno-header-guard",
4245f9996aaSopenharmony_ci    "-Wno-unused-parameter",
4255f9996aaSopenharmony_ci    "-Wno-unused-variable",
4265f9996aaSopenharmony_ci    "-Wno-int-to-pointer-cast",
4275f9996aaSopenharmony_ci    "-Wno-pointer-to-int-cast",
4285f9996aaSopenharmony_ci    "-Wno-inline-asm",
4295f9996aaSopenharmony_ci    "-Wno-shift-count-overflow",
4305f9996aaSopenharmony_ci    "-Wno-tautological-constant-out-of-range-compare",
4315f9996aaSopenharmony_ci    "-Wno-unused-function",\n""")
4325f9996aaSopenharmony_ci        fp.write('  ]\n')
4335f9996aaSopenharmony_ci    fp.write('  }\n')
4345f9996aaSopenharmony_ci
4355f9996aaSopenharmony_ci
4365f9996aaSopenharmony_cidef _generate_prebuilt_shared_library(fp, lib_type, module):
4375f9996aaSopenharmony_ci    if lib_type == 'static_library':
4385f9996aaSopenharmony_ci        fp.write('ohos_prebuilt_static_library("' + module + '") {\n')
4395f9996aaSopenharmony_ci    elif lib_type == 'executable':
4405f9996aaSopenharmony_ci        fp.write('ohos_prebuilt_executable("' + module + '") {\n')
4415f9996aaSopenharmony_ci    elif lib_type == 'etc':
4425f9996aaSopenharmony_ci        fp.write('ohos_prebuilt_etc("' + module + '") {\n')
4435f9996aaSopenharmony_ci    else:
4445f9996aaSopenharmony_ci        fp.write('ohos_prebuilt_shared_library("' + module + '") {\n')
4455f9996aaSopenharmony_ci
4465f9996aaSopenharmony_ci
4475f9996aaSopenharmony_cidef _generate_public_configs(fp, module):
4485f9996aaSopenharmony_ci    fp.write(f'  public_configs = [":{module}_configs"]\n')
4495f9996aaSopenharmony_ci
4505f9996aaSopenharmony_ci
4515f9996aaSopenharmony_cidef _public_deps_special_handler(module):
4525f9996aaSopenharmony_ci    if module == 'appexecfwk_core':
4535f9996aaSopenharmony_ci        return ["ability_base:want"]
4545f9996aaSopenharmony_ci    return []
4555f9996aaSopenharmony_ci
4565f9996aaSopenharmony_ci
4575f9996aaSopenharmony_cidef _generate_public_deps(fp, module, deps: list, components_json, public_deps_list: list):
4585f9996aaSopenharmony_ci    if not deps:
4595f9996aaSopenharmony_ci        return public_deps_list
4605f9996aaSopenharmony_ci    fp.write('  public_external_deps = [\n')
4615f9996aaSopenharmony_ci    for dep in deps:
4625f9996aaSopenharmony_ci        public_external_deps = _get_public_external_deps(components_json, dep)
4635f9996aaSopenharmony_ci        if len(public_external_deps) > 0:
4645f9996aaSopenharmony_ci            fp.write(f"""    "{public_external_deps}",\n""")
4655f9996aaSopenharmony_ci            public_deps_list.append(public_external_deps)
4665f9996aaSopenharmony_ci    for _public_external_deps in _public_deps_special_handler(module):
4675f9996aaSopenharmony_ci        fp.write(f"""    "{_public_external_deps}",\n""")
4685f9996aaSopenharmony_ci        public_deps_list.append(_public_external_deps)
4695f9996aaSopenharmony_ci    fp.write('  ]\n')
4705f9996aaSopenharmony_ci
4715f9996aaSopenharmony_ci    return public_deps_list
4725f9996aaSopenharmony_ci
4735f9996aaSopenharmony_ci
4745f9996aaSopenharmony_cidef _generate_other(fp, args, json_data, module):
4755f9996aaSopenharmony_ci    so_name = json_data.get('out_name')
4765f9996aaSopenharmony_ci    if json_data.get('type') == 'copy' and module == 'ipc_core':
4775f9996aaSopenharmony_ci        so_name = 'libipc_single.z.so'
4785f9996aaSopenharmony_ci    fp.write('  source = "libs/' + so_name + '"\n')
4795f9996aaSopenharmony_ci    fp.write('  part_name = "' + args.get("part_name") + '"\n')
4805f9996aaSopenharmony_ci    fp.write('  subsystem_name = "' + args.get("subsystem_name") + '"\n')
4815f9996aaSopenharmony_ci
4825f9996aaSopenharmony_ci
4835f9996aaSopenharmony_cidef _generate_end(fp):
4845f9996aaSopenharmony_ci    fp.write('}')
4855f9996aaSopenharmony_ci
4865f9996aaSopenharmony_ci
4875f9996aaSopenharmony_cidef _generate_build_gn(args, module, json_data, deps: list, components_json, public_deps_list):
4885f9996aaSopenharmony_ci    gn_path = os.path.join(args.get("out_path"), "component_package", args.get("part_path"),
4895f9996aaSopenharmony_ci                           "innerapis", module, "BUILD.gn")
4905f9996aaSopenharmony_ci    fd = os.open(gn_path, os.O_WRONLY | os.O_CREAT, mode=0o640)
4915f9996aaSopenharmony_ci    fp = os.fdopen(fd, 'w')
4925f9996aaSopenharmony_ci    _generate_import(fp)
4935f9996aaSopenharmony_ci    _generate_configs(fp, module)
4945f9996aaSopenharmony_ci    _generate_prebuilt_shared_library(fp, json_data.get('type'), module)
4955f9996aaSopenharmony_ci    _generate_public_configs(fp, module)
4965f9996aaSopenharmony_ci    _list = _generate_public_deps(fp, module, deps, components_json, public_deps_list)
4975f9996aaSopenharmony_ci    _generate_other(fp, args, json_data, module)
4985f9996aaSopenharmony_ci    _generate_end(fp)
4995f9996aaSopenharmony_ci    print("_generate_build_gn has done ")
5005f9996aaSopenharmony_ci    fp.close()
5015f9996aaSopenharmony_ci    return _list
5025f9996aaSopenharmony_ci
5035f9996aaSopenharmony_ci
5045f9996aaSopenharmony_cidef _toolchain_gn_modify(gn_path, file_name, toolchain_gn_file):
5055f9996aaSopenharmony_ci    if os.path.isfile(gn_path) and file_name:
5065f9996aaSopenharmony_ci        with open(gn_path, 'r') as f:
5075f9996aaSopenharmony_ci            _gn = f.read()
5085f9996aaSopenharmony_ci            pattern = r"libs/(.*.)"
5095f9996aaSopenharmony_ci            toolchain_gn = re.sub(pattern, 'libs/' + file_name + '\"', _gn)
5105f9996aaSopenharmony_ci        fd = os.open(toolchain_gn_file, os.O_WRONLY | os.O_CREAT, mode=0o640)
5115f9996aaSopenharmony_ci        fp = os.fdopen(fd, 'w')
5125f9996aaSopenharmony_ci        fp.write(toolchain_gn)
5135f9996aaSopenharmony_ci        fp.close()
5145f9996aaSopenharmony_ci
5155f9996aaSopenharmony_ci
5165f9996aaSopenharmony_cidef _get_toolchain_gn_file(lib_out_dir):
5175f9996aaSopenharmony_ci    file_name = ''
5185f9996aaSopenharmony_ci    try:
5195f9996aaSopenharmony_ci        file_list = os.scandir(lib_out_dir)
5205f9996aaSopenharmony_ci    except FileNotFoundError:
5215f9996aaSopenharmony_ci        return file_name
5225f9996aaSopenharmony_ci    for file in file_list:
5235f9996aaSopenharmony_ci        if not file.name.startswith('.') and file.is_file():
5245f9996aaSopenharmony_ci            file_name = file.name
5255f9996aaSopenharmony_ci    return file_name
5265f9996aaSopenharmony_ci
5275f9996aaSopenharmony_ci
5285f9996aaSopenharmony_cidef _toolchain_gn_copy(args, module):
5295f9996aaSopenharmony_ci    gn_path = os.path.join(args.get("out_path"), "component_package", args.get("part_path"),
5305f9996aaSopenharmony_ci                           "innerapis", module, "BUILD.gn")
5315f9996aaSopenharmony_ci    for i in args.get("toolchain_info").keys():
5325f9996aaSopenharmony_ci        lib_out_dir = os.path.join(args.get("out_path"), "component_package",
5335f9996aaSopenharmony_ci                                   args.get("part_path"), "innerapis", module, i, "libs")
5345f9996aaSopenharmony_ci        file_name = _get_toolchain_gn_file(lib_out_dir)
5355f9996aaSopenharmony_ci        if not file_name:
5365f9996aaSopenharmony_ci            continue
5375f9996aaSopenharmony_ci        toolchain_gn_file = os.path.join(args.get("out_path"), "component_package",
5385f9996aaSopenharmony_ci                                         args.get("part_path"), "innerapis", module, i, "BUILD.gn")
5395f9996aaSopenharmony_ci        if not os.path.exists(toolchain_gn_file):
5405f9996aaSopenharmony_ci            os.mknod(toolchain_gn_file)
5415f9996aaSopenharmony_ci        _toolchain_gn_modify(gn_path, file_name, toolchain_gn_file)
5425f9996aaSopenharmony_ci
5435f9996aaSopenharmony_ci
5445f9996aaSopenharmony_cidef _parse_module_list(args):
5455f9996aaSopenharmony_ci    module_list = []
5465f9996aaSopenharmony_ci    publicinfo_path = os.path.join(args.get("out_path"),
5475f9996aaSopenharmony_ci                                   args.get("subsystem_name"), args.get("part_name"), "publicinfo")
5485f9996aaSopenharmony_ci    print('publicinfo_path', publicinfo_path)
5495f9996aaSopenharmony_ci    if os.path.exists(publicinfo_path) is False:
5505f9996aaSopenharmony_ci        return module_list
5515f9996aaSopenharmony_ci    publicinfo_dir = os.listdir(publicinfo_path)
5525f9996aaSopenharmony_ci    for filename in publicinfo_dir:
5535f9996aaSopenharmony_ci        if filename.endswith(".json"):
5545f9996aaSopenharmony_ci            module_name = filename.split(".json")[0]
5555f9996aaSopenharmony_ci            module_list.append(module_name)
5565f9996aaSopenharmony_ci            print('filename', filename)
5575f9996aaSopenharmony_ci    print('module_list', module_list)
5585f9996aaSopenharmony_ci    return module_list
5595f9996aaSopenharmony_ci
5605f9996aaSopenharmony_ci
5615f9996aaSopenharmony_cidef _lib_special_handler(part_name, module, args):
5625f9996aaSopenharmony_ci    if part_name == 'mksh':
5635f9996aaSopenharmony_ci        mksh_file_path = os.path.join(args.get('out_path'), 'startup', 'init', 'sh')
5645f9996aaSopenharmony_ci        sh_out = os.path.join(args.get("out_path"), "thirdparty", "mksh")
5655f9996aaSopenharmony_ci        if os.path.isfile(mksh_file_path):
5665f9996aaSopenharmony_ci            shutil.copy(mksh_file_path, sh_out)
5675f9996aaSopenharmony_ci    if module == 'blkid':
5685f9996aaSopenharmony_ci        blkid_file_path = os.path.join(args.get('out_path'), 'filemanagement', 'storage_service', 'blkid')
5695f9996aaSopenharmony_ci        blkid_out = os.path.join(args.get("out_path"), "thirdparty", "e2fsprogs")
5705f9996aaSopenharmony_ci        if os.path.isfile(blkid_file_path):
5715f9996aaSopenharmony_ci            shutil.copy(blkid_file_path, blkid_out)
5725f9996aaSopenharmony_ci    if module == 'grpc_cpp_plugin':
5735f9996aaSopenharmony_ci        blkid_file_path = os.path.join(args.get('out_path'), 'clang_x64', 'thirdparty', 'grpc', 'grpc_cpp_plugin')
5745f9996aaSopenharmony_ci        blkid_out = os.path.join(args.get("out_path"), "thirdparty", "grpc")
5755f9996aaSopenharmony_ci        if os.path.isfile(blkid_file_path):
5765f9996aaSopenharmony_ci            shutil.copy(blkid_file_path, blkid_out)
5775f9996aaSopenharmony_ci
5785f9996aaSopenharmony_ci
5795f9996aaSopenharmony_cidef _generate_component_package(args, components_json):
5805f9996aaSopenharmony_ci    part_name = args.get("part_name")
5815f9996aaSopenharmony_ci    modules = _parse_module_list(args)
5825f9996aaSopenharmony_ci    print('modules', modules)
5835f9996aaSopenharmony_ci    if len(modules) == 0:
5845f9996aaSopenharmony_ci        return
5855f9996aaSopenharmony_ci    is_component_build = False
5865f9996aaSopenharmony_ci    _public_deps_list = []
5875f9996aaSopenharmony_ci    for module in modules:
5885f9996aaSopenharmony_ci        public_deps_list = []
5895f9996aaSopenharmony_ci        if _is_innerkit(components_json, args.get("part_name"), module) == False:
5905f9996aaSopenharmony_ci            continue
5915f9996aaSopenharmony_ci        json_data = _get_json_data(args, module)
5925f9996aaSopenharmony_ci        _lib_special_handler(part_name, module, args)
5935f9996aaSopenharmony_ci        lib_exists = _copy_lib(args, json_data, module)
5945f9996aaSopenharmony_ci        if lib_exists is False:
5955f9996aaSopenharmony_ci            continue
5965f9996aaSopenharmony_ci        is_component_build = True
5975f9996aaSopenharmony_ci        includes = _handle_includes_data(json_data)
5985f9996aaSopenharmony_ci        deps = _handle_deps_data(json_data)
5995f9996aaSopenharmony_ci        _copy_includes(args, module, includes)
6005f9996aaSopenharmony_ci        _list = _generate_build_gn(args, module, json_data, deps, components_json, public_deps_list)
6015f9996aaSopenharmony_ci        if _list:
6025f9996aaSopenharmony_ci            _public_deps_list.extend(_list)
6035f9996aaSopenharmony_ci        _toolchain_gn_copy(args, module)
6045f9996aaSopenharmony_ci    if is_component_build:
6055f9996aaSopenharmony_ci        _copy_bundlejson(args, _public_deps_list)
6065f9996aaSopenharmony_ci        _copy_license(args)
6075f9996aaSopenharmony_ci        _copy_readme(args)
6085f9996aaSopenharmony_ci        if args.get("build_type") in [0, 1]:
6095f9996aaSopenharmony_ci            _hpm_status = _hpm_pack(args)
6105f9996aaSopenharmony_ci            if _hpm_status:
6115f9996aaSopenharmony_ci                _copy_hpm_pack(args)
6125f9996aaSopenharmony_ci
6135f9996aaSopenharmony_ci
6145f9996aaSopenharmony_cidef _get_part_subsystem(components_json: dict):
6155f9996aaSopenharmony_ci    jsondata = dict()
6165f9996aaSopenharmony_ci    try:
6175f9996aaSopenharmony_ci        for component, v in components_json.items():
6185f9996aaSopenharmony_ci            jsondata[component] = v.get('subsystem')
6195f9996aaSopenharmony_ci    except Exception as e:
6205f9996aaSopenharmony_ci        print('--_get_part_subsystem parse json error--')
6215f9996aaSopenharmony_ci    return jsondata
6225f9996aaSopenharmony_ci
6235f9996aaSopenharmony_ci
6245f9996aaSopenharmony_cidef _get_parts_path_info(components_json):
6255f9996aaSopenharmony_ci    jsondata = dict()
6265f9996aaSopenharmony_ci    try:
6275f9996aaSopenharmony_ci        for component, v in components_json.items():
6285f9996aaSopenharmony_ci            jsondata[component] = v.get('path')
6295f9996aaSopenharmony_ci    except Exception as e:
6305f9996aaSopenharmony_ci        print('--_get_part_subsystem parse json error--')
6315f9996aaSopenharmony_ci    return jsondata
6325f9996aaSopenharmony_ci
6335f9996aaSopenharmony_ci
6345f9996aaSopenharmony_cidef _get_toolchain_info(root_path):
6355f9996aaSopenharmony_ci    jsondata = ""
6365f9996aaSopenharmony_ci    json_path = os.path.join(root_path + "/build/indep_configs/variants/common/toolchain.json")
6375f9996aaSopenharmony_ci    with os.fdopen(os.open(json_path, os.O_RDWR | os.O_CREAT, stat.S_IWUSR | stat.S_IRUSR),
6385f9996aaSopenharmony_ci                   'r', encoding='utf-8') as f:
6395f9996aaSopenharmony_ci        try:
6405f9996aaSopenharmony_ci            jsondata = json.load(f)
6415f9996aaSopenharmony_ci        except Exception as e:
6425f9996aaSopenharmony_ci            print('--_get_toolchain_info parse json error--')
6435f9996aaSopenharmony_ci    return jsondata
6445f9996aaSopenharmony_ci
6455f9996aaSopenharmony_ci
6465f9996aaSopenharmony_cidef _get_parts_path(json_data, part_name):
6475f9996aaSopenharmony_ci    parts_path = None
6485f9996aaSopenharmony_ci    if json_data.get(part_name) is not None:
6495f9996aaSopenharmony_ci        parts_path = json_data[part_name]
6505f9996aaSopenharmony_ci    return parts_path
6515f9996aaSopenharmony_ci
6525f9996aaSopenharmony_ci
6535f9996aaSopenharmony_cidef _hpm_pack(args):
6545f9996aaSopenharmony_ci    part_path = os.path.join(args.get("out_path"), "component_package", args.get("part_path"))
6555f9996aaSopenharmony_ci    cmd = ['hpm', 'pack']
6565f9996aaSopenharmony_ci    try:
6575f9996aaSopenharmony_ci        subprocess.run(cmd, shell=False, cwd=part_path)
6585f9996aaSopenharmony_ci    except Exception as e:
6595f9996aaSopenharmony_ci        print("{} pack fail".format(args.get("part_name")))
6605f9996aaSopenharmony_ci        return 0
6615f9996aaSopenharmony_ci    print("{} pack succ".format(args.get("part_name")))
6625f9996aaSopenharmony_ci    return 1
6635f9996aaSopenharmony_ci
6645f9996aaSopenharmony_ci
6655f9996aaSopenharmony_cidef _copy_hpm_pack(args):
6665f9996aaSopenharmony_ci    hpm_packages_path = args.get('hpm_packages_path')
6675f9996aaSopenharmony_ci    part_path = os.path.join(args.get("out_path"), "component_package", args.get("part_path"))
6685f9996aaSopenharmony_ci    dirs = os.listdir(part_path)
6695f9996aaSopenharmony_ci    tgz_file_name = ''
6705f9996aaSopenharmony_ci    for file in dirs:
6715f9996aaSopenharmony_ci        if file.endswith(".tgz"):
6725f9996aaSopenharmony_ci            tgz_file_name = file
6735f9996aaSopenharmony_ci    tgz_file_out = os.path.join(part_path, tgz_file_name)
6745f9996aaSopenharmony_ci    if tgz_file_name:
6755f9996aaSopenharmony_ci        shutil.copy(tgz_file_out, hpm_packages_path)
6765f9996aaSopenharmony_ci
6775f9996aaSopenharmony_ci
6785f9996aaSopenharmony_cidef _make_hpm_packages_dir(root_path):
6795f9996aaSopenharmony_ci    _out_path = os.path.join(root_path, 'out')
6805f9996aaSopenharmony_ci    hpm_packages_path = os.path.join(_out_path, 'hpm_packages')
6815f9996aaSopenharmony_ci    os.makedirs(hpm_packages_path, exist_ok=True)
6825f9996aaSopenharmony_ci    return hpm_packages_path
6835f9996aaSopenharmony_ci
6845f9996aaSopenharmony_ci
6855f9996aaSopenharmony_cidef _del_exist_component_package(out_path):
6865f9996aaSopenharmony_ci    _component_package_path = os.path.join(out_path, 'component_package')
6875f9996aaSopenharmony_ci    if os.path.isdir(_component_package_path):
6885f9996aaSopenharmony_ci        try:
6895f9996aaSopenharmony_ci            print('del dir component_package start..')
6905f9996aaSopenharmony_ci            shutil.rmtree(_component_package_path)
6915f9996aaSopenharmony_ci            print('del dir component_package end..')
6925f9996aaSopenharmony_ci        except Exception as e:
6935f9996aaSopenharmony_ci            print('del dir component_package FAILED')
6945f9996aaSopenharmony_ci
6955f9996aaSopenharmony_ci
6965f9996aaSopenharmony_cidef _get_component_check(local_test) -> list:
6975f9996aaSopenharmony_ci    check_list = []
6985f9996aaSopenharmony_ci    if local_test == 0:
6995f9996aaSopenharmony_ci        contents = urllib.request.urlopen(
7005f9996aaSopenharmony_ci            "https://ci.openharmony.cn/api/daily_build/component/check/list").read().decode(
7015f9996aaSopenharmony_ci            encoding="utf-8")
7025f9996aaSopenharmony_ci        _check_json = json.loads(contents)
7035f9996aaSopenharmony_ci        try:
7045f9996aaSopenharmony_ci            check_list.extend(_check_json["data"]["dep_list"])
7055f9996aaSopenharmony_ci            check_list.extend(_check_json["data"]["indep_list"])
7065f9996aaSopenharmony_ci        except Exception as e:
7075f9996aaSopenharmony_ci            print("Call the component check API something wrong, plz check the API return..")
7085f9996aaSopenharmony_ci    check_list = list(set(check_list))
7095f9996aaSopenharmony_ci    check_list = sorted(check_list)
7105f9996aaSopenharmony_ci    return check_list
7115f9996aaSopenharmony_ci
7125f9996aaSopenharmony_ci
7135f9996aaSopenharmony_cidef _package_interface(args, parts_path_info, part_name, subsystem_name, components_json):
7145f9996aaSopenharmony_ci    part_path = _get_parts_path(parts_path_info, part_name)
7155f9996aaSopenharmony_ci    if part_path is None:
7165f9996aaSopenharmony_ci        return
7175f9996aaSopenharmony_ci    args.update({"subsystem_name": subsystem_name, "part_name": part_name,
7185f9996aaSopenharmony_ci                 "part_path": part_path})
7195f9996aaSopenharmony_ci    _generate_component_package(args, components_json)
7205f9996aaSopenharmony_ci
7215f9996aaSopenharmony_ci
7225f9996aaSopenharmony_cidef generate_component_package(out_path, root_path, components_list=None, build_type=0, organization_name='ohos',
7235f9996aaSopenharmony_ci                               os_arg='linux', build_arch_arg='x86', local_test=0):
7245f9996aaSopenharmony_ci    """
7255f9996aaSopenharmony_ci
7265f9996aaSopenharmony_ci    Args:
7275f9996aaSopenharmony_ci        out_path: output path of code default : out/rk3568
7285f9996aaSopenharmony_ci        root_path: root path of code default : oh/
7295f9996aaSopenharmony_ci        components_list: list of all components that need to be built
7305f9996aaSopenharmony_ci        build_type: build type
7315f9996aaSopenharmony_ci            0: default pack,do not change organization_name
7325f9996aaSopenharmony_ci            1: pack ,change organization_name
7335f9996aaSopenharmony_ci            2: do not pack,do not change organization_name
7345f9996aaSopenharmony_ci        organization_name: default ohos, if diff then change
7355f9996aaSopenharmony_ci        os_arg: default : linux
7365f9996aaSopenharmony_ci        build_arch_arg:  default : x86
7375f9996aaSopenharmony_ci        local_test: 1 to open local test , 0 to close , 2 to pack init and init deps
7385f9996aaSopenharmony_ci    Returns:
7395f9996aaSopenharmony_ci
7405f9996aaSopenharmony_ci    """
7415f9996aaSopenharmony_ci    start_time = time.time()
7425f9996aaSopenharmony_ci    _check_list = _get_component_check(local_test)
7435f9996aaSopenharmony_ci    if local_test == 1 and not components_list:
7445f9996aaSopenharmony_ci        components_list = []
7455f9996aaSopenharmony_ci    elif local_test == 1 and components_list:
7465f9996aaSopenharmony_ci        components_list = [component for component in components_list.split(",")]
7475f9996aaSopenharmony_ci    elif local_test == 2:
7485f9996aaSopenharmony_ci        components_list = ["init", "appspawn", "safwk", "c_utils",
7495f9996aaSopenharmony_ci                           "napi", "ipc", "config_policy", "hilog", "hilog_lite", "samgr", "access_token", "common",
7505f9996aaSopenharmony_ci                           "dsoftbus", "hvb", "hisysevent", "hiprofiler", "bounds_checking_function",
7515f9996aaSopenharmony_ci                           "bundle_framework", "selinux", "selinux_adapter", "storage_service",
7525f9996aaSopenharmony_ci                           "mbedtls", "zlib", "libuv", "cJSON", "mksh", "libunwind", "toybox",
7535f9996aaSopenharmony_ci                           "bounds_checking_function",
7545f9996aaSopenharmony_ci                           "selinux", "libunwind", "mbedtls", "zlib", "cJSON", "mksh", "toybox", "config_policy",
7555f9996aaSopenharmony_ci                           "e2fsprogs", "f2fs-tools", "selinux_adapter", "storage_service"
7565f9996aaSopenharmony_ci                           ]
7575f9996aaSopenharmony_ci    elif components_list:
7585f9996aaSopenharmony_ci        components_list = [component for component in components_list.split(",") if component in _check_list]
7595f9996aaSopenharmony_ci        if not components_list:
7605f9996aaSopenharmony_ci            sys.exit("stop for no target to pack..")
7615f9996aaSopenharmony_ci    else:
7625f9996aaSopenharmony_ci        components_list = _check_list
7635f9996aaSopenharmony_ci        if not components_list:
7645f9996aaSopenharmony_ci            sys.exit("stop for no target to pack..")
7655f9996aaSopenharmony_ci    print('components_list', components_list)
7665f9996aaSopenharmony_ci    components_json = _get_components_json(out_path)
7675f9996aaSopenharmony_ci    part_subsystem = _get_part_subsystem(components_json)
7685f9996aaSopenharmony_ci    parts_path_info = _get_parts_path_info(components_json)
7695f9996aaSopenharmony_ci    hpm_packages_path = _make_hpm_packages_dir(root_path)
7705f9996aaSopenharmony_ci    toolchain_info = _get_toolchain_info(root_path)
7715f9996aaSopenharmony_ci    # del component_package
7725f9996aaSopenharmony_ci    _del_exist_component_package(out_path)
7735f9996aaSopenharmony_ci    args = {"out_path": out_path, "root_path": root_path,
7745f9996aaSopenharmony_ci            "os": os_arg, "buildArch": build_arch_arg, "hpm_packages_path": hpm_packages_path,
7755f9996aaSopenharmony_ci            "build_type": build_type, "organization_name": organization_name,
7765f9996aaSopenharmony_ci            "toolchain_info": toolchain_info
7775f9996aaSopenharmony_ci            }
7785f9996aaSopenharmony_ci    for key, value in part_subsystem.items():
7795f9996aaSopenharmony_ci        part_name = key
7805f9996aaSopenharmony_ci        subsystem_name = value
7815f9996aaSopenharmony_ci        # components_list is NONE or part name in components_list
7825f9996aaSopenharmony_ci        if not components_list:
7835f9996aaSopenharmony_ci            _package_interface(args, parts_path_info, part_name, subsystem_name, components_json)
7845f9996aaSopenharmony_ci        for component in components_list:
7855f9996aaSopenharmony_ci            if part_name == component:
7865f9996aaSopenharmony_ci                _package_interface(args, parts_path_info, part_name, subsystem_name, components_json)
7875f9996aaSopenharmony_ci
7885f9996aaSopenharmony_ci    end_time = time.time()
7895f9996aaSopenharmony_ci    run_time = end_time - start_time
7905f9996aaSopenharmony_ci    print("generate_component_package out_path", out_path)
7915f9996aaSopenharmony_ci    print(f"Generating binary product package takes time:{run_time}")
7925f9996aaSopenharmony_ci
7935f9996aaSopenharmony_ci
7945f9996aaSopenharmony_cidef main():
7955f9996aaSopenharmony_ci    py_args = _get_args()
7965f9996aaSopenharmony_ci    generate_component_package(py_args.out_path,
7975f9996aaSopenharmony_ci                               py_args.root_path,
7985f9996aaSopenharmony_ci                               components_list=py_args.components_list,
7995f9996aaSopenharmony_ci                               build_type=py_args.build_type,
8005f9996aaSopenharmony_ci                               organization_name=py_args.organization_name,
8015f9996aaSopenharmony_ci                               os_arg=py_args.os_arg,
8025f9996aaSopenharmony_ci                               build_arch_arg=py_args.build_arch,
8035f9996aaSopenharmony_ci                               local_test=py_args.local_test)
8045f9996aaSopenharmony_ci
8055f9996aaSopenharmony_ci
8065f9996aaSopenharmony_ciif __name__ == '__main__':
8075f9996aaSopenharmony_ci    main()
808