From c805e3143bada2df897927996ae23a469cf83eb3 Mon Sep 17 00:00:00 2001 From: Ulf Magnusson Date: Thu, 6 Dec 2012 17:01:09 +0100 Subject: Move examples into separate directory. --- examples/allnoconfig.py | 36 +++++++++++++++++++++++++ examples/allyesconfig.py | 66 +++++++++++++++++++++++++++++++++++++++++++++ examples/defconfig.py | 17 ++++++++++++ examples/eval_expr.py | 8 ++++++ examples/print_refs.py | 13 +++++++++ examples/print_sym_info.py | 18 +++++++++++++ examples/print_tree.py | 23 ++++++++++++++++ examples/print_undefined.py | 15 +++++++++++ 8 files changed, 196 insertions(+) create mode 100644 examples/allnoconfig.py create mode 100644 examples/allyesconfig.py create mode 100644 examples/defconfig.py create mode 100644 examples/eval_expr.py create mode 100644 examples/print_refs.py create mode 100644 examples/print_sym_info.py create mode 100644 examples/print_tree.py create mode 100644 examples/print_undefined.py (limited to 'examples') diff --git a/examples/allnoconfig.py b/examples/allnoconfig.py new file mode 100644 index 0000000..2fe151d --- /dev/null +++ b/examples/allnoconfig.py @@ -0,0 +1,36 @@ +# Works like allnoconfig. Verified to produce identical output to 'make +# allnoconfig' for all ARCHes. The looping is done in case setting one symbol +# to "n" allows other symbols to be set to "n" (due to dependencies). + +import kconfiglib +import sys + +conf = kconfiglib.Config(sys.argv[1]) + +while True: + done = True + + for sym in conf: + # Choices take care of themselves for allnoconfig, so we only need to + # worry about non-choice symbols + if not sym.is_choice_item(): + lower_bound = sym.get_lower_bound() + + # If we can assign a lower value to the symbol (where "n", "m" and + # "y" are ordered from lowest to highest), then do so. + # lower_bound() returns None for symbols whose values cannot + # (currently) be changed, as well as for non-bool, non-tristate + # symbols. + if lower_bound is not None and \ + kconfiglib.tri_less(lower_bound, sym.calc_value()): + + sym.set_value(lower_bound) + + # We just changed the value of some symbol. As this may affect + # other symbols, keep going. + done = False + + if done: + break + +conf.write_config(".config") diff --git a/examples/allyesconfig.py b/examples/allyesconfig.py new file mode 100644 index 0000000..12dc19e --- /dev/null +++ b/examples/allyesconfig.py @@ -0,0 +1,66 @@ +# Works like allyesconfig. This is a bit more involved than allnoconfig as we +# need to handle choices in two different modes: +# +# "y": One symbol is "y", the rest are "n". +# "m": Any number of symbols are "m", the rest are "n". +# +# Only tristate choices can be in "m" mode. It is safe since the code for two +# conflicting options will appear as separate modules instead of simultaneously +# in the kernel. +# +# If a choice can be in "y" mode, it will be. If it can only be in "m" mode +# (due to dependencies), then all the options will be set to "m". +# +# The looping is in case setting one symbol to "y" (or "m") allows the value of +# other symbols to be raised. + +import kconfiglib +import sys + +conf = kconfiglib.Config(sys.argv[1]) + +# Get a list of all symbols that are not in choices +non_choice_syms = [sym for sym in conf.get_symbols() if + not sym.is_choice_item()] + +while True: + done = True + + # Handle symbols outside of choices + + for sym in non_choice_syms: + upper_bound = sym.get_upper_bound() + + # See corresponding comment for allnoconfig implementation + if upper_bound is not None and \ + kconfiglib.tri_less(sym.calc_value(), upper_bound): + sym.set_value(upper_bound) + done = False + + # Handle symbols within choices + + for choice in conf.get_choices(): + + # Handle choices whose visibility allow them to be in "y" mode + + if choice.get_visibility() == "y": + selection = choice.get_selection_from_defaults() + if selection is not None and \ + selection is not choice.get_user_selection(): + selection.set_value("y") + done = False + + # Handle choices whose visibility only allow them to be in "m" mode + + elif choice.get_visibility() == "m": + for sym in choice.get_items(): + if sym.calc_value() != "m" and \ + sym.get_upper_bound() != "n": + sym.set_value("m") + done = False + + + if done: + break + +conf.write_config(".config") diff --git a/examples/defconfig.py b/examples/defconfig.py new file mode 100644 index 0000000..3ed09fa --- /dev/null +++ b/examples/defconfig.py @@ -0,0 +1,17 @@ +# Works like entering "make menuconfig" and immediately saving and exiting + +import kconfiglib +import os +import sys + +conf = kconfiglib.Config(sys.argv[1]) + +if os.path.exists(".config"): + conf.load_config(".config") +else: + defconfig = conf.get_defconfig_filename() + if defconfig is not None: + print "Using " + defconfig + conf.load_config(defconfig) + +conf.write_config(".config") diff --git a/examples/eval_expr.py b/examples/eval_expr.py new file mode 100644 index 0000000..f8e0f65 --- /dev/null +++ b/examples/eval_expr.py @@ -0,0 +1,8 @@ +# Evaluates an expression in the context of a configuration. (Here we could +# load a .config as well.) + +import kconfiglib +import sys + +conf = kconfiglib.Config(sys.argv[1]) +print conf.eval("(TRACE_IRQFLAGS_SUPPORT || PPC32) && STACKTRACE_SUPPORT") diff --git a/examples/print_refs.py b/examples/print_refs.py new file mode 100644 index 0000000..b2d9f5f --- /dev/null +++ b/examples/print_refs.py @@ -0,0 +1,13 @@ +# Prints the names of all symbols that reference a particular symbol. (There's +# also a method get_selected_symbols() for determining just selection +# relations.) + +import kconfiglib +import sys + +conf = kconfiglib.Config(sys.argv[1]) + +x86 = conf["X86"] +for sym in conf: + if x86 in sym.get_referenced_symbols(): + print sym.get_name() diff --git a/examples/print_sym_info.py b/examples/print_sym_info.py new file mode 100644 index 0000000..8c69cac --- /dev/null +++ b/examples/print_sym_info.py @@ -0,0 +1,18 @@ +# Loads a Kconfig and a .config and prints information about a symbol. + +import kconfiglib +import sys + +# Create a Config object representing a Kconfig configuration. (Any number of +# these can be created -- the library has no global state.) +conf = kconfiglib.Config(sys.argv[1]) + +# Load values from a .config file. 'srctree' is an environment variable set by +# the Linux makefiles to the top-level directory of the kernel tree. It needs +# to be used here for the script to work with alternative build directories +# (specified e.g. with O=). +conf.load_config("$srctree/arch/x86/configs/i386_defconfig") + +# Print some information about a symbol. (The Config class implements +# __getitem__() to provide a handy syntax for getting symbols.) +print conf["SERIAL_UARTLITE_CONSOLE"] diff --git a/examples/print_tree.py b/examples/print_tree.py new file mode 100644 index 0000000..35ad23e --- /dev/null +++ b/examples/print_tree.py @@ -0,0 +1,23 @@ +# Prints a tree of all items in the configuration + +import kconfiglib +import sys + +def print_with_indent(s, indent): + print (" " * indent) + s + +def print_items(items, indent): + for item in items: + if item.is_symbol(): + print_with_indent("config {0}".format(item.get_name()), indent) + elif item.is_menu(): + print_with_indent('menu "{0}"'.format(item.get_title()), indent) + print_items(item.get_items(), indent + 2) + elif item.is_choice(): + print_with_indent('choice', indent) + print_items(item.get_items(), indent + 2) + elif item.is_comment(): + print_with_indent('comment "{0}"'.format(item.get_text()), indent) + +conf = kconfiglib.Config(sys.argv[1]) +print_items(conf.get_top_level_items(), 0) diff --git a/examples/print_undefined.py b/examples/print_undefined.py new file mode 100644 index 0000000..82a29d3 --- /dev/null +++ b/examples/print_undefined.py @@ -0,0 +1,15 @@ +# Prints the names of all symbols that are referenced but never defined in the +# current configuration together with the locations where they are referenced. +# Integers being included in the list is not a bug, as these need to be treated +# as symbols per the design of Kconfig. + +import kconfiglib +import sys + +conf = kconfiglib.Config(sys.argv[1]) + +for sym in conf.get_symbols(): + if not sym.is_defined(): + print sym.get_name() + for (filename, linenr) in sym.get_ref_locations(): + print " {0}:{1}".format(filename, linenr) -- cgit v1.2.3