• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2# coding: utf-8
3
4"""
5Copyright (c) 2025 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 json
21import argparse
22import os
23
24
25REQUIRED_ATTRS = [
26    "name",
27    "grantMode",
28    "availableLevel",
29    "since",
30    "provisionEnable",
31    "distributedSceneEnable"
32]
33
34
35ATTRS_ONLY_IN_RESOURCE = [
36    "label",
37    "description"
38]
39
40
41def parse_definition_json(path):
42    permission_maps = {}
43    with open(path, "r", encoding="utf-8") as f:
44        data = json.load(f)
45        for perm in data["definePermissions"]:
46            permission_maps[perm["name"]] = perm
47    return permission_maps
48
49
50def parse_module_json(path):
51    permission_maps = {}
52    if not os.path.exists(path):
53        return {}
54    with open(path, "r", encoding="utf-8") as f:
55        data = json.load(f)
56        for perm in data["module"]["definePermissions"]:
57            permission_maps[perm["name"]] = perm
58    return permission_maps
59
60
61def check_required_param(defs, filename):
62    for attr in REQUIRED_ATTRS:
63        if not attr in defs:
64            raise Exception("Not found {} of {} in {}".format(
65                attr, defs["name"], filename))
66
67
68def check_consistency(def_in_module, full_def):
69    for attr, value in full_def.items():
70        if not attr in def_in_module:
71            continue
72        if not value == def_in_module[attr]:
73            raise Exception("{} of {} is inconsistent in module.json and permission_definition.json".format(
74                attr, def_in_module["name"]))
75
76    for attr in def_in_module.keys():
77        if attr in ATTRS_ONLY_IN_RESOURCE:
78            continue
79        elif not attr in full_def:
80            raise Exception("{} of {} should be define in permission_definition.json".format(attr,
81                def_in_module["name"]))
82
83
84def check_maps(module_map, definition_map):
85    for name, perm_def in definition_map.items():
86        if not "availableType" in perm_def:
87            raise Exception("Cannot define permission {} without availableType " \
88                "in permission_definition.json".format(name))
89        if perm_def["availableType"] == "SERVICE":
90            if name in module_map:
91                raise Exception("Cannot define permission {} for SERVICE in module.json".format(name))
92            continue
93        if not name in module_map:
94            raise Exception("To add permission definition of {} in system_global_resource.".format(name))
95        check_required_param(module_map[name], "module.json")
96        check_required_param(definition_map[name], "permission_definition.json")
97        check_consistency(module_map[name], definition_map[name])
98
99
100def parse_args():
101    parser = argparse.ArgumentParser()
102    parser.add_argument('--source-root-dir', help='build root dir', required=True)
103    parser.add_argument('--input-full-permissions', help='json file for permission definition', required=True)
104    return parser.parse_args()
105
106
107if __name__ == "__main__":
108    input_args = parse_args()
109    module_json_path = os.path.join("base/global/system_resources/systemres/main", "module.json")
110    module_json_path = os.path.join(input_args.source_root_dir, module_json_path)
111    module_json_map = parse_module_json(module_json_path)
112    if not module_json_map:
113        print("Not found {}, no need to check consistency.".format(module_json_path))
114        exit(0)
115    full_permissions_map = parse_definition_json(input_args.input_full_permissions)
116    check_maps(module_json_map, full_permissions_map)
117    print("Check permission consistency pass!")