Skip to content

Commit 4020464

Browse files
committed
fix check-qhelp.py
It turns out checking changes on `.inc.qhelp` files is a bit trickier, as we need to first find which `qhelp` files use them. The previous iteration of this script was working under the assumption that `.inc.qhelp` files were only included from the current or a parent path, but this turns out to be wrong. This time around, if we are asked to check one or more `.inc.qhelp` files we build an include map from all `qhelp` files and run the help generator on the `qhelp` files actually including them.
1 parent 9667315 commit 4020464

File tree

1 file changed

+48
-7
lines changed

1 file changed

+48
-7
lines changed

misc/scripts/check-qhelp.py

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,66 @@
44
55
This takes care of:
66
* providing a temporary directory to --output
7-
* turning .inc.qhelp arguments into their containing directory
7+
* finding usages of .inc.qhelp arguments
88
"""
99

1010
import pathlib
1111
import tempfile
1212
import sys
1313
import subprocess
14+
import xml.sax
1415

15-
def transform_input(arg):
16-
arg = pathlib.Path(arg)
17-
if arg.suffixes == ['.inc', '.qhelp']:
18-
return str(arg.parent)
19-
return str(arg)
16+
17+
include_cache = {}
18+
19+
class IncludeHandler(xml.sax.ContentHandler):
20+
def __init__(self, xml):
21+
self.__xml = xml
22+
23+
def startElement(self, name, attrs):
24+
if name == "include":
25+
src = (self.__xml.parent / attrs["src"]).resolve()
26+
include_cache.setdefault(src, set()).add(self.__xml)
27+
28+
class IgnoreErrorsHandler(xml.sax.ErrorHandler):
29+
def error(self, exc):
30+
pass
31+
32+
def fatalError(self, exc):
33+
pass
34+
35+
def warning(self, exc):
36+
pass
37+
38+
def init_include_cache():
39+
if not include_cache:
40+
for qhelp in pathlib.Path().rglob("*.qhelp"):
41+
xml.sax.parse(qhelp, IncludeHandler(qhelp), IgnoreErrorsHandler())
42+
43+
44+
def find_inc_qhelp_usages(arg):
45+
init_include_cache()
46+
return include_cache.get(arg.resolve(), ())
47+
48+
def transform_inputs(args):
49+
for arg in args:
50+
arg = pathlib.Path(arg)
51+
if arg.suffixes == ['.inc', '.qhelp']:
52+
for qhelp in find_inc_qhelp_usages(arg):
53+
yield str(qhelp)
54+
return str(arg)
55+
56+
affected_qhelp_files = list(transform_inputs(sys.argv[1:]))
57+
if not affected_qhelp_files:
58+
# can happen with changes on an unused .inc.qhelp file
59+
print("nothing to do!")
60+
sys.exit(0)
2061

2162
cmd = ["codeql", "generate", "query-help", "--format=markdown"]
2263

2364
with tempfile.TemporaryDirectory() as tmp:
2465
cmd += [f"--output={tmp}", "--"]
25-
cmd.extend(transform_input(x) for x in sys.argv[1:])
66+
cmd += affected_qhelp_files
2667
res = subprocess.run(cmd)
2768

2869
sys.exit(res.returncode)

0 commit comments

Comments
 (0)