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 HdfVendorKconfigFile(object):
19    def __init__(self, root, vendor, kernel, path):
20        if not path:
21            self.kernel = kernel
22            self.root = root
23            self.vendor = vendor
24            self.kconfig_path = hdf_utils.\
25                get_vendor_kconfig_path(self.root, self.kernel)
26        else:
27            self.kconfig_path = path
28        if os.path.exists(self.kconfig_path):
29            self.lines = hdf_utils.read_file_lines(self.kconfig_path)
30        else:
31            self.lines = []
32        drivers_path = HdfToolSettings().get_drivers_path_adapter()
33        if kernel.capitalize() == "Liteos" or re.search(
34                r'liteos/Kconfig', self.kconfig_path):
35            line_pattern = \
36                r'^\s*source\s+"../../(%s/([a-zA-Z0-9_\-]+)/.*)"'\
37                % (drivers_path)
38            self.line_re = re.compile(line_pattern)
39            self.line_prefix = 'source "../../%s/%s/%s"'\
40                               % (drivers_path, 'liteos', 'model')
41        elif kernel.capitalize() == "Linux" or re.search(
42                r'linux/Kconfig', self.kconfig_path):
43            line_pattern = \
44                r'^\s*source\s+"../../(%s/([a-zA-Z0-9_\-]+)/.*)"'\
45                % (drivers_path)
46            self.line_re = re.compile(line_pattern)
47            self.line_prefix = 'source "drivers/hdf/khdf/model/%s/Kconfig"\n'
48
49    def _save(self):
50        if self.lines:
51            hdf_utils.write_file(self.kconfig_path, ''.join(self.lines))
52
53    def get_module_and_config_path(self):
54        tuples = []
55        for line in self.lines:
56            match_obj = self.line_re.search(line)
57            if match_obj:
58                k_path_raw = match_obj.group(1).replace('/', os.sep)
59                k_path = os.path.join(self.root, k_path_raw)
60                tuples.append((match_obj.group(2), k_path))
61        return tuples
62
63    def _find_line(self, line_part):
64        for index, line in enumerate(self.lines):
65            if line_part in line:
66                return index, line
67        return 0, ''
68
69    def add_module(self, module_to_k_path_parts):
70        module_k_part = '/'.join(module_to_k_path_parts)
71        index, line = self._find_line("model/" + module_k_part)
72        if line:
73            return
74        line = '\n%s/%s"\n' % (self.line_prefix, module_k_part)
75        if self.lines:
76            if self.lines[-1].endswith('\n'):
77                if self.kernel.capitalize() == "Liteos":
78                    line = '%s/%s"\n' % (self.line_prefix[:-1], module_k_part)
79                elif self.kernel.capitalize() == "Linux":
80                    line = self.line_prefix % (module_to_k_path_parts[0])
81        for file_index, file_line in enumerate(self.lines):
82            if file_line.strip() == "endif":
83                self.lines.insert(file_index - 1, line)
84                break
85        self._save()
86        return self.kconfig_path
87
88    def delete_module(self, module):
89        if self.line_prefix.find("Kconfig") > 0:
90            line_part = self.line_prefix % module
91        else:
92            line_part = '%s/%s/Kconfig"\n' % (self.line_prefix[:-1], module)
93        index, line = self._find_line(line_part)
94        if line:
95            self.lines[index] = ''
96        self._save()
97
98    def rename_vendor(self, ):
99        for i, line in enumerate(self.lines):
100            pattern = r'vendor/([a-zA-Z0-9_\-]+)/'
101            replacement = 'vendor/%s/' % self.vendor
102            self.lines[i] = re.sub(pattern, replacement, line)
103        self._save()
104