]> code.delx.au - monosys/blob - bin/aptorphan
aptorphan: fixes
[monosys] / bin / aptorphan
1 #!/usr/bin/python3
2
3 import codecs
4 import subprocess
5 import os
6 import sys
7
8 APTORPHAN_PATH = os.path.expanduser("~/.aptorphan")
9
10 def run(cmd):
11 result = []
12 for line in subprocess.check_output(cmd).decode("utf-8").split("\n"):
13 line = line.strip()
14 if line:
15 result.append(line)
16 return result
17
18 def strip_comment(line):
19 pos = line.find("#")
20 if pos >= 0:
21 line = line[:pos]
22 return line.strip()
23
24
25 required_config = """
26 APT::Install-Recommends "false";
27 APT::Install-Suggests "false";
28 APT::AutoRemove::RecommendsImportant "false";
29 APT::AutoRemove::SuggestsImportant "false";
30 """.strip().split("\n")
31
32 actual_config = run(["apt-config", "dump"])
33
34 missing_lines = []
35 for required_line in required_config:
36 for line in actual_config:
37 if line == required_line:
38 break
39 else:
40 missing_lines.append(required_line)
41 if missing_lines:
42 print("Missing apt-config, add these lines to /etc/apt/apt.conf.d/99recommends-disable")
43 print("\n".join(missing_lines))
44 sys.exit(1)
45
46
47
48
49 keep_pkg_list = []
50 mark_explicit_list = []
51 need_install_list = []
52 installed_pkg_list = []
53 explicit_pkg_list = []
54
55 for filename in os.listdir(APTORPHAN_PATH):
56 if filename.startswith("."):
57 continue
58 filename = os.path.join(APTORPHAN_PATH, filename)
59 for pkg in codecs.open(filename, "r", "utf-8"):
60 pkg = strip_comment(pkg)
61 if pkg in keep_pkg_list:
62 print("# Duplicate entry: " + pkg)
63 if pkg:
64 keep_pkg_list.append(pkg.strip())
65
66 for pkg in run(["aptitude", "search", "?or(~i!~aremove,~ainstall)", "-F", "%p"]):
67 installed_pkg_list.append(pkg.strip())
68
69 for pkg in run(["aptitude", "search", "?or(~i!~M!~aremove,~ainstall!~M)", "-F", "%p"]):
70 explicit_pkg_list.append(pkg.strip())
71
72
73 for pkg in keep_pkg_list:
74 if pkg in explicit_pkg_list:
75 explicit_pkg_list.remove(pkg)
76 else:
77 if pkg in installed_pkg_list:
78 mark_explicit_list.append(pkg)
79 else:
80 need_install_list.append(pkg)
81
82
83 if mark_explicit_list:
84 print("# Found packages which should be marked as explicitly installed")
85 print("sudo aptitude --schedule-only install " + " ".join(mark_explicit_list))
86 print()
87
88 if need_install_list:
89 print("# Found packages which should be installed")
90 print("sudo aptitude --schedule-only install " + " ".join(need_install_list))
91 print()
92
93 if explicit_pkg_list:
94 print("# Found explicitly installed packages to keep or remove")
95 print("echo " + " ".join(explicit_pkg_list) + " | tr ' ' '\\n' >> ~/.aptorphan/keep")
96 print("sudo aptitude --schedule-only install " + " ".join([(x+"+M") for x in explicit_pkg_list]))
97 print()
98