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 23import stat 24 25PERMISSION_DEFINITION_PREFIX = ''' 26/* 27 * Copyright (c) 2025 Huawei Device Co., Ltd. 28 * Licensed under the Apache License, Version 2.0 (the "License"); 29 * you may not use this file except in compliance with the License. 30 * You may obtain a copy of the License at 31 * 32 * http://www.apache.org/licenses/LICENSE-2.0 33 * 34 * Unless required by applicable law or agreed to in writing, software 35 * distributed under the License is distributed on an "AS IS" BASIS, 36 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 37 * See the License for the specific language governing permissions and 38 * limitations under the License. 39 */ 40 41#ifndef PERMISSION_DEFINITION_PARSER_H 42#define PERMISSION_DEFINITION_PARSER_H 43 44#include "permission_map.h" 45 46namespace OHOS { 47namespace Security { 48namespace AccessToken { 49''' 50 51PERMISSION_DEFINITION_SUFFIX = ''' 52}; 53 54const uint32_t MAX_PERM_SIZE = sizeof(g_permList) / sizeof(PermissionBriefDef); 55} // namespace AccessToken 56} // namespace Security 57} // namespace OHOS 58#endif // PERMISSION_DEFINITION_PARSER_H 59''' 60 61PERMISSION_NAME_STRING = "char PERMISSION_NAME_%i[] = \"%s\";\n" 62 63PERMISSION_LIST_DECLEARE = "const static PermissionBriefDef g_permList[] = {" 64 65PERMISSION_BRIEF_DEFINE_PATTERN = ''' 66{ 67 .permissionName = PERMISSION_NAME_%i, 68 .grantMode = %s, 69 .availableLevel = %s, 70 .availableType = %s, 71 .provisionEnable = %s, 72 .distributedSceneEnable = %s, 73 .isKernelEffect = %s, 74 .hasValue = %s 75},''' 76 77JSON_VALUE_CONVERT_TO_CPP_DICT = { 78 "user_grant": "USER_GRANT", 79 "system_grant": "SYSTEM_GRANT", 80 "normal": "APL_NORMAL", 81 "system_basic": "APL_SYSTEM_BASIC", 82 "system_core": "APL_SYSTEM_CORE", 83} 84 85 86class PermissionDef(object): 87 def __init__(self, permission_def_dict, code): 88 self.name = permission_def_dict["name"] 89 self.grant_mode = JSON_VALUE_CONVERT_TO_CPP_DICT[ 90 permission_def_dict["grantMode"]] 91 92 self.available_level = JSON_VALUE_CONVERT_TO_CPP_DICT[ 93 permission_def_dict["availableLevel"] 94 ] 95 self.available_type = permission_def_dict["availableType"] 96 97 if "provisionEnable" in permission_def_dict and permission_def_dict["provisionEnable"]: 98 self.provision_enable = "true" 99 else: 100 self.provision_enable = "false" 101 102 if "distributedSceneEnable" in permission_def_dict and permission_def_dict["distributedSceneEnable"]: 103 self.distributed_scene_enable = "true" 104 else: 105 self.distributed_scene_enable = "false" 106 107 if "isKernelEffect" in permission_def_dict and permission_def_dict["isKernelEffect"]: 108 self.is_kernel_effect = "true" 109 else: 110 self.is_kernel_effect = "false" 111 112 if "hasValue" in permission_def_dict and permission_def_dict["hasValue"]: 113 self.has_value = "true" 114 else: 115 self.has_value = "false" 116 117 self.code = code 118 119 def dump_permission_name(self): 120 return PERMISSION_NAME_STRING % ( 121 self.code, self.name 122 ) 123 124 def dump_struct(self): 125 entry = PERMISSION_BRIEF_DEFINE_PATTERN % ( 126 self.code, self.grant_mode, self.available_level, 127 self.available_type, self.provision_enable, self.distributed_scene_enable, 128 self.is_kernel_effect, self.has_value 129 ) 130 return entry 131 132 133def parse_json(path): 134 extend_perm = { 135 'name' : 'ohos.permission.KERNEL_ATM_SELF_USE', 136 'grantMode' : 'system_grant', 137 'availableLevel' : 'system_core', 138 'availableType' : 'SYSTEM', 139 'since' : 18, 140 'deprecated' : '', 141 'provisionEnable' : True, 142 'distributedSceneEnable' : True, 143 'isKernelEffect' : True, 144 'hasValue' : True 145 } 146 147 permission_list = [] 148 149 with open(path, "r", encoding="utf-8") as f: 150 data = json.load(f) 151 index = 0 152 for perm in data["systemGrantPermissions"]: 153 permission_list.append(PermissionDef(perm, index)) 154 index += 1 155 156 for perm in data["userGrantPermissions"]: 157 permission_list.append(PermissionDef(perm, index)) 158 index += 1 159 permission_list.append(PermissionDef(extend_perm, index)) 160 return permission_list 161 162 163def convert_to_cpp(path, permission_list): 164 flags = os.O_WRONLY | os.O_CREAT | os.O_TRUNC 165 mode = stat.S_IWUSR | stat.S_IRUSR 166 with os.fdopen(os.open(path, flags, mode), "w") as f: 167 f.write(PERMISSION_DEFINITION_PREFIX) 168 for perm in permission_list: 169 f.write(perm.dump_permission_name()) 170 f.write(PERMISSION_LIST_DECLEARE) 171 for perm in permission_list: 172 f.write(perm.dump_struct()) 173 f.write(PERMISSION_DEFINITION_SUFFIX) 174 175 176def parse_args(): 177 parser = argparse.ArgumentParser() 178 parser.add_argument('--output-path', help='the output cpp path', required=True) 179 parser.add_argument('--input-json', help='json file for permission difinition', required=True) 180 return parser.parse_args() 181 182 183if __name__ == "__main__": 184 input_args = parse_args() 185 permission_list = parse_json(input_args.input_json) 186 convert_to_cpp(input_args.output_path, permission_list)