Skip to content

Commit 9897849

Browse files
dlatypovshuahkh
authored andcommitted
kunit: tool: move Kconfig read_from_file/parse_from_string to package-level
read_from_file() clears its `self` Kconfig object and parses a config file. It is a way to construct Kconfig objects more so than an operation on Kconfig objects. This is reflected in the fact its only ever used as: kconfig = kunit_config.Kconfig() kconfig.read_from_file(path) So clean this up and simplify callers by replacing it with kconfig = kunit_config.parse_file(path) Do the same thing for the related parse_from_string() function as well. Signed-off-by: Daniel Latypov <[email protected]> Reviewed-by: David Gow <[email protected]> Reviewed-by: Brendan Higgins <[email protected]> Signed-off-by: Shuah Khan <[email protected]>
1 parent 142189f commit 9897849

File tree

3 files changed

+37
-42
lines changed

3 files changed

+37
-42
lines changed

tools/testing/kunit/kunit_config.py

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -62,33 +62,34 @@ def write_to_file(self, path: str) -> None:
6262
for entry in self.entries():
6363
f.write(str(entry) + '\n')
6464

65-
def parse_from_string(self, blob: str) -> None:
66-
"""Parses a string containing KconfigEntrys and populates this Kconfig."""
67-
self._entries = []
68-
is_not_set_matcher = re.compile(CONFIG_IS_NOT_SET_PATTERN)
69-
config_matcher = re.compile(CONFIG_PATTERN)
70-
for line in blob.split('\n'):
71-
line = line.strip()
72-
if not line:
73-
continue
74-
75-
match = config_matcher.match(line)
76-
if match:
77-
entry = KconfigEntry(match.group(1), match.group(2))
78-
self.add_entry(entry)
79-
continue
80-
81-
empty_match = is_not_set_matcher.match(line)
82-
if empty_match:
83-
entry = KconfigEntry(empty_match.group(1), 'n')
84-
self.add_entry(entry)
85-
continue
86-
87-
if line[0] == '#':
88-
continue
89-
else:
90-
raise KconfigParseError('Failed to parse: ' + line)
91-
92-
def read_from_file(self, path: str) -> None:
93-
with open(path, 'r') as f:
94-
self.parse_from_string(f.read())
65+
def parse_file(path: str) -> Kconfig:
66+
with open(path, 'r') as f:
67+
return parse_from_string(f.read())
68+
69+
def parse_from_string(blob: str) -> Kconfig:
70+
"""Parses a string containing Kconfig entries."""
71+
kconfig = Kconfig()
72+
is_not_set_matcher = re.compile(CONFIG_IS_NOT_SET_PATTERN)
73+
config_matcher = re.compile(CONFIG_PATTERN)
74+
for line in blob.split('\n'):
75+
line = line.strip()
76+
if not line:
77+
continue
78+
79+
match = config_matcher.match(line)
80+
if match:
81+
entry = KconfigEntry(match.group(1), match.group(2))
82+
kconfig.add_entry(entry)
83+
continue
84+
85+
empty_match = is_not_set_matcher.match(line)
86+
if empty_match:
87+
entry = KconfigEntry(empty_match.group(1), 'n')
88+
kconfig.add_entry(entry)
89+
continue
90+
91+
if line[0] == '#':
92+
continue
93+
else:
94+
raise KconfigParseError('Failed to parse: ' + line)
95+
return kconfig

tools/testing/kunit/kunit_kernel.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,7 @@ def __init__(self, qemu_arch_params: qemu_config.QemuArchParams, cross_compile:
116116
self._extra_qemu_params = qemu_arch_params.extra_qemu_params
117117

118118
def make_arch_qemuconfig(self, base_kunitconfig: kunit_config.Kconfig) -> None:
119-
kconfig = kunit_config.Kconfig()
120-
kconfig.parse_from_string(self._kconfig)
119+
kconfig = kunit_config.parse_from_string(self._kconfig)
121120
base_kunitconfig.merge_in_entries(kconfig)
122121

123122
def start(self, params: List[str], build_dir: str) -> subprocess.Popen:
@@ -249,8 +248,7 @@ def __init__(
249248
if not os.path.exists(kunitconfig_path):
250249
shutil.copyfile(DEFAULT_KUNITCONFIG_PATH, kunitconfig_path)
251250

252-
self._kconfig = kunit_config.Kconfig()
253-
self._kconfig.read_from_file(kunitconfig_path)
251+
self._kconfig = kunit_config.parse_file(kunitconfig_path)
254252

255253
def clean(self) -> bool:
256254
try:
@@ -262,8 +260,7 @@ def clean(self) -> bool:
262260

263261
def validate_config(self, build_dir) -> bool:
264262
kconfig_path = get_kconfig_path(build_dir)
265-
validated_kconfig = kunit_config.Kconfig()
266-
validated_kconfig.read_from_file(kconfig_path)
263+
validated_kconfig = kunit_config.parse_file(kconfig_path)
267264
if not self._kconfig.is_subset_of(validated_kconfig):
268265
invalid = self._kconfig.entries() - validated_kconfig.entries()
269266
message = 'Provided Kconfig is not contained in validated .config. Following fields found in kunitconfig, ' \
@@ -291,8 +288,7 @@ def build_reconfig(self, build_dir, make_options) -> bool:
291288
"""Creates a new .config if it is not a subset of the .kunitconfig."""
292289
kconfig_path = get_kconfig_path(build_dir)
293290
if os.path.exists(kconfig_path):
294-
existing_kconfig = kunit_config.Kconfig()
295-
existing_kconfig.read_from_file(kconfig_path)
291+
existing_kconfig = kunit_config.parse_file(kconfig_path)
296292
self._ops.make_arch_qemuconfig(self._kconfig)
297293
if not self._kconfig.is_subset_of(existing_kconfig):
298294
print('Regenerating .config ...')

tools/testing/kunit/kunit_tool_test.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,9 @@ def test_is_subset_of(self):
5151
self.assertFalse(kconfig1.is_subset_of(kconfig0))
5252

5353
def test_read_from_file(self):
54-
kconfig = kunit_config.Kconfig()
5554
kconfig_path = test_data_path('test_read_from_file.kconfig')
5655

57-
kconfig.read_from_file(kconfig_path)
56+
kconfig = kunit_config.parse_file(kconfig_path)
5857

5958
expected_kconfig = kunit_config.Kconfig()
6059
expected_kconfig.add_entry(
@@ -87,8 +86,7 @@ def test_write_to_file(self):
8786

8887
expected_kconfig.write_to_file(kconfig_path)
8988

90-
actual_kconfig = kunit_config.Kconfig()
91-
actual_kconfig.read_from_file(kconfig_path)
89+
actual_kconfig = kunit_config.parse_file(kconfig_path)
9290

9391
self.assertEqual(actual_kconfig.entries(),
9492
expected_kconfig.entries())

0 commit comments

Comments
 (0)