| 1 |
#!/usr/bin/env python3 |
| 2 |
|
| 3 |
# A small wrapper around xmllint and xsltproc that collects dependency |
| 4 |
# information (in gcc's format) using --load-trace. |
| 5 |
|
| 6 |
import argparse |
| 7 |
import re |
| 8 |
import subprocess |
| 9 |
import sys |
| 10 |
|
| 11 |
parser = argparse.ArgumentParser( |
| 12 |
description='generate dependency file for docs') |
| 13 |
|
| 14 |
parser.add_argument('--targetname', type=str, required=False, nargs='+') |
| 15 |
parser.add_argument('--depfile', type=str, required=False) |
| 16 |
parser.add_argument('--tool', type=str, required=True) |
| 17 |
parser.add_argument('flags', nargs='*') |
| 18 |
|
| 19 |
args = parser.parse_args() |
| 20 |
|
| 21 |
if args.depfile: |
| 22 |
command = [args.tool, '--load-trace'] + args.flags |
| 23 |
|
| 24 |
# list of targets that depend on the loaded files we see via --load-trace |
| 25 |
line_start = ' '.join(args.targetname) + ': ' |
| 26 |
|
| 27 |
# --load-trace flag displays all the documents loaded during the processing |
| 28 |
# to stderr |
| 29 |
res = subprocess.run(command, stderr=subprocess.PIPE, |
| 30 |
universal_newlines=True) |
| 31 |
|
| 32 |
line_re = re.compile('^Loaded URL="([^"]+)"') |
| 33 |
with open(args.depfile, 'w') as f: |
| 34 |
for line in res.stderr.splitlines(): |
| 35 |
m = re.match(line_re, line) |
| 36 |
|
| 37 |
# continue to show errors |
| 38 |
if m is None: |
| 39 |
print(line, file=sys.stderr) |
| 40 |
continue |
| 41 |
# Absolute paths are printed as file://, relative paths as-is. We |
| 42 |
# don't care about http://, as a) those will be printed even if |
| 43 |
# resolved locally b) we couldn't have a dependency anyway. |
| 44 |
fname = m.group(1) |
| 45 |
if fname.startswith('http://'): |
| 46 |
continue |
| 47 |
if fname.startswith('file://'): |
| 48 |
fname = fname.split('file://')[1] |
| 49 |
f.write(line_start + fname + '\n') |
| 50 |
else: |
| 51 |
command = [args.tool] + args.flags |
| 52 |
res = subprocess.run(command) |
| 53 |
|
| 54 |
exit(res.returncode) |