-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathbattle_pipeline.py
More file actions
117 lines (96 loc) · 3.79 KB
/
battle_pipeline.py
File metadata and controls
117 lines (96 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/env python3
import argparse
import os
import subprocess
import sys
import shutil
from pathlib import Path
# --- Optional: Load secrets from .env file ---
try:
from dotenv import load_dotenv
load_dotenv()
except ImportError:
print("💡 Tip: install python-dotenv for .env support (pip install python-dotenv)")
# --- Parse arguments ---
parser = argparse.ArgumentParser(description="RL Tournament Battle Pipeline (Local Version for Windows)")
parser.add_argument("--username1", required=True, help="GitHub username of first participant (fork owner)")
parser.add_argument("--username2", required=True, help="GitHub username of second participant (fork owner)")
args = parser.parse_args()
u1 = args.username1
u2 = args.username2
# --- Step 1: Validate Inputs ---
if u1 == u2:
print("❌ Error: Usernames must be different.")
sys.exit(1)
print(f"✅ Usernames validated: {u1} vs {u2}")
# --- Step 2: Check environment variables ---
SUPABASE_URL = os.getenv("SUPABASE_URL")
SUPABASE_SERVICE_ROLE_KEY = os.getenv("SUPABASE_SERVICE_ROLE_KEY")
if not SUPABASE_URL or not SUPABASE_SERVICE_ROLE_KEY:
print("⚠️ Warning: Missing SUPABASE_URL or SUPABASE_SERVICE_ROLE_KEY environment variables.")
print("Please set them in your environment or a .env file.")
else:
print("✅ Supabase environment variables loaded.")
# --- Step 3: Install dependencies (if install.bat or install.sh exists) ---
# if Path("install.bat").exists():
# print("🔧 Running install.bat...")
# subprocess.run(["install.bat"], shell=True, check=True)
# elif Path("install.sh").exists():
# print("🔧 Running install.sh via bash...")
# subprocess.run(["bash", "install.sh"], shell=True, check=True)
# else:
# print("⚠️ No install script found, skipping dependency installation.")
# --- Step 4: Validate Participants via API ---
print("🧩 Checking participant validation via api.py...")
server_path = Path("server").resolve()
sys.path.insert(0, str(server_path))
try:
import api
except ImportError as e:
print(f"❌ Could not import api.py from {server_path}. Error: {e}")
sys.exit(1)
try:
is_valid = api.validate_battle(u1, u2)
except Exception as e:
print(f"❌ Error calling validate_battle: {e}")
sys.exit(1)
if not is_valid:
print(f"❌ Validation failed: One or both users have not passed validation (u1={u1}, u2={u2})")
sys.exit(1)
else:
print(f"✅ Validation passed: Both users have passed validation (u1={u1}, u2={u2})")
# --- Step 5: Clone Forks ---
repo_name = Path.cwd().name
print("🌀 Cloning participant forks...")
def clone_repo(username, dest):
if Path(dest).exists():
shutil.rmtree(dest)
url = f"https://github.com/{username}/{repo_name}.git"
print(f"→ Cloning {url} into {dest}")
subprocess.run(["git", "clone", url, dest], shell=True, check=True)
clone_repo(u1, "fork1")
clone_repo(u2, "fork2")
# --- Step 6: Copy agents to main repo ---
print("📦 Copying agents...")
os.makedirs(f"agents/{u1}", exist_ok=True)
os.makedirs(f"agents/{u2}", exist_ok=True)
try:
shutil.copy("fork1/user/my_agent.py", f"agents/{u1}/my_agent.py")
shutil.copy("fork2/user/my_agent.py", f"agents/{u2}/my_agent.py")
except FileNotFoundError as e:
print(f"❌ Missing agent file: {e}")
sys.exit(1)
print("✅ Agents copied successfully.")
for root, _, files in os.walk("agents"):
for f in files:
print(" -", Path(root) / f)
# --- Step 7: Run Battle ---
print("⚔️ Running battle...")
os.environ["AGENT1_PATH"] = f"agents/{u1}/my_agent.py"
os.environ["AGENT2_PATH"] = f"agents/{u2}/my_agent.py"
try:
subprocess.run(["pytest", "-s", "user/battle.py"], check=True, shell=True)
print("🏁 Battle completed successfully!")
except subprocess.CalledProcessError:
print("❌ Battle failed. Check logs above.")
sys.exit(1)