]>
code.delx.au - monosys/blob - bin/pacorphan
9 name
= os
.path
.basename(sys
.argv
[0])
10 if name
== "pacorphan":
12 elif name
== "aptorphan":
15 print("This script must be named pacorphan or aptorphan!", file=sys
.stderr
)
19 keep_pkg_list
= load_keep_pkg_list("pacorphan")
20 unneeded_pkg_list
= list(run(["pacman", "-Qttq"]))
21 installed_pkg_list
= list(run(["pacman", "-Qq"]))
22 explicit_pkg_list
= list(run(["pacman", "-Qeq"]))
23 mark_explicit_list
= []
24 need_install_list
= []
26 for pkg
in keep_pkg_list
:
27 if pkg
in unneeded_pkg_list
:
28 unneeded_pkg_list
.remove(pkg
)
30 if pkg
in explicit_pkg_list
:
31 explicit_pkg_list
.remove(pkg
)
33 if pkg
in installed_pkg_list
:
34 mark_explicit_list
.append(pkg
)
36 need_install_list
.append(pkg
)
39 print("# Found packages to remove")
40 print("sudo pacman -R " + " ".join(unneeded_pkg_list
))
44 print("# Found explicitly installed packages to keep or remove")
45 print("echo " + " ".join(explicit_pkg_list
) + " | tr ' ' '\\n' >> ~/.pacorphan/keep")
46 print("sudo pacman -D --asdeps " + " ".join(explicit_pkg_list
))
49 if mark_explicit_list
:
50 print("# Found packages which should be marked as explicitly installed")
51 print("sudo pacman -D --asexplicit " + " ".join(mark_explicit_list
))
55 print("# Found packages which should be installed")
56 print("sudo pacman -S " + " ".join(need_install_list
))
60 ensure_apt_config_is_sane()
62 keep_pkg_list
= load_keep_pkg_list("aptorphan")
63 mark_explicit_list
= []
64 need_install_list
= []
65 installed_pkg_list
= list(run(["aptitude", "search", "?or(~i!~aremove,~ainstall)", "-F", "%p"]))
66 explicit_pkg_list
= list(run(["aptitude", "search", "?or(~i!~M!~aremove,~ainstall!~M)", "-F", "%p"]))
68 for pkg
in keep_pkg_list
:
69 if pkg
in explicit_pkg_list
:
70 explicit_pkg_list
.remove(pkg
)
72 if pkg
in installed_pkg_list
:
73 mark_explicit_list
.append(pkg
)
75 need_install_list
.append(pkg
)
78 if mark_explicit_list
:
79 print("# Found packages which should be marked as explicitly installed")
80 print("sudo aptitude --schedule-only install " + " ".join([("'"+x
+"&m'") for x
in mark_explicit_list
]))
84 print("# Found packages which should be installed")
85 print("sudo aptitude --schedule-only install " + " ".join(need_install_list
))
89 print("# Found explicitly installed packages to keep or remove")
90 print("echo " + " ".join(explicit_pkg_list
) + " | tr ' ' '\\n' >> ~/.aptorphan/keep")
91 print("sudo aptitude --schedule-only install " + " ".join([(x
+"+M") for x
in explicit_pkg_list
]))
94 def ensure_apt_config_is_sane():
96 APT::Install-Recommends "false";
97 APT::Install-Suggests "false";
98 APT::AutoRemove::RecommendsImportant "false";
99 APT::AutoRemove::SuggestsImportant "false";
100 """.strip().split("\n")
102 actual_config
= run(["apt-config", "dump"])
105 for required_line
in required_config
:
106 for line
in actual_config
:
107 if line
== required_line
:
110 missing_lines
.append(required_line
)
112 print("Missing apt-config, add these lines to /etc/apt/apt.conf.d/99recommends-disable")
113 print("\n".join(missing_lines
))
117 def load_keep_pkg_list(name
):
118 config_path
= find_config_path(name
)
119 if not os
.path
.isdir(config_path
):
120 print("# WARNING, you should create a directory at %s" % config_path
)
125 for filename
in sorted(os
.listdir(config_path
)):
126 if filename
.startswith("."):
128 full_filename
= os
.path
.join(config_path
, filename
)
129 for pkg
in codecs
.open(full_filename
, "r", "utf-8"):
130 pkg
= strip_comment(pkg
).strip()
133 if filename
[0] != "~" and pkg
[0] != "~":
135 print("# Duplicate entry:", pkg
, "in file", filename
)
140 if pkg
not in result
:
141 print("# Redundant removal:", pkg
, "in file", filename
)
147 def find_config_path(name
):
148 homedir_path
= os
.path
.expanduser("~/.%s" % name
)
149 if os
.path
.isdir(homedir_path
):
152 if "XDG_CONFIG_HOME" not in os
.environ
:
153 return os
.path
.expanduser("~/.config/%s" % name
)
155 return os
.path
.join(os
.environ
["XDG_CONFIG_HOME"], name
)
157 def strip_comment(line
):
164 for line
in subprocess
.check_output(cmd
).decode("utf-8").split("\n"):
169 if __name__
== "__main__":