]> code.delx.au - monosys/blob - scripts/pacorphan
39f90f0d5f452dfc887b5099a5ec9a25ece6c7bc
[monosys] / scripts / pacorphan
1 #!/usr/bin/python3
2
3 import codecs
4 import subprocess
5 import os
6 import sys
7
8 PACORPHAN_PATH = os.path.expanduser("~/.pacorphan")
9
10 def run(cmd):
11 for line in subprocess.check_output(cmd).decode("utf-8").split("\n"):
12 line = line.strip()
13 if line:
14 yield line
15
16 def strip_comment(line):
17 pos = line.find("#")
18 if pos >= 0:
19 line = line[:pos]
20 return line.strip()
21
22
23 keep_pkg_list = []
24 mark_explicit_list = []
25 need_install_list = []
26 unneeded_pkg_list = []
27 installed_pkg_list = []
28 explicit_pkg_list = []
29
30 for filename in os.listdir(PACORPHAN_PATH):
31 if filename.startswith("."):
32 continue
33 filename = os.path.join(PACORPHAN_PATH, filename)
34 for pkg in codecs.open(filename, "r", "utf-8"):
35 pkg = strip_comment(pkg)
36 if pkg in keep_pkg_list:
37 print("# Duplicate entry: " + pkg)
38 if pkg:
39 keep_pkg_list.append(pkg.strip())
40
41 for pkg in run(["pacman", "-Qq"]):
42 installed_pkg_list.append(pkg)
43
44 for pkg in run(["pacman", "-Qttq"]):
45 unneeded_pkg_list.append(pkg)
46
47 for pkg in run(["pacman", "-Qeq"]):
48 explicit_pkg_list.append(pkg)
49
50
51 for pkg in keep_pkg_list:
52 if pkg in unneeded_pkg_list:
53 unneeded_pkg_list.remove(pkg)
54
55 if pkg in explicit_pkg_list:
56 explicit_pkg_list.remove(pkg)
57 else:
58 if pkg in installed_pkg_list:
59 mark_explicit_list.append(pkg)
60 else:
61 need_install_list.append(pkg)
62
63
64 if unneeded_pkg_list:
65 print("# Found packages to remove")
66 print("sudo pacman -R " + " ".join(unneeded_pkg_list))
67 print()
68
69 if explicit_pkg_list:
70 print("# Found explicitly installed packages to keep")
71 print("echo " + " ".join(explicit_pkg_list) + " | tr ' ' '\\n' >> ~/.pacorphan/keep")
72 print()
73
74 if mark_explicit_list:
75 print("# Found packages which should be marked as explicitly installed")
76 print("sudo pacman -D --asexplicit " + " ".join(mark_explicit_list))
77 print()
78
79 if need_install_list:
80 print("# Found packages which should be installed")
81 print("sudo pacman -S " + " ".join(need_install_list))
82 print()
83