Skip to content

Instantly share code, notes, and snippets.

@jessestricker
Last active October 22, 2020 00:01
Show Gist options
  • Save jessestricker/81e047cd6f3abcc1965366fc6ee1d178 to your computer and use it in GitHub Desktop.
Save jessestricker/81e047cd6f3abcc1965366fc6ee1d178 to your computer and use it in GitHub Desktop.
List and interactively purge all non-important APT packages.
#!/bin/bash
pkgs=$(./list_purgable.py)
for pkg in $pkgs; do
echo "$(tput setaf 6)purging $pkg$(tput sgr0)"
sudo apt purge "$pkg" || sudo apt-mark manual "$pkg"
done
#!/bin/python3
import apt
CACHE = apt.cache.Cache()
META_PKG_NAMES = ["desktop", "minimal", "server", "standard", "wsl"]
META_PKG_NAMES = ["ubuntu-" + x for x in META_PKG_NAMES]
IMPORTANT_PRIOS = ["required", "important"]
def is_important_pkg(pkg):
return (pkg.essential or # essential
(not pkg.is_auto_installed) or # installed manually
(pkg.shortname in META_PKG_NAMES) or # ubuntu meta package
(pkg.installed.priority in IMPORTANT_PRIOS)) # prio = required or important
def add_recursive_dependencies(ver, deps):
for dep in ver.dependencies:
# if len(dep.installed_target_versions) > 1:
# print(f"NOTE: {dep} is satisfied by more than one package")
dep_ver = dep.installed_target_versions[0]
if not dep_ver in deps:
deps += [dep_ver]
add_recursive_dependencies(dep_ver, deps)
def print_pkg_list(pkgs):
for pkg in pkgs:
print(f" - {pkg}")
pass
# list installed important packages and their transitive dependencies
IMPORTANT_PKGS = []
IMPORTANT_PKGS_DEPS = []
for pkg in CACHE:
# skip non-installed
if pkg.installed is None:
continue
ver = pkg.installed
# check whether important
if is_important_pkg(pkg):
IMPORTANT_PKGS += [ver]
add_recursive_dependencies(ver, IMPORTANT_PKGS_DEPS)
# check each package if it could be purged
PURGES = []
for pkg in CACHE:
# skip not installed packages
if pkg.installed is None:
continue
pkg = pkg.installed
# skip important packages
if pkg in IMPORTANT_PKGS:
continue
# skip dependencies of important packages
if pkg in IMPORTANT_PKGS_DEPS:
continue
PURGES += [pkg]
# print result
for pkg in PURGES:
print(pkg.package.shortname)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment