CUDApp
CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit.
Brought by drakestapleton
CUDApp / tests / amd-exec / documented.py
| 1 | #!/usr/bin/env python3 |
| 2 | # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | """Run the shell blocks in this directory's Markdown verbatim, from the repo root. |
| 4 | Caller supplies CPU isolation, PATH, OMEGA_OMC, AMD_WEIGHTS and AMD_DOC_OUT. |
| 5 | No safe-path flags or substitute interpreter are injected into documented commands. |
| 6 | """ |
| 7 | import hashlib |
| 8 | import json |
| 9 | import os |
| 10 | import pathlib |
| 11 | import re |
| 12 | import shutil |
| 13 | import subprocess |
| 14 | import sys |
| 15 | |
| 16 | |
| 17 | def main(): |
| 18 | root = pathlib.Path(__file__).resolve().parents[2] |
| 19 | out = pathlib.Path(os.environ['AMD_DOC_OUT']).resolve() |
| 20 | out.mkdir(parents=True, exist_ok=False) |
| 21 | records = [] |
| 22 | failed = False |
| 23 | not_run = False |
| 24 | for doc in sorted((root / 'tests/amd-exec').glob('*.md')): |
| 25 | text = doc.read_text() |
| 26 | blocks = re.findall(r'^```sh\n(.*?)^```', text, re.M | re.S) |
| 27 | assert blocks, f'no executable shell block: {doc}' |
| 28 | for index, command in enumerate(blocks): |
| 29 | assert not re.search(r'\bpython\S*\s+-(?:P|I)\b', command), 'documented safe-path workaround' |
| 30 | name = f'{doc.stem}-{index}' |
| 31 | log = out / (name + '.log') |
| 32 | interpreter = shutil.which(command.split()[0]) |
| 33 | print(f'DOCUMENTED command={command!r} interpreter={interpreter}', flush=True) |
| 34 | with log.open('wb') as f: |
| 35 | p = subprocess.run(['sh', '-eu', '-c', command], cwd=root, stdout=f, stderr=subprocess.STDOUT) |
| 36 | data = log.read_bytes() |
| 37 | missing = b'ModuleNotFoundError:' in data or p.returncode == 127 |
| 38 | status = 'PASS' if p.returncode == 0 else 'NOT_RUN' if missing else 'FAIL' |
| 39 | records.append({'document': str(doc.relative_to(root)), 'command': command, |
| 40 | 'interpreter': interpreter, 'exit': p.returncode, |
| 41 | 'status': status, 'log': str(log), 'sha256': hashlib.sha256(data).hexdigest()}) |
| 42 | (out / 'commands.json').write_text(json.dumps(records, indent=2) + '\n') |
| 43 | print('DOCUMENTED ' + json.dumps(records[-1]), flush=True) |
| 44 | failed |= status == 'FAIL' |
| 45 | not_run |= status == 'NOT_RUN' |
| 46 | if p.returncode: |
| 47 | # Dependent bundle generation cannot produce evidence after a failed run. |
| 48 | break |
| 49 | return 1 if failed else 77 if not_run else 0 |
| 50 | |
| 51 | |
| 52 | if __name__ == '__main__': |
| 53 | sys.exit(main()) |