#!/usr/bin/env python3 """Junta as licencas dos pacotes npm que entram no bundle (lidos dos source maps do build) e o texto de licenca de cada um. Uso: licenses.py . Falha se algum pacote nao tiver licenca permissiva conhecida (MIT, ISC, BSD, Apache-2.0, 0BSD).""" import glob, json, os, re, sys build, nm, out = sys.argv[1:4] pk = set() for f in glob.glob(os.path.join(build, "static/js/*.map")): for s in json.load(open(f))["sources"]: m = re.search(r"node_modules/((?:@[^/]+/)?[^/]+)/", s) if m: pk.add(m.group(1)) # so o carregador do webpack e as ferramentas de desenvolvimento (hot reload) aparecem sem entrar no codigo do jogo pk -= {"webpack", "process", "react-hot-loader"} ok = ("MIT", "ISC", "BSD", "Apache", "0BSD") lines, bad = [], [] for name in sorted(pk): d = os.path.join(nm, name) meta = json.load(open(os.path.join(d, "package.json"))) lic = meta.get("license") or (meta.get("licenses") or [{}])[0].get("type") or "" if isinstance(lic, dict): lic = lic.get("type", "") if not any(o in lic for o in ok): bad.append((name, lic)) files = sorted(f for f in glob.glob(os.path.join(d, "*")) if os.path.isfile(f) and os.path.basename(f).upper().startswith(("LICENSE", "LICENCE", "COPYING"))) lines.append("=" * 78) lines.append("%s %s (%s)" % (name, meta.get("version", ""), lic or "see files")) lines.append("=" * 78) if not files: lines.append("(no license file in the published package; license field above)") for f in files: lines.append(open(f, encoding="utf-8", errors="replace").read().rstrip()) lines.append("") if bad: sys.exit("licencas a conferir: %r" % bad) open(out, "w", encoding="utf-8").write("npm packages bundled in the game (read from the build's source maps), with their licence texts.\n\n" + "\n".join(lines) + "\n") print("pacotes:", len(pk))