Files
codeql/misc/scripts/check-qhelp.py
Paolo Tranquilli ef4d1de9c3 check-qhelp: call super init in IncludeHandler
`xml.sax.ContentHandler` has a non-trivial `__init__`. While this is
probably harmless, it does not hurt to fix this.
2022-03-01 16:50:55 +01:00

76 lines
1.8 KiB
Python
Executable File

#!/bin/env python3
"""cross platform wrapper around codeql generate query-help to check .qhelp files
This takes care of:
* providing a temporary directory to --output
* finding usages of .inc.qhelp arguments
"""
import pathlib
import subprocess
import sys
import tempfile
import xml.sax
include_cache = {}
class IncludeHandler(xml.sax.ContentHandler):
def __init__(self, xml):
super().__init__()
self.__xml = xml
def startElement(self, name, attrs):
if name == "include":
src = (self.__xml.parent / attrs["src"]).resolve()
include_cache.setdefault(src, set()).add(self.__xml)
class IgnoreErrorsHandler(xml.sax.ErrorHandler):
def error(self, exc):
pass
def fatalError(self, exc):
pass
def warning(self, exc):
pass
def init_include_cache():
if not include_cache:
for qhelp in pathlib.Path().rglob("*.qhelp"):
xml.sax.parse(qhelp, IncludeHandler(qhelp), IgnoreErrorsHandler())
def find_inc_qhelp_usages(arg):
init_include_cache()
return include_cache.get(arg.resolve(), ())
def transform_inputs(args):
for arg in args:
arg = pathlib.Path(arg)
if arg.suffixes == ['.inc', '.qhelp']:
for qhelp in find_inc_qhelp_usages(arg):
yield str(qhelp)
else:
yield str(arg)
affected_qhelp_files = list(transform_inputs(sys.argv[1:]))
if not affected_qhelp_files:
# can happen with changes on an unused .inc.qhelp file
print("nothing to do!")
sys.exit(0)
cmd = ["codeql", "generate", "query-help", "--format=markdown"]
with tempfile.TemporaryDirectory() as tmp:
cmd += [f"--output={tmp}", "--"]
cmd += affected_qhelp_files
res = subprocess.run(cmd)
sys.exit(res.returncode)