omp-py 0.1.0

Self-contained embedded CPython runtime with frozen standard-library and project modules
"""Collects bundled wheels' license texts into THIRD-PARTY-NOTICES.txt.

Scans every *.dist-info under the bundled tree — `licenses/` per Metadata
2.4, or legacy LICENSE*/COPYING*/NOTICE* files in the dist-info root — and
writes one plain-text notices file (tracked in git; BSD-style terms require
these notices to accompany binary redistributions). A wheel shipping no
license file is a hard error: add its notice by hand before bundling it.
Run via omp-py's scripts/fetch-python.sh, which keeps the file in sync with
requirements.txt.

Usage: gen-py-notices.py BUNDLED_DIR OUT
"""

import os
import sys

bundled, out = sys.argv[1], sys.argv[2]

HEADER = """Third-party notices
===================

License texts for the pure-Python packages pinned in requirements.txt
and frozen into omp-py binaries. Generated by omp-py's scripts/fetch-python.sh
from the fetched wheels — do not edit by hand.
"""

chunks = [HEADER]
dists = sorted(
    d
    for d in (os.listdir(bundled) if os.path.isdir(bundled) else [])
    if d.endswith(".dist-info")
)
for dist in dists:
    path = os.path.join(bundled, dist)
    name, _, version = dist[: -len(".dist-info")].rpartition("-")
    lic_dir = os.path.join(path, "licenses")
    if os.path.isdir(lic_dir):
        files = sorted(
            os.path.join(dp, f) for dp, _, fs in os.walk(lic_dir) for f in fs
        )
    else:
        files = sorted(
            os.path.join(path, f)
            for f in os.listdir(path)
            if f.upper().startswith(("LICENSE", "COPYING", "NOTICE"))
        )
    if not files:
        sys.exit(f"error: {dist} ships no license file — add its notice by hand")
    for f in files:
        title = f"{name} {version} ({os.path.basename(f)})"
        with open(f, encoding="utf-8") as fh:
            body = fh.read().rstrip()
        chunks.append(f"\n{title}\n{'-' * len(title)}\n{body}\n")

text = "".join(chunks)
try:
    stale = open(out, encoding="utf-8").read() != text
except OSError:
    stale = True
if stale:
    with open(out, "w", encoding="utf-8") as fh:
        fh.write(text)
    print(f"{os.path.basename(out)}: updated ({len(dists)} packages) — commit it",
          file=sys.stderr)