kunit_config.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. from dataclasses import dataclass
  9. import re
  10. from typing import Any, Dict, Iterable, List, 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: Any) -> 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 is_subset_of(self, other: 'Kconfig') -> bool:
  39. for name, value in self._entries.items():
  40. b = other._entries.get(name)
  41. if b is None:
  42. if value == 'n':
  43. continue
  44. return False
  45. if value != b:
  46. return False
  47. return True
  48. def conflicting_options(self, other: 'Kconfig') -> List[Tuple[KconfigEntry, KconfigEntry]]:
  49. diff = [] # type: List[Tuple[KconfigEntry, KconfigEntry]]
  50. for name, value in self._entries.items():
  51. b = other._entries.get(name)
  52. if b and value != b:
  53. pair = (KconfigEntry(name, value), KconfigEntry(name, b))
  54. diff.append(pair)
  55. return diff
  56. def merge_in_entries(self, other: 'Kconfig') -> None:
  57. for name, value in other._entries.items():
  58. self._entries[name] = value
  59. def write_to_file(self, path: str) -> None:
  60. with open(path, 'a+') as f:
  61. for e in self.as_entries():
  62. f.write(str(e) + '\n')
  63. def parse_file(path: str) -> Kconfig:
  64. with open(path, 'r') as f:
  65. return parse_from_string(f.read())
  66. def parse_from_string(blob: str) -> Kconfig:
  67. """Parses a string containing Kconfig entries."""
  68. kconfig = Kconfig()
  69. is_not_set_matcher = re.compile(CONFIG_IS_NOT_SET_PATTERN)
  70. config_matcher = re.compile(CONFIG_PATTERN)
  71. for line in blob.split('\n'):
  72. line = line.strip()
  73. if not line:
  74. continue
  75. match = config_matcher.match(line)
  76. if match:
  77. kconfig.add_entry(match.group(1), match.group(2))
  78. continue
  79. empty_match = is_not_set_matcher.match(line)
  80. if empty_match:
  81. kconfig.add_entry(empty_match.group(1), 'n')
  82. continue
  83. if line[0] == '#':
  84. continue
  85. raise KconfigParseError('Failed to parse: ' + line)
  86. return kconfig