|
4 | 4 |
|
5 | 5 | This takes care of:
|
6 | 6 | * providing a temporary directory to --output
|
7 |
| -* turning .inc.qhelp arguments into their containing directory |
| 7 | +* finding usages of .inc.qhelp arguments |
8 | 8 | """
|
9 | 9 |
|
10 | 10 | import pathlib
|
11 | 11 | import tempfile
|
12 | 12 | import sys
|
13 | 13 | import subprocess
|
| 14 | +import xml.sax |
14 | 15 |
|
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) |
20 | 61 |
|
21 | 62 | cmd = ["codeql", "generate", "query-help", "--format=markdown"]
|
22 | 63 |
|
23 | 64 | with tempfile.TemporaryDirectory() as tmp:
|
24 | 65 | cmd += [f"--output={tmp}", "--"]
|
25 |
| - cmd.extend(transform_input(x) for x in sys.argv[1:]) |
| 66 | + cmd += affected_qhelp_files |
26 | 67 | res = subprocess.run(cmd)
|
27 | 68 |
|
28 | 69 | sys.exit(res.returncode)
|
0 commit comments