1#!/usr/bin/env python 2# -*- coding: utf-8 -*- 3 4# Copyright (c) 2020-2021 Huawei Device Co., Ltd. 5# 6# HDF is dual licensed: you can use it either under the terms of 7# the GPL, or the BSD license, at your option. 8# See the LICENSE file in the root of this repository for complete details. 9 10 11import os 12import json 13 14from hdf_tool_exception import HdfToolException 15from command_line.hdf_command_error_code import CommandErrorCode 16 17 18def singleton(clazz): 19 _instances = {} 20 21 def create_instance(): 22 if clazz not in _instances: 23 _instances[clazz] = clazz() 24 return _instances.get(clazz) 25 return create_instance 26 27 28def get_hdf_tool_resources_path(): 29 cur_dir = os.path.realpath(os.path.dirname(__file__)) 30 return os.path.join(cur_dir, 'resources') 31 32 33@singleton 34class HdfToolSettings(object): 35 def __init__(self): 36 self.file_path = os.path.join(get_hdf_tool_resources_path(), 'settings.json') 37 self.settings = {} 38 if not os.path.exists(self.file_path): 39 return 40 with open(self.file_path) as file_write: 41 try: 42 self.settings = json.load(file_write) 43 except ValueError as exc: 44 raise HdfToolException('file: %s format wrong, %s' % 45 (self.file_path, str(exc)), 46 CommandErrorCode.FILE_FORMAT_WRONG) 47 finally: 48 pass 49 self.supported_boards_key = 'supported_boards' 50 self.drivers_path_key_framework = 'drivers_path_relative_framework' 51 self.drivers_path_key_peripheral = 'drivers_path_relative_peripheral' 52 self.drivers_path_key_interface = 'drivers_path_relative_interface' 53 self.drivers_adapter_path_key = 'drivers_path_relative_adapter' 54 self.user_adapter_path_key = 'user_model_path_relative_adapter' 55 self.module_save_path_key = "module_save_path" 56 self.dot_configs_key = 'dot_configs' 57 self.board_path_key = 'board_parent_path' 58 self.dot_config_path_key = 'dot_config_path' 59 self.template_path_key = 'template_file_path' 60 self.hdi_config_key = "hdi_config" 61 self.passwd_group_key = "passwd_group_config" 62 self.config_setting_info = "config_setting_file_info" 63 self.create_file_config_info = "create_file_config" 64 65 def get_supported_boards(self): 66 key = self.supported_boards_key 67 if key in self.settings: 68 return ','.join(self.settings.get(key).keys()) 69 return '' 70 71 def get_board_parent_path(self, board_name): 72 key = self.supported_boards_key 73 board_entry = {} 74 if key in self.settings: 75 if board_name in self.settings.get(key): 76 board_entry = self.settings.get(key).get(board_name) 77 key = self.board_path_key 78 return board_entry.get(key, '') 79 80 def get_drivers_path_framework(self): 81 key = self.drivers_path_key_framework 82 return self.settings.get(key, 'hdf') 83 84 def get_drivers_path_peripheral(self): 85 key = self.drivers_path_key_peripheral 86 return self.settings.get(key, 'hdf') 87 88 def get_drivers_path_interface(self): 89 key = self.drivers_path_key_interface 90 return self.settings.get(key, 'hdf') 91 92 def get_drivers_path_adapter(self): 93 key = self.drivers_adapter_path_key 94 return self.settings.get(key, 'hdf') 95 96 def get_template_path(self): 97 key = self.template_path_key 98 return self.settings.get(key, 'hdf') 99 100 def get_dot_configs(self, board_name): 101 key = self.supported_boards_key 102 boards = self.settings.get(key, None) 103 if not boards: 104 return [] 105 board = boards.get(board_name, None) 106 if not board: 107 return [] 108 dot_config_path = board.get(self.dot_config_path_key, '') 109 if not dot_config_path: 110 return [] 111 configs = board.get(self.dot_configs_key, []) 112 return [os.path.join(dot_config_path, config) for config in configs] 113 114 def get_board_parent_file(self, board_name): 115 key = self.supported_boards_key 116 if key in self.settings: 117 if board_name in self.settings.get(key): 118 return self.settings.get(key).get(board_name).get("patch_and_config") 119 return '' 120 121 def get_board_list(self): 122 key = self.supported_boards_key 123 return list(self.settings.get(key).keys()) 124 125 def get_user_adapter_path(self): 126 key = self.user_adapter_path_key 127 return self.settings.get(key, 'hdf') 128 129 def get_hdi_config(self): 130 key = self.hdi_config_key 131 return self.settings.get(key, 'hdf') 132 133 def get_hdi_file_path(self): 134 cur_dir = os.path.realpath(os.path.dirname(__file__)) 135 return os.path.join(cur_dir, 'resources') 136 137 def get_module_save_path(self): 138 key = self.module_save_path_key 139 return self.settings.get(key, 'hdf') 140 141 def get_resources_file_path(self): 142 return get_hdf_tool_resources_path() 143 144 def get_passwd_group_config(self): 145 key = self.passwd_group_key 146 return self.settings.get(key, 'hdf') 147 148 def get_config_setting_info(self): 149 key = self.config_setting_info 150 return self.settings.get(key, 'hdf') 151 152 def get_file_config_info(self): 153 key = self.create_file_config_info 154 return self.settings.get(key, 'hdf') 155 156 157@singleton 158class HdiToolConfig(object): 159 def __init__(self): 160 hdf_tool = HdfToolSettings() 161 hdi_config_path = hdf_tool.get_hdi_config()["config_path"] 162 cur_dir = os.path.realpath(os.path.dirname(__file__)) 163 self.hdi_file_path = os.path.join(cur_dir, hdi_config_path) 164 if not os.path.exists(self.hdi_file_path): 165 raise HdfToolException('file: %s file not exist!' % self.hdi_file_path, 166 CommandErrorCode.TARGET_NOT_EXIST) 167 with open(self.hdi_file_path, "r") as hdi_file_read: 168 try: 169 self.hdi_settings = json.load(hdi_file_read) 170 except ValueError as exc: 171 raise HdfToolException('file: %s format wrong, %s' % 172 (self.file_path, str(exc)), 173 CommandErrorCode.FILE_FORMAT_WRONG) 174 self.passwd_key = 'passwd' 175 self.group_key = 'group' 176 self.selinux_key = 'selinux' 177 self.selinux_type_key = 'type.te' 178 self.selinux_hdf_service_key = 'hdf_service.te' 179 self.selinux_hdf_service_contexts_key = 'hdf_service_contexts' 180 self.selinux_hdf_host_key = 'hdf_host.te' 181 self.selinux_peripheral_key = "peripheral_config" 182 183 def get_hdi_passwd(self): 184 key = self.passwd_key 185 return self.hdi_settings.get(key, 'hdi') 186 187 def get_hdi_group(self): 188 key = self.group_key 189 return self.hdi_settings.get(key, 'hdi') 190 191 def _get_hdi_selinux(self): 192 key = self.selinux_key 193 return self.hdi_settings.get(key, 'hdi') 194 195 def get_hdi_selinux_type(self): 196 key = self.selinux_type_key 197 selinux_config_info = self._get_hdi_selinux() 198 parent_path = selinux_config_info.get("pre_path") 199 return parent_path, selinux_config_info.get(key, 'hdi') 200 201 def get_hdi_selinux_hdf_service(self): 202 key = self.selinux_hdf_service_key 203 selinux_config_info = self._get_hdi_selinux() 204 parent_path = selinux_config_info.get("pre_path") 205 return parent_path, selinux_config_info.get(key, 'hdi') 206 207 def get_hdi_selinux_hdf_service_contexts(self): 208 key = self.selinux_hdf_service_contexts_key 209 selinux_config_info = self._get_hdi_selinux() 210 parent_path = selinux_config_info.get("pre_path") 211 return parent_path, selinux_config_info.get(key, 'hdi') 212 213 def get_hdi_selinux_hdf_host(self): 214 key = self.selinux_hdf_host_key 215 selinux_config_info = self._get_hdi_selinux() 216 parent_path = selinux_config_info.get("pre_path") 217 return parent_path, selinux_config_info.get(key, 'hdi') 218 219 def get_selinux_peripheral_hdf_host(self): 220 key = self.selinux_peripheral_key 221 selinux_config_info = self._get_hdi_selinux() 222 parent_path = selinux_config_info.get("pre_path") 223 return parent_path, selinux_config_info.get(key, 'hdi') 224