Repository navigation
Expand file tree
/
Copy pathsnap_cleanup.py
More file actions
executable file
·57 lines (47 loc) · 2.02 KB
/
Copy pathsnap_cleanup.py
File metadata and controls
executable file
·57 lines (47 loc) · 2.02 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
#!/usr/bin/env python3
# SPDX-FileCopyrightText: Copyright (c) 2025-2026 Maulik Mistry
# SPDX-License-Identifier: Apache-2.0
#
# snap_cleanup.py - Removes disabled or old Snap packages.
#
# Author: Maulik Mistry
# Please share support: https://www.paypal.com/paypalme/m1st0
# https://venmo.com/code?user_id=3319592654995456106&created=1753283702
import subprocess
import sys
#LANG=en_US.UTF-8 snap list --all | awk '/disabled/{print $1, $3}' | while read snapname revision; do snap remove $snapname --revision=$revision; done
snap_script = "LANG=en_US.UTF-8 sudo snap list --all | awk '/disabled/{print $1, $3}' | " \
"while read snapname revision; do " \
"sudo snap remove \"$snapname\" --revision=\"$revision\"; " \
"done"
process1 = subprocess.run([snap_script],
shell=True,
check=True,
stdout=subprocess.PIPE,
text=True)
if process1.stdout:
print("Snap cleanup stdout:", process1.stdout)
if process1.stderr:
print("Snap cleanup stderr:", process1.stderr)
# On Kubuntu?
process2 = subprocess.run(['sudo', 'systemctl', 'restart', 'snapd'],
check=True,
capture_output=True,
text=True) # Better than universal_newlines=True
# Check the restart command output
if process2.stdout:
print("Restart command stdout:", process2.stdout)
if process2.stderr:
print("Restart command stderr:", process2.stderr)
# Check service status too
status_result = subprocess.run(['sudo', 'systemctl', 'status', 'snapd'],
capture_output=True,
text=True)
if status_result.stdout:
print(status_result.stdout)
# Check return code (0 = active, non-zero = not active/failed)
if status_result.returncode == 0:
print("Service snapd is running successfully")
else:
if status_result.stderr:
print("snapd failed to restart:", status_result.stderr)