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
|
#!/usr/bin/env python3
import subprocess
import logging
import re
import sys
from collections import defaultdict
kernel_version_p = re.compile(r"[\d\-\.]{8,}")
def run_command(command, ignore_exception=False, timeout=60 * 60 * 24, no_out=False):
logging.debug('exec command :"%s"', command)
if no_out:
return subprocess.Popen(command, shell=True)
proc = subprocess.Popen(
command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True
)
try:
outs, errs = [o.decode() for o in proc.communicate(timeout=timeout)]
except subprocess.SubprocessError as exp:
outs, errs = "", str(exp)
if ignore_exception:
logging.warning(errs)
else:
raise exp
finally:
proc.kill()
return outs, errs
def parse(kernels):
generic_kernels = defaultdict(set)
oem_kernels = defaultdict(set)
for k in kernels:
match = kernel_version_p.search(k)
if not match:
logging.warning(f"wrong: {k}")
continue
num_k = int(match[0].replace("-", ".").replace(".", ""))
if "edge" in k:
oem_kernels[num_k].add(k)
else:
generic_kernels[num_k].add(k)
for k in kernels:
match = kernel_version_p.search(k)
if not match:
logging.warning(f"wrong: {k}")
continue
num_k = int(match[0].replace("-", ".").replace(".", ""))
for d in [generic_kernels, oem_kernels]:
if num_k in d:
d[num_k].add(k)
return generic_kernels, oem_kernels
if __name__ == "__main__":
cli = "dpkg --get-selections | egrep -i 'linux-[himo]' | awk '{print $1}' | grep -v $(egrep -i '^DISTRIB_RELEASE=' /etc/lsb-release | awk -F '=' '{print $NF}')"
outs, errors = run_command(cli)
print(outs)
if errors:
sys.exit(errors)
kernels = [
i for i in outs.split("\n") if i and not re.search(r"[a-z]+\-generic$", i)
]
for d in parse(kernels):
for _, k in sorted(d.items(), key=lambda x: x[0], reverse=True):
cmd = f"apt purge {' '.join(k)} -y"
op = input(cmd).strip()
if op.lower() == 'y':
run_command(cmd)
|