Jotley / website / scripts / video-refresh / capture-physical.py
| 1 | #!/usr/bin/env python3 |
| 2 | """Capture real speech on a ready paired device using Xcode's on-device video. |
| 3 | |
| 4 | Requires the isolated signed capture build and the user's device-readiness reply. |
| 5 | Uses internal storage while recording, so external-drive disconnects cannot lose |
| 6 | an active movie. Does not install over the user's shipping Jotley application. |
| 7 | """ |
| 8 | import argparse |
| 9 | import json |
| 10 | import plistlib |
| 11 | from pathlib import Path |
| 12 | import re |
| 13 | import subprocess |
| 14 | import time |
| 15 | |
| 16 | |
| 17 | def main(): |
| 18 | parser = argparse.ArgumentParser(description=__doc__) |
| 19 | parser.add_argument('device', choices=['iphone', 'ipad']) |
| 20 | parser.add_argument('--udid', required=True) |
| 21 | parser.add_argument('--test-run', type=Path, required=True) |
| 22 | parser.add_argument('--audio', type=Path, required=True) |
| 23 | parser.add_argument('--output', type=Path, required=True) |
| 24 | args = parser.parse_args() |
| 25 | args.output.mkdir(parents=True, exist_ok=False) |
| 26 | result = args.output / 'result.xcresult' |
| 27 | log = args.output / 'test.log' |
| 28 | command = ['xcodebuild', 'test-without-building', '-xctestrun', str(args.test_run), |
| 29 | '-destination', f'platform=iOS,id={args.udid}', '-parallel-testing-enabled', 'NO', |
| 30 | '-collect-test-diagnostics', 'never', |
| 31 | '-only-testing:Jotley-iOSUITests/JotleyVideoCaptureUITests/testCaptureRealLiveTranscription', |
| 32 | '-resultBundlePath', str(result)] |
| 33 | config = plistlib.loads(args.test_run.read_bytes()) |
| 34 | capture_formats = sorted({t.get('PreferredScreenCaptureFormat', 'screenshots') for c in config['TestConfigurations'] for t in c['TestTargets']}) |
| 35 | receipt = {'device': args.device, 'udid': args.udid, 'test_run': str(args.test_run), |
| 36 | 'state': 'capturing', 'source_commit': '89422fb2bbfb839f24eb4ba85f4a34be1ff51020', |
| 37 | 'speech': 'Real physical microphone and Apple Speech; no simulated speech hooks', |
| 38 | 'capture_formats': capture_formats, 'started_wall': time.time()} |
| 39 | receipt_path = args.output / 'capture.json' |
| 40 | receipt_path.write_text(json.dumps(receipt, indent=2) + '\n') |
| 41 | audio = None |
| 42 | with log.open('w') as output: |
| 43 | process = subprocess.Popen(command, stdout=output, stderr=subprocess.STDOUT) |
| 44 | deadline = time.monotonic() + 360 |
| 45 | try: |
| 46 | while process.poll() is None: |
| 47 | content = log.read_text() |
| 48 | ready = re.search(r'CAPTURE-MARK live-ready ([0-9.]+)', content) |
| 49 | if ready and audio is None and time.time() >= float(ready[1]) + 1: |
| 50 | receipt['audio_start_wall'] = time.time() |
| 51 | receipt['demo_audio'] = str(args.audio) |
| 52 | audio = subprocess.Popen(['afplay', str(args.audio)]) |
| 53 | receipt_path.write_text(json.dumps(receipt, indent=2) + '\n') |
| 54 | if time.monotonic() > deadline: |
| 55 | process.terminate() |
| 56 | raise TimeoutError('Physical capture exceeded six minutes') |
| 57 | time.sleep(0.15) |
| 58 | finally: |
| 59 | if audio and audio.poll() is None: |
| 60 | audio.terminate() |
| 61 | content = log.read_text() |
| 62 | receipt['marks'] = {name: float(wall) for name, wall in re.findall(r'CAPTURE-MARK ([\w-]+) ([0-9.]+)', content)} |
| 63 | receipt['test_exit_code'] = process.returncode |
| 64 | receipt['one_test_executed'] = bool(re.search(r'Executed 1 test,', content)) |
| 65 | receipt['state'] = 'passed' if process.returncode == 0 and receipt['one_test_executed'] else 'failed' |
| 66 | attachments = args.output / 'attachments' |
| 67 | if result.exists(): |
| 68 | export = subprocess.run(['xcrun', 'xcresulttool', 'export', 'attachments', '--path', str(result), |
| 69 | '--output-path', str(attachments)], capture_output=True, text=True) |
| 70 | (args.output / 'attachments-export.log').write_text(export.stdout + export.stderr) |
| 71 | receipt['attachments_export_exit_code'] = export.returncode |
| 72 | movies = [] |
| 73 | for movie in attachments.glob('*.mp4'): |
| 74 | probe = subprocess.run(['ffprobe', '-v', 'error', '-show_format', '-show_streams', '-of', 'json', str(movie)], |
| 75 | capture_output=True, text=True) |
| 76 | decode = subprocess.run(['ffmpeg', '-v', 'error', '-i', str(movie), '-f', 'null', '-'], capture_output=True, text=True) |
| 77 | movies.append({'file': str(movie), 'probe': json.loads(probe.stdout) if probe.returncode == 0 else None, |
| 78 | 'full_decode': decode.returncode == 0}) |
| 79 | receipt['movies'] = movies |
| 80 | receipt_path.write_text(json.dumps(receipt, indent=2) + '\n') |
| 81 | print(json.dumps(receipt, indent=2), flush=True) |
| 82 | raise SystemExit(0 if receipt['state'] == 'passed' else process.returncode or 2) |
| 83 | |
| 84 | |
| 85 | if __name__ == '__main__': |
| 86 | main() |