From 929e7f6dba24881c1904782cbb83e07f023b18b0 Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Thu, 18 Nov 2021 09:43:56 -0800 Subject: [PATCH] packages: Move checking for unavailable packages to component Signed-off-by: Sunil Mohan Adapa Reviewed-by: James Valleroy --- plinth/package.py | 20 ++++++++++++++++++++ plinth/setup.py | 29 ----------------------------- plinth/templates/setup.html | 4 ++-- plinth/tests/test_package.py | 19 +++++++++++++++++++ plinth/views.py | 10 ++++++++++ 5 files changed, 51 insertions(+), 31 deletions(-) diff --git a/plinth/package.py b/plinth/package.py index 2d41c4806..80951c39e 100644 --- a/plinth/package.py +++ b/plinth/package.py @@ -6,6 +6,7 @@ Framework for installing and updating distribution packages import enum import json import logging +import pathlib import subprocess import sys import threading @@ -84,6 +85,25 @@ class Packages(app.FollowerComponent): return packages_installed(self.conflicts) + def has_unavailable_packages(self): + """Return whether any of the packages are not available. + + Returns True if one or more of the packages is not available in the + user's Debian distribution or False otherwise. Returns None if it + cannot be reliably determined whether the packages are available or + not. + """ + apt_lists_dir = pathlib.Path('/var/lib/apt/lists/') + num_files = len( + [child for child in apt_lists_dir.iterdir() if child.is_file()]) + if num_files < 2: # not counting the lock file + return None + + # List of all packages from all Package components + cache = apt.Cache() + return any(package for package in self.packages + if package not in cache) + class PackageException(Exception): """A package operation has failed.""" diff --git a/plinth/setup.py b/plinth/setup.py index ac436eaf6..ac73f94b7 100644 --- a/plinth/setup.py +++ b/plinth/setup.py @@ -5,7 +5,6 @@ Utilities for performing application setup operations. import importlib import logging -import os import threading import time from collections import defaultdict @@ -159,34 +158,6 @@ class Helper(object): models.Module.objects.update_or_create( pk=self.module_name, defaults={'setup_version': version}) - def has_unavailable_packages(self): - """Find if any of the packages managed by the module are not available. - - Returns True if one or more of the packages is not available in the - user's Debian distribution or False otherwise. - Returns None if it cannot be reliably determined whether the - packages are available or not. - """ - APT_LISTS_DIR = '/var/lib/apt/lists/' - num_files = len([ - name for name in os.listdir(APT_LISTS_DIR) - if os.path.isfile(os.path.join(APT_LISTS_DIR, name)) - ]) - if num_files < 2: # not counting the lock file - return None - - pkg_components = list(self.module.app.get_components_of_type(Packages)) - if not pkg_components: # This app has no packages to install - return False - - # List of all packages from all Package components - managed_pkgs = (package for component in pkg_components - for package in component.packages) - cache = apt.Cache() - unavailable_pkgs = (pkg_name for pkg_name in managed_pkgs - if pkg_name not in cache) - return any(unavailable_pkgs) - def init(module_name, module): """Create a setup helper for a module for later use.""" diff --git a/plinth/templates/setup.html b/plinth/templates/setup.html index 0555d04bd..f0f07821f 100644 --- a/plinth/templates/setup.html +++ b/plinth/templates/setup.html @@ -41,7 +41,7 @@ Please wait for a few moments before trying again. {% endblocktrans %} - {% elif setup_helper.has_unavailable_packages %} + {% elif has_unavailable_packages %}