import os
import subprocess
import sys

def find_free_port(start_port):
    import socket
    port = start_port
    while True:
        with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
            if s.connect_ex(('localhost', port)) != 0:
                return port
            port += 1
def run(exp_name, exp_root="experiments",port=None):
    port = 6007 if port is None else port
    max_attempts = 10
    project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    tensorboard_root = os.path.join(project_root, exp_root, exp_name, "tensorboard")
    
    for attempt in range(max_attempts):
        try:
            print(f"Trying to launch TensorBoard on port {port}...")
            subprocess.check_call([
                sys.executable, "-m", "tensorboard.main",
                f"--logdir={tensorboard_root}",
                f"--port={port}"
            ])
            break
        except subprocess.CalledProcessError as e:
            print(f"Port {port} is in use, trying next port...")
            port = find_free_port(port + 1)
    else:
        print("Failed to launch TensorBoard after multiple attempts.")

if __name__ == "__main__":
    exp_root = "experiments"
    exp_name = "sample_train_100_item_overfit_foreground_0"
    run(exp_name,exp_root,port=6009)