summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorUlf Magnusson <ulfalizer@gmail.com>2018-09-15 02:19:34 +0200
committerUlf Magnusson <ulfalizer@gmail.com>2018-09-15 03:07:11 +0200
commit35a60b786c646c846d9bad6a5f15711acc9a62c6 (patch)
treee0848683c8b4f39d1f0959fb6b9a95c14ca2a3f0
parentf861c271dcbb06e0101c01add3d07006c2d3a09c (diff)
Update some examples to use node_iter()
Simplifies the code. Should promote new APIs. Also fix list_undefined.py for recent kernels. More environment variables are referenced now.
-rw-r--r--examples/allnoconfig_walk.py3
-rw-r--r--examples/find_symbol.py30
-rw-r--r--examples/help_grep.py37
-rw-r--r--examples/list_undefined.py21
4 files changed, 33 insertions, 58 deletions
diff --git a/examples/allnoconfig_walk.py b/examples/allnoconfig_walk.py
index b94a169..c35b998 100644
--- a/examples/allnoconfig_walk.py
+++ b/examples/allnoconfig_walk.py
@@ -2,6 +2,9 @@
# Verified by the test suite to generate identical output to 'make allnoconfig'
# for all ARCHes.
#
+# Note: A more practical version would use Kconfig.node_iter(). The manual tree
+# walking is for demonstration purposes.
+#
# Usage for the Linux kernel:
#
# $ make [ARCH=<arch>] scriptconfig SCRIPT=Kconfiglib/examples/allnoconfig_walk.py
diff --git a/examples/find_symbol.py b/examples/find_symbol.py
index 0d3c968..132d45f 100644
--- a/examples/find_symbol.py
+++ b/examples/find_symbol.py
@@ -79,43 +79,25 @@ import sys
import kconfiglib
-def referencing_nodes(node, sym):
- # Returns a list of all menu nodes that reference 'sym' in any of their
- # properties or property conditions
-
- res = []
-
- while node:
- if sym in node.referenced:
- res.append(node)
-
- if node.list:
- res.extend(referencing_nodes(node.list, sym))
-
- node = node.next
-
- return res
-
-
if len(sys.argv) < 3:
sys.exit('Pass symbol name (without "CONFIG_" prefix) with SCRIPT_ARG=<name>')
kconf = kconfiglib.Kconfig(sys.argv[1])
sym_name = sys.argv[2]
-
if sym_name not in kconf.syms:
print("No symbol {} exists in the configuration".format(sym_name))
sys.exit(0)
-nodes = referencing_nodes(kconf.top_node, kconf.syms[sym_name])
-if not nodes:
- print("No reference to {} found".format(sym_name))
+referencing = [node for node in kconf.node_iter()
+ if kconf.syms[sym_name] in node.referenced]
+if not referencing:
+ print("No references to {} found".format(sym_name))
sys.exit(0)
print("Found {} locations that reference {}:\n"
- .format(len(nodes), sym_name))
+ .format(len(referencing), sym_name))
-for i, node in enumerate(nodes, 1):
+for i, node in enumerate(referencing, 1):
print("========== Location {} ({}:{}) ==========\n\n{}"
.format(i, node.filename, node.linenr, node))
diff --git a/examples/help_grep.py b/examples/help_grep.py
index 20a4911..f05c66f 100644
--- a/examples/help_grep.py
+++ b/examples/help_grep.py
@@ -42,30 +42,21 @@ if len(sys.argv) < 3:
search = re.compile(sys.argv[2], re.IGNORECASE).search
-def search_tree(node):
- while node:
- match = False
+for node in Kconfig(sys.argv[1]).node_iter():
+ match = False
- if isinstance(node.item, (Symbol, Choice)) and \
- node.help is not None and search(node.help):
- print(node.item)
- match = True
+ if isinstance(node.item, (Symbol, Choice)) and \
+ node.help is not None and search(node.help):
+ print(node.item)
+ match = True
- elif node.item == MENU and search(node.prompt[0]):
- print('menu "{}"'.format(node.prompt[0]))
- match = True
+ elif node.item == MENU and search(node.prompt[0]):
+ print('menu "{}"'.format(node.prompt[0]))
+ match = True
- elif node.item == COMMENT and search(node.prompt[0]):
- print('comment "{}"'.format(node.prompt[0]))
- match = True
+ elif node.item == COMMENT and search(node.prompt[0]):
+ print('comment "{}"'.format(node.prompt[0]))
+ match = True
- if match:
- print("location: {}:{}\n".format(node.filename, node.linenr))
-
- if node.list:
- search_tree(node.list)
-
- node = node.next
-
-kconf = Kconfig(sys.argv[1])
-search_tree(kconf.top_node)
+ if match:
+ print("location: {}:{}\n".format(node.filename, node.linenr))
diff --git a/examples/list_undefined.py b/examples/list_undefined.py
index 0207975..4a3bc9b 100644
--- a/examples/list_undefined.py
+++ b/examples/list_undefined.py
@@ -70,9 +70,6 @@ def all_arch_srcarch_pairs():
yield ("sh64", "sh")
- yield ("tilepro", "tile")
- yield ("tilegx", "tile")
-
yield ("um", "um")
@@ -80,6 +77,13 @@ def all_arch_srcarch_kconfigs():
"""
Generates Kconfig instances for all the architectures in the kernel
"""
+
+ os.environ["srctree"] = "."
+ os.environ["HOSTCC"] = "gcc"
+ os.environ["HOSTCXX"] = "g++"
+ os.environ["CC"] = "gcc"
+ os.environ["LD"] = "ld"
+
for arch, srcarch in all_arch_srcarch_pairs():
print(" Processing " + arch)
@@ -118,21 +122,16 @@ for kconf in all_arch_srcarch_kconfigs():
print("\nFinding references to each undefined symbol")
-def referencing_nodes(node, name):
+def referencing_nodes(kconf, name):
# Returns a list of all menu nodes that reference a symbol named 'name' in
# any of their properties or property conditions
res = []
- while node:
+ for node in kconf.node_iter():
for ref in node.referenced:
if ref.name == name:
res.append(node)
- if node.list:
- res.extend(referencing_nodes(node.list, name))
-
- node = node.next
-
return res
@@ -145,7 +144,7 @@ for kconf in all_arch_srcarch_kconfigs():
# undefined symbol, which is terribly inefficient. We could speed
# things up by tweaking referencing_nodes() to compare each symbol to
# multiple symbols while walking the configuration tree.
- for node in referencing_nodes(kconf.top_node, name):
+ for node in referencing_nodes(kconf, name):
refs.add("{}:{}".format(node.filename, node.linenr))