Deckstride/scripts/notices.py

55 lines
2.8 KiB
Python

#!/usr/bin/env python3
"""Collect locked crate notices, including bundled font licenses, for offline display."""
import hashlib
import json
from pathlib import Path
import subprocess
ROOT = Path(__file__).resolve().parents[1]
def main():
metadata = json.loads(subprocess.check_output(
["cargo", "metadata", "--locked", "--offline", "--format-version", "1"], cwd=ROOT))
entries, texts = [], {}
for package in sorted(metadata["packages"], key=lambda p: (p["name"], p["version"])):
if package["id"] in metadata["workspace_members"]:
continue
root = Path(package["manifest_path"]).parent
notices = []
for path in sorted(root.rglob("*")):
name = path.name.lower()
if path.is_file() and (name.startswith(("license", "licence", "copying", "notice"))
or "license" in name or "licence" in name
or ("fonts" in path.parts and path.suffix == ".txt")):
try:
text = path.read_text()
except (UnicodeError, OSError):
continue
digest = hashlib.sha256(text.encode()).hexdigest()[:16]
texts[digest] = text
notices.append(f" {path.relative_to(root)}: text {digest}")
entries.append(f"{package['name']} {package['version']}\n"
f"License expression: {package['license']}\n"
f"Source: {package.get('repository') or 'https://crates.io/crates/' + package['name']}\n"
+ "\n".join(notices))
font_license = ROOT / "crates/deckstride/assets/fonts/OFL.txt"
font_text = font_license.read_text()
font_digest = hashlib.sha256(font_text.encode()).hexdigest()[:16]
texts[font_digest] = font_text
entries.insert(0, "Noto Sans CJK 2.004\n"
"License expression: OFL-1.1\n"
"Source: https://github.com/notofonts/noto-cjk/releases/tag/Sans2.004\n"
f" assets/fonts/OFL.txt: text {font_digest}")
header = ("Deckstride — locked dependency and font notices\n"
"Generated by scripts/notices.py. Includes target/build-only dependencies;\n"
"the presence of a package here does not mean it is linked on every OS.\n"
"Unmodified source packages are available at https://crates.io.\n"
"For alternative licenses, permissive MIT/Apache alternatives are selected.\n\n")
content = header + "\n\n".join(entries) + "\n\nLICENSE TEXTS\n"
content += "\n".join(f"\n=== {key} ===\n{value}" for key, value in sorted(texts.items()))
(ROOT / "THIRD_PARTY_NOTICES.txt").write_text(content)
print(f"Collected {len(entries)} dependency entries and {len(texts)} distinct notice texts")
if __name__ == "__main__":
main()