From 69058645ab8cdc4b6d7f147d10ff25d249393dad Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Tue, 4 Mar 2025 19:52:39 -0800 Subject: [PATCH] upgrades: Perform sources file update more reliably Signed-off-by: Sunil Mohan Adapa Reviewed-by: James Valleroy --- plinth/modules/upgrades/distupgrade.py | 38 +++++++++++++++++--------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/plinth/modules/upgrades/distupgrade.py b/plinth/modules/upgrades/distupgrade.py index 7eca2caa2..7943581a8 100644 --- a/plinth/modules/upgrades/distupgrade.py +++ b/plinth/modules/upgrades/distupgrade.py @@ -8,6 +8,8 @@ import subprocess import time from typing import Generator +import augeas + from plinth import action_utils from plinth.modules import snapshot as snapshot_module @@ -28,6 +30,28 @@ dist_upgrade_flag = pathlib.Path( '/var/lib/freedombox/dist-upgrade-in-progress') +def _sources_list_update(old_codename: str, new_codename: str): + """Change the distribution in /etc/apt/sources.list.""" + logging.info('Upgrading from %s to %s...', old_codename, new_codename) + aug = augeas.Augeas(flags=augeas.Augeas.NO_LOAD + + augeas.Augeas.NO_MODL_AUTOLOAD) + aug.transform('aptsources', SOURCES_LIST) + aug.set('/augeas/context', '/files' + SOURCES_LIST) + aug.load() + + for match_ in aug.match('*'): + dist_path = match_ + '/distribution' + dist = aug.get(dist_path) + if dist in (old_codename, 'stable'): + aug.set(dist_path, new_codename) + elif dist and (dist.startswith(old_codename + '-') + or dist.startswith('stable' + '-')): + new_value = new_codename + '-' + dist.partition('-')[2] + aug.set(dist_path, new_value) + + aug.save() + + def check(test_upgrade=False) -> tuple[bool, str]: """Check if a distribution upgrade be performed. @@ -85,19 +109,7 @@ def check(test_upgrade=False) -> tuple[bool, str]: if not utils.is_sufficient_free_space(): return (False, 'not-enough-free-space') - logging.info('Upgrading from %s to %s...', dist, codename) - with open(SOURCES_LIST, 'r', encoding='utf-8') as sources_list: - lines = sources_list.readlines() - - with open(SOURCES_LIST, 'w', encoding='utf-8') as sources_list: - for line in lines: - # E.g. replace 'bullseye' with 'bookworm'. - new_line = line.replace(dist, codename) - if check_dist == 'testing': - # E.g. replace 'stable' with 'bookworm'. - new_line = new_line.replace('stable', codename) - - sources_list.write(new_line) + _sources_list_update(dist, codename) logging.info('Dist upgrade in progress. Setting flag.') dist_upgrade_flag.touch(mode=0o660)