• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
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 sys
18import json
19import re
20import os
21
22
23def get_features(features):
24    feats = {}
25    for feat in features:
26        match = re.match(r'(\w*?)(\s*=\s*)(["\w]*?)$', feat)
27        if match:
28            key = match.group(1)
29            val = match.group(3)
30            if val == 'true':
31                feats[key] = True
32            elif val == 'false':
33                feats[key] = False
34            elif re.match(r'[0-9]+', val):
35                feats[key] = int(val)
36            else:
37                feats[key] = val.replace('\"', '"')
38
39    pairs = dict()
40    pairs['features'] = feats
41    return pairs
42
43
44def from_ss_to_parts(subsystems):
45    parts = dict()
46    for subsystem in subsystems:
47        ss_name = subsystem.get('subsystem')
48        components = subsystem.get('components')
49        if components:
50            for com in components:
51                com_name = com.get('component')
52                features = com.get('features')
53                if features:
54                    pairs = get_features(features)
55                    parts['{}:{}'.format(ss_name, com_name)] = pairs
56                else:
57                    parts['{}:{}'.format(ss_name, com_name)] = dict()
58    return parts
59
60
61def transform(config):
62    subsystems = config.get('subsystems')
63    if subsystems:
64        config.pop('subsystems')
65        parts = from_ss_to_parts(subsystems)
66        config['parts'] = parts
67    return config
68
69
70def save_transformed_config(config, output_file):
71    new_config = json.dumps(config, indent=2, sort_keys=True)
72    with open(output_file, 'wt') as fout:
73        fout.write(new_config)
74
75
76def get_product_config(config_dir, product_name, company):
77    company_path = os.path.join(config_dir, company)
78    if not os.path.isdir(company_path):
79        raise Exception(f'Error: {company_path} is not a directory')
80
81    for product in os.listdir(company_path):
82        product_path = os.path.join(company_path, product)
83        config_json = os.path.join(product_path, 'config.json')
84
85        if os.path.isfile(config_json):
86            with open(config_json, 'rb') as fin:
87                config = json.load(fin)
88                if product_name == config.get('product_name'):
89                    return config
90    raise Exception(f'Error: failed to get product config for {product_name}')
91
92
93def get_vendor_parts_list(config):
94    return transform(config).get('parts')
95
96
97def main():
98    parser = argparse.ArgumentParser()
99    parser.add_argument('--product-name', required=True)
100    parser.add_argument('--company', required=True)
101    parser.add_argument('--config-dir', required=True)
102    options = parser.parse_args()
103    config = get_product_config(options.config_dir, options.product_name,
104                                options.company)
105    get_vendor_parts_list(config)
106
107
108if __name__ == '__main__':
109    sys.exit(main())
110