1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3# Copyright (c) 2021 Huawei Device Co., Ltd. 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15 16import argparse 17import os 18import sys 19import json 20 21 22def merge_files(args): 23 products_dir = "../../productdefine/common/products" 24 device_dir = "../../productdefine/common/device" 25 products_path = r'%s/%s' % (products_dir, args) 26 with open(products_path, 'r+', encoding='utf-8') as f: 27 data = json.load(f) 28 name = data["product_name"] 29 company = data["product_company"] 30 device = data["product_device"] 31 device_path = os.path.join(device_dir, f"{device}.json") 32 path_str = os.path.join("../../vendor/", company, name) 33 path = os.path.join(path_str) 34 new_file_name = os.path.join(path, "config.json") 35 if os.path.exists(path): 36 pass 37 else: 38 os.mkdir(path) 39 40 with open(device_path, "r", encoding='utf-8') as device_read: 41 data_device_read = json.load(device_read) 42 43 with open(products_path, "r", encoding='utf-8') as products_read: 44 data_products_read = json.load(products_read) 45 46 data_all = merge(data_device_read, data_products_read) 47 new_json = json.dumps(data_all, indent=4) 48 49 flags = os.O_RDWR | os.O_CREAT 50 modes = stat.S_IWUSR | stat.S_IRUSR 51 with os.fdopen(os.open(new_file_name, flags, modes), "w") as new_write: 52 new_write.write(new_json) 53 readjson(new_file_name, device) 54 55 56def readjson(path, device): 57 subsystems_list = list() 58 config_dic = {} 59 with open(path, 'r+', encoding='utf-8') as f: 60 data = json.load(f) 61 parts = data['parts'] 62 subsystem_list = list() 63 for key in parts: 64 substr = str(key).split(":") 65 if substr[0] not in subsystem_list: 66 subsystem_list.append(substr[0]) 67 components_list = list() 68 for key_sub, value_sub in parts.items(): 69 features = [] 70 for value_fea in value_sub.values(): 71 for k, v in value_fea.items(): 72 fea = "{} = {}".format(str(k), str(v).lower()) 73 features.append(fea) 74 if substr[0] == str(key_sub).split(":")[0]: 75 components_list.append({"component": str(key_sub).split(":")[1], "features": features}) 76 subsystems_list.append({"subsystem": substr[0], "components": components_list}) 77 config_dic["subsystems"] = subsystems_list 78 del data['parts'] 79 data.update({"version": "3.0"}) 80 data.update({"board": device}) 81 data.update(config_dic) 82 for datakey in data: 83 if "enable_ramdisk" in datakey: 84 dict_keys = ["product_name", "device_company", "device_build_path", "target_cpu", "type", "version", 85 "board", "enable_ramdisk", "enable_absystem", "subsystems"] 86 break 87 else: 88 dict_keys = ["product_name", "device_company", "device_build_path", "target_cpu", "type", "version", 89 "board", "enable_absystem", "subsystems"] 90 dict_you_want = {new_key: data[new_key] for new_key in dict_keys} 91 json_data = json.dumps(dict_you_want, indent=2) 92 f.seek(0) 93 f.write(json_data) 94 f.truncate() 95 96 97def merge(dict1, dict2): 98 res = {**dict1, **dict2} 99 return res 100 101 102def main(args): 103 merge_files(args) 104 105 106if __name__ == '__main__': 107 main(sys.argv[1]) 108