From 45138fcdf009f04454053b46e4ea71e5b0c49d3e Mon Sep 17 00:00:00 2001 From: Joseph Nuthalapati Date: Mon, 2 Jul 2018 17:33:07 +0530 Subject: [PATCH] packages: Button to refresh package lists - This is a fallback solution to manually refresh package lists on AWS images since they come with no apt package lists. - This can also be occasionally useful for people running the testing distribution where packages might be frequently added and removed. Signed-off-by: Joseph Nuthalapati Reviewed-by: James Valleroy --- plinth/package.py | 19 ++++++++++++++++--- plinth/templates/setup.html | 6 +++++- plinth/views.py | 28 +++++++++++++++++----------- 3 files changed, 38 insertions(+), 15 deletions(-) diff --git a/plinth/package.py b/plinth/package.py index 897dcd812..f6d454734 100644 --- a/plinth/package.py +++ b/plinth/package.py @@ -14,18 +14,17 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . # - """ Framework for installing and updating distribution packages """ -from django.utils.translation import ugettext as _ import logging import subprocess import threading -from plinth import actions +from django.utils.translation import ugettext as _ +from plinth import actions logger = logging.getLogger(__name__) @@ -90,6 +89,14 @@ class Transaction(object): logger.exception('Error installing package: %s', exception) raise + def refresh_package_lists(self): + """Refresh apt package lists.""" + try: + self._run_apt_command(['update']) + except subprocess.CalledProcessError as exception: + logger.exception('Error updating package lists: %s', exception) + raise + def _run_apt_command(self, arguments): """Run apt-get and update progress.""" self._reset_status() @@ -151,3 +158,9 @@ def is_package_manager_busy(): return True except actions.ActionError: return False + + +def refresh_package_lists(): + """To be run in case apt package lists are outdated.""" + transaction = Transaction(None, None) + transaction.refresh_package_lists() diff --git a/plinth/templates/setup.html b/plinth/templates/setup.html index 9d79bce43..e00bace70 100644 --- a/plinth/templates/setup.html +++ b/plinth/templates/setup.html @@ -77,10 +77,13 @@ {% blocktrans trimmed %} This application is currently not available in your distribution. {% endblocktrans %} + {% endif %} - + {% else %} {% if setup_helper.current_operation.step == 'pre' %} diff --git a/plinth/views.py b/plinth/views.py index e3856fe85..da6287e5a 100644 --- a/plinth/views.py +++ b/plinth/views.py @@ -181,16 +181,22 @@ class SetupView(TemplateView): context['package_manager_is_busy'] = package.is_package_manager_busy() return context - def post(self, *args, **kwargs): - """Handle installing/upgrading applications. + def dispatch(self, request, *args, **kwargs): + if request.method == 'POST': + if 'install' in request.POST: + # Handle installing/upgrading applications. + # Start the application setup, and refresh the page every few + # seconds to keep displaying the status. + self.kwargs['setup_helper'].run_in_thread() - Start the application setup, and refresh the page every few - seconds to keep displaying the status. - """ - self.kwargs['setup_helper'].run_in_thread() + # Give a moment for the setup process to start and show + # meaningful status. + time.sleep(1) + return self.render_to_response(self.get_context_data()) - # Give a moment for the setup process to start and show - # meaningful status. - time.sleep(1) - - return self.render_to_response(self.get_context_data()) + elif 'refresh-packages' in request.POST: + # Refresh apt package lists + package.refresh_package_lists() + return self.render_to_response(self.get_context_data()) + else: + return super(SetupView, self).dispatch(request, *args, **kwargs)