Skip to content

Commit 2a6ac31

Browse files
committed
Return a message from Kconfig.write_autoconf()
Like for Kconfig.write_config() and Kconfig.write_min_config(), return a string from Kconfig.write_autoconf() with a message saying that the header got saved, or that there were no changes to it. Can be handy in tools. Also make the "no change" message for the various files more specific, by mentioning what type of file it is (configuration, header, etc.) Return True/False from Kconfig._write_if_changed() to indicate if the file was updated. This also allows it to be reused in Kconfig.write_min_config().
1 parent 34a6c21 commit 2a6ac31

File tree

1 file changed

+22
-15
lines changed

1 file changed

+22
-15
lines changed

kconfiglib.py

+22-15
Original file line numberDiff line numberDiff line change
@@ -1444,12 +1444,18 @@ def write_autoconf(self, filename=None, header=None):
14441444
KCONFIG_AUTOHEADER_HEADER had when the Kconfig instance was created
14451445
will be used if it was set, and no header otherwise. See the
14461446
Kconfig.header_header attribute.
1447+
1448+
Returns a string with a message saying that the header got saved, or
1449+
that there were no changes to it. This is meant to reduce boilerplate
1450+
in tools, which can do e.g. print(kconf.write_autoconf()).
14471451
"""
14481452
if filename is None:
14491453
filename = os.getenv("KCONFIG_AUTOHEADER",
14501454
"include/generated/autoconf.h")
14511455

1452-
self._write_if_changed(filename, self._autoconf_contents(header))
1456+
if self._write_if_changed(filename, self._autoconf_contents(header)):
1457+
return "Kconfig header saved to '{}'".format(filename)
1458+
return "No change to Kconfig header in '{}'".format(filename)
14531459

14541460
def _autoconf_contents(self, header):
14551461
# write_autoconf() helper. Returns the contents to write as a string,
@@ -1564,7 +1570,7 @@ def write_config(self, filename=None, header=None, save_old=True,
15641570

15651571
contents = self._config_contents(header)
15661572
if self._contents_eq(filename, contents):
1567-
return "No change to '{}'".format(filename)
1573+
return "No change to configuration in '{}'".format(filename)
15681574

15691575
if save_old:
15701576
_save_old(filename)
@@ -1677,18 +1683,14 @@ def write_min_config(self, filename, header=None):
16771683
be used if it was set, and no header otherwise. See the
16781684
Kconfig.config_header attribute.
16791685
1680-
Returns a string with a message saying which file got saved. This is
1681-
meant to reduce boilerplate in tools, which can do e.g.
1686+
Returns a string with a message saying the minimal configuration got
1687+
saved, or that there were no changes to it. This is meant to reduce
1688+
boilerplate in tools, which can do e.g.
16821689
print(kconf.write_min_config()).
16831690
"""
1684-
contents = self._min_config_contents(header)
1685-
if self._contents_eq(filename, contents):
1686-
return "No change to '{}'".format(filename)
1687-
1688-
with self._open(filename, "w") as f:
1689-
f.write(contents)
1690-
1691-
return "Minimal configuration saved to '{}'".format(filename)
1691+
if self._write_if_changed(filename, self._min_config_contents(header)):
1692+
return "Minimal configuration saved to '{}'".format(filename)
1693+
return "No change to minimal configuration in '{}'".format(filename)
16921694

16931695
def _min_config_contents(self, header):
16941696
# write_min_config() helper. Returns the contents to write as a string,
@@ -2264,10 +2266,15 @@ def _write_if_changed(self, filename, contents):
22642266
# differs, but it breaks stuff like write_config("/dev/null"), which is
22652267
# used out there to force evaluation-related warnings to be generated.
22662268
# This simple version is pretty failsafe and portable.
2269+
#
2270+
# Returns True if the file has changed and is updated, and False
2271+
# otherwise.
22672272

2268-
if not self._contents_eq(filename, contents):
2269-
with self._open(filename, "w") as f:
2270-
f.write(contents)
2273+
if self._contents_eq(filename, contents):
2274+
return False
2275+
with self._open(filename, "w") as f:
2276+
f.write(contents)
2277+
return True
22712278

22722279
def _contents_eq(self, filename, contents):
22732280
# Returns True if the contents of 'filename' is 'contents' (a string),

0 commit comments

Comments
 (0)