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_driver_supported_type = "create_driver_board_type"
64        self.create_file_config_info = "create_file_config"
65
66    def get_supported_boards(self):
67        key = self.supported_boards_key
68        if key in self.settings:
69            return ','.join(self.settings.get(key).keys())
70        return ''
71
72    def get_board_parent_path(self, board_name):
73        key = self.supported_boards_key
74        board_entry = {}
75        if key in self.settings:
76            if board_name in self.settings.get(key):
77                board_entry = self.settings.get(key).get(board_name)
78        key = self.board_path_key
79        return board_entry.get(key, '')
80
81    def get_drivers_path_framework(self):
82        key = self.drivers_path_key_framework
83        return self.settings.get(key, 'hdf')
84
85    def get_drivers_path_peripheral(self):
86        key = self.drivers_path_key_peripheral
87        return self.settings.get(key, 'hdf')
88
89    def get_drivers_path_interface(self):
90        key = self.drivers_path_key_interface
91        return self.settings.get(key, 'hdf')
92
93    def get_drivers_path_adapter(self):
94        key = self.drivers_adapter_path_key
95        return self.settings.get(key, 'hdf')
96
97    def get_template_path(self):
98        key = self.template_path_key
99        return self.settings.get(key, 'hdf')
100
101    def get_dot_configs(self, board_name):
102        key = self.supported_boards_key
103        boards = self.settings.get(key, None)
104        if not boards:
105            return []
106        board = boards.get(board_name, None)
107        if not board:
108            return []
109        dot_config_path = board.get(self.dot_config_path_key, '')
110        if not dot_config_path:
111            return []
112        configs = board.get(self.dot_configs_key, [])
113        return [os.path.join(dot_config_path, config) for config in configs]
114
115    def get_board_parent_file(self, board_name):
116        key = self.supported_boards_key
117        if key in self.settings:
118            if board_name in self.settings.get(key):
119                return self.settings.get(key).get(board_name).get("patch_and_config")
120        return ''
121
122    def get_board_list(self):
123        key = self.supported_boards_key
124        return list(self.settings.get(key).keys())
125
126    def get_create_board_type(self):
127        key = self.create_driver_supported_type
128        return list(self.settings.get(key))
129
130    def get_user_adapter_path(self):
131        key = self.user_adapter_path_key
132        return self.settings.get(key, 'hdf')
133
134    def get_hdi_config(self):
135        key = self.hdi_config_key
136        return self.settings.get(key, 'hdf')
137
138    def get_hdi_file_path(self):
139        cur_dir = os.path.realpath(os.path.dirname(__file__))
140        return os.path.join(cur_dir, 'resources')
141
142    def get_module_save_path(self):
143        key = self.module_save_path_key
144        return self.settings.get(key, 'hdf')
145
146    def get_resources_file_path(self):
147        return get_hdf_tool_resources_path()
148
149    def get_passwd_group_config(self):
150        key = self.passwd_group_key
151        return self.settings.get(key, 'hdf')
152
153    def get_config_setting_info(self):
154        key = self.config_setting_info
155        return self.settings.get(key, 'hdf')
156
157    def get_file_config_info(self):
158        key = self.create_file_config_info
159        return self.settings.get(key, 'hdf')
160
161
162@singleton
163class HdiToolConfig(object):
164    def __init__(self):
165        hdf_tool = HdfToolSettings()
166        hdi_config_path = hdf_tool.get_hdi_config()["config_path"]
167        cur_dir = os.path.realpath(os.path.dirname(__file__))
168        self.hdi_file_path = os.path.join(cur_dir, hdi_config_path)
169        if not os.path.exists(self.hdi_file_path):
170            raise HdfToolException('file: %s file not exist!' % self.hdi_file_path,
171                                   CommandErrorCode.TARGET_NOT_EXIST)
172        with open(self.hdi_file_path, "r") as hdi_file_read:
173            try:
174                self.hdi_settings = json.load(hdi_file_read)
175            except ValueError as exc:
176                raise HdfToolException('file: %s format wrong, %s' %
177                                       (self.file_path, str(exc)),
178                                       CommandErrorCode.FILE_FORMAT_WRONG)
179        self.passwd_key = 'passwd'
180        self.group_key = 'group'
181        self.selinux_key = 'selinux'
182        self.selinux_type_key = 'type.te'
183        self.selinux_hdf_service_key = 'hdf_service.te'
184        self.selinux_hdf_service_contexts_key = 'hdf_service_contexts'
185        self.selinux_hdf_host_key = 'hdf_host.te'
186        self.selinux_peripheral_key = "peripheral_config"
187
188    def get_hdi_passwd(self):
189        key = self.passwd_key
190        return self.hdi_settings.get(key, 'hdi')
191
192    def get_hdi_group(self):
193        key = self.group_key
194        return self.hdi_settings.get(key, 'hdi')
195
196    def _get_hdi_selinux(self):
197        key = self.selinux_key
198        return self.hdi_settings.get(key, 'hdi')
199
200    def get_hdi_selinux_type(self):
201        key = self.selinux_type_key
202        selinux_config_info = self._get_hdi_selinux()
203        parent_path = selinux_config_info.get("pre_path")
204        return parent_path, selinux_config_info.get(key, 'hdi')
205
206    def get_hdi_selinux_hdf_service(self):
207        key = self.selinux_hdf_service_key
208        selinux_config_info = self._get_hdi_selinux()
209        parent_path = selinux_config_info.get("pre_path")
210        return parent_path, selinux_config_info.get(key, 'hdi')
211
212    def get_hdi_selinux_hdf_service_contexts(self):
213        key = self.selinux_hdf_service_contexts_key
214        selinux_config_info = self._get_hdi_selinux()
215        parent_path = selinux_config_info.get("pre_path")
216        return parent_path, selinux_config_info.get(key, 'hdi')
217
218    def get_hdi_selinux_hdf_host(self):
219        key = self.selinux_hdf_host_key
220        selinux_config_info = self._get_hdi_selinux()
221        parent_path = selinux_config_info.get("pre_path")
222        return parent_path, selinux_config_info.get(key, 'hdi')
223
224    def get_selinux_peripheral_hdf_host(self):
225        key = self.selinux_peripheral_key
226        selinux_config_info = self._get_hdi_selinux()
227        parent_path = selinux_config_info.get("pre_path")
228        return parent_path, selinux_config_info.get(key, 'hdi')
229