#!/usr/bin/env python3 """Inventaire des dépendances réellement livrées dans l'appli de bureau. dependencies.py mac [arm64|x64|universal] | win - paquets npm compilés dans main, renderer et preload, d'après les rapports de webpack-oss.cjs, avec leurs fichiers de licence ; - crates Rust des modules natifs (napi, proxy), d'après cargo metadata filtré sur la cible, sans les dépendances de développement ; - Electron et Chromium, dont les licences sont déjà livrées dans le paquet (LICENSE, LICENSES.chromium.html). Écrit THIRD-PARTY.json et third-party/ dans le dossier du paquet (dist/mac, dist/mac-x64, dist/mac-universal, dist/win). """ import json from pathlib import Path import shutil import subprocess import sys sys.path.insert(0, str(Path(__file__).resolve().parent)) from loksi import MAC_ARCHS, NATIVE, REPORTS, SOURCE, dist_dir, tool_env # noqa: E402 platform = sys.argv[1] arch = sys.argv[2] if len(sys.argv) > 2 else "arm64" target = dist_dir(platform, arch) TRIPLES = MAC_ARCHS[arch]["rust"] if platform == "mac" else ("x86_64-pc-windows-msvc",) packages = {} reports = list(REPORTS.glob("desktop-*-modules.json")) if not reports: raise RuntimeError("Compiler d'abord avec webpack-oss.cjs") third = target / "third-party" if third.exists(): shutil.rmtree(third) for report in reports: for resource in json.loads(report.read_text()): if "node_modules/" not in resource: continue folder = (SOURCE / resource.split("?")[0]).resolve().parent while folder != SOURCE: if (folder / "package.json").is_file(): data = json.loads((folder / "package.json").read_text()) if data.get("name") and data.get("version"): break folder = folder.parent if folder == SOURCE: raise RuntimeError(f"Paquet non identifié : {resource}") data = json.loads((folder / "package.json").read_text()) key = data["name"] + "@" + data["version"] if key in packages: continue license_files = [p for p in folder.iterdir() if p.is_file() and p.name.lower().startswith(("license", "licence", "notice", "copying"))] license = data.get("license", "UNKNOWN") if folder.is_relative_to(SOURCE / "libs"): # Bibliothèques du monorepo : GPL v3 par LICENSE.txt, sans champ license. license = "GPL-3.0" if isinstance(license, dict): license = license.get("type", "UNKNOWN") packages[key] = {"source": "npm", "name": data["name"], "version": data["version"], "license": license, "license_files": [p.name for p in license_files]} destination = third / "npm" / key.replace("/", "__") destination.mkdir(parents=True, exist_ok=True) for file in license_files: shutil.copy2(file, destination / file.name) shutil.copy2(folder / "package.json", destination / "package.json") metadata = json.loads(subprocess.run( ["cargo", "metadata", "--format-version", "1", "--locked", *[arg for triple in TRIPLES for arg in ("--filter-platform", triple)]], cwd=NATIVE, env=tool_env(), capture_output=True, text=True, check=True).stdout) by_id = {p["id"]: p for p in metadata["packages"]} nodes = {n["id"]: n for n in metadata["resolve"]["nodes"]} roots = [p["id"] for p in metadata["packages"] if p["name"] in ("desktop_napi", "desktop_proxy")] if len(roots) != 2: raise RuntimeError("crates racines desktop_napi et desktop_proxy introuvables") seen, stack = set(), list(roots) while stack: current = stack.pop() if current in seen: continue seen.add(current) for dep in nodes[current]["deps"]: if any(k["kind"] in (None, "build") for k in dep["dep_kinds"]): stack.append(dep["pkg"]) for pid in sorted(seen): crate = by_id[pid] key = crate["name"] + "@" + crate["version"] local = crate["source"] is None license = crate["license"] or ("GPL-3.0" if local else "UNKNOWN") files_extra = [] if license == "UNKNOWN" and crate.get("license_file") and "/sdk-internal" in crate["manifest_path"]: # Dépôt sdk-internal : « GPL v3.0 OR BITWARDEN SOFTWARE DEVELOPMENT KIT LICENSE » hors # des dossiers bitwarden_license (refusés plus bas). On retient la GPL v3. root = Path(crate["manifest_path"]).parent / crate["license_file"] license = "GPL-3.0 (au choix, LICENSE de sdk-internal)" files_extra = [root.resolve(), root.resolve().parent / "LICENSE_GPL.txt"] if crate["name"] == "bitwarden-russh" and license == "UNKNOWN": # Pas de champ license ; le dépôt porte le texte Apache-2.0 (LICENSE-2.0.txt), relu le 30/09/2026. license = "Apache-2.0" packages["crate:" + key] = {"source": "crate", "name": crate["name"], "version": crate["version"], "license": license} folder = Path(crate["manifest_path"]).parent if "bitwarden_license" in crate["manifest_path"] or "bitwarden-license" in crate["name"]: raise RuntimeError("Crate sous licence commerciale Bitwarden : " + key) files = [p for p in folder.iterdir() if p.is_file() and p.name.lower().startswith(("license", "licence", "notice", "copying"))] files += [f for f in files_extra if f.is_file()] if files: destination = third / "crates" / key destination.mkdir(parents=True, exist_ok=True) for file in files: shutil.copy2(file, destination / file.name) (target / "THIRD-PARTY.json").write_text(json.dumps(list(packages.values()), indent=2) + "\n") for name in ("LICENSE.txt", "LICENSE_GPL.txt"): shutil.copy2(SOURCE / name, target / name) licenses = sorted({str(p["license"]) for p in packages.values()}) print(f"{sum(p['source'] == 'npm' for p in packages.values())} paquets npm et " f"{sum(p['source'] == 'crate' for p in packages.values())} crates inventoriés dans {target}") print("Licences déclarées :", licenses) unknown = [k for k, p in packages.items() if p["license"] in ("UNKNOWN", None)] print("Sans licence déclarée :", unknown)