Spaces:
Sleeping
Sleeping
| 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 | |