1 # SPDX-License-Identifier: GPL-2.0
3 # Builds a .config from a kunitconfig.
5 # Copyright (C) 2019, Google LLC.
6 # Author: Felix Guo <felixguoxiuping@gmail.com>
7 # Author: Brendan Higgins <brendanhiggins@google.com>
12 CONFIG_IS_NOT_SET_PATTERN = r'^# CONFIG_\w+ is not set$'
13 CONFIG_PATTERN = r'^CONFIG_\w+=\S+$'
15 KconfigEntryBase = collections.namedtuple('KconfigEntry', ['raw_entry'])
18 class KconfigEntry(KconfigEntryBase):
20 def __str__(self) -> str:
24 class KconfigParseError(Exception):
25 """Error parsing Kconfig defconfig or .config."""
28 class Kconfig(object):
29 """Represents defconfig or .config specified using the Kconfig language."""
35 return set(self._entries)
37 def add_entry(self, entry: KconfigEntry) -> None:
38 self._entries.append(entry)
40 def is_subset_of(self, other: 'Kconfig') -> bool:
41 return self.entries().issubset(other.entries())
43 def write_to_file(self, path: str) -> None:
44 with open(path, 'w') as f:
45 for entry in self.entries():
46 f.write(str(entry) + '\n')
48 def parse_from_string(self, blob: str) -> None:
49 """Parses a string containing KconfigEntrys and populates this Kconfig."""
51 is_not_set_matcher = re.compile(CONFIG_IS_NOT_SET_PATTERN)
52 config_matcher = re.compile(CONFIG_PATTERN)
53 for line in blob.split('\n'):
57 elif config_matcher.match(line) or is_not_set_matcher.match(line):
58 self._entries.append(KconfigEntry(line))
62 raise KconfigParseError('Failed to parse: ' + line)
64 def read_from_file(self, path: str) -> None:
65 with open(path, 'r') as f:
66 self.parse_from_string(f.read())