-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsystem_cleanup_module.py
More file actions
404 lines (355 loc) · 13.9 KB
/
system_cleanup_module.py
File metadata and controls
404 lines (355 loc) · 13.9 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#!/usr/bin/env python3
"""
System Cleanup Module for Desktop AI Agent
Handles system optimization and cache clearing
"""
import os
import subprocess
import shutil
import logging
from typing import Dict, Any, List
from pathlib import Path
from module_framework import BaseModule, ModuleResult, ResultStatus
logger = logging.getLogger(__name__)
class SystemCleanupModule(BaseModule):
"""
Cleanup and optimization module
Clears caches, removes old files, optimizes system
"""
def __init__(self):
super().__init__(
name="system_cleanup",
description="System cleanup and optimization",
version="1.0.0"
)
def get_supported_actions(self) -> List[str]:
"""Get supported cleanup actions"""
return [
"clear_package_cache",
"clear_temp_files",
"clear_thumbnail_cache",
"remove_old_kernels",
"clear_browser_cache",
"empty_trash",
"cleanup_logs",
"full_cleanup",
"get_space_usage"
]
def execute(self, action: str, parameters: Dict[str, Any]) -> ModuleResult:
"""Execute cleanup action"""
try:
if action == "clear_package_cache":
return self._clear_package_cache()
elif action == "clear_temp_files":
return self._clear_temp_files()
elif action == "clear_thumbnail_cache":
return self._clear_thumbnail_cache()
elif action == "remove_old_kernels":
return self._remove_old_kernels()
elif action == "clear_browser_cache":
return self._clear_browser_cache()
elif action == "empty_trash":
return self._empty_trash()
elif action == "cleanup_logs":
return self._cleanup_logs()
elif action == "full_cleanup":
return self._full_cleanup()
elif action == "get_space_usage":
return self._get_space_usage()
else:
return ModuleResult(
status=ResultStatus.FAILED,
message=f"Unknown action: {action}",
data={}
)
except Exception as e:
return ModuleResult(
status=ResultStatus.FAILED,
message=f"Error executing {action}",
data={},
error=str(e)
)
def _clear_package_cache(self) -> ModuleResult:
"""Clear package manager cache (apt/dnf/pacman)"""
data = {"cleared": []}
errors = []
# APT (Debian/Ubuntu)
try:
subprocess.run(["sudo", "apt", "clean"], capture_output=True, timeout=30)
subprocess.run(["sudo", "apt", "autoclean"], capture_output=True, timeout=30)
data["cleared"].append("apt cache")
except Exception as e:
errors.append(f"APT cleanup failed: {e}")
# DNF (Fedora/RHEL)
try:
subprocess.run(["sudo", "dnf", "clean", "all"], capture_output=True, timeout=30)
data["cleared"].append("dnf cache")
except Exception as e:
errors.append(f"DNF cleanup failed: {e}")
# Pacman (Arch)
try:
subprocess.run(["sudo", "pacman", "-Sc", "--noconfirm"],
capture_output=True, timeout=30)
data["cleared"].append("pacman cache")
except Exception as e:
errors.append(f"Pacman cleanup failed: {e}")
status = ResultStatus.SUCCESS if data["cleared"] else ResultStatus.FAILED
message = f"Cleared: {', '.join(data['cleared'])}"
return ModuleResult(
status=status,
message=message,
data=data,
error="; ".join(errors) if errors else None
)
def _clear_temp_files(self) -> ModuleResult:
"""Clear temporary files"""
data = {"cleared": 0, "size_freed": 0}
errors = []
temp_dirs = [
"/tmp",
"/var/tmp",
os.path.expanduser("~/.cache/tmp")
]
for temp_dir in temp_dirs:
if os.path.exists(temp_dir):
try:
for item in os.listdir(temp_dir):
item_path = os.path.join(temp_dir, item)
if os.path.isfile(item_path):
size = os.path.getsize(item_path)
os.remove(item_path)
data["cleared"] += 1
data["size_freed"] += size
elif os.path.isdir(item_path):
size = self._get_dir_size(item_path)
shutil.rmtree(item_path, ignore_errors=True)
data["cleared"] += 1
data["size_freed"] += size
except Exception as e:
errors.append(f"Error cleaning {temp_dir}: {e}")
return ModuleResult(
status=ResultStatus.SUCCESS,
message=f"Cleared {data['cleared']} items, freed {self._format_size(data['size_freed'])}",
data=data,
error="; ".join(errors) if errors else None
)
def _clear_thumbnail_cache(self) -> ModuleResult:
"""Clear thumbnail cache"""
data = {"cleared": False, "size_freed": 0}
cache_dir = os.path.expanduser("~/.cache/thumbnails")
if os.path.exists(cache_dir):
try:
size = self._get_dir_size(cache_dir)
shutil.rmtree(cache_dir)
os.makedirs(cache_dir)
data["cleared"] = True
data["size_freed"] = size
except Exception as e:
return ModuleResult(
status=ResultStatus.FAILED,
message="Failed to clear thumbnail cache",
data=data,
error=str(e)
)
return ModuleResult(
status=ResultStatus.SUCCESS,
message=f"Cleared thumbnail cache, freed {self._format_size(data['size_freed'])}",
data=data
)
def _remove_old_kernels(self) -> ModuleResult:
"""Remove old kernel versions"""
data = {"removed": []}
try:
# Get current kernel
result = subprocess.run(
["uname", "-r"],
capture_output=True,
text=True,
timeout=10
)
current_kernel = result.stdout.strip()
# List installed kernels
result = subprocess.run(
["dpkg", "-l", "|", "grep", "linux-image"],
shell=True,
capture_output=True,
text=True,
timeout=10
)
# Remove old kernels (requires sudo)
subprocess.run(
["sudo", "apt", "autoremove", "--purge", "-y"],
capture_output=True,
timeout=60
)
data["current_kernel"] = current_kernel
data["removed"] = ["Old kernel versions removed via autoremove"]
return ModuleResult(
status=ResultStatus.SUCCESS,
message="Old kernels removed",
data=data
)
except Exception as e:
return ModuleResult(
status=ResultStatus.FAILED,
message="Failed to remove old kernels",
data=data,
error=str(e)
)
def _clear_browser_cache(self) -> ModuleResult:
"""Clear browser caches"""
data = {"cleared": [], "size_freed": 0}
browser_caches = {
"Firefox": os.path.expanduser("~/.cache/mozilla/firefox"),
"Chrome": os.path.expanduser("~/.cache/google-chrome"),
"Chromium": os.path.expanduser("~/.cache/chromium"),
"Brave": os.path.expanduser("~/.cache/BraveSoftware/Brave-Browser")
}
for browser, cache_path in browser_caches.items():
if os.path.exists(cache_path):
try:
size = self._get_dir_size(cache_path)
shutil.rmtree(cache_path)
os.makedirs(cache_path)
data["cleared"].append(browser)
data["size_freed"] += size
except Exception as e:
logger.warning(f"Failed to clear {browser} cache: {e}")
return ModuleResult(
status=ResultStatus.SUCCESS,
message=f"Cleared caches for: {', '.join(data['cleared'])}",
data=data
)
def _empty_trash(self) -> ModuleResult:
"""Empty trash/recycle bin"""
data = {"cleared": False, "size_freed": 0}
trash_dir = os.path.expanduser("~/.local/share/Trash")
if os.path.exists(trash_dir):
try:
size = self._get_dir_size(trash_dir)
shutil.rmtree(trash_dir)
os.makedirs(trash_dir)
data["cleared"] = True
data["size_freed"] = size
except Exception as e:
return ModuleResult(
status=ResultStatus.FAILED,
message="Failed to empty trash",
data=data,
error=str(e)
)
return ModuleResult(
status=ResultStatus.SUCCESS,
message=f"Emptied trash, freed {self._format_size(data['size_freed'])}",
data=data
)
def _cleanup_logs(self) -> ModuleResult:
"""Clean up old log files"""
data = {"cleaned": 0, "size_freed": 0}
log_dirs = [
"/var/log",
os.path.expanduser("~/.local/share/*/logs")
]
for log_dir in log_dirs:
if os.path.exists(log_dir):
try:
for item in Path(log_dir).glob("*.log*"):
if item.is_file():
try:
size = item.stat().st_size
item.unlink()
data["cleaned"] += 1
data["size_freed"] += size
except Exception as e:
logger.warning(f"Failed to remove {item}: {e}")
except Exception as e:
logger.warning(f"Error cleaning {log_dir}: {e}")
return ModuleResult(
status=ResultStatus.SUCCESS,
message=f"Cleaned {data['cleaned']} log files, freed {self._format_size(data['size_freed'])}",
data=data
)
def _full_cleanup(self) -> ModuleResult:
"""Execute full system cleanup"""
results = {}
total_freed = 0
actions = [
"clear_package_cache",
"clear_temp_files",
"clear_thumbnail_cache",
"clear_browser_cache",
"empty_trash",
"cleanup_logs"
]
for action in actions:
result = self.execute(action, {})
results[action] = result.to_dict()
if "size_freed" in result.data:
total_freed += result.data.get("size_freed", 0)
return ModuleResult(
status=ResultStatus.SUCCESS,
message=f"Full cleanup completed, freed {self._format_size(total_freed)}",
data={
"actions": results,
"total_freed": total_freed
}
)
def _get_space_usage(self) -> ModuleResult:
"""Get disk space usage"""
try:
result = subprocess.run(
["df", "-h", "/"],
capture_output=True,
text=True,
timeout=10
)
lines = result.stdout.strip().split("\n")
if len(lines) >= 2:
parts = lines[1].split()
data = {
"total": parts[1],
"used": parts[2],
"available": parts[3],
"percent": parts[4]
}
return ModuleResult(
status=ResultStatus.SUCCESS,
message=f"Disk usage: {data['used']} / {data['total']} ({data['percent']})",
data=data
)
except Exception as e:
return ModuleResult(
status=ResultStatus.FAILED,
message="Failed to get disk usage",
data={},
error=str(e)
)
@staticmethod
def _get_dir_size(path: str) -> int:
"""Get directory size in bytes"""
total = 0
try:
for entry in os.scandir(path):
if entry.is_file(follow_symlinks=False):
total += entry.stat().st_size
elif entry.is_dir(follow_symlinks=False):
total += SystemCleanupModule._get_dir_size(entry.path)
except Exception:
pass
return total
@staticmethod
def _format_size(size_bytes: int) -> str:
"""Format bytes to human readable size"""
for unit in ["B", "KB", "MB", "GB"]:
if size_bytes < 1024:
return f"{size_bytes:.2f} {unit}"
size_bytes /= 1024
return f"{size_bytes:.2f} TB"
if __name__ == "__main__":
module = SystemCleanupModule()
print("System Cleanup Module Test")
print(f"Supported actions: {module.get_supported_actions()}")
# Test get_space_usage
result = module.execute("get_space_usage", {})
print(f"\nDisk Usage: {result.message}")
print(f"Data: {result.data}")