From dd0e227216e247d2040cdd40bf7397702880cdc4 Mon Sep 17 00:00:00 2001 From: Ulf Magnusson Date: Mon, 9 Oct 2017 23:05:00 +0200 Subject: Kconfiglib 2 backup WIP --- examples/help_grep.py | 109 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 66 insertions(+), 43 deletions(-) (limited to 'examples/help_grep.py') diff --git a/examples/help_grep.py b/examples/help_grep.py index 61ac936..fed1731 100644 --- a/examples/help_grep.py +++ b/examples/help_grep.py @@ -1,49 +1,72 @@ -# Does a case-insensitive search for a string in the help texts for symbols and -# choices and the titles of menus and comments. Prints the matching items -# together with their locations and the matching text. Used like +# Does a case-insensitive search for a regular expression in the help texts of +# symbols and choices and the prompts of menus and comments. Prints the +# matching items together with their locations and the matching text. # -# $ make [ARCH=] scriptconfig SCRIPT=Kconfiglib/examples/help_grep.py SCRIPT_ARG= +# Usage: +# +# $ make [ARCH=] scriptconfig SCRIPT=Kconfiglib/examples/help_grep.py SCRIPT_ARG= +# +# Shortened example output for SCRIPT_ARG=general: +# +# menu "General setup" +# location: init/Kconfig:39 +# +# config SYSVIPC +# bool +# prompt "System V IPC" +# help +# ... +# exchange information. It is generally considered to be a good thing, +# ... +# +# location: init/Kconfig:233 +# +# config BSD_PROCESS_ACCT +# bool +# prompt "BSD Process Accounting" if MULTIUSER +# help +# ... +# information. This is generally a good idea, so say Y. +# +# location: init/Kconfig:403 +# +# ... + -import kconfiglib +from kconfiglib import Config, Symbol, Choice, MENU, COMMENT +import re import sys if len(sys.argv) < 3: - print('Pass search string with SCRIPT_ARG="search string"') + print('Pass the regex with SCRIPT_ARG=regex') sys.exit(1) -search_string = sys.argv[2].lower() - -conf = kconfiglib.Config(sys.argv[1]) - -for item in conf.get_symbols() + \ - conf.get_choices() + conf.get_menus() + conf.get_comments(): - if item.is_symbol() or item.is_choice(): - text = item.get_help() - elif item.is_menu(): - text = item.get_title() - else: - # Comment - text = item.get_text() - - # Case-insensitive search - if text is not None and search_string in text.lower(): - if item.is_symbol() or item.is_choice(): - # Indent lines in help text. (There might be a nicer way. :) - text = "\n".join([" " + s for s in text.splitlines()]) - - # Don't worry about symbols/choices defined in multiple locations to - # keep things simple - fname, linenr = item.get_def_locations()[0] - if item.is_symbol(): - print("config {0} at {1}:{2}:\n{3}" - .format(item.get_name(), fname, linenr, text)) - elif item.is_choice(): - print("choice at {0}:{1}:\n{2}".format(fname, linenr, text)) - - else: - # Menu or comment - fname, linenr = item.get_location() - if item.is_menu(): - print('menu "{0}" at {1}:{2}'.format(text, fname, linenr)) - else: - # Comment - print('comment "{0}" at {1}:{2}'.format(text, fname, linenr)) + +search = re.compile(sys.argv[2], re.IGNORECASE).search + +def search_tree(node): + while node is not None: + match = False + + 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 == 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 is not None: + search_tree(node.list) + + node = node.next + +conf = Config(sys.argv[1]) +search_tree(conf.top_menu) -- cgit v1.2.3