#!/usr/bin/env python3 """Costruisce un sysroot aarch64 dai .deb di Debian, senza root. Risolve le dipendenze delle librerie che servono al motore di GrumpyDove (libetpan, gpgme, jansson, curl, gnutls, libxml2) e le estrae in un albero locale, da usare come --sysroot del compilatore cross. Uso: cross_sysroot.py [indice Packages.xz] """ import os import subprocess import sys import urllib.request BASE = "http://ftp.it.debian.org/debian" ROOTS = ["libetpan-dev", "libjansson-dev", "libgpgme-dev", "libcurl4-gnutls-dev", "libgnutls28-dev", "libxml2-dev", "libc6-dev", "zlib1g-dev"] # gia' forniti dal compilatore cross o non servono per compilare SKIP = {"libgcc-12-dev", "gcc-12-base", "dpkg", "debconf", "libcrypt1", "rpcsvc-proto"} def parse_index(path): # Python decompresso questo indice in minuti: si usa xz, che lo fa in # un secondo (verificato: 47 MB in 1,8 s contro oltre 120 s). text_path = path[:-3] + ".txt" if path.endswith(".xz") else path if path.endswith(".xz") and not os.path.exists(text_path): with open(text_path, "w") as out: subprocess.run(["xz", "-dc", path], stdout=out, check=True) with open(text_path, "r", encoding="utf-8", errors="replace") as fh: data = fh.read() pkgs = {} for block in data.split("\n\n"): d = {} for line in block.split("\n"): if line[:1] in (" ", "") or ":" not in line: continue k, v = line.split(":", 1) d[k] = v.strip() if "Package" in d and "Filename" in d: pkgs[d["Package"]] = d return pkgs def deps_of(pkg): out = [] for field in ("Pre-Depends", "Depends"): raw = pkg.get(field, "") for item in raw.split(","): item = item.strip() if not item: continue first = item.split("|")[0].strip() name = first.split(" ")[0].split(":")[0] if name: out.append(name) return out def main(): dest = sys.argv[1] if len(sys.argv) > 1 else "cross-sysroot" index = sys.argv[2] if len(sys.argv) > 2 else "/tmp/arm64-Packages.xz" pkgs = parse_index(index) print("indice:", len(pkgs), "pacchetti") queue = list(ROOTS) chosen, missing = {}, set() while queue: name = queue.pop() if name in chosen or name in SKIP: continue pkg = pkgs.get(name) if not pkg: missing.add(name) continue chosen[name] = pkg queue.extend(deps_of(pkg)) if missing: print("non trovati (ignorati):", ", ".join(sorted(missing))) os.makedirs(dest, exist_ok=True) debs = os.path.join(dest, ".debs") os.makedirs(debs, exist_ok=True) total = 0 for name, pkg in sorted(chosen.items()): fn = pkg["Filename"] out = os.path.join(debs, os.path.basename(fn)) if not os.path.exists(out): url = "%s/%s" % (BASE, fn) urllib.request.urlretrieve(url, out) total += os.path.getsize(out) subprocess.run(["dpkg-deb", "-x", out, dest], check=True) print(" estratto %-28s %s" % (name, pkg.get("Version", ""))) print("pacchetti: %d, scaricati %.1f MB -> %s" % (len(chosen), total / 1e6, dest)) if __name__ == "__main__": main()