code-atlas-provenance / scripts /build_stack_v3_census.py
arpandeepk's picture
Initialize Code-ATLAS provenance registry
4f040da verified
Raw
History Blame Contribute Delete
3.32 kB
#!/usr/bin/env python3
"""Build a deterministic candidate-language census from official Stack v3 stats."""
from __future__ import annotations
import argparse
import csv
import hashlib
import json
from pathlib import Path
def sha256(path: Path) -> str:
digest = hashlib.sha256()
with path.open("rb") as handle:
for chunk in iter(lambda: handle.read(1024 * 1024), b""):
digest.update(chunk)
return digest.hexdigest()
def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument("--stats", type=Path, required=True)
parser.add_argument("--license-stats", type=Path, required=True)
parser.add_argument("--candidates", type=Path, required=True)
parser.add_argument("--source-revision", required=True)
parser.add_argument("--generated-at", required=True)
parser.add_argument("--output", type=Path, required=True)
args = parser.parse_args()
stats_rows = json.loads(args.stats.read_text())
license_rows = json.loads(args.license_stats.read_text())
candidates = json.loads(args.candidates.read_text())["candidates"]
by_label = {row["language"]: row for row in stats_rows}
by_language_license = {
(row["language"], row["license_type"]): row for row in license_rows
}
output_rows = []
for candidate in candidates:
label = candidate["stack_v3_label"]
stats = by_label.get(label) if label is not None else None
permissive = by_language_license.get((label, "permissive")) if label else None
no_license = by_language_license.get((label, "no_license")) if label else None
permissive_tokens = permissive["estimated_tokens"] if permissive else 0
no_license_tokens = no_license["estimated_tokens"] if no_license else 0
categorized_tokens = permissive_tokens + no_license_tokens
output_rows.append(
{
"language": candidate["language"],
"stack_v3_label": label or "",
"priority": candidate["priority"],
"repo_count": stats["repo_count"] if stats else "",
"file_count": stats["file_count"] if stats else "",
"total_size_bytes": stats["total_size_bytes"] if stats else "",
"estimated_tokens": stats["estimated_tokens"] if stats else "",
"permissive_estimated_tokens": permissive_tokens if stats else "",
"no_license_estimated_tokens": no_license_tokens if stats else "",
"permissive_token_share": (
permissive_tokens / categorized_tokens if categorized_tokens else ""
),
"source_revision": args.source_revision,
"source_manifest_sha256": sha256(args.stats),
"license_manifest_sha256": sha256(args.license_stats),
"generated_at": args.generated_at,
"count_status": "official_pre_project_filters" if stats else "not_a_stack_language_partition",
}
)
args.output.parent.mkdir(parents=True, exist_ok=True)
with args.output.open("w", newline="") as handle:
writer = csv.DictWriter(handle, fieldnames=list(output_rows[0]))
writer.writeheader()
writer.writerows(output_rows)
if __name__ == "__main__":
main()