elixir/utils/query.py
Théo Lebrun e1bdface0c templates/ident: improve "Identifier not used" message
"Identifier not used" is weird, replace it by "unknown identifier" and
 give the search identifier name in the header.

Also improve the autocomplete comment. Make it human-friendly and
explain why people got autocompleted to a symbol that does not exist.
Also, only show it if the symbol actually exists. That way we only
tell "maybe autocomplete lead you here" only in cases where
autocomplete can actually lead you here.

To test this feature:
 - search for any random symbol, you'll see "Unknown identifier '...'"
 - search for a symbol that exists in another version as the selected
   one. For example __NR_epoll_pwait2 appeared in musl v1.2.5. You'll
   see the same header message plus a comment about how the symbol
   exists in another version.

Signed-off-by: Théo Lebrun <theo.lebrun@bootlin.com>
2025-11-05 14:18:41 +01:00

63 lines
2.4 KiB
Python

from elixir.query import Query
from elixir import lib
def cmd_stats(q, **kwargs):
print("Versions: ", len(q.db.vers))
print("Blobs: ", len(q.db.blob))
if len(q.db.blob) != len(q.db.hash) or len(q.db.hash) != len(q.db.file):
print("Warning, number of blobs, hashes or files is not equal")
print("Definitions: ", len(q.db.defs))
print("References: ", len(q.db.refs))
def cmd_versions(q, **kwargs):
for major in q.get_versions().values():
for minor in major.values():
for v in minor:
print(v)
def cmd_ident(q, version, ident, family, **kwargs):
symbol_definitions, symbol_references, symbol_doccomments, _ = q.search_ident(version, ident, family)
print("Symbol Definitions:")
for symbol_definition in symbol_definitions:
print(symbol_definition)
print("\nSymbol References:")
for symbol_reference in symbol_references:
print(symbol_reference)
print("\nDocumented in:")
for symbol_doccomment in symbol_doccomments:
print(symbol_doccomment)
def cmd_file(q, version, path, **kwargs):
code = q.get_tokenized_file(version, path)
print(code)
if __name__ == "__main__":
import argparse
query = Query(lib.getDataDir(), lib.getRepoDir())
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(required=True)
ident_subparser = subparsers.add_parser('stats', help="Get basic database stats")
ident_subparser.set_defaults(func=cmd_stats, q=query)
ident_subparser = subparsers.add_parser('versions', help="Get list of versions in the project")
ident_subparser.set_defaults(func=cmd_versions, q=query)
ident_subparser = subparsers.add_parser('ident', help="Get definitions and references of an identifier")
ident_subparser.add_argument("version", help="The version of the project", type=str, default="latest")
ident_subparser.add_argument('ident', type=str, help="The name of the identifier")
ident_subparser.add_argument('family', type=str, help="The file family requested")
ident_subparser.set_defaults(func=cmd_ident, q=query)
file_subparser = subparsers.add_parser('file', help="Get a source file")
file_subparser.add_argument("version", help="The version of the project", type=str, default="latest")
file_subparser.add_argument('path', type=str, help="The path of the source file")
file_subparser.set_defaults(func=cmd_file, q=query)
args = parser.parse_args()
args.func(**vars(args))