Skip to content

Commit 16cf792

Browse files
committed
Push diffs to gh
1 parent 098d491 commit 16cf792

1 file changed

Lines changed: 82 additions & 0 deletions

File tree

scripts/push_log_to_gh.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
"""
2+
Pushes diffs as a branch to Github.
3+
"""
4+
5+
import argparse
6+
import json
7+
import os
8+
import subprocess
9+
from pathlib import Path
10+
11+
12+
def main(cc_folder: Path):
13+
folder_contents = [f.name for f in list(cc_folder.iterdir())]
14+
assert "metadata.json" in folder_contents, "No metadata.json found"
15+
assert "players" in folder_contents, "No players/ folder found"
16+
17+
arena = cc_folder.name.split(".", 2)[1]
18+
19+
# Clone GitHub repository if it doesn't exist
20+
if arena not in os.listdir():
21+
clone_cmd = f"git clone git@github.com:emagedoc/{arena}.git"
22+
subprocess.run(clone_cmd, shell=True, check=True)
23+
24+
# Get existing remote branches
25+
remote_branches = (
26+
subprocess.run(
27+
"git branch -r",
28+
shell=True,
29+
check=True,
30+
capture_output=True,
31+
cwd=arena,
32+
)
33+
.stdout.decode("utf-8")
34+
.splitlines()
35+
)
36+
remote_branches = [b.split("/")[-1] for b in remote_branches if "PvpTournament" in b]
37+
38+
# Identify players
39+
with open(cc_folder / "metadata.json") as f:
40+
metadata = json.load(f)
41+
players = [x["name"] for x in metadata["config"]["players"]]
42+
43+
# Push diffs for each player
44+
for player in players:
45+
player_log_folder = cc_folder / "players" / player
46+
branch_name = f"{cc_folder.name}.{player}"
47+
if branch_name in remote_branches:
48+
print(f"Branch {branch_name} already exists, skipping...")
49+
continue
50+
subprocess.run(
51+
f"git checkout main; git branch {branch_name} -D; git checkout -b {branch_name}",
52+
shell=True,
53+
check=True,
54+
cwd=arena,
55+
)
56+
for idx in range(1, 16):
57+
changes_file = player_log_folder / f"changes_r{idx}.json"
58+
if not changes_file.exists():
59+
continue
60+
with open(changes_file) as f:
61+
changes = json.load(f)
62+
with open("temp.diff", "w") as f:
63+
f.write(changes["incremental_diff"])
64+
65+
apply_cmd = "git apply ../temp.diff"
66+
if arena == "BattleSnake":
67+
apply_cmd += " --exclude=game/battlesnake"
68+
elif arena == "CoreWar":
69+
apply_cmd += " --exclude=src/pmars"
70+
elif arena == "RobotRumble":
71+
apply_cmd += " --exclude=rumblebot"
72+
subprocess.run(apply_cmd, shell=True, check=True, cwd=arena)
73+
subprocess.run("git add .", shell=True, check=True, cwd=arena)
74+
subprocess.run(f"git commit -m 'Round {idx} changes'", shell=True, check=True, cwd=arena)
75+
subprocess.run(f"git push origin {branch_name}", shell=True, check=True, cwd=arena)
76+
77+
78+
if __name__ == "__main__":
79+
parser = argparse.ArgumentParser()
80+
parser.add_argument("cc_folder", type=Path, help="Path to the CodeClash log folder")
81+
args = parser.parse_args()
82+
main(args.cc_folder)

0 commit comments

Comments
 (0)