-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcleaner.py
More file actions
152 lines (125 loc) · 4.41 KB
/
cleaner.py
File metadata and controls
152 lines (125 loc) · 4.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import curses
import os
import subprocess
import shutil
from pathlib import Path
import hashlib
# ---------- Aufräumfunktionen ----------
def run_command(command):
subprocess.run(command, shell=True)
def clean_apt():
run_command("sudo apt update")
run_command("sudo apt autoremove -y")
run_command("sudo apt autoclean")
run_command("sudo apt clean")
def clean_journal():
run_command("sudo journalctl --vacuum-time=7d")
def empty_trash():
trash_dirs = [Path.home() / ".local/share/Trash/files", Path.home() / ".Trash"]
for trash in trash_dirs:
shutil.rmtree(trash, ignore_errors=True)
def clean_tmp():
tmp_dirs = ["/tmp", str(Path.home() / ".cache")]
for tmp in tmp_dirs:
run_command(f"rm -rf {tmp}/*")
def clean_flatpak():
run_command("flatpak uninstall --unused -y")
def clean_snap():
run_command("snap list --all | awk '/disabled/{print $1, $2}' | "
"while read snapname revision; do sudo snap remove \"$snapname\" --revision=\"$revision\"; done")
def clean_browser_cache():
browsers = {
"Firefox": Path.home() / ".mozilla/firefox",
"Chromium": Path.home() / ".cache/chromium",
"Google Chrome": Path.home() / ".cache/google-chrome",
"Brave": Path.home() / ".cache/BraveSoftware"
}
for path in browsers.values():
if path.exists():
shutil.rmtree(path, ignore_errors=True)
def file_hash(path, algo="sha256"):
h = hashlib.new(algo)
with open(path, "rb") as f:
while chunk := f.read(8192):
h.update(chunk)
return h.hexdigest()
def find_and_remove_duplicates(search_path=Path.home()):
size_map = {}
for root, _, files in os.walk(search_path):
for file in files:
filepath = Path(root) / file
if not filepath.is_file() or filepath.is_symlink():
continue
try:
size = filepath.stat().st_size
size_map.setdefault(size, []).append(filepath)
except:
continue
duplicates = []
for files in size_map.values():
if len(files) < 2:
continue
hash_dict = {}
for file in files:
try:
h = file_hash(file)
if h in hash_dict:
duplicates.append(file)
else:
hash_dict[h] = file
except:
continue
for dup in duplicates:
try:
os.remove(dup)
except:
pass
# ---------- Menü mit curses ----------
menu_items = [
("System bereinigen (APT + Logs)", [clean_apt, clean_journal]),
("Temporäre Dateien löschen", [clean_tmp]),
("Papierkorb leeren", [empty_trash]),
("Browser-Cache löschen", [clean_browser_cache]),
("Nicht verwendete Flatpak/Snap entfernen", [clean_flatpak, clean_snap]),
("Doppelte Dateien finden & löschen", [find_and_remove_duplicates]),
]
def menu(stdscr):
curses.curs_set(0)
current = 0
selected = [False] * len(menu_items)
while True:
stdscr.clear()
stdscr.addstr("🔧 Was möchtest du bereinigen? (SPACE = auswählen, ENTER = starten)\n\n")
for idx, (label, _) in enumerate(menu_items):
if idx == current:
stdscr.addstr("> ", curses.A_REVERSE)
else:
stdscr.addstr(" ")
mark = "★" if selected[idx] else " "
stdscr.addstr(f"[{mark}] {label}\n")
key = stdscr.getch()
if key == curses.KEY_UP:
current = (current - 1) % len(menu_items)
elif key == curses.KEY_DOWN:
current = (current + 1) % len(menu_items)
elif key == ord(" "):
selected[current] = not selected[current]
elif key == curses.KEY_ENTER or key in [10, 13]:
break
stdscr.clear()
stdscr.addstr("🚀 Starte Bereinigung...\n\n")
stdscr.refresh()
for i, chosen in enumerate(selected):
if chosen:
for func in menu_items[i][1]:
stdscr.addstr(f"➡️ {func.__name__}\n")
stdscr.refresh()
try:
func()
except Exception as e:
stdscr.addstr(f"❌ Fehler: {e}\n")
stdscr.addstr("\n✅ Bereinigung abgeschlossen. Drücke eine Taste zum Beenden.")
stdscr.getch()
# ---------- Main ----------
if __name__ == "__main__":
curses.wrapper(menu)