• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 = r'%s/%s' % (device_dir,device+'.json' )
32    path_str = "../../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    try:
40        device_read = open(device_path, "r", encoding='utf-8')
41        products_read = open(products_path, "r", encoding='utf-8')
42        data_device_read = json.load(device_read)
43        data_products_read = json.load(products_read)
44        data_all = merge(data_device_read , data_products_read)
45        new_json = json.dumps(data_all, indent=4)
46        new_write = open(new_file_name,"w")
47        new_write.write(new_json)
48    finally:
49        device_read.close()
50        products_read.close()
51        new_write.close()
52    readjson(new_file_name,device)
53
54
55def readjson(path,device):
56    subsystems_list = list()
57    config_dic = {}
58    with open(path,'r+',encoding='utf-8') as f:
59        data = json.load(f)
60        parts = data['parts']
61        subsystem_list = list()
62        for key in parts:
63            substr = str(key).split(":")
64            if substr[0] not in subsystem_list:
65                subsystem_list.append(substr[0])
66                components_list = list()
67                for key_sub,value_sub in parts.items():
68                    features = []
69                    for value_fea in value_sub.values():
70                        for k,v in value_fea.items():
71                            fea = str(k)+" = "+str(v).lower()
72                            features.append(fea)
73                    if substr[0] == str(key_sub).split(":")[0]:
74                        components_list.append({"component":str(key_sub).split(":")[1],"features":features})
75                subsystems_list.append({"subsystem":substr[0],"components":components_list})
76        config_dic["subsystems"] = subsystems_list
77        del data['parts']
78        data.update({"version": "3.0"})
79        data.update({"board": device})
80        data.update(config_dic)
81        for datakey in data:
82            if "enable_ramdisk" in datakey:
83                dict_keys = ["product_name","device_company","device_build_path","target_cpu","type","version","board","enable_ramdisk","subsystems"]
84                break
85            else:
86                dict_keys = ["product_name","device_company","device_build_path","target_cpu","type","version","board","subsystems"]
87        dict_you_want = { new_key: data[new_key] for new_key in dict_keys }
88        json_data = json.dumps(dict_you_want, indent=2)
89        f.seek(0)
90        f.write(json_data)
91        f.truncate()
92
93def merge(dict1, dict2):
94    res = {**dict1, **dict2}
95    return res
96
97def main(args):
98    merge_files(args)
99
100if __name__ == '__main__':
101    main(sys.argv[1])
102