Files
nvdriver/tools/generate_software_control_replay.py

62 lines
2.3 KiB
Python

#!/usr/bin/env python3
"""Generate software_controls.rs fixture and replay JSON from recorded REA evidence."""
import argparse
import json
from pathlib import Path
R = Path(__file__).resolve().parent.parent
def _arr(h: str) -> str:
return '&[' + ','.join('0x' + h[i:i + 2] for i in range(0, len(h), 2)) + ']'
def main(argv=None) -> None:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--check', action='store_true', help='verify checked-in fixture and replay JSON')
args = parser.parse_args(argv)
ev_path = R / 'reports/software-controls-replay-original-rea.json'
if not ev_path.is_file():
raise SystemExit(f'evidence file not found: {ev_path}')
evidence = json.loads(ev_path.read_text(encoding='utf-8'))
d = json.loads(''.join(f['data'] for f in evidence['normalized_result']['frames']).replace('\r', ''))
replay_json = json.dumps(d, indent=2) + '\n'
rows = []
for row in d['records']:
rows.append(
' ('
+ json.dumps(row['label'])
+ ','
+ row['request']
+ ','
+ _arr(row['input_top'])
+ ','
+ _arr(row['input_payload'] or '')
+ ','
+ _arr(row['response'])
+ ','
+ _arr(row.get('payload', ''))
+ '),'
)
fixture_text = '// Original REA Evidence ' + evidence['evidence_id'] + '\n[\n' + '\n'.join(rows) + '\n]\n'
replay_path = R / 'reports/software-controls-replay-original.json'
fixture_path = R / 'src/nvidia/tests/fixtures/software_controls.rs'
if args.check:
if not replay_path.is_file() or replay_path.read_text(encoding='utf-8') != replay_json:
raise SystemExit(f'stale {replay_path}')
if not fixture_path.is_file() or fixture_path.read_text(encoding='utf-8') != fixture_text:
raise SystemExit(f'stale {fixture_path}')
else:
replay_path.parent.mkdir(parents=True, exist_ok=True)
fixture_path.parent.mkdir(parents=True, exist_ok=True)
replay_path.write_text(replay_json, encoding='utf-8')
fixture_path.write_text(fixture_text, encoding='utf-8')
print('Generated', len(rows), 'original control records')
if __name__ == '__main__':
main()