#!/usr/bin/env python3 """Build the Hugging Face release layout for the current benchmark.""" from __future__ import annotations import json import shutil from pathlib import Path ROOT = Path(__file__).resolve().parents[5] EVAL_STAGES = ROOT / "Kaggle" / "analyze_code" / "eval_stages" HF_DIR = EVAL_STAGES / "huggingface_version" QUESTION_SRC = EVAL_STAGES / "supplementmaterial" / "datasets" ENV_DIR = ( EVAL_STAGES / "eval_environment" / "eval_run_260118_finalset_AGENT_swe-agent-gpt-5.2_20260331" ) QUESTION_FILES = ("codabench.json", "codabench-hard.json") SKIP_ROOT_FILES = {"result.txt", "task_description.txt"} def community_sort_key(pair: tuple[str, str]) -> tuple[str, int]: source_type, community = pair return source_type, int(community.rsplit("_", 1)[1]) def load_questions() -> dict[str, list[dict]]: return { name: json.loads((QUESTION_SRC / name).read_text(encoding="utf-8")) for name in QUESTION_FILES } def write_json(path: Path, data: object) -> None: path.parent.mkdir(parents=True, exist_ok=True) path.write_text( json.dumps(data, ensure_ascii=False, indent=2) + "\n", encoding="utf-8", ) def copy_tree_deref(src: Path, dst: Path) -> None: """Copy a file or directory while dereferencing symlinks.""" if src.is_dir(): shutil.copytree(src, dst, symlinks=False, dirs_exist_ok=True) elif src.is_file(): dst.parent.mkdir(parents=True, exist_ok=True) shutil.copy2(src, dst, follow_symlinks=True) def build() -> dict: questions_by_file = load_questions() all_pairs = sorted( { (item["source_type"], item["community"]) for rows in questions_by_file.values() for item in rows }, key=community_sort_key, ) community_map = { f"{source_type}/{community}": { "community_id": f"community_{idx}", "source_type": source_type, "source_community": community, "source_env_dir": f"{source_type}_{community}", "data_path": f"data/community_{idx}/full_community", } for idx, (source_type, community) in enumerate(all_pairs) } datasets_dir = HF_DIR / "datasets" data_dir = HF_DIR / "data" datasets_dir.mkdir(parents=True, exist_ok=True) data_dir.mkdir(parents=True, exist_ok=True) for name, rows in questions_by_file.items(): enriched = [] for item in rows: key = f"{item['source_type']}/{item['community']}" mapped = community_map[key] enriched.append( { **item, "release_community": mapped["community_id"], "data_path": mapped["data_path"], } ) write_json(datasets_dir / name, enriched) copied_roots = 0 copied_instances = 0 copied_dataset_entries = 0 missing_env_dirs: list[str] = [] for community_index, (source_key, mapped) in enumerate(community_map.items(), start=1): env_comm_dir = ENV_DIR / mapped["source_env_dir"] dst_full = HF_DIR / mapped["data_path"] dst_full.mkdir(parents=True, exist_ok=True) copied_entry_names: set[str] = set() print( f"[{community_index}/{len(community_map)}] {source_key} -> {mapped['community_id']}", flush=True, ) if not env_comm_dir.is_dir(): missing_env_dirs.append(str(env_comm_dir)) continue instance_dirs = sorted( (p for p in env_comm_dir.glob("instance_*") if p.is_dir()), key=lambda p: int(p.name.rsplit("_", 1)[1]), ) for instance_dir in instance_dirs: full_dir = instance_dir / "full_community" if not full_dir.is_dir(): continue copied_instances += 1 for entry in full_dir.iterdir(): if entry.name in SKIP_ROOT_FILES: continue if entry.name in copied_entry_names: continue if (dst_full / entry.name).exists(): copied_entry_names.add(entry.name) continue copy_tree_deref(entry, dst_full / entry.name) copied_entry_names.add(entry.name) copied_dataset_entries += 1 copied_roots += 1 manifest = { "name": "huggingface_release_current_benchmark", "source_eval_environment": str(ENV_DIR), "question_files": list(QUESTION_FILES), "num_release_communities": len(community_map), "num_full_questions": len(questions_by_file["codabench.json"]), "num_hard_questions": len(questions_by_file["codabench-hard.json"]), "copied_community_roots": copied_roots, "copied_instance_full_community_dirs": copied_instances, "copied_dataset_entries_before_dedup": copied_dataset_entries, "missing_env_dirs": missing_env_dirs, "community_map": community_map, "copy_policy": "All data files are copied with symlinks dereferenced; root result.txt and task_description.txt are excluded.", } write_json(HF_DIR / "release_manifest.json", manifest) readme = """# Hugging Face Benchmark Release This directory contains the current benchmark release split into question JSON files and copied data files. - `datasets/codabench.json`: full benchmark questions. - `datasets/codabench-hard.json`: hard subset questions. - `data/community_*/full_community`: data directories referenced by the JSON `data_path` field. - `release_manifest.json`: source-to-release community mapping and copy statistics. The data copy dereferences the symlinks produced by `stage1_setup`, so the release contains actual file contents rather than symlink placeholders. """ (HF_DIR / "README.md").write_text(readme, encoding="utf-8") return manifest if __name__ == "__main__": result = build() print(json.dumps(result, ensure_ascii=False, indent=2))