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 14import hdf_utils 15import hdf_tool_version 16from command_line.hdf_command_error_code import CommandErrorCode 17from hdf_tool_settings import HdfToolSettings 18from hdf_tool_exception import HdfToolException 19from .hdf_command_handler_base import HdfCommandHandlerBase 20from .hdf_linux_scann import HdfLinuxScan 21from .hdf_lite_mk_file import HdfLiteMkFile 22from .hdf_liteos_scann import HdfLiteScan 23from .hdf_vendor_kconfig_file import HdfVendorKconfigFile 24from .hdf_module_kconfig_file import HdfModuleKconfigFile 25from .hdf_driver_config_file import HdfDriverConfigFile 26 27 28class HdfGetHandler(HdfCommandHandlerBase): 29 HDF_VERSION_MAJOR = 0 30 HDF_VERSION_MINOR = 1 31 32 def __init__(self, args): 33 super(HdfGetHandler, self).__init__() 34 self.cmd = 'get' 35 self.handlers = { 36 'vendor_list': self._get_vendor_list_handler, 37 'current_vendor': self._get_current_vendor_handler, 38 'vendor_parent_path': self._get_vendor_parent_path_handler, 39 'individual_vendor_path': self._get_individual_vendor_path_handler, 40 'board_list': self._get_board_list_handler, 41 'driver_list': self._get_driver_list_handler, 42 'driver_file': self._get_driver_file_handler, 43 'drv_config_file': self._get_drv_config_file_handler, 44 'hdf_tool_core_version': self._get_version_handler, 45 'model_list': self._get_model_dict, 46 'model_scan': self._mode_scan, 47 'version': self.__get_version, 48 } 49 self.parser.add_argument("--action_type", 50 help=' '.join(self.handlers.keys()), 51 required=True) 52 self.parser.add_argument("--root_dir") 53 self.parser.add_argument("--vendor_name") 54 self.parser.add_argument("--module_name") 55 self.parser.add_argument("--driver_name") 56 self.parser.add_argument("--board_name") 57 self.args = self.parser.parse_args(args) 58 59 def _get_vendor_list_handler(self): 60 self.check_arg_raise_if_not_exist("root_dir") 61 root = self.args.root_dir 62 vendor_root_dir = hdf_utils.get_vendor_root_dir(root) 63 vendors = [] 64 if os.path.exists(vendor_root_dir): 65 for vendor in os.listdir(vendor_root_dir): 66 hdf = hdf_utils.get_vendor_hdf_dir(root, vendor) 67 if os.path.exists(hdf): 68 vendors.append(vendor) 69 return ','.join(vendors) 70 71 def _get_current_vendor_handler(self): 72 self.check_arg_raise_if_not_exist("root_dir") 73 return HdfLiteMkFile(self.args.root_dir).get_current_vendor() 74 75 @staticmethod 76 def _get_board_list_handler(): 77 settings = HdfToolSettings() 78 return settings.get_supported_boards() 79 80 def _get_vendor_parent_path_handler(self): 81 self.check_arg_raise_if_not_exist("root_dir") 82 target = hdf_utils.get_vendor_root_dir(self.args.root_dir) 83 return os.path.realpath(target) 84 85 def _get_individual_vendor_path_handler(self): 86 self.check_arg_raise_if_not_exist("root_dir") 87 self.check_arg_raise_if_not_exist("vendor_name") 88 root, vendor, _, _, _ = self.get_args() 89 target = hdf_utils.get_vendor_dir(root, vendor) 90 return os.path.realpath(target) 91 92 @staticmethod 93 def _get_version_handler(): 94 return hdf_tool_version.get_version() 95 96 def _get_driver_list_handler(self): 97 self.check_arg_raise_if_not_exist("root_dir") 98 self.check_arg_raise_if_not_exist("vendor_name") 99 root, vendor, _, _, _ = self.get_args() 100 hdf_dir = hdf_utils.get_vendor_hdf_dir(root, vendor) 101 if not os.path.exists(hdf_dir): 102 raise HdfToolException('vendor "%s" not exist' % vendor, 103 CommandErrorCode.TARGET_NOT_EXIST) 104 modules = os.listdir(hdf_dir) 105 vendor_k = HdfVendorKconfigFile(root, vendor, kernel=None, path='') 106 module_items = vendor_k.get_module_and_config_path() 107 drivers = {} 108 for item in module_items: 109 module, k_path = item 110 if module in modules: 111 models = \ 112 HdfModuleKconfigFile(root, module, 113 k_path).get_models() 114 drivers[module] = models 115 return json.dumps(drivers) 116 117 def _get_driver_file_handler(self): 118 self.check_arg_raise_if_not_exist("root_dir") 119 self.check_arg_raise_if_not_exist("vendor_name") 120 self.check_arg_raise_if_not_exist("module_name") 121 self.check_arg_raise_if_not_exist("driver_name") 122 root = os.path.realpath(self.args.root_dir) 123 _, vendor, module, driver, _ = self.get_args() 124 drv_dir = hdf_utils.get_drv_dir(root, vendor, module, driver) 125 if not os.path.exists(drv_dir): 126 raise HdfToolException( 127 'driver directory: %s not exist' % 128 drv_dir, CommandErrorCode.TARGET_NOT_EXIST) 129 for root_path, dirs, files in os.walk(drv_dir): 130 for file_name in files: 131 if file_name.endswith('.c'): 132 return os.path.realpath(os.path.join(root_path, file_name)) 133 return '' 134 135 def _get_drv_config_file_handler(self): 136 self.check_arg_raise_if_not_exist("root_dir") 137 self.check_arg_raise_if_not_exist("module_name") 138 self.check_arg_raise_if_not_exist("driver_name") 139 self.check_arg_raise_if_not_exist("board_name") 140 root, _, module, driver, board = self.get_args() 141 drv_config = HdfDriverConfigFile(root, board, module, driver, True) 142 return drv_config.get_drv_config_path() 143 144 def _get_model_dict(self): 145 self.check_arg_raise_if_not_exist("root_dir") 146 root, _, _, _, _, _ = self.get_args() 147 adapter_framework = hdf_utils.get_vendor_hdf_dir_framework(root=root) 148 if not os.path.exists(adapter_framework): 149 raise HdfToolException( 150 ' adapter model path "%s" not exist' % 151 adapter_framework, CommandErrorCode.TARGET_NOT_EXIST) 152 create_file_save_path = os.path.join( 153 adapter_framework, "tools", "hdf_dev_eco_tool", 154 "resources", "create_model.config") 155 if not os.path.exists(create_file_save_path): 156 raise HdfToolException( 157 'create file config "%s" not exist' % 158 create_file_save_path, CommandErrorCode.TARGET_NOT_EXIST) 159 out_model_list = [] 160 data = hdf_utils.read_file(create_file_save_path) 161 json_type = json.loads(data) 162 if not json_type: 163 return out_model_list 164 file_key_list = list(list(json_type. 165 items())[0][-1].keys())[:-1] 166 for k, _ in json_type.items(): 167 model_file_path = {} 168 for key in file_key_list: 169 if key.split("_")[-1] == "path": 170 path_dict = json_type[k][key] 171 model_file_path = self._model_info( 172 path_dict, root, model_file_path, key) 173 out_model_list.append({k: model_file_path}) 174 return out_model_list 175 176 def __get_version(self): 177 version_end = "\nCopyright (c) 2020-2021 Huawei Device Co., Ltd." 178 version_head = "hdf_dev_eco_tool version : " 179 return version_head + str(self.HDF_VERSION_MAJOR) \ 180 + "." + str(self.HDF_VERSION_MINOR) + version_end 181 182 def _model_info(self, path_dict, root, model_file_path, key): 183 if isinstance(path_dict, dict): 184 for k_filename, file_path in path_dict.items(): 185 if not os.path.exists(os.path.join(root, file_path)): 186 model_file_path[k_filename] = " " 187 else: 188 model_file_path[k_filename] = path_dict[k_filename] 189 else: 190 hcs_file_path = os.path.join(root, path_dict) 191 if not os.path.exists(hcs_file_path): 192 model_file_path[key] = " " 193 else: 194 model_file_path[key] = path_dict 195 return model_file_path 196 197 def _mode_scan(self): 198 self.check_arg_raise_if_not_exist("root_dir") 199 self.check_arg_raise_if_not_exist("vendor_name") 200 self.check_arg_raise_if_not_exist("board_name") 201 root, vendor, _, _, board, _ = self.get_args() 202 if board.split("_")[-1] != "linux": 203 return HdfLiteScan( 204 root=root, vendor=vendor, board=board).get_model_scan() 205 else: 206 return HdfLinuxScan( 207 root=root, vendor=vendor, board=board).get_model_scan()