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 %}