kunit_config.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 <[email protected]>
  7. # Author: Brendan Higgins <[email protected]>
  8. from dataclasses import dataclass
  9. import re
  10. from typing import Dict, Iterable, List, Set, Tuple
  11. CONFIG_IS_NOT_SET_PATTERN = r'^# CONFIG_(\w+) is not set$'
  12. CONFIG_PATTERN = r'^CONFIG_(\w+)=(\S+|".*")$'
  13. @dataclass(frozen=True)
  14. class KconfigEntry:
  15. name: str
  16. value: str
  17. def __str__(self) -> str:
  18. if self.value == 'n':
  19. return f'# CONFIG_{self.name} is not set'
  20. return f'CONFIG_{self.name}={self.value}'
  21. class KconfigParseError(Exception):
  22. """Error parsing Kconfig defconfig or .config."""
  23. class Kconfig:
  24. """Represents defconfig or .config specified using the Kconfig language."""
  25. def __init__(self) -> None:
  26. self._entries = {} # type: Dict[str, str]
  27. def __eq__(self, other) -> bool:
  28. if not isinstance(other, self.__class__):
  29. return False
  30. return self._entries == other._entries
  31. def __repr__(self) -> str:
  32. return ','.join(str(e) for e in self.as_entries())
  33. def as_entries(self) -> Iterable[KconfigEntry]:
  34. for name, value in self._entries.items():
  35. yield KconfigEntry(name, value)
  36. def add_entry(self, name: str, value: str) -> None:
  37. self._entries[name] = value
  38. def remove_entry(self, entry_set) -> None:
  39. for entry in entry_set:
  40. print(f"Remove invalid config: {str(entry)}")
  41. self._entries.pop(entry.name, None)
  42. def is_subset_of(self, other: 'Kconfig') -> bool:
  43. for name, value in self._entries.items():
  44. b = other._entries.get(name)
  45. if b is None:
  46. if value == 'n':
  47. continue
  48. return False
  49. if value != b:
  50. return False
  51. return True
  52. def conflicting_options(self, other: 'Kconfig') -> List[Tuple[KconfigEntry, KconfigEntry]]:
  53. diff = [] # type: List[Tuple[KconfigEntry, KconfigEntry]]
  54. for name, value in self._entries.items():
  55. b = other._entries.get(name)
  56. if b and value != b:
  57. pair = (KconfigEntry(name, value), KconfigEntry(name, b))
  58. diff.append(pair)
  59. return diff
  60. def merge_in_entries(self, other: 'Kconfig') -> None:
  61. for name, value in other._entries.items():
  62. self._entries[name] = value
  63. def write_to_file(self, path: str) -> None:
  64. with open(path, 'a+') as f:
  65. for e in self.as_entries():
  66. f.write(str(e) + '\n')
  67. def parse_file(path: str) -> Kconfig:
  68. with open(path, 'r') as f:
  69. return parse_from_string(f.read())
  70. def parse_from_string(blob: str) -> Kconfig:
  71. """Parses a string containing Kconfig entries."""
  72. kconfig = Kconfig()
  73. is_not_set_matcher = re.compile(CONFIG_IS_NOT_SET_PATTERN)
  74. config_matcher = re.compile(CONFIG_PATTERN)
  75. for line in blob.split('\n'):
  76. line = line.strip()
  77. if not line:
  78. continue
  79. match = config_matcher.match(line)
  80. if match:
  81. kconfig.add_entry(match.group(1), match.group(2))
  82. continue
  83. empty_match = is_not_set_matcher.match(line)
  84. if empty_match:
  85. kconfig.add_entry(empty_match.group(1), 'n')
  86. continue
  87. if line[0] == '#':
  88. continue
  89. raise KconfigParseError('Failed to parse: ' + line)
  90. return kconfig