0001
0002
0003
0004
0005
0006
0007
0008
0009 from dataclasses import dataclass
0010 import re
0011 from typing import Dict, Iterable, List, Set, Tuple
0012
0013 CONFIG_IS_NOT_SET_PATTERN = r'^# CONFIG_(\w+) is not set$'
0014 CONFIG_PATTERN = r'^CONFIG_(\w+)=(\S+|".*")$'
0015
0016 @dataclass(frozen=True)
0017 class KconfigEntry:
0018 name: str
0019 value: str
0020
0021 def __str__(self) -> str:
0022 if self.value == 'n':
0023 return f'# CONFIG_{self.name} is not set'
0024 return f'CONFIG_{self.name}={self.value}'
0025
0026
0027 class KconfigParseError(Exception):
0028 """Error parsing Kconfig defconfig or .config."""
0029
0030
0031 class Kconfig:
0032 """Represents defconfig or .config specified using the Kconfig language."""
0033
0034 def __init__(self) -> None:
0035 self._entries = {}
0036
0037 def __eq__(self, other) -> bool:
0038 if not isinstance(other, self.__class__):
0039 return False
0040 return self._entries == other._entries
0041
0042 def __repr__(self) -> str:
0043 return ','.join(str(e) for e in self.as_entries())
0044
0045 def as_entries(self) -> Iterable[KconfigEntry]:
0046 for name, value in self._entries.items():
0047 yield KconfigEntry(name, value)
0048
0049 def add_entry(self, name: str, value: str) -> None:
0050 self._entries[name] = value
0051
0052 def is_subset_of(self, other: 'Kconfig') -> bool:
0053 for name, value in self._entries.items():
0054 b = other._entries.get(name)
0055 if b is None:
0056 if value == 'n':
0057 continue
0058 return False
0059 if value != b:
0060 return False
0061 return True
0062
0063 def conflicting_options(self, other: 'Kconfig') -> List[Tuple[KconfigEntry, KconfigEntry]]:
0064 diff = []
0065 for name, value in self._entries.items():
0066 b = other._entries.get(name)
0067 if b and value != b:
0068 pair = (KconfigEntry(name, value), KconfigEntry(name, b))
0069 diff.append(pair)
0070 return diff
0071
0072 def merge_in_entries(self, other: 'Kconfig') -> None:
0073 for name, value in other._entries.items():
0074 self._entries[name] = value
0075
0076 def write_to_file(self, path: str) -> None:
0077 with open(path, 'a+') as f:
0078 for e in self.as_entries():
0079 f.write(str(e) + '\n')
0080
0081 def parse_file(path: str) -> Kconfig:
0082 with open(path, 'r') as f:
0083 return parse_from_string(f.read())
0084
0085 def parse_from_string(blob: str) -> Kconfig:
0086 """Parses a string containing Kconfig entries."""
0087 kconfig = Kconfig()
0088 is_not_set_matcher = re.compile(CONFIG_IS_NOT_SET_PATTERN)
0089 config_matcher = re.compile(CONFIG_PATTERN)
0090 for line in blob.split('\n'):
0091 line = line.strip()
0092 if not line:
0093 continue
0094
0095 match = config_matcher.match(line)
0096 if match:
0097 kconfig.add_entry(match.group(1), match.group(2))
0098 continue
0099
0100 empty_match = is_not_set_matcher.match(line)
0101 if empty_match:
0102 kconfig.add_entry(empty_match.group(1), 'n')
0103 continue
0104
0105 if line[0] == '#':
0106 continue
0107 raise KconfigParseError('Failed to parse: ' + line)
0108 return kconfig