Spaces:
Sleeping
Sleeping
File size: 3,469 Bytes
141f1e0 38308df | 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 118 119 120 121 122 123 124 125 126 127 128 | import hashlib
import json
import os
from datetime import datetime, timezone
from src.envs import API, TEAMS_PATH, TEAMS_REPO
def hash_token(token: str) -> str:
return hashlib.sha256(token.encode()).hexdigest()
def ensure_teams_dir():
os.makedirs(TEAMS_PATH, exist_ok=True)
def register_team(team_name: str, num_teammates: int, token: str) -> dict:
ensure_teams_dir()
token_hash = hash_token(token)
team_data = {
"team_name": team_name,
"num_teammates": num_teammates,
"token_hash": token_hash,
"created_at": datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ"),
}
team_file = os.path.join(TEAMS_PATH, f"{team_name}.json")
if os.path.exists(team_file):
with open(team_file, "r") as f:
existing = json.load(f)
if existing.get("token_hash") != token_hash:
raise ValueError("Team name already exists with different token")
return existing
with open(team_file, "w") as f:
json.dump(team_data, f)
try:
API.upload_file(
path_or_fileobj=team_file,
path_in_repo=f"{team_name}.json",
repo_id=TEAMS_REPO,
repo_type="dataset",
commit_message=f"Register team: {team_name}",
)
except Exception as e:
print(f"Warning: Could not upload team registration to hub: {e}")
return team_data
def get_team_by_token(token: str) -> dict | None:
ensure_teams_dir()
token_hash = hash_token(token)
for filename in os.listdir(TEAMS_PATH):
if not filename.endswith(".json"):
continue
filepath = os.path.join(TEAMS_PATH, filename)
try:
with open(filepath, "r") as f:
team_data = json.load(f)
if team_data.get("token_hash") == token_hash:
return team_data
except Exception:
continue
return None
def get_all_teams() -> list[dict]:
ensure_teams_dir()
teams = []
for filename in os.listdir(TEAMS_PATH):
if not filename.endswith(".json"):
continue
filepath = os.path.join(TEAMS_PATH, filename)
try:
with open(filepath, "r") as f:
teams.append(json.load(f))
except Exception:
continue
return teams
def update_last_valid_submission(team_name: str) -> None:
ensure_teams_dir()
team_file = os.path.join(TEAMS_PATH, f"{team_name}.json")
if not os.path.exists(team_file):
return
with open(team_file, "r") as f:
team_data = json.load(f)
team_data["last_valid_submission"] = datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
with open(team_file, "w") as f:
json.dump(team_data, f)
try:
API.upload_file(
path_or_fileobj=team_file,
path_in_repo=f"{team_name}.json",
repo_id=TEAMS_REPO,
repo_type="dataset",
commit_message=f"Update last submission for team: {team_name}",
)
except Exception as e:
print(f"Warning: Could not upload team update to hub: {e}")
def get_team_by_name(team_name: str) -> dict | None:
ensure_teams_dir()
team_file = os.path.join(TEAMS_PATH, f"{team_name}.json")
if not os.path.exists(team_file):
return None
try:
with open(team_file, "r") as f:
return json.load(f)
except Exception:
return None
|