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_ci 165f9996aaSopenharmony_ciimport argparse 175f9996aaSopenharmony_ciimport json 185f9996aaSopenharmony_ciimport os 195f9996aaSopenharmony_ciimport time 205f9996aaSopenharmony_ciimport stat 215f9996aaSopenharmony_ciimport utils 225f9996aaSopenharmony_ci 235f9996aaSopenharmony_ci 245f9996aaSopenharmony_cidef _get_args(): 255f9996aaSopenharmony_ci parser = argparse.ArgumentParser(add_help=True) 265f9996aaSopenharmony_ci parser.add_argument( 275f9996aaSopenharmony_ci "-p", "--input_path", 285f9996aaSopenharmony_ci default=r"./", type=str, 295f9996aaSopenharmony_ci help="Path of source code", 305f9996aaSopenharmony_ci ) 315f9996aaSopenharmony_ci parser.add_argument( 325f9996aaSopenharmony_ci "-rp", "--root_path", 335f9996aaSopenharmony_ci default=r"./", type=str, 345f9996aaSopenharmony_ci help="Path of root", 355f9996aaSopenharmony_ci ) 365f9996aaSopenharmony_ci parser.add_argument( 375f9996aaSopenharmony_ci "-t", "--test", 385f9996aaSopenharmony_ci default=1, type=int, 395f9996aaSopenharmony_ci help="whether the target contains test type. default 0 , choices: 0 or 1 2", 405f9996aaSopenharmony_ci ) 415f9996aaSopenharmony_ci args = parser.parse_args() 425f9996aaSopenharmony_ci return args 435f9996aaSopenharmony_ci 445f9996aaSopenharmony_ci 455f9996aaSopenharmony_cidef _judge_type(element, deps_list: list): 465f9996aaSopenharmony_ci if isinstance(element, dict): 475f9996aaSopenharmony_ci for k, v in element.items(): 485f9996aaSopenharmony_ci _judge_type(v, deps_list) 495f9996aaSopenharmony_ci elif isinstance(element, list): 505f9996aaSopenharmony_ci for v in element: 515f9996aaSopenharmony_ci _judge_type(v, deps_list) 525f9996aaSopenharmony_ci elif isinstance(element, str): 535f9996aaSopenharmony_ci deps_list.append(element) 545f9996aaSopenharmony_ci 555f9996aaSopenharmony_ci 565f9996aaSopenharmony_cidef _inner_kits_name(inner_kits_list, deps_list): 575f9996aaSopenharmony_ci if inner_kits_list: 585f9996aaSopenharmony_ci for k in inner_kits_list: 595f9996aaSopenharmony_ci deps_list.append(k['name']) 605f9996aaSopenharmony_ci 615f9996aaSopenharmony_ci 625f9996aaSopenharmony_cidef _output_build_gn(deps_list, output_path, _test_check): 635f9996aaSopenharmony_ci file_name = os.path.join(output_path, 'BUILD.gn') 645f9996aaSopenharmony_ci if os.path.exists(file_name): 655f9996aaSopenharmony_ci os.remove(file_name) 665f9996aaSopenharmony_ci flags = os.O_WRONLY | os.O_CREAT 675f9996aaSopenharmony_ci modes = stat.S_IWUSR | stat.S_IRUSR 685f9996aaSopenharmony_ci with os.fdopen(os.open(file_name, flags, modes), 'w') as f: 695f9996aaSopenharmony_ci f.write('import("//build/ohos_var.gni")\n') 705f9996aaSopenharmony_ci f.write('\n') 715f9996aaSopenharmony_ci f.write('group("default") {\n') 725f9996aaSopenharmony_ci if _test_check: 735f9996aaSopenharmony_ci f.write(' testonly = true\n') 745f9996aaSopenharmony_ci f.write(' deps = [\n') 755f9996aaSopenharmony_ci for i in deps_list: 765f9996aaSopenharmony_ci f.write(f" \"{i}\",\n") 775f9996aaSopenharmony_ci f.write(' ]\n') 785f9996aaSopenharmony_ci f.write('}\n') 795f9996aaSopenharmony_ci 805f9996aaSopenharmony_ci 815f9996aaSopenharmony_cidef _get_bundle_path(source_code_path): 825f9996aaSopenharmony_ci bundle_paths = dict() 835f9996aaSopenharmony_ci for root, dirs, files in os.walk(source_code_path): 845f9996aaSopenharmony_ci for file in files: 855f9996aaSopenharmony_ci if file.endswith("bundle.json"): 865f9996aaSopenharmony_ci bundle_paths.update({os.path.join(root, file): root}) 875f9996aaSopenharmony_ci return bundle_paths 885f9996aaSopenharmony_ci 895f9996aaSopenharmony_ci 905f9996aaSopenharmony_cidef _get_src_part_name(src_bundle_paths): 915f9996aaSopenharmony_ci _name = '' 925f9996aaSopenharmony_ci _path = '' 935f9996aaSopenharmony_ci _bundle_path = '' 945f9996aaSopenharmony_ci for src_bundle_path, v_path in src_bundle_paths.items(): 955f9996aaSopenharmony_ci src_bundle_json = utils.get_json(src_bundle_path) 965f9996aaSopenharmony_ci part_name = '' 975f9996aaSopenharmony_ci try: 985f9996aaSopenharmony_ci part_name = src_bundle_json['component']['name'] 995f9996aaSopenharmony_ci except KeyError: 1005f9996aaSopenharmony_ci print(f'--get bundle json component name error--') 1015f9996aaSopenharmony_ci if part_name.endswith('_lite'): 1025f9996aaSopenharmony_ci pass 1035f9996aaSopenharmony_ci else: 1045f9996aaSopenharmony_ci _name = part_name 1055f9996aaSopenharmony_ci _bundle_path = src_bundle_path 1065f9996aaSopenharmony_ci _path = v_path 1075f9996aaSopenharmony_ci return _bundle_path, _path 1085f9996aaSopenharmony_ci 1095f9996aaSopenharmony_ci 1105f9996aaSopenharmony_cidef _target_handle(ele, build_data, deps_list, _test_check): 1115f9996aaSopenharmony_ci if ele not in ['inner_kits', 'test', 'inner_api']: 1125f9996aaSopenharmony_ci _judge_type(build_data[ele], deps_list) 1135f9996aaSopenharmony_ci elif ele in ['inner_kits', 'inner_api']: 1145f9996aaSopenharmony_ci inner_kits_list = build_data[ele] 1155f9996aaSopenharmony_ci _inner_kits_name(inner_kits_list, deps_list) 1165f9996aaSopenharmony_ci 1175f9996aaSopenharmony_ci if _test_check == 1 and ele == 'test': 1185f9996aaSopenharmony_ci inner_kits_list = build_data[ele] 1195f9996aaSopenharmony_ci for k in inner_kits_list: 1205f9996aaSopenharmony_ci deps_list.append(k) 1215f9996aaSopenharmony_ci 1225f9996aaSopenharmony_ci 1235f9996aaSopenharmony_cidef main(): 1245f9996aaSopenharmony_ci args = _get_args() 1255f9996aaSopenharmony_ci source_code_path = args.input_path 1265f9996aaSopenharmony_ci _test_check = args.test 1275f9996aaSopenharmony_ci deps_list = list() 1285f9996aaSopenharmony_ci bundle_paths = _get_bundle_path(source_code_path) 1295f9996aaSopenharmony_ci _bundle_path, dir_path = _get_src_part_name(bundle_paths) 1305f9996aaSopenharmony_ci bundle_json = utils.get_json(_bundle_path) 1315f9996aaSopenharmony_ci build_data = dict() 1325f9996aaSopenharmony_ci try: 1335f9996aaSopenharmony_ci build_data = bundle_json["component"]["build"] 1345f9996aaSopenharmony_ci except KeyError: 1355f9996aaSopenharmony_ci print(f'--get bundle json component build dict error--') 1365f9996aaSopenharmony_ci if _test_check != 2: 1375f9996aaSopenharmony_ci for ele in build_data.keys(): 1385f9996aaSopenharmony_ci _target_handle(ele, build_data, deps_list, _test_check) 1395f9996aaSopenharmony_ci elif _test_check == 2: 1405f9996aaSopenharmony_ci inner_kits_list = build_data.get('test') 1415f9996aaSopenharmony_ci deps_list = [] 1425f9996aaSopenharmony_ci if inner_kits_list: 1435f9996aaSopenharmony_ci for k in inner_kits_list: 1445f9996aaSopenharmony_ci deps_list.append(k) 1455f9996aaSopenharmony_ci else: 1465f9996aaSopenharmony_ci deps_list = [] 1475f9996aaSopenharmony_ci 1485f9996aaSopenharmony_ci output_path = os.path.join(args.root_path, 'out') 1495f9996aaSopenharmony_ci _output_build_gn(deps_list, output_path, _test_check) 1505f9996aaSopenharmony_ci 1515f9996aaSopenharmony_ci 1525f9996aaSopenharmony_ciif __name__ == '__main__': 1535f9996aaSopenharmony_ci main() 154