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 14from hdf_tool_settings import HdfToolSettings 15import hdf_utils 16 17 18class HdfLiteKconfigFile(object): 19 def __init__(self, root): 20 self.file_path = hdf_utils.get_hdf_lite_kconfig_path(root) 21 self.orig_exist = False 22 if os.path.exists(self.file_path): 23 self.lines = hdf_utils.read_file_lines(self.file_path) 24 self.orig_exist = True 25 else: 26 self.lines = [] 27 self.vendor_pattern = r'source\s+".*/vendor/.*/Kconfig"' 28 self.target_pattern_format = r'source\s+".*/vendor/%s/.*/Kconfig"' 29 self.target_index = -1 30 31 def _save(self): 32 if self.orig_exist: 33 hdf_utils.write_file(self.file_path, ''.join(self.lines)) 34 35 def _find_all_vendors(self, target_vendor): 36 vendors = [] 37 for index, line in enumerate(self.lines): 38 if self.target_index == -1: 39 if re.search(self.target_pattern_format % target_vendor, line): 40 self.target_index = index 41 continue 42 if re.search(self.vendor_pattern, line): 43 vendors.append(index) 44 return vendors 45 46 def _comment_other_vendors(self, other_vendors): 47 for index in other_vendors: 48 if not hdf_utils.is_commented_line(self.lines[index], '#'): 49 self.lines[index] = '# %s' % self.lines[index] 50 51 def set_vendor(self, current_vendor): 52 other_vendors = self._find_all_vendors(current_vendor) 53 self._comment_other_vendors(other_vendors) 54 drivers_path = HdfToolSettings().get_drivers_path() 55 new_line = 'source "../../vendor/%s/%s/Kconfig"\n' % \ 56 (current_vendor, drivers_path) 57 if self.target_index != -1: 58 self.lines[self.target_index] = new_line 59 else: 60 pos = len(self.lines) - 1 61 while pos > 0 and len(self.lines[pos].strip()) == 0: 62 pos -= 1 63 pos += 1 64 self.lines.insert(pos, new_line) 65 self._save() 66