]> code.delx.au - monosys/blob - bin/pacorphan
pacorphan: Inspired by deborphan on Debian
[monosys] / bin / pacorphan
1 #!/usr/bin/python
2
3 import codecs
4 import subprocess
5 import os
6 import sys
7
8 PACORPHAN_PATH = os.path.expanduser("~/.pacorphan")
9
10 keep_pkg_list = []
11 unneeded_pkg_list = []
12 explicit_pkg_list = []
13
14 def strip_comment(line):
15 pos = line.find("#")
16 if pos >= 0:
17 line = line[:pos]
18 return line.strip()
19
20 for dirpath, dirnames, filenames in os.walk(PACORPHAN_PATH):
21 for filename in filenames:
22 if filename.startswith("."):
23 continue
24 filename = os.path.join(dirpath, filename)
25 for pkg in codecs.open(filename, "r", "utf-8"):
26 pkg = strip_comment(pkg)
27 if pkg in keep_pkg_list:
28 print("# Duplicate entry: " + pkg)
29 if pkg:
30 keep_pkg_list.append(pkg.strip())
31
32 for pkg in subprocess.check_output(["pacman", "-Qtq"]).decode("utf-8").split():
33 unneeded_pkg_list.append(pkg.strip())
34
35 for pkg in subprocess.check_output(["pacman", "-Qeq"]).decode("utf-8").split():
36 explicit_pkg_list.append(pkg.strip())
37
38
39 for pkg in keep_pkg_list:
40 if pkg in unneeded_pkg_list:
41 unneeded_pkg_list.remove(pkg)
42
43 if pkg in explicit_pkg_list:
44 explicit_pkg_list.remove(pkg)
45 else:
46 print("# Not explicitly installed: " + pkg)
47
48
49 if unneeded_pkg_list:
50 print("# Found packages to remove")
51 print("sudo pacman -R " + " ".join(unneeded_pkg_list))
52 print()
53
54 if explicit_pkg_list:
55 print("# Found explicitly installed packages to keep")
56 print("echo " + " ".join(explicit_pkg_list) + " | tr ' ' '\\n' >> ~/.pacorphan/keep")
57 print()
58