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 sys
17import os
18import argparse
19
20sys.path.append(
21    os.path.dirname(os.path.dirname(os.path.dirname(
22        os.path.abspath(__file__)))))
23from scripts.util.file_utils import read_json_file, write_json_file  # noqa: E402
24from scripts.util import build_utils  # noqa: E402
25
26
27def read_install_info(system_installed_info_file: str, depfiles: list):
28    install_module_dict = {}
29    parts_list = _read_file_content(system_installed_info_file)
30    if parts_list is None:
31        raise Exception(
32            "read file '{}' failed.".format(system_installed_info_file))
33    for _part_info in parts_list:
34        is_source = _part_info.get('is_source')
35        if is_source is False:
36            continue
37        part_name = _part_info.get('part_name')
38        part_info_file = _part_info.get('part_info_file')
39        depfiles.append(part_info_file)
40        module_info_file_list = _read_file_and_get_content(
41            part_info_file, 'module_info_file')
42        install_module_list = []
43        for module_info_file in module_info_file_list:
44            module_info = _read_file_content(module_info_file)
45            depfiles.append(module_info_file)
46            install_enable = module_info.get('install_enable')
47            if not install_enable:
48                continue
49            install_module_list.append(module_info)
50        install_module_dict[part_name] = install_module_list
51    return install_module_dict
52
53
54def _read_file_content(input_file: str):
55    if not os.path.exists(input_file):
56        raise Exception("file '{}' does not exist.".format(input_file))
57    data = read_json_file(input_file)
58    if data is None:
59        raise Exception("read file '{}' failed.".format(input_file))
60    return data
61
62
63def _read_file_and_get_content(input_file: str, get_arrt_name=None):
64    data = _read_file_content(input_file)
65    result = []
66    for info in data:
67        result.append(info.get(get_arrt_name))
68    return result
69
70
71def main():
72    parser = argparse.ArgumentParser()
73    parser.add_argument('--system-installed-info-file', required=True)
74    parser.add_argument('--required-install-modules-file', required=True)
75    parser.add_argument('--depfile', required=True)
76    args = parser.parse_args()
77
78    depfiles = [args.system_installed_info_file]
79    install_moudle_dict = read_install_info(args.system_installed_info_file,
80                                            depfiles)
81    write_json_file(args.required_install_modules_file, install_moudle_dict)
82    build_utils.write_depfile(args.depfile, args.required_install_modules_file,
83                              depfiles)
84    return 0
85
86
87if __name__ == '__main__':
88    sys.exit(main())
89