| 1 |
#!/usr/bin/env python3 |
| 2 |
from __future__ import annotations |
| 3 |
|
| 4 |
import argparse |
| 5 |
import subprocess |
| 6 |
import shutil |
| 7 |
import sys |
| 8 |
from pathlib import Path |
| 9 |
import typing as T |
| 10 |
|
| 11 |
def run(argsv: T.List[str]) -> int: |
| 12 |
commands: T.List[T.List[str]] = [[]] |
| 13 |
SEPARATOR = ';;;' |
| 14 |
|
| 15 |
# Generate CMD parameters |
| 16 |
parser = argparse.ArgumentParser(description='Wrapper for add_custom_command') |
| 17 |
parser.add_argument('-d', '--directory', type=str, metavar='D', required=True, help='Working directory to cwd to') |
| 18 |
parser.add_argument('-o', '--outputs', nargs='+', metavar='O', required=True, help='Expected output files') |
| 19 |
parser.add_argument('-O', '--original-outputs', nargs='*', metavar='O', default=[], help='Output files expected by CMake') |
| 20 |
parser.add_argument('commands', nargs=argparse.REMAINDER, help=f'A "{SEPARATOR}" separated list of commands') |
| 21 |
|
| 22 |
# Parse |
| 23 |
args = parser.parse_args(argsv) |
| 24 |
directory = Path(args.directory) |
| 25 |
|
| 26 |
dummy_target = None |
| 27 |
if len(args.outputs) == 1 and len(args.original_outputs) == 0: |
| 28 |
dummy_target = Path(args.outputs[0]) |
| 29 |
elif len(args.outputs) != len(args.original_outputs): |
| 30 |
print('Length of output list and original output list differ') |
| 31 |
return 1 |
| 32 |
|
| 33 |
for i in args.commands: |
| 34 |
if i == SEPARATOR: |
| 35 |
commands += [[]] |
| 36 |
continue |
| 37 |
|
| 38 |
i = i.replace('"', '') # Remove leftover quotes |
| 39 |
commands[-1] += [i] |
| 40 |
|
| 41 |
# Execute |
| 42 |
for i in commands: |
| 43 |
# Skip empty lists |
| 44 |
if not i: |
| 45 |
continue |
| 46 |
|
| 47 |
cmd = [] |
| 48 |
stdout = None |
| 49 |
stderr = None |
| 50 |
capture_file = '' |
| 51 |
|
| 52 |
for j in i: |
| 53 |
if j in {'>', '>>'}: |
| 54 |
stdout = subprocess.PIPE |
| 55 |
continue |
| 56 |
elif j in {'&>', '&>>'}: |
| 57 |
stdout = subprocess.PIPE |
| 58 |
stderr = subprocess.STDOUT |
| 59 |
continue |
| 60 |
|
| 61 |
if stdout is not None or stderr is not None: |
| 62 |
capture_file += j |
| 63 |
else: |
| 64 |
cmd += [j] |
| 65 |
|
| 66 |
try: |
| 67 |
directory.mkdir(parents=True, exist_ok=True) |
| 68 |
|
| 69 |
res = subprocess.run(cmd, stdout=stdout, stderr=stderr, cwd=str(directory), check=True) |
| 70 |
if capture_file: |
| 71 |
out_file = directory / capture_file |
| 72 |
out_file.write_bytes(res.stdout) |
| 73 |
except subprocess.CalledProcessError: |
| 74 |
return 1 |
| 75 |
|
| 76 |
if dummy_target: |
| 77 |
dummy_target.touch() |
| 78 |
return 0 |
| 79 |
|
| 80 |
# Copy outputs |
| 81 |
zipped_outputs = zip([Path(x) for x in args.outputs], [Path(x) for x in args.original_outputs]) |
| 82 |
for expected, generated in zipped_outputs: |
| 83 |
do_copy = False |
| 84 |
if not expected.exists(): |
| 85 |
if not generated.exists(): |
| 86 |
print('Unable to find generated file. This can cause the build to fail:') |
| 87 |
print(generated) |
| 88 |
do_copy = False |
| 89 |
else: |
| 90 |
do_copy = True |
| 91 |
elif generated.exists(): |
| 92 |
if generated.stat().st_mtime > expected.stat().st_mtime: |
| 93 |
do_copy = True |
| 94 |
|
| 95 |
if do_copy: |
| 96 |
if expected.exists(): |
| 97 |
expected.unlink() |
| 98 |
shutil.copyfile(str(generated), str(expected)) |
| 99 |
|
| 100 |
return 0 |
| 101 |
|
| 102 |
if __name__ == '__main__': |
| 103 |
sys.exit(run(sys.argv[1:])) |