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 12from string import Template 13 14import hdf_utils 15from hdf_tool_settings import HdfToolSettings 16from hdf_tool_exception import HdfToolException 17from command_line.hdf_command_error_code import CommandErrorCode 18from .hdf_manager_config_file import HdfManagerConfigFile 19from .hdf_hcs_file import HdfHcsFile 20 21 22 23 24class HdfDriverConfigFile(object): 25 def __init__(self, root, board, module, driver, kernel, only_path=False): 26 self.root = root 27 self.board = board 28 self.module = module 29 self.driver = driver 30 self.kernel = kernel 31 bpp = HdfToolSettings().get_board_parent_path(self.board) 32 board_path = os.path.join(self.root, bpp, self.board) 33 34 if not os.path.exists(board_path): 35 raise HdfToolException('board: %s not exist' % board_path, 36 CommandErrorCode.TARGET_NOT_EXIST) 37 self.config_dir = os.path.join(board_path, 'hdf_config') 38 self.drv_dir = os.path.join(self.config_dir, self.module) 39 self.drv_config_path = os.path.join( 40 self.drv_dir, '%s_config.hcs' % self.driver) 41 if only_path: 42 return 43 manager_hcs_path = os.path.join(self.config_dir, 'device_info', 44 'device_info.hcs') 45 self.manager_hcs = HdfManagerConfigFile(manager_hcs_path) 46 hdf_hcs_path = os.path.join(self.config_dir, 'hdf.hcs') 47 self.hdf_hcs = HdfHcsFile(hdf_hcs_path) 48 49 def _check_and_create_common_config(self): 50 self.manager_hcs.check_and_create() 51 self.hdf_hcs.check_and_create() 52 if not os.path.exists(self.drv_dir): 53 os.makedirs(self.drv_dir) 54 55 def create_driver(self): 56 self._check_and_create_common_config() 57 template_str = hdf_utils.get_template('hdf_driver_config.template') 58 config_content = Template(template_str).safe_substitute({}) 59 hdf_utils.write_file(self.drv_config_path, config_content) 60 self.manager_hcs.add_device(self.module, self.driver) 61 self.hdf_hcs.add_driver(self.module, self.driver) 62 63 def delete_driver(self): 64 if not os.path.exists(self.drv_config_path): 65 return 66 os.remove(self.drv_config_path) 67 self.manager_hcs.delete_device(self.module, self.driver) 68 self.hdf_hcs.delete_driver(self.module, self.driver) 69 70 def get_drv_config_path(self): 71 if os.path.exists(self.drv_config_path): 72 return os.path.realpath(self.drv_config_path) 73 else: 74 return '' 75