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 9import collections 10import re 11from typing import List, Set 12 13CONFIG_IS_NOT_SET_PATTERN = r'^# CONFIG_(\w+) is not set$' 14CONFIG_PATTERN = r'^CONFIG_(\w+)=(\S+|".*")$' 15 16KconfigEntryBase = collections.namedtuple('KconfigEntry', ['name', 'value']) 17 18class KconfigEntry(KconfigEntryBase): 19 20 def __str__(self) -> str: 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) 25 26 27class KconfigParseError(Exception): 28 """Error parsing Kconfig defconfig or .config.""" 29 30 31class Kconfig(object): 32 """Represents defconfig or .config specified using the Kconfig language.""" 33 34 def __init__(self) -> None: 35 self._entries = [] # type: List[KconfigEntry] 36 37 def entries(self) -> Set[KconfigEntry]: 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: 44 for a in self.entries(): 45 found = False 46 for b in other.entries(): 47 if a.name != b.name: 48 continue 49 if a.value != b.value: 50 return False 51 found = True 52 if a.value != 'n' and found == False: 53 return False 54 return True 55 56 def write_to_file(self, path: str) -> None: 57 with open(path, 'w') as f: 58 for entry in self.entries(): 59 f.write(str(entry) + '\n') 60 61 def parse_from_string(self, blob: str) -> None: 62 """Parses a string containing KconfigEntrys and populates this Kconfig.""" 63 self._entries = [] 64 is_not_set_matcher = re.compile(CONFIG_IS_NOT_SET_PATTERN) 65 config_matcher = re.compile(CONFIG_PATTERN) 66 for line in blob.split('\n'): 67 line = line.strip() 68 if not line: 69 continue 70 71 match = config_matcher.match(line) 72 if match: 73 entry = KconfigEntry(match.group(1), match.group(2)) 74 self.add_entry(entry) 75 continue 76 77 empty_match = is_not_set_matcher.match(line) 78 if empty_match: 79 entry = KconfigEntry(empty_match.group(1), 'n') 80 self.add_entry(entry) 81 continue 82 83 if line[0] == '#': 84 continue 85 else: 86 raise KconfigParseError('Failed to parse: ' + line) 87 88 def read_from_file(self, path: str) -> None: 89 with open(path, 'r') as f: 90 self.parse_from_string(f.read()) 91