#!/usr/bin/env python3
import shlex
import subprocess
import sys
from pathlib import Path
root=Path(__file__).parent
lane=sys.argv[1]
assert lane in ('B','C')
script=(root/'cleanup-runtime.php').read_bytes().replace(b"$lane=$argv[1]??getenv('PM_LANE');",('$lane='+repr(lane)+';').encode())
args=['docker','exec','-i','zhctapp_v2_0','/usr/local/php7.3/bin/php'] if lane=='B' else ['ssh','-S','/private/tmp/cdx-train-root-20261010.sock','-o','BatchMode=yes','root@121.43.241.161','/www/server/php/73/bin/php']
evidence=root/'cleanup-private-evidence'
evidence.mkdir(mode=0o700,exist_ok=True)
assert not (evidence/(lane+'-cleanup.jsonl')).exists(),'Do not blindly repeat cleanup'
with (evidence/(lane+'-cleanup.jsonl')).open('xb')as output,(evidence/(lane+'-cleanup.stderr')).open('xb')as error:
    (evidence/(lane+'-cleanup.jsonl')).chmod(0o600);(evidence/(lane+'-cleanup.stderr')).chmod(0o600)
    result=subprocess.run(args,input=script,stdout=output,stderr=error,check=False)
print(lane,'cleanup exit',result.returncode)
print((evidence/(lane+'-cleanup.jsonl')).read_text().splitlines()[-1])
raise SystemExit(result.returncode)
