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 re 13from string import Template 14 15import hdf_utils 16from hdf_tool_exception import HdfToolException 17from hdf_tool_settings import HdfToolSettings 18from .hdf_command_error_code import CommandErrorCode 19 20 21class HdfDeviceInfoHcsFile(object): 22 def __init__(self, root, vendor, module, board, driver, path): 23 self.model_space_num = " " * 8 24 self.driver_space_num = " " * 12 25 if not path: 26 self.module = module 27 self.vendor = vendor 28 self.board = board 29 self.root = root 30 self.driver = driver 31 self.lines = None 32 board_parent_path = HdfToolSettings().get_board_parent_path(self.board) 33 self.hcspath = os.path.join(self.root, board_parent_path, "device_info.hcs") 34 self.data = { 35 "driver_name": self.driver, 36 "model_name": self.module, 37 "module_upper_case": self.module.upper(), 38 "driver_upper_case": self.driver.upper(), 39 "module_name": "_".join([self.module, self.driver]).upper() 40 } 41 else: 42 self.hcspath = path 43 self.root = root 44 self.file_path = hdf_utils.get_template_file_path(root) 45 if not os.path.exists(self.file_path): 46 raise HdfToolException( 47 'template file: %s not exist' % 48 self.file_path, CommandErrorCode.TARGET_NOT_EXIST) 49 if not os.path.exists(self.hcspath): 50 raise HdfToolException( 51 'hcs file: %s not exist' % 52 self.hcspath, CommandErrorCode.TARGET_NOT_EXIST) 53 54 def _save(self): 55 if self.lines: 56 config_info = HdfToolSettings().get_file_config_info() 57 write_fd = os.open(self.hcspath, config_info["flags"], config_info["modes"]) 58 with os.fdopen(write_fd, "w+", encoding="utf-8") as lwrite: 59 for line in self.lines: 60 lwrite.write(line) 61 62 def delete_driver(self, module, temp_flag="host"): 63 hcs_config = hdf_utils.read_file_lines(self.hcspath) 64 index_info = {} 65 count = 0 66 for index, line in enumerate(hcs_config): 67 if line.find("%s :: %s" % (module, temp_flag)) == -1: 68 continue 69 index_info["start_index"] = index 70 for child_index in range( 71 index_info.get("start_index"), len(hcs_config)): 72 if hcs_config[child_index].strip().find("{") != -1: 73 count += 1 74 elif hcs_config[child_index].strip() == "}": 75 count -= 1 76 if count == 0: 77 index_info["end_index"] = child_index 78 break 79 break 80 if index_info: 81 self.lines = hcs_config[0:index_info.get("start_index")] \ 82 + hcs_config[index_info.get("end_index") + 1:] 83 self._save() 84 return True 85 86 def add_model_hcs_file_config(self): 87 template_path = os.path.join(self.file_path, 88 'device_info_hcs.template') 89 lines = list(map(lambda x: self.model_space_num + x, 90 hdf_utils.read_file_lines(template_path))) 91 old_lines = list(filter(lambda x: x != "\n", 92 hdf_utils.read_file_lines(self.hcspath))) 93 94 new_data = old_lines[:-2] + lines + old_lines[-2:] 95 for index, _ in enumerate(new_data): 96 new_data[index] = Template(new_data[index]).substitute(self.data) 97 98 self.lines = new_data 99 self._save() 100 return self.hcspath 101 102 def add_model_hcs_file_config_user(self): 103 template_path = os.path.join(self.file_path, 104 'User_device_info_hcs.template') 105 lines = list(map(lambda x: self.model_space_num + x, 106 hdf_utils.read_file_lines(template_path))) 107 lines[-1] = self.model_space_num + lines[-1].strip() + "\n" 108 old_lines = list(filter(lambda x: x != "\n", 109 hdf_utils.read_file_lines(self.hcspath))) 110 111 new_data = old_lines[:-2] + lines + old_lines[-2:] 112 for index, _ in enumerate(new_data): 113 new_data[index] = Template(new_data[index]).substitute(self.data) 114 115 self.lines = new_data 116 self._save() 117 return self.hcspath 118 119 def add_hcs_config_to_exists_model(self, device): 120 template_path = os.path.join(self.file_path, 121 'exists_model_hcs_info.template') 122 lines = list(map(lambda x: self.driver_space_num + x, 123 hdf_utils.read_file_lines(template_path))) 124 old_lines = list(filter(lambda x: x != "\n", 125 hdf_utils.read_file_lines(self.hcspath))) 126 127 if self.judge_module_branch_exists(date_lines=old_lines): 128 return self.hcspath 129 end_index, start_index = self._get_model_index(old_lines) 130 model_hcs_lines = old_lines[start_index:end_index] 131 hcs_judge = self.judge_driver_hcs_exists(date_lines=model_hcs_lines) 132 if hcs_judge: 133 return self.hcspath 134 temp_replace_dict = { 135 "device_upper_case": device.upper() 136 } 137 self.data.update(temp_replace_dict) 138 for index, _ in enumerate(lines): 139 lines[index] = Template(lines[index]).substitute(self.data) 140 self.lines = old_lines[:end_index] + lines + old_lines[end_index:] 141 self._save() 142 return self.hcspath 143 144 def _get_model_index(self, old_lines): 145 model_start_index = 0 146 model_end_index = 0 147 start_state = False 148 count = 0 149 for index, old_line in enumerate(old_lines): 150 if old_line.strip().startswith(self.module) and start_state == False: 151 model_start_index = index 152 start_state = True 153 if start_state and old_line.find("{") != -1: 154 count += 1 155 elif start_state and old_line.find("}") != -1: 156 count -= 1 157 if count != 0: 158 continue 159 start_state = False 160 model_end_index = index 161 return model_end_index, model_start_index 162 163 def judge_driver_hcs_exists(self, date_lines): 164 for _, line in enumerate(date_lines): 165 if line.startswith("#"): 166 continue 167 elif line.find(self.driver) != -1: 168 return True 169 return False 170 171 def judge_module_branch_exists(self, date_lines): 172 module_branch_start = "%s :: host" % self.module 173 for _, line in enumerate(date_lines): 174 if line.startswith("#"): 175 continue 176 elif line.strip().find(module_branch_start) != -1: 177 return False 178 return True 179