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 13 14import hdf_utils 15from hdf_tool_settings import HdfToolSettings 16 17 18class HdfDefconfigAndPatch(object): 19 def __init__(self, root, vendor, kernel, 20 board, data_model, new_demo_config): 21 self.root = root 22 self.vendor = vendor 23 self.kernel = kernel 24 self.board = board 25 self.data_model = data_model 26 self.new_demo_config = new_demo_config 27 self.file_path = hdf_utils.get_template_file_path(root) 28 self.drivers_path_list = HdfToolSettings().\ 29 get_board_parent_file(self.board) 30 31 def get_config_config(self): 32 return os.path.join(self.root, "kernel", self.kernel, "config") 33 34 def get_config_patch(self): 35 return os.path.join(self.root, "kernel", self.kernel, "patches") 36 37 def add_module(self, path, files, codetype): 38 for filename in os.listdir(path): 39 new_path = os.path.join(path, filename) 40 if not os.path.isdir(new_path): 41 self.find_file(new_path, files, codetype) 42 else: 43 self.add_module(path=new_path, files=files, codetype=codetype) 44 return files 45 46 def delete_module(self, path): 47 with open(path, "rb") as f_read: 48 lines = f_read.readlines() 49 path_state = False 50 config_state = False 51 if self.new_demo_config[-1].encode("utf-8") in lines: 52 config_state = True 53 index_num = lines.index(self.new_demo_config[-1].encode("utf-8")) 54 if ("+" + self.new_demo_config[-1]).encode("utf-8") in lines: 55 path_state = True 56 index_num = lines.index(("+" + self.new_demo_config[-1]).encode("utf-8")) 57 if config_state or path_state: 58 if path.split(".")[-1] != "patch": 59 if lines[index_num - 1] == self.new_demo_config[0].encode("utf-8"): 60 del lines[index_num - 1] 61 lines.remove(self.new_demo_config[-1].encode("utf-8")) 62 else: 63 if lines[index_num - 1] == ("+" + self.new_demo_config[0]).encode("utf-8"): 64 del lines[index_num - 1] 65 lines.remove(("+" + self.new_demo_config[-1]).encode("utf-8")) 66 config_info = HdfToolSettings().get_file_config_info() 67 write_fd = os.open(path, config_info["flags"], config_info["modes"]) 68 with os.fdopen(write_fd, "wb") as f_write: 69 f_write.writelines(lines) 70 71 def rename_vendor(self): 72 pattern = r'vendor/([a-zA-Z0-9_\-]+)/' 73 replacement = 'vendor/%s/' % self.vendor 74 new_content = re.sub(pattern, replacement, self.contents) 75 hdf_utils.write_file(self.file_path, new_content) 76 77 def find_file(self, path, files, codetype): 78 if path.split("\\")[-1] in self.drivers_path_list or \ 79 path.split("/")[-1] in self.drivers_path_list: 80 files.append(path) 81 if codetype is None: 82 self.binary_type_write(path) 83 else: 84 self.utf_type_write(path, codetype) 85 return files 86 87 def binary_type_write(self, path): 88 with open(path, "rb") as fread: 89 data_lines_old = fread.readlines() 90 data_lines = self.filter_lines(data_lines_old) 91 insert_index = None 92 state = False 93 for index, line in enumerate(data_lines): 94 if line.find("CONFIG_DRIVERS_HDF_INPUT=y".encode('utf-8')) >= 0: 95 insert_index = index 96 elif line.find(self.new_demo_config[-1].encode('utf-8')) >= 0: 97 state = True 98 break 99 if not state: 100 if path.split(".")[-1] != "patch": 101 for index_num, info in enumerate(self.new_demo_config): 102 data_lines.insert(insert_index + (1 + index_num), info.encode('utf-8')) 103 else: 104 for index_num, info in enumerate(self.new_demo_config): 105 data_lines.insert( 106 insert_index + (1 + index_num), ("+" + info).encode('utf-8')) 107 108 with open(path, "wb") as fwrite: 109 fwrite.writelines(data_lines) 110 111 def utf_type_write(self, path, codetype): 112 with open(path, "r+", encoding=codetype) as fread: 113 data_lines_old = fread.readlines() 114 data_lines = data_lines_old 115 insert_index = None 116 state = False 117 for index, line in enumerate(data_lines): 118 if line.find("CONFIG_DRIVERS_HDF_INPUT=y") >= 0: 119 insert_index = index 120 elif line.find(self.new_demo_config[-1]) >= 0: 121 state = True 122 break 123 if not state: 124 if path.split(".")[-1] != "patch": 125 for index_num, info in enumerate(self.new_demo_config): 126 data_lines.insert(insert_index + (1 + index_num), info) 127 else: 128 for index_num, info in enumerate(self.new_demo_config): 129 data_lines.insert( 130 insert_index + (1 + index_num), ("+" + info)) 131 with open(path, "w", encoding=codetype) as fwrite: 132 fwrite.writelines(data_lines) 133 134 def filter_lines(self, data_lines_old): 135 if len(self.new_demo_config) == 2: 136 temp_str = self.new_demo_config[0] 137 data_lines = list( 138 filter( 139 lambda x: 140 hdf_utils.judge_enable_line( 141 enable_line=x, device_base=temp_str.split("=")[0]), 142 data_lines_old)) 143 else: 144 data_lines = data_lines_old 145 return data_lines 146 147