• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_settings_path():
29    cur_dir = os.path.realpath(os.path.dirname(__file__))
30    return os.path.join(cur_dir, 'resources', 'settings.json')
31
32
33@singleton
34class HdfToolSettings(object):
35    def __init__(self):
36        self.file_path = get_hdf_tool_settings_path()
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        self.supported_boards_key = 'supported_boards'
48        self.drivers_path_key = 'drivers_path_relative_to_vendor'
49        self.drivers_adapter_path_key = 'drivers_path_relative_adapter'
50        self.user_adapter_path_key = 'user_model_path_relative_adapter'
51        self.dot_configs_key = 'dot_configs'
52        self.board_path_key = 'board_parent_path'
53        self.dot_config_path_key = 'dot_config_path'
54        self.template_path_key = 'template_file_path'
55
56    def get_supported_boards(self):
57        key = self.supported_boards_key
58        if key in self.settings:
59            return ','.join(self.settings.get(key).keys())
60        return ''
61
62    def get_board_parent_path(self, board_name):
63        key = self.supported_boards_key
64        board_entry = {}
65        if key in self.settings:
66            if board_name in self.settings.get(key):
67                board_entry = self.settings.get(key).get(board_name)
68        key = self.board_path_key
69        return board_entry.get(key, '')
70
71    def get_drivers_path_framework(self):
72        key = self.drivers_path_key
73        return self.settings.get(key, 'hdf')
74
75    def get_drivers_path_adapter(self):
76        key = self.drivers_adapter_path_key
77        return self.settings.get(key, 'hdf')
78
79    def get_template_path(self):
80        key = self.template_path_key
81        return self.settings.get(key, 'hdf')
82
83    def get_dot_configs(self, board_name):
84        key = self.supported_boards_key
85        boards = self.settings.get(key, None)
86        if not boards:
87            return []
88        board = boards.get(board_name, None)
89        if not board:
90            return []
91        dot_config_path = board.get(self.dot_config_path_key, '')
92        if not dot_config_path:
93            return []
94        configs = board.get(self.dot_configs_key, [])
95        return [os.path.join(dot_config_path, config) for config in configs]
96
97    def get_board_parent_file(self, board_name):
98        key = self.supported_boards_key
99        if key in self.settings:
100            if board_name in self.settings.get(key):
101                return self.settings.get(key).get(board_name).get("patch_and_config")
102        return ''
103
104    def get_board_list(self):
105        key = self.supported_boards_key
106        return list(self.settings.get(key).keys())
107
108    def get_user_adapter_path(self):
109        key = self.user_adapter_path_key
110        return self.settings.get(key, 'hdf')