]> code.delx.au - monosys/commitdiff
pacorphan: Inspired by deborphan on Debian
authorJames Bunton <jamesbunton@delx.net.au>
Sat, 1 Feb 2014 12:40:28 +0000 (23:40 +1100)
committerJames Bunton <jamesbunton@delx.net.au>
Sat, 1 Feb 2014 12:40:28 +0000 (23:40 +1100)
bin/pacorphan [new file with mode: 0755]

diff --git a/bin/pacorphan b/bin/pacorphan
new file mode 100755 (executable)
index 0000000..2e922f0
--- /dev/null
@@ -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()
+