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