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_command_error_code import CommandErrorCode 18 19 20class HdfVendorMakeFile(object): 21 def __init__(self, root, vendor, kernel, path): 22 if path: 23 self.file_path = path 24 else: 25 self.vendor = vendor 26 self.kernel = kernel 27 self.file_path = hdf_utils.\ 28 get_vendor_makefile_path(root, self.kernel) 29 if not os.path.exists(self.file_path): 30 raise HdfToolException('file: %s not exist' % self.file_path, 31 CommandErrorCode.TARGET_NOT_EXIST) 32 self.contents = hdf_utils.read_file_lines(self.file_path) 33 self.template_str = \ 34 "obj-$(CONFIG_DRIVERS_HDF_${module_upper_case}) " \ 35 "+= model/${module_lower_case}/\n" 36 37 def _begin_end(self, module): 38 module_id = hdf_utils.get_id(self.vendor, module) 39 begin = '\n# <begin %s\n' % module_id 40 end = '\n# %s end>\n' % module_id 41 return begin, end 42 43 def add_module(self, data_model): 44 self.contents.append(Template(self.template_str) 45 .safe_substitute(data_model)) 46 hdf_utils.write_file_lines(self.file_path, self.contents) 47 return self.file_path 48 49 def delete_module(self, module): 50 module_converter = hdf_utils.WordsConverter(module) 51 data_model = { 52 'module_upper_case': module_converter.upper_case(), 53 'module_lower_case': module_converter.lower_case(), 54 } 55 delete_template = Template( 56 self.template_str).safe_substitute(data_model) 57 if delete_template in self.contents: 58 self.contents.remove(delete_template) 59 hdf_utils.write_file_lines( 60 content=self.contents, file_path=self.file_path) 61 62 def rename_vendor(self): 63 pattern = r'vendor/([a-zA-Z0-9_\-]+)/' 64 replacement = 'vendor/%s/' % self.vendor 65 new_content = re.sub(pattern, replacement, self.contents) 66 hdf_utils.write_file(self.file_path, new_content) 67