minimize-manual.py: Followup with some cleanup, correct permissions

This commit is contained in:
Julian Andres Klode 2018-09-18 09:15:42 +02:00
parent 9360135b2c
commit 2e9349c543

40
live-build/auto/minimize-manual.py Normal file → Executable file
View File

@ -1,29 +1,31 @@
#!/usr/bin/python3 #!/usr/bin/python3
"""Minimize the number of manually installed packages in the image. """Minimize the number of manually installed packages in the image.
Finds all manually meta packages and marks their dependencies as Finds all manually installed meta packages, and marks their dependencies
automatically installed. as automatically installed.
""" """
import apt
import sys import sys
import apt
def is_root(pkg): def is_root(pkg):
"""Check if the package is a root package (manually inst. meta)"""
return (pkg.is_installed and return (pkg.is_installed and
not pkg.is_auto_installed and not pkg.is_auto_installed and
(pkg.section == "metapackages" or (pkg.section == "metapackages" or
pkg.section.endswith("/metapackages"))) 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)) with cache.actiongroup():
workset = set(roots)
seen = set()
with c.actiongroup():
while True: while True:
print("Iteration", file=sys.stderr) print("Iteration", file=sys.stderr)
to_proc = workset - seen to_proc = workset - seen
if not to_proc: if not to_proc:
@ -31,18 +33,20 @@ with c.actiongroup():
for pkg in sorted(to_proc): for pkg in sorted(to_proc):
print(" Visiting", pkg, file=sys.stderr) print(" Visiting", pkg, file=sys.stderr)
# Mark every
if pkg not in roots: if pkg not in roots:
pkg.mark_auto() pkg.mark_auto()
for dep in pkg.installed.dependencies + pkg.installed.recommends: for dep in (pkg.installed.dependencies +
if dep.rawtype not in ('Depends', 'PreDepends', 'Recommends'): pkg.installed.recommends):
continue
for bdep in dep.or_dependencies: for bdep in dep.or_dependencies:
for v in bdep.target_versions: for ver in bdep.target_versions:
if v.package.is_installed: if ver.package.is_installed:
workset.add(v.package) workset.add(ver.package)
seen.add(pkg) seen.add(pkg)
c.commit() cache.commit()
if __name__ == '__main__':
main()