File size: 3,315 Bytes
4f040da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/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()