diff --git a/live-build/auto/minimize-manual.py b/live-build/auto/minimize-manual.py old mode 100644 new mode 100755 index 712f35a4..6bd18780 --- a/live-build/auto/minimize-manual.py +++ b/live-build/auto/minimize-manual.py @@ -1,48 +1,52 @@ #!/usr/bin/python3 """Minimize the number of manually installed packages in the image. -Finds all manually meta packages and marks their dependencies as -automatically installed. +Finds all manually installed meta packages, and marks their dependencies +as automatically installed. """ -import apt import sys +import apt + def is_root(pkg): + """Check if the package is a root package (manually inst. meta)""" return (pkg.is_installed and not pkg.is_auto_installed and (pkg.section == "metapackages" or pkg.section.endswith("/metapackages"))) -c = apt.Cache(rootdir=sys.argv[1] if len(sys.argv) > 1 else None) +def main(): + """Main function""" + cache = apt.Cache(rootdir=sys.argv[1] if len(sys.argv) > 1 else None) + roots = set(pkg for pkg in cache if is_root(pkg)) + workset = set(roots) + seen = set() -roots = set(pkg for pkg in c if is_root(pkg)) -workset = set(roots) -seen = set() + with cache.actiongroup(): + while True: + print("Iteration", file=sys.stderr) + to_proc = workset - seen + if not to_proc: + break + for pkg in sorted(to_proc): + print(" Visiting", pkg, file=sys.stderr) -with c.actiongroup(): - while True: + if pkg not in roots: + pkg.mark_auto() - print("Iteration", file=sys.stderr) - to_proc = workset - seen - if not to_proc: - break - for pkg in sorted(to_proc): - print(" Visiting", pkg, file=sys.stderr) + for dep in (pkg.installed.dependencies + + pkg.installed.recommends): + for bdep in dep.or_dependencies: + for ver in bdep.target_versions: + if ver.package.is_installed: + workset.add(ver.package) - # Mark every - if pkg not in roots: - pkg.mark_auto() + seen.add(pkg) - for dep in pkg.installed.dependencies + pkg.installed.recommends: - if dep.rawtype not in ('Depends', 'PreDepends', 'Recommends'): - continue - for bdep in dep.or_dependencies: - for v in bdep.target_versions: - if v.package.is_installed: - workset.add(v.package) + cache.commit() - seen.add(pkg) - c.commit() +if __name__ == '__main__': + main()