1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3""" 4Copyright (c) 2021 Huawei Device Co., Ltd. 5Licensed under the Apache License, Version 2.0 (the "License"); 6you may not use this file except in compliance with the License. 7You may obtain a copy of the License at 8 9 http://www.apache.org/licenses/LICENSE-2.0 10 11Unless required by applicable law or agreed to in writing, software 12distributed under the License is distributed on an "AS IS" BASIS, 13WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14See the License for the specific language governing permissions and 15limitations under the License. 16 17""" 18import os 19import stat 20import json 21import argparse 22from hb.resources.config import Config 23 24""" 25@Desc: 26 This script is used to generate full components 27 example for creating new products 28 29@GUID: 30basic: 31 cmd1: cd ${repo_root} 32 cmd2: python3 ./build/tools/component_tools/full_components_generator.py 33 base_product.json will be created in ./productdefine/common/base 34 35advanced: 36 cmd: python3 full_components_generator.py -h 37 Get more information 38 39@Date 2022/01/14 40""" 41 42 43def find_files(path: str, name: str): 44 ret, files, folders = [], [], [path] 45 for folder in folders: 46 for file in os.listdir(folder): 47 abs_file = os.path.join(folder, file) 48 if str(file) == name: 49 files.append(abs_file) 50 if os.path.isdir(abs_file): 51 folders.append(abs_file) 52 53 for file in files: 54 if len(file) > 0 and os.path.exists(file): 55 ret.append(file) 56 return ret 57 58 59def read_component_from_ohos_build(file: str): 60 ret = {"subsystem": "", "components": []} 61 with open(file, "rb") as f: 62 data = json.load(f) 63 ret["subsystem"] = data.get("subsystem") 64 for k, _ in data.get("parts").items(): 65 ret.get("components").append(k) 66 return ret 67 68 69def read_component_from_bundle_json(file: str): 70 ret = {"subsystem": "", "components": []} 71 with open(file, "rb") as f: 72 data = json.load(f) 73 ret["subsystem"] = data.get("component").get("subsystem") 74 ret.get("components").append(data.get("component").get("name")) 75 return ret 76 77 78def find_component_in_path(subsys: str, path: str): 79 ret = set() 80 if not os.path.exists(path): 81 return [] 82 files_ohos_build = find_files(path, "ohos.build") 83 files_bundle_json = find_files(path, "bundle.json") 84 for ohos_build in files_ohos_build: 85 data = read_component_from_ohos_build(ohos_build) 86 if data.get("subsystem") == subsys: 87 ret = ret.union(set(data.get("components"))) 88 89 for bundle_json in files_bundle_json: 90 data = read_component_from_bundle_json(bundle_json) 91 if data.get("subsystem") == subsys: 92 ret = ret.union(set(data.get("components"))) 93 return ret 94 95 96def update_components(subsys_file: str): 97 ret = {"subsystems": []} 98 with open(subsys_file, "rb") as f: 99 data = json.load(f) 100 for subsys, v in data.items(): 101 components_path = v.get("path") 102 parts = find_component_in_path(subsys, components_path) 103 components = [] 104 for part in parts: 105 components.append({"component": part, "features": []}) 106 ret.get("subsystems").append( 107 {"subsystem": subsys, "components": components}) 108 return ret 109 110 111def main(): 112 conf = Config() 113 subsystem_json_overlay_path = os.path.join(conf.product_path, 'subsystem_config_overlay.json') 114 parser = argparse.ArgumentParser() 115 parser.add_argument('--subsys', type=str, default="./build/subsystem_config.json", 116 help='subsystem config file location, default=//build/subsystem_config.json') 117 parser.add_argument('--subsys_overlay', type=str, default=subsystem_json_overlay_path, 118 help='subsystem config overlay file location, default={}'.format(subsystem_json_overlay_path)) 119 parser.add_argument('--out', type=str, default="./productdefine/common/base/base_product.json", 120 help='base_config output path default //productdefine/common/base') 121 args = parser.parse_args() 122 # Only for version 3.0 config 123 # 'device_name' has been replaced by 'board' in 3.0 124 # device info has been merged into product in 3.0 125 # 'target cpu' need to be arm instead of arm64 due to adaption work has not been done 126 ret = { 127 "product_name": "ohos-arm64", 128 "version": "3.0", 129 "type": "standard", 130 "ohos_version": "OpenHarmony 3.x", 131 "board": "arm64", 132 "kernel_type": "", 133 "kernel_version": "", 134 "device_name": "arm64", 135 "device_company": "openharmony", 136 "target_os": "ohos", 137 "target_cpu": "arm", 138 "subsystems": [] 139 } 140 data = update_components(args.subsys) 141 ret["subsystems"] = data.get("subsystems") 142 if os.path.isfile(subsystem_json_overlay_path): 143 overlay_data = update_components(args.subsys_overlay) 144 ret["subsystems"].update(overlay_data.get("subsystems")) 145 with os.fdopen(os.open(args.out, 146 os.O_RDWR | os.O_CREAT, stat.S_IWUSR | stat.S_IRUSR), 'w') as f: 147 f.write(json.dumps(ret, indent=2)) 148 print("file has generated in path: {}".format(args.out)) 149 150 151if __name__ == '__main__': 152 main() 153