Download apply_licenses.py from PiotrSty/code-search-net-java: direct link, hf CLI and curl.
- Browser
- Download file 5.77 kB
-
https://huggingface.co/datasets/PiotrSty/code-search-net-java/resolve/main/apply_licenses.py
- Command line
-
hf download hf://datasets/PiotrSty/code-search-net-java/apply_licenses.py
-
curl -L -o apply_licenses.py https://huggingface.co/datasets/PiotrSty/code-search-net-java/resolve/main/apply_licenses.py
5.77 kB
| #!/usr/bin/env python | |
| """Join repo_licenses.jsonl into the parquet and update stats/sample. | |
| Adds `license` (SPDX id, NOASSERTION, or unknown) and `license_source` | |
| (GitHub licence API URL), recomputes `record_sha256`, updates build.json and | |
| the 12-row sample's licence field. | |
| """ | |
| import gc | |
| import hashlib | |
| import json | |
| import os | |
| import time | |
| from collections import Counter | |
| from pathlib import Path | |
| import pyarrow as pa | |
| import pyarrow.parquet as pq | |
| HERE = Path(__file__).resolve().parent | |
| SRC = HERE / "code_search_net_java.parquet" | |
| TMP = HERE / "code_search_net_java.licensed.parquet" | |
| AUDIT = HERE / "repo_licenses.jsonl" | |
| BUILD = HERE / "code_search_net_java.build.json" | |
| SAMPLE = HERE / "code_search_net_java.sample.jsonl" | |
| SAMPLE_LIC = HERE / "sample_licenses.json" | |
| BASE_COLS = [ | |
| ("id", pa.string()), ("repo", pa.string()), ("path", pa.string()), | |
| ("func_name", pa.string()), ("url", pa.string()), ("split", pa.string()), | |
| ("language", pa.string()), ("docstring", pa.string()), ("code", pa.string()), | |
| ("text", pa.string()), ("token_count", pa.int32()), | |
| ("doc_chars", pa.int32()), ("code_chars", pa.int32()), | |
| ("license_basis", pa.string()), ("source", pa.string()), | |
| ("added", pa.string()), ("record_sha256", pa.string()), | |
| ] | |
| SCHEMA = pa.schema(BASE_COLS + [("license", pa.string()), | |
| ("license_class", pa.string()), | |
| ("license_source", pa.string())]) | |
| PERMISSIVE = { | |
| "MIT", "Apache-2.0", "BSD-2-Clause", "BSD-3-Clause", "ISC", "0BSD", | |
| "Unlicense", "Zlib", "BSL-1.0", "CC0-1.0", "PostgreSQL", "X11", | |
| "WTFPL", "Artistic-2.0", "Python-2.0", | |
| } | |
| def lic_class(spdx): | |
| if not spdx or spdx in ("NOASSERTION", "unknown"): | |
| return "unknown" | |
| if spdx in PERMISSIVE: | |
| return "permissive" | |
| if spdx.startswith(("GPL-", "AGPL-")): | |
| return "strong-copyleft" | |
| if spdx.startswith(("LGPL-", "EPL-", "MPL-", "CDDL-", "CPL-", "OSL-")): | |
| return "weak-copyleft" | |
| return "other" | |
| def canon(row): | |
| return json.dumps(row, ensure_ascii=False, sort_keys=True, | |
| separators=(",", ":")).encode("utf-8") | |
| def main(): | |
| audit = {} | |
| for line in AUDIT.open(encoding="utf-8"): | |
| j = json.loads(line) | |
| audit[j["repo"]] = j | |
| def lic_for(repo): | |
| a = audit.get(repo) | |
| if a and a["status"] == "ok" and a.get("spdx") and a["spdx"] != "NOASSERTION": | |
| spdx = a["spdx"] | |
| return spdx, (a.get("license_url") or ""), lic_class(spdx) | |
| if a and a["status"] == "no_license": | |
| return "NOASSERTION", "", "unknown" | |
| return "unknown", "", "unknown" | |
| row_dist = Counter() | |
| class_dist = Counter() | |
| n = 0 | |
| pf = pq.ParquetFile(SRC) | |
| writer = pq.ParquetWriter(TMP, SCHEMA, compression="zstd") | |
| for batch in pf.iter_batches(batch_size=8192): | |
| rows = batch.to_pylist() | |
| for r in rows: | |
| lic, src, cls = lic_for(r["repo"]) | |
| r["license"] = lic | |
| r["license_class"] = cls | |
| r["license_source"] = src | |
| payload = {k: v for k, v in r.items() if k != "record_sha256"} | |
| r["record_sha256"] = hashlib.sha256(canon(payload)).hexdigest() | |
| row_dist[lic] += 1 | |
| class_dist[cls] += 1 | |
| n += 1 | |
| writer.write_table(pa.Table.from_pylist(rows, schema=SCHEMA)) | |
| writer.close() | |
| # release the read handle; on Windows an open ParquetFile blocks os.replace | |
| try: | |
| pf.close() | |
| except Exception: | |
| pass | |
| del pf | |
| gc.collect() | |
| for attempt in range(5): | |
| try: | |
| os.replace(TMP, SRC) | |
| break | |
| except PermissionError: | |
| time.sleep(1) | |
| else: | |
| raise SystemExit("could not replace parquet (locked)") | |
| repo_dist = Counter() | |
| repo_class_dist = Counter() | |
| status_dist = Counter() | |
| for a in audit.values(): | |
| status_dist[a["status"]] += 1 | |
| if a["status"] == "ok" and a.get("spdx") and a["spdx"] != "NOASSERTION": | |
| repo_dist[a["spdx"]] += 1 | |
| repo_class_dist[lic_class(a["spdx"])] += 1 | |
| elif a["status"] == "no_license": | |
| repo_dist["NOASSERTION"] += 1 | |
| repo_class_dist["unknown"] += 1 | |
| else: | |
| repo_dist["unknown"] += 1 | |
| repo_class_dist["unknown"] += 1 | |
| build = json.loads(BUILD.read_text(encoding="utf-8")) | |
| build.update({ | |
| "rows": n, | |
| "bytes": SRC.stat().st_size, | |
| "license_distribution_rows": dict(row_dist.most_common()), | |
| "license_class_rows": dict(class_dist.most_common()), | |
| "license_distribution_repos": dict(repo_dist.most_common()), | |
| "license_class_repos": dict(repo_class_dist.most_common()), | |
| "license_audit_status": dict(status_dist.most_common()), | |
| "license_audit_repos": len(audit), | |
| }) | |
| BUILD.write_text(json.dumps(build, indent=2, ensure_ascii=False), encoding="utf-8") | |
| # sample: refresh licence from the audit | |
| if SAMPLE.exists(): | |
| recs = [json.loads(l) for l in SAMPLE.open(encoding="utf-8")] | |
| cache = {} | |
| for r in recs: | |
| lic, _, _ = lic_for(r["author"]) | |
| r["license"] = lic if lic != "unknown" else "other" | |
| cache[r["author"]] = r["license"] | |
| SAMPLE.write_text("".join(json.dumps(r, ensure_ascii=False) + "\n" for r in recs), | |
| encoding="utf-8") | |
| SAMPLE_LIC.write_text(json.dumps(cache, indent=2, ensure_ascii=False), encoding="utf-8") | |
| print("rows:", n) | |
| print("rows by licence:", row_dist.most_common()) | |
| print("repos by licence:", repo_dist.most_common()) | |
| print("audit status:", status_dist.most_common()) | |
| print("bytes:", SRC.stat().st_size) | |
| if __name__ == "__main__": | |
| main() | |