Skip to content
Snippets Groups Projects
Commit 511267b8 authored by Nicolas KAROLAK's avatar Nicolas KAROLAK
Browse files

modulare utils + add utils.apt module

parent b8e86b45
No related branches found
No related tags found
No related merge requests found
......@@ -4,6 +4,7 @@ from subprocess import run, DEVNULL, PIPE, STDOUT
PACKAGES = [
"bsd-mailx", # for "mail" command used in tester
"python3-apt", # for: test_apt
"python3-defusedxml", # for: test_wowza
"python3-dnspython", # for: test_caches
"python3-openssl", # for: test_ssl
......
File moved
#!/usr/bin/env python3
"""
A wrapper of apt module that is actually usable.
"""
import utils as u
try:
import apt
import apt_pkg
except ModuleNotFoundError:
u.warning("apt python module not found")
exit(2)
class Apt:
cache: apt.cache.Cache
packages: list
installed_packages: list
removable_packages: list
upgradable_packages: list
def __init__(self, update: bool = False):
self.cache = self.get_cache(update)
self.packages = self.get_packages()
self.installed_packages = self.get_installed_packages()
self.removable_packages = self.get_removable_packages()
self.upgradable_packages = self.get_upgradable_packages()
def install(self, name: str) -> bool:
"""Install a package with APT.
:param name: Package name
:type name: str
:return: Wether installation is successful or not
:rtype: bool
"""
pkg = self.cache[name]
if not pkg.installed:
pkg.mark_install(auto_fix=False)
success = self.cache.commit()
self.reload()
return success
def remove(self, name: str) -> bool:
"""Remove a package with APT.
:param name: Package name
:type name: str
:return: Wether uninstallation is successful or not
:rtype: bool
"""
pkg = self.cache[name]
if pkg.installed:
pkg.mark_delete(auto_fix=False)
success = self.cache.commit()
self.reload()
return success
def reload(self):
"""Reload object."""
self.cache.clear()
self.__init__()
def get_cache(self, update: bool = False) -> apt.cache.Cache:
"""Get an eventually updated Cache object.
:param update: Wether to update cacheor not, default=False
:type update: bool
:return: An APT Cache object
:rtype: apt.cache.Cache
"""
apt_cache = apt.cache.Cache()
if update:
apt_cache.update()
apt_cache.open()
return apt_cache
def get_packages(self) -> list:
"""Get packages list.
:return: Packages list
:rtype: list
"""
packages = list(self.cache)
return packages
def get_installed_packages(self) -> list:
"""Get installed packages list.
:return: Installed packages list
:rtype: list
"""
installed_packages = [p for p in self.packages if p.is_installed]
return installed_packages
def get_removable_packages(self) -> list:
"""Get auto-removable packages list.
:return: Auto-removable packages list
:rtype: list
"""
removable_packages = [p for p in self.installed_packages if p.is_auto_removable]
return removable_packages
def get_upgradable_packages(self) -> list:
"""Get upgradable packages list.
:return: Upgradable packages list
:rtype: list
"""
upgradable_packages = [p for p in self.installed_packages if p.is_upgradable]
return upgradable_packages
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment