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
19from gen_def_from_all_yaml import merge_hisysevent_config  # noqa: E402
20
21sys.path.append(
22    os.path.dirname(os.path.dirname(os.path.dirname(
23        os.path.abspath(__file__)))))
24
25from scripts.util.file_utils import read_json_file, write_json_file  # noqa: E402 E501
26from scripts.util import build_utils  # noqa: E402
27
28
29def _get_src_parts(system_install_info_file: str):
30    parts_list = read_json_file(system_install_info_file)
31    if parts_list is None:
32        raise Exception(
33            "read file '{}' failed.".format(system_install_info_file))
34    src_part_list = []
35    for _part_info in parts_list:
36        if 'is_source' not in _part_info:
37            raise Exception("read subsystem info error, missing key is_source")
38        if _part_info.get('is_source') is True:
39            src_part_list.append(_part_info.get('origin_part_name'))
40    return src_part_list
41
42
43def _get_all_config_files(hisysevent_config_info: dict, src_part_list: list,
44                          source_root_dir: str, gn_root_build_dir: str):
45    hisysevent_config_files = []
46    for origin_part_name in src_part_list:
47        _config_files = hisysevent_config_info.get(origin_part_name, [])
48        for _config_file in _config_files:
49            if not _config_file.startswith('//'):
50                raise Exception(
51                    "part '{}' hisysevent config incorrect.".format(
52                        origin_part_name))
53            _relpath = os.path.relpath(
54                os.path.join(source_root_dir, _config_file[2:]),
55                gn_root_build_dir)
56            hisysevent_config_files.append(_relpath)
57    return hisysevent_config_files
58
59
60def _get_install_info(hisysevent_config_files: str, output_path: str,
61                      config_install_dest_dir: str):
62    install_info_list = []
63    if hisysevent_config_files:
64        hisysevent_merge_result_file = merge_hisysevent_config(
65            hisysevent_config_files, output_path)
66        _config_install_info = {}
67        _config_install_info['type'] = 'hisysevent_config'
68        _config_install_info['source'] = hisysevent_merge_result_file
69        _config_install_info['install_enable'] = True
70        _install_dest = os.path.join(
71            config_install_dest_dir,
72            os.path.basename(hisysevent_merge_result_file))
73        _config_install_info['dest'] = [_install_dest]
74        install_info_list.append(_config_install_info)
75    return install_info_list
76
77
78def main(argv):
79    parser = argparse.ArgumentParser()
80    parser.add_argument('--parts-config-file', required=True)
81    parser.add_argument('--system-install-info-file', required=True)
82    parser.add_argument('--config-install-dest-dir', required=True)
83    parser.add_argument('--hisysevent-install-info-file', required=True)
84    parser.add_argument('--depfile', required=True)
85    parser.add_argument('--source-root-dir', required=True)
86    parser.add_argument('--gn-root-build-dir', required=True)
87    args = parser.parse_args(argv)
88
89    depfiles = [args.parts_config_file]
90    hisysevent_config_info = read_json_file(args.parts_config_file)
91    if hisysevent_config_info is None:
92        raise Exception("read file '{}' failed.".format(
93            args.parts_config_file))
94    src_part_list = _get_src_parts(args.system_install_info_file)
95
96    hisysevent_config_files = _get_all_config_files(hisysevent_config_info,
97                                                    src_part_list,
98                                                    args.source_root_dir,
99                                                    args.gn_root_build_dir)
100
101    _output_path = os.path.dirname(args.hisysevent_install_info_file)
102    install_info_list = _get_install_info(hisysevent_config_files,
103                                          _output_path,
104                                          args.config_install_dest_dir)
105    write_json_file(args.hisysevent_install_info_file, install_info_list)
106    build_utils.write_depfile(args.depfile, args.hisysevent_install_info_file,
107                              depfiles)
108
109    return 0
110
111
112if __name__ == '__main__':
113    sys.exit(main(sys.argv[1:]))