1 # SPDX-License-Identifier: GPL-2.0 1 # SPDX-License-Identifier: GPL-2.0 2 # 2 # 3 # Builds a .config from a kunitconfig. 3 # Builds a .config from a kunitconfig. 4 # 4 # 5 # Copyright (C) 2019, Google LLC. 5 # Copyright (C) 2019, Google LLC. 6 # Author: Felix Guo <felixguoxiuping@gmail.com> 6 # Author: Felix Guo <felixguoxiuping@gmail.com> 7 # Author: Brendan Higgins <brendanhiggins@googl 7 # Author: Brendan Higgins <brendanhiggins@google.com> 8 8 9 from dataclasses import dataclass !! 9 import collections 10 import re 10 import re 11 from typing import Any, Dict, Iterable, List, !! 11 from typing import List, Set 12 12 13 CONFIG_IS_NOT_SET_PATTERN = r'^# CONFIG_(\w+) 13 CONFIG_IS_NOT_SET_PATTERN = r'^# CONFIG_(\w+) is not set$' 14 CONFIG_PATTERN = r'^CONFIG_(\w+)=(\S+|".*")$' 14 CONFIG_PATTERN = r'^CONFIG_(\w+)=(\S+|".*")$' 15 15 16 @dataclass(frozen=True) !! 16 KconfigEntryBase = collections.namedtuple('KconfigEntryBase', ['name', 'value']) 17 class KconfigEntry: !! 17 18 name: str !! 18 class KconfigEntry(KconfigEntryBase): 19 value: str << 20 19 21 def __str__(self) -> str: 20 def __str__(self) -> str: 22 if self.value == 'n': 21 if self.value == 'n': 23 return f'# CONFIG_{sel !! 22 return r'# CONFIG_%s is not set' % (self.name) 24 return f'CONFIG_{self.name}={s !! 23 else: >> 24 return r'CONFIG_%s=%s' % (self.name, self.value) 25 25 26 26 27 class KconfigParseError(Exception): 27 class KconfigParseError(Exception): 28 """Error parsing Kconfig defconfig or 28 """Error parsing Kconfig defconfig or .config.""" 29 29 30 30 31 class Kconfig: !! 31 class Kconfig(object): 32 """Represents defconfig or .config spe 32 """Represents defconfig or .config specified using the Kconfig language.""" 33 33 34 def __init__(self) -> None: 34 def __init__(self) -> None: 35 self._entries = {} # type: Di !! 35 self._entries = [] # type: List[KconfigEntry] 36 36 37 def __eq__(self, other: Any) -> bool: !! 37 def entries(self) -> Set[KconfigEntry]: 38 if not isinstance(other, self. !! 38 return set(self._entries) 39 return False << 40 return self._entries == other. << 41 << 42 def __repr__(self) -> str: << 43 return ','.join(str(e) for e i << 44 << 45 def as_entries(self) -> Iterable[Kconf << 46 for name, value in self._entri << 47 yield KconfigEntry(nam << 48 39 49 def add_entry(self, name: str, value: !! 40 def add_entry(self, entry: KconfigEntry) -> None: 50 self._entries[name] = value !! 41 self._entries.append(entry) 51 42 52 def is_subset_of(self, other: 'Kconfig 43 def is_subset_of(self, other: 'Kconfig') -> bool: 53 for name, value in self._entri !! 44 for a in self.entries(): 54 b = other._entries.get !! 45 found = False 55 if b is None: !! 46 for b in other.entries(): 56 if value == 'n !! 47 if a.name != b.name: 57 contin 48 continue 58 return False !! 49 if a.value != b.value: 59 if value != b: !! 50 return False >> 51 found = True >> 52 if a.value != 'n' and found == False: 60 return False 53 return False 61 return True 54 return True 62 55 63 def conflicting_options(self, other: ' << 64 diff = [] # type: List[Tuple[ << 65 for name, value in self._entri << 66 b = other._entries.get << 67 if b and value != b: << 68 pair = (Kconfi << 69 diff.append(pa << 70 return diff << 71 << 72 def merge_in_entries(self, other: 'Kco << 73 for name, value in other._entr << 74 self._entries[name] = << 75 << 76 def write_to_file(self, path: str) -> 56 def write_to_file(self, path: str) -> None: 77 with open(path, 'a+') as f: !! 57 with open(path, 'w') as f: 78 for e in self.as_entri !! 58 for entry in self.entries(): 79 f.write(str(e) !! 59 f.write(str(entry) + '\n') 80 !! 60 81 def parse_file(path: str) -> Kconfig: !! 61 def parse_from_string(self, blob: str) -> None: 82 with open(path, 'r') as f: !! 62 """Parses a string containing KconfigEntrys and populates this Kconfig.""" 83 return parse_from_string(f.rea !! 63 self._entries = [] 84 !! 64 is_not_set_matcher = re.compile(CONFIG_IS_NOT_SET_PATTERN) 85 def parse_from_string(blob: str) -> Kconfig: !! 65 config_matcher = re.compile(CONFIG_PATTERN) 86 """Parses a string containing Kconfig !! 66 for line in blob.split('\n'): 87 kconfig = Kconfig() !! 67 line = line.strip() 88 is_not_set_matcher = re.compile(CONFIG !! 68 if not line: 89 config_matcher = re.compile(CONFIG_PAT !! 69 continue 90 for line in blob.split('\n'): !! 70 91 line = line.strip() !! 71 match = config_matcher.match(line) 92 if not line: !! 72 if match: 93 continue !! 73 entry = KconfigEntry(match.group(1), match.group(2)) 94 !! 74 self.add_entry(entry) 95 match = config_matcher.match(l !! 75 continue 96 if match: !! 76 97 kconfig.add_entry(matc !! 77 empty_match = is_not_set_matcher.match(line) 98 continue !! 78 if empty_match: 99 !! 79 entry = KconfigEntry(empty_match.group(1), 'n') 100 empty_match = is_not_set_match !! 80 self.add_entry(entry) 101 if empty_match: !! 81 continue 102 kconfig.add_entry(empt !! 82 103 continue !! 83 if line[0] == '#': 104 !! 84 continue 105 if line[0] == '#': !! 85 else: 106 continue !! 86 raise KconfigParseError('Failed to parse: ' + line) 107 raise KconfigParseError('Faile !! 87 108 return kconfig !! 88 def read_from_file(self, path: str) -> None: >> 89 with open(path, 'r') as f: >> 90 self.parse_from_string(f.read())
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.