Commit | Line | Data |
---|---|---|
6ebf5866 FG |
1 | # SPDX-License-Identifier: GPL-2.0 |
2 | # | |
3 | # Builds a .config from a kunitconfig. | |
4 | # | |
5 | # Copyright (C) 2019, Google LLC. | |
6 | # Author: Felix Guo <felixguoxiuping@gmail.com> | |
7 | # Author: Brendan Higgins <brendanhiggins@google.com> | |
8 | ||
9 | import collections | |
10 | import re | |
09641f7c | 11 | from typing import List, Set |
6ebf5866 | 12 | |
97752c39 | 13 | CONFIG_IS_NOT_SET_PATTERN = r'^# CONFIG_(\w+) is not set$' |
3f37d14b | 14 | CONFIG_PATTERN = r'^CONFIG_(\w+)=(\S+|".*")$' |
6ebf5866 | 15 | |
7421b1a4 | 16 | KconfigEntryBase = collections.namedtuple('KconfigEntryBase', ['name', 'value']) |
6ebf5866 FG |
17 | |
18 | class KconfigEntry(KconfigEntryBase): | |
19 | ||
20 | def __str__(self) -> str: | |
97752c39 DG |
21 | if self.value == 'n': |
22 | return r'# CONFIG_%s is not set' % (self.name) | |
23 | else: | |
24 | return r'CONFIG_%s=%s' % (self.name, self.value) | |
6ebf5866 FG |
25 | |
26 | ||
27 | class KconfigParseError(Exception): | |
28 | """Error parsing Kconfig defconfig or .config.""" | |
29 | ||
30 | ||
31 | class Kconfig(object): | |
32 | """Represents defconfig or .config specified using the Kconfig language.""" | |
33 | ||
09641f7c DL |
34 | def __init__(self) -> None: |
35 | self._entries = [] # type: List[KconfigEntry] | |
6ebf5866 | 36 | |
09641f7c | 37 | def entries(self) -> Set[KconfigEntry]: |
6ebf5866 FG |
38 | return set(self._entries) |
39 | ||
40 | def add_entry(self, entry: KconfigEntry) -> None: | |
41 | self._entries.append(entry) | |
42 | ||
43 | def is_subset_of(self, other: 'Kconfig') -> bool: | |
d3bae4a0 | 44 | other_dict = {e.name: e.value for e in other.entries()} |
97752c39 | 45 | for a in self.entries(): |
d3bae4a0 DL |
46 | b = other_dict.get(a.name) |
47 | if b is None: | |
48 | if a.value == 'n': | |
97752c39 | 49 | continue |
d3bae4a0 DL |
50 | return False |
51 | elif a.value != b: | |
97752c39 DG |
52 | return False |
53 | return True | |
6ebf5866 | 54 | |
87c9c163 BH |
55 | def merge_in_entries(self, other: 'Kconfig') -> None: |
56 | if other.is_subset_of(self): | |
57 | return | |
58 | self._entries = list(self.entries().union(other.entries())) | |
59 | ||
6ebf5866 | 60 | def write_to_file(self, path: str) -> None: |
87c9c163 | 61 | with open(path, 'a+') as f: |
6ebf5866 FG |
62 | for entry in self.entries(): |
63 | f.write(str(entry) + '\n') | |
64 | ||
98978490 DL |
65 | def parse_file(path: str) -> Kconfig: |
66 | with open(path, 'r') as f: | |
67 | return parse_from_string(f.read()) | |
68 | ||
69 | def parse_from_string(blob: str) -> Kconfig: | |
70 | """Parses a string containing Kconfig entries.""" | |
71 | kconfig = Kconfig() | |
72 | is_not_set_matcher = re.compile(CONFIG_IS_NOT_SET_PATTERN) | |
73 | config_matcher = re.compile(CONFIG_PATTERN) | |
74 | for line in blob.split('\n'): | |
75 | line = line.strip() | |
76 | if not line: | |
77 | continue | |
78 | ||
79 | match = config_matcher.match(line) | |
80 | if match: | |
81 | entry = KconfigEntry(match.group(1), match.group(2)) | |
82 | kconfig.add_entry(entry) | |
83 | continue | |
84 | ||
85 | empty_match = is_not_set_matcher.match(line) | |
86 | if empty_match: | |
87 | entry = KconfigEntry(empty_match.group(1), 'n') | |
88 | kconfig.add_entry(entry) | |
89 | continue | |
90 | ||
91 | if line[0] == '#': | |
92 | continue | |
93 | else: | |
94 | raise KconfigParseError('Failed to parse: ' + line) | |
95 | return kconfig |