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)