]> code.delx.au - monosys/blob - pacorphan
Split repository, only keep scripts
[monosys] / 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 sorted(os.listdir(PACORPHAN_PATH)):
31 if filename.startswith("."):
32 continue
33 full_filename = os.path.join(PACORPHAN_PATH, filename)
34 for pkg in codecs.open(full_filename, "r", "utf-8"):
35 pkg = strip_comment(pkg).strip()
36 if not pkg:
37 continue
38 if filename[0] != "~":
39 if pkg in keep_pkg_list:
40 print("# Duplicate entry:", pkg, "in file", filename)
41 continue
42 keep_pkg_list.append(pkg)
43 else:
44 if pkg not in keep_pkg_list:
45 print("# Redundant removal:", pkg, "in file", filename)
46 continue
47 keep_pkg_list.remove(pkg)
48
49 for pkg in run(["pacman", "-Qq"]):
50 installed_pkg_list.append(pkg)
51
52 for pkg in run(["pacman", "-Qttq"]):
53 unneeded_pkg_list.append(pkg)
54
55 for pkg in run(["pacman", "-Qeq"]):
56 explicit_pkg_list.append(pkg)
57
58
59 for pkg in keep_pkg_list:
60 if pkg in unneeded_pkg_list:
61 unneeded_pkg_list.remove(pkg)
62
63 if pkg in explicit_pkg_list:
64 explicit_pkg_list.remove(pkg)
65 else:
66 if pkg in installed_pkg_list:
67 mark_explicit_list.append(pkg)
68 else:
69 need_install_list.append(pkg)
70
71
72 if unneeded_pkg_list:
73 print("# Found packages to remove")
74 print("sudo pacman -R " + " ".join(unneeded_pkg_list))
75 print()
76
77 if explicit_pkg_list:
78 print("# Found explicitly installed packages to keep")
79 print("echo " + " ".join(explicit_pkg_list) + " | tr ' ' '\\n' >> ~/.pacorphan/keep")
80 print()
81
82 if mark_explicit_list:
83 print("# Found packages which should be marked as explicitly installed")
84 print("sudo pacman -D --asexplicit " + " ".join(mark_explicit_list))
85 print()
86
87 if need_install_list:
88 print("# Found packages which should be installed")
89 print("sudo pacman -S " + " ".join(need_install_list))
90 print()
91