summaryrefslogtreecommitdiff
path: root/kconfiglib.py
diff options
context:
space:
mode:
Diffstat (limited to 'kconfiglib.py')
-rw-r--r--kconfiglib.py87
1 files changed, 79 insertions, 8 deletions
diff --git a/kconfiglib.py b/kconfiglib.py
index b854186..a376d62 100644
--- a/kconfiglib.py
+++ b/kconfiglib.py
@@ -1021,7 +1021,7 @@ class Kconfig(object):
return None
- def load_config(self, filename, replace=True):
+ def load_config(self, filename=None, replace=True, verbose=True):
"""
Loads symbol values from a file in the .config format. Equivalent to
calling Symbol.set_value() to set each of the values.
@@ -1039,13 +1039,61 @@ class Kconfig(object):
caught as OSError on Python 3.
filename:
- The file to load. Respects $srctree if set (see the class
- documentation).
+ Path to load configuration from (a string). Respects $srctree if set
+ (see the class documentation).
+
+ If 'filename' is None, the configuration file to load (if any) is
+ calculated automatically, giving the behavior you'd usually want:
+
+ 1. If the KCONFIG_CONFIG environment variable is set, it gives the
+ path to the configuration file to load. Otherwise, ".config" is
+ used. See standard_config_filename().
+
+ 2. If the path from (1.) doesn't exist, the configuration file
+ given by kconf.defconfig_filename is loaded instead, which is
+ derived from the 'option defconfig_list' symbol.
+
+ 3. If (1.) and (2.) fail to find a configuration file to load, no
+ configuration file is loaded, and symbols retain their current
+ values (e.g., their default values). This is not an error.
+
+ See the return value as well.
replace (default: True):
- True if all existing user values should be cleared before loading the
+ If True, all existing user values will be cleared before loading the
.config. Pass False to merge configurations.
+
+ verbose (default: True):
+ If True and filename is None (automatically infer configuration
+ file), a message will be printed to stdout telling which file got
+ loaded (or that no file got loaded). This is meant to reduce
+ boilerplate in tools.
+
+ Returns True if an existing configuration was loaded (that didn't come
+ from the 'option defconfig_list' symbol), and False otherwise. This is
+ mostly useful in conjunction with filename=None, as True will always be
+ returned otherwise.
"""
+ loaded_existing = True
+ if filename is None:
+ filename = standard_config_filename()
+ if os.path.exists(filename):
+ if verbose:
+ print("Using existing configuration '{}' as base"
+ .format(filename))
+ else:
+ filename = self.defconfig_filename
+ if filename is None:
+ if verbose:
+ print("Using default symbol values as base")
+ return False
+
+ if verbose:
+ print("Using default configuration found in '{}' as "
+ "base".format(filename))
+
+ loaded_existing = False
+
# Disable the warning about assigning to symbols without prompts. This
# is normal and expected within a .config file.
self._warn_for_no_prompt = False
@@ -1058,6 +1106,8 @@ class Kconfig(object):
finally:
self._warn_for_no_prompt = True
+ return loaded_existing
+
def _load_config(self, filename, replace):
with self._open_config(filename) as f:
if replace:
@@ -1243,9 +1293,9 @@ class Kconfig(object):
f.write("#define {}{} {}\n"
.format(self.config_prefix, sym.name, val))
- def write_config(self, filename,
+ def write_config(self, filename=None,
header="# Generated by Kconfiglib (https://github.com/ulfalizer/Kconfiglib)\n",
- save_old=True):
+ save_old=True, verbose=True):
r"""
Writes out symbol values in the .config format. The format matches the
C implementation, including ordering.
@@ -1258,8 +1308,12 @@ class Kconfig(object):
See the 'Intro to symbol values' section in the module docstring to
understand which symbols get written out.
- filename:
- Self-explanatory.
+ filename (default: None):
+ Filename to save configuration to (a string).
+
+ If None, the filename in the the environment variable KCONFIG_CONFIG
+ is used if set, and ".config" otherwise. See
+ standard_config_filename().
header (default: "# Generated by Kconfiglib (https://github.com/ulfalizer/Kconfiglib)\n"):
Text that will be inserted verbatim at the beginning of the file. You
@@ -1274,7 +1328,18 @@ class Kconfig(object):
Errors are silently ignored if .<filename>.old cannot be written
(e.g. due to being a directory).
+
+ verbose (default: True):
+ If True and filename is None (automatically infer configuration
+ file), a message will be printed to stdout telling which file got
+ written. This is meant to reduce boilerplate in tools.
"""
+ if filename is None:
+ filename = standard_config_filename()
+ be_verbose = verbose
+ else:
+ be_verbose = False
+
if save_old:
_save_old(filename)
@@ -1293,6 +1358,9 @@ class Kconfig(object):
f.write("\n#\n# {}\n#\n".format(node.prompt[0]))
+ if be_verbose:
+ print("Configuration written to '{}'".format(filename))
+
def write_min_config(self, filename,
header="# Generated by Kconfiglib (https://github.com/ulfalizer/Kconfiglib)\n"):
"""
@@ -5629,6 +5697,9 @@ def standard_config_filename():
"""
Helper for tools. Returns the value of KCONFIG_CONFIG (which specifies the
.config file to load/save) if it is set, and ".config" otherwise.
+
+ Note: Calling load_config() with filename=None might give the behavior you
+ want, without having to use this function.
"""
return os.environ.get("KCONFIG_CONFIG", ".config")