From: James Bunton Date: Sat, 1 Feb 2014 12:40:28 +0000 (+1100) Subject: pacorphan: Inspired by deborphan on Debian X-Git-Url: https://code.delx.au/monosys/commitdiff_plain/b0dc36c8781d757762eb972f06758982cdc9de3d pacorphan: Inspired by deborphan on Debian --- diff --git a/bin/pacorphan b/bin/pacorphan new file mode 100755 index 0000000..2e922f0 --- /dev/null +++ b/bin/pacorphan @@ -0,0 +1,58 @@ +#!/usr/bin/python + +import codecs +import subprocess +import os +import sys + +PACORPHAN_PATH = os.path.expanduser("~/.pacorphan") + +keep_pkg_list = [] +unneeded_pkg_list = [] +explicit_pkg_list = [] + +def strip_comment(line): + pos = line.find("#") + if pos >= 0: + line = line[:pos] + return line.strip() + +for dirpath, dirnames, filenames in os.walk(PACORPHAN_PATH): + for filename in filenames: + if filename.startswith("."): + continue + filename = os.path.join(dirpath, filename) + for pkg in codecs.open(filename, "r", "utf-8"): + pkg = strip_comment(pkg) + if pkg in keep_pkg_list: + print("# Duplicate entry: " + pkg) + if pkg: + keep_pkg_list.append(pkg.strip()) + +for pkg in subprocess.check_output(["pacman", "-Qtq"]).decode("utf-8").split(): + unneeded_pkg_list.append(pkg.strip()) + +for pkg in subprocess.check_output(["pacman", "-Qeq"]).decode("utf-8").split(): + explicit_pkg_list.append(pkg.strip()) + + +for pkg in keep_pkg_list: + if pkg in unneeded_pkg_list: + unneeded_pkg_list.remove(pkg) + + if pkg in explicit_pkg_list: + explicit_pkg_list.remove(pkg) + else: + print("# Not explicitly installed: " + pkg) + + +if unneeded_pkg_list: + print("# Found packages to remove") + print("sudo pacman -R " + " ".join(unneeded_pkg_list)) + print() + +if explicit_pkg_list: + print("# Found explicitly installed packages to keep") + print("echo " + " ".join(explicit_pkg_list) + " | tr ' ' '\\n' >> ~/.pacorphan/keep") + print() +