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

silent apt install

parent aba671a0
No related branches found
No related tags found
No related merge requests found
......@@ -32,6 +32,7 @@ conf*.sh
auto-generated-conf.sh
log*.txt
tests/ms-testing-suite
*.log
# virtualenv
.venv/
......
......@@ -51,6 +51,11 @@ def main():
else:
u.success("system clean")
# detect rc state
purgeable = len(apt.purgeable_packages)
if purgeable:
u.info("there is {} packages in rc state".format(purgeable))
# installation
try:
installed = apt.install("sl")
......
......@@ -15,6 +15,16 @@ except ModuleNotFoundError:
exit(2)
class AptInstallProgress(apt.progress.base.InstallProgress):
def fork(self):
pid = os.fork()
if pid == 0:
logfd = os.open("/tmp/envsetup-dpkg.log", os.O_RDWR | os.O_CREAT, 0o644)
os.dup2(logfd, 1)
os.dup2(logfd, 2)
return pid
class Apt:
# cache: apt.cache.Cache
# packages: list
......@@ -22,18 +32,24 @@ class Apt:
# installed_packages: list
# _removable_packages: list
# removable_packages: list
# _purgeable_packages: list
# purgeable_packages: list
# _upgradable_packages: list
# upgradable_packages: list
def __init__(self, update: bool = False):
os.environ["DEBIAN_FRONTEND"] = "noninteractive"
os.environ["PATH"] = "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
os.environ[
"PATH"
] = "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
self.cache = self.get_cache(update)
self.packages = self.get_packages()
self._installed_packages = self.get_installed_packages()
self.installed_packages = list(map(str, self._installed_packages))
self._removable_packages = self.get_removable_packages()
self.removable_packages = list(map(str, self._removable_packages))
self._purgeable_packages = self.get_purgeable_packages()
self.purgeable_packages = list(map(str, self._purgeable_packages))
self._upgradable_packages = self.get_upgradable_packages()
self.upgradable_packages = list(map(str, self._upgradable_packages))
......@@ -49,7 +65,7 @@ class Apt:
pkg = self.cache[name]
if not pkg.installed:
pkg.mark_install(auto_fix=False)
success = self.cache.commit()
success = self.cache.commit(install_progress=AptInstallProgress())
self.reload()
return success
......@@ -68,23 +84,36 @@ class Apt:
return False
def remove(self, name: str) -> bool:
def remove(self, name: str, purge: bool = False) -> bool:
"""Remove a package with APT.
:param name: Package name
:type name: str
:param purge: Wether to purge package configuration files ot not, default=False
:type purge: 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()
pkg.mark_delete(auto_fix=False, purge=purge)
success = self.cache.commit(install_progress=AptInstallProgress())
self.reload()
return success
def purge(self, name: str) -> bool:
"""Purge a package with APT.
:param name: Package name
:type name: str
:return: Wether uninstallation is successful or not
:rtype: bool
"""
return self.remove(name, purge=True)
def reload(self):
"""Reload object."""
......@@ -94,7 +123,7 @@ class Apt:
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
:param update: Wether to update cache or not, default=False
:type update: bool
:return: An APT Cache object
:rtype: apt.cache.Cache
......@@ -142,6 +171,19 @@ class Apt:
return _removable_packages
def get_purgeable_packages(self) -> list:
"""Get auto-removable packages list.
:return: Auto-removable packages list
:rtype: list
"""
_removable_packages = [
p for p in self.packages if not p.is_installed and p.has_config_files
]
return _removable_packages
def get_upgradable_packages(self) -> list:
"""Get upgradable packages list.
......
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