Datasets:
Download scripts/fetch_sources.py from drawais/circuitqa: direct link, hf CLI and curl.
- Browser
- Download file 3.6 kB
-
https://huggingface.co/datasets/drawais/circuitqa/resolve/main/scripts/fetch_sources.py
- Command line
-
hf download hf://datasets/drawais/circuitqa/scripts/fetch_sources.py
-
curl -L -o fetch_sources.py https://huggingface.co/datasets/drawais/circuitqa/resolve/main/scripts/fetch_sources.py
3.6 kB
| #!/usr/bin/env python3 | |
| """Download the design sources referenced by this dataset from their upstream repositories | |
| and verify them against the recorded SHA-256. No design source is redistributed here; every | |
| design remains under the licence of its upstream repository. | |
| python3 scripts/fetch_sources.py corpus/test.jsonl compositional/designs.jsonl --out sources/ | |
| Each record's `source` gives the repository, the commit the dataset was built from (when known), | |
| the path inside the repository and the SHA-256 of the file. The script downloads each repository | |
| archive at that commit (or the default branch when no commit is recorded), extracts the referenced | |
| files, and writes sources/<sha256>.v for every file whose content matches. Records without a | |
| `source` field are ignored. Files are decoded as UTF-8 (undecodable bytes dropped), as when the | |
| dataset was built, and the checksum is computed on that text. Paths that lie inside git | |
| submodules of their repository are not part of the archive and are reported as not found.""" | |
| import argparse, collections, hashlib, json, os, sys, tarfile, tempfile, urllib.request | |
| ap = argparse.ArgumentParser(); ap.add_argument("files", nargs="+"); ap.add_argument("--out", default="sources") | |
| a = ap.parse_args(); os.makedirs(a.out, exist_ok=True) | |
| need = collections.defaultdict(dict) | |
| for fn in a.files: | |
| for line in open(fn): | |
| if not line.strip(): | |
| continue | |
| s = json.loads(line).get("source") | |
| if s and (s.get("repository") or "").startswith("https://github.com/"): | |
| need[(s["repository"], s.get("commit") or "HEAD")][s["path"]] = s["sha256"] | |
| ok = changed = missing = failed = 0 | |
| for (repo, ref), files in sorted(need.items()): | |
| owner_repo = repo.split("github.com/")[1] | |
| print(f"{owner_repo}@{ref[:10]}: {len(files)} files", file=sys.stderr) | |
| with tempfile.NamedTemporaryFile(suffix=".tar.gz") as tmp: | |
| try: | |
| with urllib.request.urlopen(f"https://codeload.github.com/{owner_repo}/tar.gz/{ref}", timeout=300) as r: | |
| while True: | |
| chunk = r.read(1 << 20) | |
| if not chunk: break | |
| tmp.write(chunk) | |
| except OSError as e: # repository renamed, removed or unreachable | |
| print(f" could not download {owner_repo}@{ref[:10]}: {e}", file=sys.stderr) | |
| failed += 1; missing += len(files); continue | |
| tmp.flush() | |
| found = set() | |
| try: | |
| with tarfile.open(tmp.name, mode="r:gz") as t: | |
| for m in t: | |
| rel = m.name.split("/", 1)[1] if "/" in m.name else "" | |
| if m.isfile() and rel in files: | |
| text = t.extractfile(m).read().decode("utf-8", errors="ignore") | |
| h = hashlib.sha256(text.encode()).hexdigest(); found.add(rel) | |
| if h == files[rel]: | |
| with open(os.path.join(a.out, h + ".v"), "w", encoding="utf-8", newline="") as o: o.write(text) | |
| ok += 1 | |
| else: | |
| changed += 1 | |
| except (tarfile.TarError, EOFError, OSError) as e: # truncated or unreadable archive | |
| print(f" could not read the archive of {owner_repo}: {e}", file=sys.stderr) | |
| failed += 1 | |
| missing += len(set(files) - found) | |
| print(f"verified {ok} files; {changed} differ from the recorded checksum; {missing} not found" | |
| f" ({failed} repositories could not be downloaded)", file=sys.stderr) | |
| sys.exit(1 if failed or missing or changed else 0) | |