-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathmain.py
More file actions
105 lines (93 loc) · 3.26 KB
/
main.py
File metadata and controls
105 lines (93 loc) · 3.26 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
import argparse
import getpass
import random
import time
import uuid
from pathlib import Path
import yaml
from codeclash import CONFIG_DIR
from codeclash.constants import LOCAL_LOG_DIR
from codeclash.tournaments.pvp import PvpTournament
from codeclash.utils.aws import is_running_in_aws_batch
from codeclash.utils.yaml_utils import resolve_includes
def main(
config_path: Path,
*,
cleanup: bool = False,
output_dir: Path | None = None,
suffix: str = "",
keep_containers: bool = False,
):
yaml_content = config_path.read_text()
preprocessed_yaml = resolve_includes(yaml_content, base_dir=CONFIG_DIR)
config = yaml.safe_load(preprocessed_yaml)
def get_output_path() -> Path:
if is_running_in_aws_batch():
# Offset timestamp by random seconds to avoid collisions
# Hopefully that means we can just remove the uuid part later on
offset = random.randint(0, 600)
timestamp = time.strftime("%y%m%d%H%M%S", time.localtime(time.time() + offset))
else:
timestamp = time.strftime("%y%m%d%H%M%S")
rounds = config["tournament"]["rounds"]
transparent = config["tournament"].get("transparent", False)
sims = config["game"]["sims_per_round"]
players = [p["name"] for p in config["players"]]
p_num = len(players)
p_list = ".".join(sorted(players))
suffix_part = f".{suffix}" if suffix else ""
folder_name = (
f"PvpTournament.{config['game']['name']}.r{rounds}.s{sims}.p{p_num}.{p_list}{suffix_part}.{timestamp}"
)
if transparent:
folder_name += ".transparent"
if is_running_in_aws_batch():
# Also add a UUID just to be safe
_uuid = str(uuid.uuid4())
folder_name += f".{_uuid}-uuid"
if output_dir is None:
if is_running_in_aws_batch():
return LOCAL_LOG_DIR / "batch" / folder_name
else:
return LOCAL_LOG_DIR / getpass.getuser() / folder_name
else:
return output_dir / folder_name
full_output_dir = get_output_path()
tournament = PvpTournament(config, output_dir=full_output_dir, cleanup=cleanup, keep_containers=keep_containers)
tournament.run()
def main_cli(argv: list[str] | None = None):
parser = argparse.ArgumentParser(description="CodeClash")
parser.add_argument(
"config_path",
type=Path,
help="Path to the config file.",
)
parser.add_argument(
"-c",
"--cleanup",
action="store_true",
help="If set, do not clean up the game environment after running.",
)
parser.add_argument(
"-o",
"--output-dir",
type=Path,
help="Sets the output directory (default is 'logs' with current user subdirectory).",
)
parser.add_argument(
"-s",
"--suffix",
type=str,
help="Suffix to attach to the folder name. Does not include leading dot or underscore.",
default="",
)
parser.add_argument(
"-k",
"--keep-containers",
action="store_true",
help="Do not remove containers after games/agent finish",
)
args = parser.parse_args(argv)
main(**vars(args))
if __name__ == "__main__":
main_cli()