Skip to content
Snippets Groups Projects
Unverified Commit 31b1271d authored by Max Adamo's avatar Max Adamo
Browse files

use python apt instead of subprocess

parent c73747e9
No related branches found
No related tags found
No related merge requests found
...@@ -15,7 +15,7 @@ Purge unused Ubuntu kernels ...@@ -15,7 +15,7 @@ Purge unused Ubuntu kernels
### Setup ### Setup
I havent' created a release process yet. I did not create a release process yet.
Meanwhile I use Git LFS to upload the package in the folder [deb/](https://gitlab.com/maxadamo/ubuntu-kernel-cleanup/-/tree/master/deb) directory. Meanwhile I use Git LFS to upload the package in the folder [deb/](https://gitlab.com/maxadamo/ubuntu-kernel-cleanup/-/tree/master/deb) directory.
......
...@@ -15,7 +15,7 @@ Table of Contents ...@@ -15,7 +15,7 @@ Table of Contents
Setup Setup
I havent’ created a release process yet. I did not create a release process yet.
Meanwhile I use Git LFS to upload the package in the folder deb/ Meanwhile I use Git LFS to upload the package in the folder deb/
directory. directory.
......
...@@ -36,24 +36,15 @@ except ModuleNotFoundError as err: ...@@ -36,24 +36,15 @@ except ModuleNotFoundError as err:
def get_packages_list(pkg_match): def get_packages_list(pkg_match):
""" """
run a shell command like this one: return a list of kernel package versions installed on the system
dpkg --list linux-image-[1-9]* | awk '/^ii /&&/linux-image-/{print $2}'
and create a sorted list of installed kernels.
Or if we need the version only it could be:
dpkg-query -f '${Status} ${Version}\n' -W linux-image-[1-9]* | awk '/ok installed/{print $NF}'
""" """
installed = sp.Popen( c_list = list(CACHE)
"dpkg --list %s[1-9]* | awk \'/^ii /&&/%s/{print $2}\'" % ( c_list_names = [element.name for element in c_list if CACHE[element].is_installed]
pkg_match, pkg_match), c_list_installed = [element for element in c_list_names if element.startswith(
stdout=sp.PIPE, stderr=sp.PIPE, shell=True pkg_match) and any(chr.isdigit() for chr in element)]
) c_list_installed_versions = [re.sub(r"(^\D+-|-\D+)", r"", x) for x in c_list_installed]
installed_out, _ = installed.communicate()
installed_list = installed_out.decode('utf-8').split('\n')
kernels = [x for x in installed_list if x != ''] # delete empty items
kernel_versions = [get_version(item) for item in kernels]
# sort by version and unique return natsorted(c_list_installed_versions)
return natsorted(list(set(kernel_versions)))
def process_packages(version_number, prefix, execution_type): def process_packages(version_number, prefix, execution_type):
...@@ -165,7 +156,6 @@ if __name__ == '__main__': ...@@ -165,7 +156,6 @@ if __name__ == '__main__':
# we are running the latest kernel # we are running the latest kernel
sanitized_kernel_list = kern_list sanitized_kernel_list = kern_list
del kern_list[-1] del kern_list[-1]
try: try:
sanitized_kernel_list sanitized_kernel_list
except NameError: except NameError:
...@@ -180,9 +170,9 @@ if __name__ == '__main__': ...@@ -180,9 +170,9 @@ if __name__ == '__main__':
for kernel_version in purged_list: for kernel_version in purged_list:
process_packages(kernel_version, kernel_pkg, EXECUTION) process_packages(kernel_version, kernel_pkg, EXECUTION)
CACHE.close() # it returns always true... keeps closing :) CACHE.close() # it returns always true... keeps closing like swivel doors :)
# purge empty packages # purge empty packages (it doesn't remove kernel but orphan packages without files)
if EXECUTION == 'real': if EXECUTION == 'real':
CMD = "dpkg-query -f \'${Package} ${Status}\\n\' -W linux-* | awk \'/deinstall ok/{print $1}\' | xargs apt-get purge -y" # pylint: disable=C0301 CMD = "dpkg-query -f \'${Package} ${Status}\\n\' -W linux-* | awk \'/deinstall ok/{print $1}\' | xargs apt-get purge -y" # pylint: disable=C0301
else: else:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment