#!/usr/bin/env python3
"""Check the build error from PyTorch CMake on BC-250."""
import paramiko

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('192.168.178.150', username='fabian', key_filename=r'C:\Users\fabia\.ssh\id_ed25519')

def run(cmd, timeout=30, desc=""):
    if desc:
        print(f"\n{'='*60}")
        print(f"  {desc}")
        print(f"{'='*60}")
    _, stdout, stderr = ssh.exec_command(cmd, timeout=timeout)
    out = stdout.read().decode()
    if out.strip():
        print(out.strip())

# Get the full cmake error
run("grep -A5 -B2 'Error\\|error\\|FATAL\\|fatal\\|Could not find' /home/fabian/pytorch_build.log | head -60",
    desc="CMake errors in build log")

# Also check the cmake output file if it exists
run("cat /home/fabian/pytorch/build/CMakeFiles/CMakeOutput.log 2>/dev/null | tail -30 || echo 'no output log'",
    desc="CMake output log")

run("cat /home/fabian/pytorch/build/CMakeFiles/CMakeError.log 2>/dev/null | tail -50 || echo 'no error log'",
    desc="CMake error log")

# Check specifically what's missing
run("grep -i 'not found\\|could not find\\|missing' /home/fabian/pytorch_build.log | head -20",
    desc="Missing packages")

# Also check if the process is still running
run("pgrep -fa 'setup.py\\|build_pytorch' || echo 'Build process not running'",
    desc="Build process status")

# Check roctracer
run("find /opt/rocm -name 'roctracer*' 2>/dev/null | head -10",
    desc="roctracer files")

ssh.close()
