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 HdfVendorMkFile(object):
22    def __init__(self, root, vendor):
23        self.vendor = vendor
24        self.file_path = hdf_utils.get_vendor_lite_mk_path(root)
25        if not os.path.exists(self.file_path):
26            raise HdfToolException('file: %s not exist' % self.file_path,
27                                   CommandErrorCode.TARGET_NOT_EXIST)
28        self.contents = hdf_utils.read_file(self.file_path)
29
30    def _begin_end(self, module):
31        module_id = hdf_utils.get_id(module)
32        begin = '\n# <begin %s\n' % module_id
33        end = '\n# %s end>\n' % module_id
34        return begin, end
35
36    def _create_module(self, module, need_content=True):
37        begin, end = self._begin_end(module)
38        if not need_content:
39            return hdf_utils.SectionContent(begin, '', end)
40        vendor_converter = hdf_utils.WordsConverter(self.vendor)
41        module_converter = hdf_utils.WordsConverter(module)
42        data_model = {
43            'module_upper_case': module_converter.upper_case(),
44            'module_lower_case': module_converter.lower_case(),
45            'vendor_lower_case': vendor_converter.lower_case(),
46            'drivers_path': HdfToolSettings().get_drivers_path_adapter()
47        }
48        template_str = hdf_utils.get_template('hdf_vendor_mk_module.template')
49        module_item = Template(template_str).safe_substitute(data_model)
50        return hdf_utils.SectionContent(begin, module_item, end)
51
52    def add_module(self, module):
53        module_ = self._create_module(module)
54        old_range = hdf_utils.find_section(self.contents, module_)
55        if old_range:
56            hdf_utils.replace_and_save(self.contents, self.file_path,
57                                       old_range, module_)
58            return
59        hdf_utils.append_and_save(self.contents, self.file_path, module_)
60        return self.file_path
61
62    def delete_module(self, file_path, module):
63        module_content = self._create_module(module, False)
64        file_contents = hdf_utils.read_file(file_path)
65        old_range = hdf_utils.find_section(file_contents, module_content)
66        if not old_range:
67            return
68        hdf_utils.delete_and_save(file_contents, self.file_path, old_range)
69
70    def rename_vendor(self):
71        pattern = r'vendor/([a-zA-Z0-9_\-]+)/'
72        replacement = 'vendor/%s/' % self.vendor
73        new_content = re.sub(pattern, replacement, self.contents)
74        hdf_utils.write_file(self.file_path, new_content)
75