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 os
17import sys
18import argparse
19
20sys.path.append(
21    os.path.dirname(
22        os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
23from scripts.util.file_utils import write_json_file  # noqa E402
24
25
26def get_allowlist():
27    return ['hilog_java']
28
29
30def get_kits_signature_list(sign_file_root_dir: str, subsystem_name: str,
31                            sign_file_name: str):
32    kits_signature_list = []
33    subsystem_sign_file_dir = os.path.join(sign_file_root_dir, subsystem_name)
34    if not os.path.exists(subsystem_sign_file_dir):
35        return kits_signature_list
36    kits_module_list = os.listdir(subsystem_sign_file_dir)
37    for module_name in kits_module_list:
38        signature_file = os.path.join(subsystem_sign_file_dir, module_name,
39                                      sign_file_name)
40        if os.path.exists(signature_file):
41            kits_signature_list.append(module_name)
42    return kits_signature_list
43
44
45def check_remove(sign_file_root_dir: str, subsystem_name: str, kit_list: list, kit_type: str):
46    if not os.path.exists(sign_file_root_dir):
47        return True
48
49    sign_file_name = 'signature'
50    if kit_type == 'so':
51        sign_file_name = 'check.txt'
52
53    kits_signature_list = get_kits_signature_list(sign_file_root_dir,
54                                                  subsystem_name,
55                                                  sign_file_name)
56
57    if len(kits_signature_list) != len(kit_list):
58        return False
59
60    for kits_signature_module in kits_signature_list:
61        if kits_signature_module not in kit_list:
62            return False
63    return True
64
65
66def main():
67    parser = argparse.ArgumentParser()
68    parser.add_argument('--subsystem-name', required=True)
69    parser.add_argument('--sign-file-root-dir', required=True)
70    parser.add_argument('--kit-list', nargs='+', default=[])
71    parser.add_argument('--output-file', required=True)
72    parser.add_argument('--kit-type', required=True)
73    parser.add_argument('--depfile', required=False)
74    args = parser.parse_args()
75
76    kit_list = args.kit_list
77    for _kit in get_allowlist():
78        if _kit in kit_list:
79            kit_list.remove(_kit)
80
81    result = check_remove(args.sign_file_root_dir, args.subsystem_name,
82                          kit_list, args.kit_type)
83    if not result:
84        raise Exception(
85            "Error, part '{}' kit remove, please check kit config.".
86            format(args.subsystem_name))
87    write_json_file(args.output_file, {})
88    return 0
89
90
91if __name__ == '__main__':
92    sys.exit(main())
93