Skip to content

Commit

Permalink
Have load_config() and write_(min_)config() return messages
Browse files Browse the repository at this point in the history
Hardcoding load_config() and write_(min_)config() to write any message
to stdout is awkward, because it means that the message can't be easily
reused when stdout is the wrong place to write it to (e.g. in
menuconfig/guiconfig). This gets extra bad now that there's also the "No
change to ..." message.

Modify load_config() and write_(min_)config() to return the message as a
string instead, and have them always return a message, instead of just
when 'filename' is None and verbose=True. This makes things flexible and
straightforward.

Use the new behavior in menuconfig.py and guiconfig.py. They now show
"No change to ..." when saving a file doesn't modify it.

Tools that want to write messages to stdout should now do
print(kconf.load_config()) / print(kconf.write_config()).

There's no clean way to preserve perfect backwards compatibility here,
but keep accepting the 'verbose' argument and print a deprecation
warning if a value is ever passed for it. That way, scripts will keep
running, though possibly with less output on stdout.

This changes the meaning of the load_config() return value as well,
though I suspect it was only ever used by the menuconfig/guiconfig
interfaces.

The new behavior applies for kconfiglib.VERSION >= (12, 0, 0).
  • Loading branch information
ulfalizer committed Jun 3, 2019
1 parent 455e366 commit 55bc8c3
Show file tree
Hide file tree
Showing 18 changed files with 226 additions and 166 deletions.
2 changes: 1 addition & 1 deletion alldefconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
def main():
kconf = kconfiglib.standard_kconfig()
kconfiglib.load_allconfig(kconf, "alldef.config")
kconf.write_config()
print(kconf.write_config())


if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion allmodconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def main():

kconfiglib.load_allconfig(kconf, "allmod.config")

kconf.write_config()
print(kconf.write_config())


if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion allnoconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def main():

kconfiglib.load_allconfig(kconf, "allno.config")

kconf.write_config()
print(kconf.write_config())


if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion allyesconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def main():

kconfiglib.load_allconfig(kconf, "allyes.config")

kconf.write_config()
print(kconf.write_config())


if __name__ == "__main__":
Expand Down
4 changes: 2 additions & 2 deletions defconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ def main():
args = parser.parse_args()

kconf = kconfiglib.Kconfig(args.kconfig)
kconf.load_config(args.config)
kconf.write_config()
print(kconf.load_config(args.config))
print(kconf.write_config())


if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion examples/allnoconfig_walk.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,4 @@ def do_allnoconfig(node):
if not changed:
break

kconf.write_config()
print(kconf.write_config())
2 changes: 1 addition & 1 deletion examples/defconfig_oldconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@
s.set_value(0)

# Write the final configuration
kconf.write_config()
print(kconf.write_config())
24 changes: 11 additions & 13 deletions examples/menuconfig_example.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env python

# Implements a simple configuration interface on top of Kconfiglib to
# demonstrate concepts for building a menuconfig-like. Emulates how the
# standard menuconfig prints menu entries.
Expand Down Expand Up @@ -117,6 +119,7 @@
#
# Enter a symbol/choice name, "load_config", or "write_config" (or press CTRL+D to exit): ^D

from __future__ import print_function
import readline
import sys

Expand Down Expand Up @@ -300,27 +303,22 @@ def get_value_from_user(sc):

if cmd == "load_config":
config_filename = input(".config file to load: ")

try:
kconf.load_config(config_filename)
# Returns a message telling which file got loaded
print(kconf.load_config(config_filename))
except IOError as e:
# Print the (spammy) error from Kconfiglib itself
print(e.message + "\n")
else:
print("Configuration loaded from " + config_filename)
print(e, file=sys.stderr)

print_menuconfig(kconf)
continue

if cmd == "write_config":
config_filename = input("To this file: ")

try:
kconf.write_config(config_filename)
# Returns a message telling which file got saved
print(kconf.write_config(config_filename))
except IOError as e:
print(e.message)
else:
print("Configuration written to " + config_filename)
print(e, file=sys.stderr)

continue

Expand All @@ -340,5 +338,5 @@ def get_value_from_user(sc):

continue

print("No symbol/choice named '{}' in the configuration"
.format(cmd))
print("No symbol/choice named '{}' in the configuration".format(cmd),
file=sys.stderr)
28 changes: 19 additions & 9 deletions examples/merge_config.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env python

# This script functions similarly to scripts/kconfig/merge_config.sh from the
# kernel tree, merging multiple configurations fragments to produce a complete
# .config, with unspecified values filled in as for alldefconfig.
Expand Down Expand Up @@ -37,7 +39,8 @@
#
# conf3 contents:
#
# # Ops... assigned twice
# # Assigned twice (would generate warning if 'warn_assign_override' was
# # True)
# # CONFIG_FOO is not set
#
# # Ops... this symbol doesn't exist
Expand All @@ -54,15 +57,20 @@
# Running:
#
# $ python(3) merge_config.py Kconfig merged conf1 conf2 conf3 conf4
# conf3:2: warning: FOO (defined at Kconfig:1) set more than once. Old value: "y", new value: "n".
# conf3:5: warning: attempt to assign the value "y" to the undefined symbol OPS
# warning: QAZ (defined at Kconfig:10) was assigned the value "y" but got the value "n" -- check dependencies
# Merged configuration 'conf1'
# Merged configuration 'conf2'
# conf3:5: warning: attempt to assign the value 'y' to the undefined symbol OPS
# Merged configuration 'conf3'
# Merged configuration 'conf4'
# Configuration saved to 'merged'
# warning: QAZ (defined at Kconfig:10) was assigned the value 'y' but got the value 'n' -- check dependencies
# $ cat merged
# Generated by Kconfiglib (https://github.com/ulfalizer/Kconfiglib)
# # CONFIG_FOO is not set
# CONFIG_BAR=y
# CONFIG_BAZ="baz string"

from __future__ import print_function
import sys

from kconfiglib import Kconfig, BOOL, TRISTATE, TRI_TO_STR
Expand All @@ -86,12 +94,13 @@
kconf.disable_override_warnings()
kconf.disable_redun_warnings()

# Create a merged configuration by loading the fragments with replace=False
# Create a merged configuration by loading the fragments with replace=False.
# load_config() and write_config() returns a message to print.
for config in sys.argv[3:]:
kconf.load_config(config, replace=False)
print(kconf.load_config(config, replace=False))

# Write the merged configuration
kconf.write_config(sys.argv[2])
print(kconf.write_config(sys.argv[2]))

# Print warnings for symbols whose actual value doesn't match the assigned
# value
Expand Down Expand Up @@ -119,5 +128,6 @@ def name_and_loc(sym):

if user_value != sym.str_value:
print("warning: {} was assigned the value '{}' but got the "
"value '{}' -- check dependencies"
.format(name_and_loc(sym), user_value, sym.str_value))
"value '{}' -- check dependencies".format(
name_and_loc(sym), user_value, sym.str_value),
file=sys.stderr)
2 changes: 1 addition & 1 deletion genconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def main():


kconf = kconfiglib.Kconfig(args.kconfig_filename)
kconf.load_config(verbose=False)
kconf.load_config()

kconf.write_autoconf(args.header_path)

Expand Down
26 changes: 14 additions & 12 deletions guiconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,12 @@ def menuconfig(kconf):

_create_ui()

# Load existing configuration and check if it's outdated
_set_conf_changed(_load_config())

# Filename to save configuration to
_conf_filename = standard_config_filename()

# Load existing configuration and check if it's outdated
_set_conf_changed(_load_config())

# Filename to save minimal configuration to
_minconf_filename = "defconfig"

Expand Down Expand Up @@ -238,7 +238,8 @@ def _load_config():
# Returns True if .config is missing or outdated. We always prompt for
# saving the configuration in that case.

if not _kconf.load_config():
print(_kconf.load_config())
if not os.path.exists(_conf_filename):
# No .config
return True

Expand Down Expand Up @@ -639,7 +640,8 @@ def _set_conf_changed(changed):
global _conf_changed

_conf_changed = changed
_set_status("Modified" if changed else "")
if changed:
_set_status("Modified")


def _update_tree():
Expand Down Expand Up @@ -1341,7 +1343,6 @@ def _save(_=None):

if _try_save(_kconf.write_config, _conf_filename, "configuration"):
_set_conf_changed(False)
_set_status("Configuration saved to " + _conf_filename)

_tree.focus_set()

Expand All @@ -1363,7 +1364,6 @@ def _save_as():
break

if _try_save(_kconf.write_config, filename, "configuration"):
_set_status("Configuration saved to " + filename)
_conf_filename = filename
break

Expand Down Expand Up @@ -1433,7 +1433,6 @@ def _open(_=None):

_update_tree()

_set_status("Configuration loaded from " + filename)
break

_tree.focus_set()
Expand Down Expand Up @@ -1694,8 +1693,10 @@ def _try_save(save_fn, filename, description):
# String describing the thing being saved

try:
save_fn(filename)
print("{} saved to '{}'".format(description, filename))
# save_fn() returns a message to print
msg = save_fn(filename)
_set_status(msg)
print(msg)
return True
except (OSError, IOError) as e:
messagebox.showerror(
Expand All @@ -1714,8 +1715,9 @@ def _try_load(filename):
# Configuration file to load

try:
_kconf.load_config(filename)
print("configuration loaded from " + filename)
msg = _kconf.load_config(filename)
_set_status(msg)
print(msg)
return True
except (OSError, IOError) as e:
messagebox.showerror(
Expand Down
Loading

0 comments on commit 55bc8c3

Please sign in to comment.