Files

36 lines
2.5 KiB
Python

#!/usr/bin/env python3
"""One scratch diagnostic, executable only after Sage grants its gap."""
import argparse,json,os,signal,subprocess,time
from pathlib import Path
HERE=Path(__file__).resolve().parent
def main():
ap=argparse.ArgumentParser();ap.add_argument('--grant',required=True);args=ap.parse_args()
if not os.environ.get('TMPDIR','').startswith('/mnt/storage/scratch/tmp/g'):
raise RuntimeError('create the granted gXXX TMPDIR first')
prep=json.loads((HERE/'PREPARE.json').read_text());copy=Path(prep['scratch_copy'])
busy=subprocess.run(['bash','/mnt/storage/scratch/sage-busy-now.sh'],capture_output=True,text=True)
if busy.returncode or busy.stdout.splitlines()[-1:]!=['CLEAR']:
raise RuntimeError('busy refusal: '+busy.stdout+busy.stderr)
env=os.environ.copy();env['ROW253_P10_DIAG']=str(HERE/'P10.jsonl')
if (HERE/'RUN-RESULT.json').exists() or (HERE/'P10.jsonl').exists():
raise RuntimeError('one diagnostic only; existing output refuses automatic rerun')
command=['timeout','-k','10','418','node','--test','--test-reporter=tap','--test-name-pattern','row253 P10 refuses unsafe metadata','packages/cli/tests/case1-accept.test.mjs']
command_file=HERE/'command.json';command_file.write_text(json.dumps(command)+'\n')
header=dict(grant=args.grant,base=prep['base'],scratch_copy=str(copy),tmpdir=env['TMPDIR'],command=command,reason_oracle='EXPECTED-REASONS.md drafted before diagnostic; output does not define expected reasons')
(HERE/'RUN-HEADER.json').write_text(json.dumps(header,indent=2)+'\n');print(json.dumps(header),flush=True)
started=time.monotonic()
p=subprocess.Popen(['bash','-o','pipefail','-c','python3 "$1" "$2" 2>&1 | tee "$3"','--',str(HERE.parent/'launch.py'),str(command_file),str(HERE/'diagnostic.log')],cwd=copy,env=env,start_new_session=True)
timeout=False
try:
code=p.wait(timeout=448);timeout=code in (124,137)
except subprocess.TimeoutExpired:
timeout=True;os.killpg(p.pid,signal.SIGKILL);code=p.wait()
finally:
try:os.killpg(p.pid,signal.SIGKILL)
except ProcessLookupError:pass
result=dict(code=code,timeout=timeout,seconds=round(time.monotonic()-started,3),header=header,log=str(HERE/'diagnostic.log'),p10_file=str(HERE/'P10.jsonl'))
(HERE/'RUN-RESULT.json').write_text(json.dumps(result,indent=2)+'\n');print(json.dumps(result),flush=True)
if timeout or code:raise RuntimeError('diagnostic stopped; no retry or repair before ruling')
if __name__=='__main__':main()