Datasets:
Download scripts/common.py from CrossSWE/CrossSWE-Bench: direct link, hf CLI and curl.
- Browser
- Download file 2.14 kB
-
https://huggingface.co/datasets/CrossSWE/CrossSWE-Bench/resolve/main/scripts/common.py
- Command line
-
hf download hf://datasets/CrossSWE/CrossSWE-Bench/scripts/common.py
-
curl -L -o common.py https://huggingface.co/datasets/CrossSWE/CrossSWE-Bench/resolve/main/scripts/common.py
2.14 kB
| """Shared data loading, file integrity and subprocess helpers.""" | |
| import hashlib | |
| import csv | |
| import json | |
| from pathlib import Path | |
| import subprocess | |
| ROOT = Path(__file__).resolve().parents[1] | |
| def sha256(path): | |
| digest = hashlib.sha256() | |
| with Path(path).open("rb") as stream: | |
| for chunk in iter(lambda: stream.read(1024 * 1024), b""): | |
| digest.update(chunk) | |
| return digest.hexdigest() | |
| def contained(root, relative): | |
| path = Path(relative) | |
| if path.is_absolute() or ".." in path.parts or not path.parts: | |
| raise ValueError(f"Unsafe relative path: {relative}") | |
| resolved = (Path(root) / path).resolve() | |
| if not resolved.is_relative_to(Path(root).resolve()): | |
| raise ValueError(f"Path escapes root: {relative}") | |
| return resolved | |
| def resolve_instance_id(instance_id): | |
| # Check input paths before looking up aliases, including unknown identifiers. | |
| contained(ROOT / "instances", instance_id) | |
| mapping_path = ROOT / "data/instance-id-map.tsv" | |
| if mapping_path.is_file(): | |
| with mapping_path.open() as stream: | |
| rows = list(csv.DictReader(stream, delimiter="\t")) | |
| aliases = {} | |
| for row in rows: | |
| canonical = row["instance_id"] | |
| for alias in (row["original_instance_id"], canonical): | |
| if alias in aliases and aliases[alias] != canonical: | |
| raise ValueError("Ambiguous instance ID mapping") | |
| aliases[alias] = canonical | |
| if instance_id not in aliases: | |
| raise ValueError(f"Unknown instance ID: {instance_id}") | |
| return aliases[instance_id] | |
| return instance_id | |
| def load_task(instance_id): | |
| canonical = resolve_instance_id(instance_id) | |
| path = contained(ROOT / "instances", canonical) / "task.json" | |
| task = json.loads(path.read_text()) | |
| if task["instance_id"] != canonical: | |
| raise ValueError("Task ID does not match directory") | |
| return task | |
| def run(*args, cwd=None, capture=False): | |
| return subprocess.run(args, cwd=cwd, check=True, text=True, | |
| stdout=subprocess.PIPE if capture else None).stdout | |