]> code.delx.au - monosys/blob - scripts/aptorphan
ssh-screen-wrapper doesn't use tput
[monosys] / scripts / 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 sorted(os.listdir(APTORPHAN_PATH)):
56 if filename.startswith("."):
57 continue
58 full_filename = os.path.join(APTORPHAN_PATH, filename)
59 for pkg in codecs.open(full_filename, "r", "utf-8"):
60 pkg = strip_comment(pkg).strip()
61 if not pkg:
62 continue
63 if filename[0] != "~":
64 if pkg in keep_pkg_list:
65 print("# Duplicate entry:", pkg, "in file", filename)
66 continue
67 keep_pkg_list.append(pkg)
68 else:
69 if pkg not in keep_pkg_list:
70 print("# Redundant removal:", pkg, "in file", filename)
71 continue
72 keep_pkg_list.remove(pkg)
73
74 for pkg in run(["aptitude", "search", "?or(~i!~aremove,~ainstall)", "-F", "%p"]):
75 installed_pkg_list.append(pkg.strip())
76
77 for pkg in run(["aptitude", "search", "?or(~i!~M!~aremove,~ainstall!~M)", "-F", "%p"]):
78 explicit_pkg_list.append(pkg.strip())
79
80
81 for pkg in keep_pkg_list:
82 if pkg in explicit_pkg_list:
83 explicit_pkg_list.remove(pkg)
84 else:
85 if pkg in installed_pkg_list:
86 mark_explicit_list.append(pkg)
87 else:
88 need_install_list.append(pkg)
89
90
91 if mark_explicit_list:
92 print("# Found packages which should be marked as explicitly installed")
93 print("sudo aptitude --schedule-only install " + " ".join([("'"+x+"&m'") for x in mark_explicit_list]))
94 print()
95
96 if need_install_list:
97 print("# Found packages which should be installed")
98 print("sudo aptitude --schedule-only install " + " ".join(need_install_list))
99 print()
100
101 if explicit_pkg_list:
102 print("# Found explicitly installed packages to keep or remove")
103 print("echo " + " ".join(explicit_pkg_list) + " | tr ' ' '\\n' >> ~/.aptorphan/keep")
104 print("sudo aptitude --schedule-only install " + " ".join([(x+"+M") for x in explicit_pkg_list]))
105 print()
106