1#!/usr/bin/env python
2# coding: utf-8
3
4"""
5Copyright (c) 2023 Huawei Device Co., Ltd.
6Licensed under the Apache License, Version 2.0 (the "License");
7you may not use this file except in compliance with the License.
8You may obtain a copy of the License at
9
10    http://www.apache.org/licenses/LICENSE-2.0
11
12Unless required by applicable law or agreed to in writing, software
13distributed under the License is distributed on an "AS IS" BASIS,
14WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15See the License for the specific language governing permissions and
16limitations under the License.
17
18"""
19
20import argparse
21import os
22from collections import defaultdict
23from check_common import read_file, traverse_file_in_each_type
24
25WHITELIST_FILE_NAME = "partition_label_use_whitelist.txt"
26
27
28def check_file_contexts(args, file_contexts, whitelist_map, label_list):
29    label_list_set = set()
30    for it in label_list:
31        label_list_set.add(it)
32
33    line_index = 0
34    err = False
35    for line in file_contexts:
36        line_index += 1
37        elements = line.split()
38        if len(elements) < 2:
39            continue
40        path = elements[0]
41        label = elements[1]
42        if label in label_list_set:
43            if path in whitelist_map[label]:
44                continue
45            print("partition label is not allow to use,",
46                  "check '{} {}' failed in file {}:{}\n".format(path, label, args.file_contexts, line_index),
47                  "There are two solutions:\n",
48                  "1. Add '{} {}' to whitelist file \'{}\' under \'{}\';\n".format(
49                        path, label, WHITELIST_FILE_NAME, args.policy_dir_list),
50                  "2. Change '{} {}' to avoid using label in {}\n".format(path, label, label_list))
51            err = True
52    if err:
53        raise Exception(-1)
54
55
56def get_whitelist(args):
57    whitelist_file_list = traverse_file_in_each_type(args.policy_dir_list, WHITELIST_FILE_NAME)
58    whitelist_map = defaultdict(list)
59    for path in whitelist_file_list:
60        whitelist = read_file(path)
61        for it in whitelist:
62            split_str = it.split()
63            if len(split_str) < 2:
64                continue
65            whitelist_map[split_str[1]].append(split_str[0])
66    return whitelist_map
67
68
69def parse_args():
70    parser = argparse.ArgumentParser()
71    parser.add_argument(
72        '--file_contexts', help='the file_contexts file path', required=True)
73    parser.add_argument(
74        '--policy-dir-list', help='the whitelist path list', required=True)
75    parser.add_argument(
76        '--config', help='the config file path', required=True)
77    return parser.parse_args()
78
79
80if __name__ == "__main__":
81    input_args = parse_args()
82    script_path = os.path.dirname(os.path.realpath(__file__))
83    whitelist_data = get_whitelist(input_args)
84
85    label_path = os.path.join(script_path, input_args.config)
86    label_data = read_file(label_path)
87
88    file_contexts_data = read_file(input_args.file_contexts)
89    check_file_contexts(input_args, file_contexts_data, whitelist_data, label_data)
90