upgrades: Refactor use of lsb_release

Tests:
- On unstable and testing:
  - Ran functional tests for upgrades.
  - Ran actions "upgrades setup" and "upgrades setup-repositories".
- On testing:
  - In develop mode, activated backports.

Signed-off-by: James Valleroy <jvalleroy@mailbox.org>
Reviewed-by: Sunil Mohan Adapa <sunil@medhas.org>
This commit is contained in:
James Valleroy 2020-07-06 08:49:31 -04:00 committed by Sunil Mohan Adapa
parent a60341f0d8
commit f8eb6c8cc6
No known key found for this signature in database
GPG Key ID: 43EA1CFF0AA7C5F2
3 changed files with 18 additions and 15 deletions

View File

@ -13,7 +13,8 @@ import sys
from plinth.action_utils import run_apt_command from plinth.action_utils import run_apt_command
from plinth.modules.apache.components import check_url from plinth.modules.apache.components import check_url
from plinth.modules.upgrades import is_backports_current, SOURCES_LIST from plinth.modules.upgrades import (get_current_release, is_backports_current,
SOURCES_LIST)
AUTO_CONF_FILE = '/etc/apt/apt.conf.d/20auto-upgrades' AUTO_CONF_FILE = '/etc/apt/apt.conf.d/20auto-upgrades'
LOG_FILE = '/var/log/unattended-upgrades/unattended-upgrades.log' LOG_FILE = '/var/log/unattended-upgrades/unattended-upgrades.log'
@ -218,8 +219,7 @@ def _check_and_backports_sources(develop=False):
'backports.') 'backports.')
return return
release = subprocess.check_output(['lsb_release', '--release', release, dist = get_current_release()
'--short']).decode().strip()
if release == 'unstable' or (release == 'testing' and not develop): if release == 'unstable' or (release == 'testing' and not develop):
print(f'System release is {release}. Skip enabling backports.') print(f'System release is {release}. Skip enabling backports.')
return return
@ -228,8 +228,6 @@ def _check_and_backports_sources(develop=False):
if protocol == 'tor+http': if protocol == 'tor+http':
print('Package download over Tor is enabled.') print('Package download over Tor is enabled.')
dist = subprocess.check_output(['lsb_release', '--codename',
'--short']).decode().strip()
if not _is_release_file_available(protocol, dist): if not _is_release_file_available(protocol, dist):
print(f'Release file for {dist}-backports is not available yet.') print(f'Release file for {dist}-backports is not available yet.')
return return
@ -251,8 +249,7 @@ def _add_apt_preferences():
# Don't try to remove 50freedombox3.pref as this file is shipped with the # Don't try to remove 50freedombox3.pref as this file is shipped with the
# Debian package and is removed using maintainer scripts. # Debian package and is removed using maintainer scripts.
dist = subprocess.check_output(['lsb_release', '--codename', _, dist = get_current_release()
'--short']).decode().strip()
if dist == 'sid': if dist == 'sid':
print(f'System distribution is {dist}. Skip setting apt preferences ' print(f'System distribution is {dist}. Skip setting apt preferences '
'for backports.') 'for backports.')

View File

@ -148,13 +148,22 @@ def is_backports_enabled():
return os.path.exists(SOURCES_LIST) return os.path.exists(SOURCES_LIST)
def get_current_release():
"""Return current release and codename as a tuple."""
output = subprocess.check_output(
['lsb_release', '--release', '--codename',
'--short']).decode().strip()
lines = output.split('\n')
return lines[0], lines[1]
def is_backports_current(): def is_backports_current():
"""Return whether backports are enabled for the current release.""" """Return whether backports are enabled for the current release."""
if not is_backports_enabled: if not is_backports_enabled:
return False return False
dist = subprocess.check_output(['lsb_release', '--codename', '--short' _, dist = get_current_release()
]).decode().strip() + '-backports' dist += '-backports'
sources = sourceslist.SourcesList() sources = sourceslist.SourcesList()
for source in sources: for source in sources:
if source.dist == dist: if source.dist == dist:
@ -168,8 +177,7 @@ def can_activate_backports():
if is_backports_current(): if is_backports_current():
return False return False
release = subprocess.check_output(['lsb_release', '--release', release, _ = get_current_release()
'--short']).decode().strip()
if release == 'unstable' or (release == 'testing' and not cfg.develop): if release == 'unstable' or (release == 'testing' and not cfg.develop):
return False return False

View File

@ -3,8 +3,6 @@
FreedomBox app for upgrades. FreedomBox app for upgrades.
""" """
import subprocess
from django.contrib import messages from django.contrib import messages
from django.shortcuts import redirect from django.shortcuts import redirect
from django.urls import reverse_lazy from django.urls import reverse_lazy
@ -31,8 +29,8 @@ class UpgradesConfigurationView(AppView):
def get_context_data(self, *args, **kwargs): def get_context_data(self, *args, **kwargs):
context = super().get_context_data(*args, **kwargs) context = super().get_context_data(*args, **kwargs)
context['can_activate_backports'] = upgrades.can_activate_backports() context['can_activate_backports'] = upgrades.can_activate_backports()
context['dist'] = subprocess.check_output( _, dist = upgrades.get_current_release()
['lsb_release', '--codename', '--short']).decode().strip() context['dist'] = dist
context['is_busy'] = package.is_package_manager_busy() context['is_busy'] = package.is_package_manager_busy()
context['log'] = get_log() context['log'] = get_log()
context['refresh_page_sec'] = 3 if context['is_busy'] else None context['refresh_page_sec'] = 3 if context['is_busy'] else None