*: Use action_utils.run instead of subprocess.check_output

- This is to capture stdout and stderr and transmit that from privileged daemon
back to the service to be displayed in HTML.

Tests:

- Unit tests and code checks pass.

- Some of the modified actions work as expected.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: Veiko Aasa <veiko17@disroot.org>
This commit is contained in:
Sunil Mohan Adapa authored and Veiko Aasa committed 2025-09-29 16:58:56 +03:00
1 parent bf9005ac48
commit b253166f6d
17 files changed
+101 -72

No files matched your search

+9
View File
@@ -351,6 +351,15 @@ def is_backports_enabled():
return os.path.exists(privileged.BACKPORTS_SOURCES_LIST)
def get_current_release():
"""Return current release and codename as a tuple."""
output = action_utils.run(
['lsb_release', '--release', '--codename', '--short'],
check=True).stdout.decode().strip()
lines = output.split('\n')
return lines[0], lines[1]
def is_backports_current():
"""Return whether backports are enabled for the current release."""
if not is_backports_enabled():
+2 -1
View File
@@ -128,7 +128,8 @@ def release_held_packages():
'holds.')
return
output = subprocess.check_output(['apt-mark', 'showhold']).decode().strip()
output = action_utils.run(['apt-mark', 'showhold'],
check=True).stdout.decode().strip()
holds = output.split('\n')
logger.info('Releasing package holds: %s', holds)
action_utils.run(['apt-mark', 'unhold', *holds], stdout=subprocess.DEVNULL,
@@ -7,7 +7,7 @@ import re
import subprocess
from datetime import datetime as datetime_original
from datetime import timezone
from unittest.mock import call, patch
from unittest.mock import Mock, call, patch
import pytest
@@ -264,28 +264,40 @@ def test_services_disable(service_is_running, service_disable, service_enable):
@patch('subprocess.run')
@patch('subprocess.check_output')
def test_apt_hold_packages(check_output, run, tmp_path):
def test_apt_hold_packages(run, tmp_path):
"""Test that holding apt packages works."""
def _run(command, **kwargs):
if 'showhold' in command:
return Mock(stdout=False)
return Mock(returncode=0)
hold_flag = tmp_path / 'flag'
run.return_value.returncode = 0
run.side_effect = _run
with patch('plinth.action_utils.apt_hold_flag', hold_flag), \
patch('plinth.modules.upgrades.distupgrade.PACKAGES_WITH_PROMPTS',
['package1', 'package2']):
check_output.return_value = False
with distupgrade._apt_hold_packages():
assert hold_flag.exists()
assert hold_flag.stat().st_mode & 0o117 == 0
expected_calls = [
call(['apt-mark', 'hold', 'freedombox'],
call(['apt-mark', 'showhold', 'freedombox'], check=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE),
call(['apt-mark', 'hold', 'freedombox'], check=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE),
call(['apt-mark', 'showhold', 'package1'],
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
check=True),
call(['apt-mark', 'hold', 'package1'], stdout=subprocess.PIPE,
stderr=subprocess.PIPE, check=False),
call(['apt-mark', 'hold', 'package2'], stdout=subprocess.PIPE,
stderr=subprocess.PIPE, check=False)
call(['apt-mark', 'hold', 'package1'], check=False,
stdout=subprocess.PIPE, stderr=subprocess.PIPE),
call(['apt-mark', 'showhold', 'package2'],
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
check=True),
call(['apt-mark', 'hold', 'package2'], check=False,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
]
assert run.call_args_list == expected_calls
assert run.mock_calls == expected_calls
run.reset_mock()
expected_call = [
+3 -3
View File
@@ -64,10 +64,10 @@ deb http://deb.debian.org/debian trixie main
assert utils.get_sources_list_codename() == 'testing'
@patch('subprocess.check_output')
def test_get_current_release(check_output):
@patch('subprocess.run')
def test_get_current_release(run):
"""Test that getting current release works."""
check_output.return_value = b'test-release\ntest-codename\n\n'
run.return_value.stdout = b'test-release\ntest-codename\n\n'
assert utils.get_current_release() == ('test-release', 'test-codename')
+6 -6
View File
@@ -3,10 +3,10 @@
import pathlib
import re
import subprocess
import augeas
from plinth import action_utils
from plinth.modules.apache.components import check_url
RELEASE_FILE_URL = \
@@ -23,7 +23,7 @@ def check_auto() -> bool:
'apt-config', 'shell', 'UpdateInterval',
'APT::Periodic::Update-Package-Lists'
]
output = subprocess.check_output(arguments).decode()
output = action_utils.run(arguments, check=True).stdout.decode()
update_interval = 0
match = re.match(r"UpdateInterval='(.*)'", output)
if match:
@@ -62,7 +62,7 @@ def is_release_file_available(protocol: str, dist: str,
def is_sufficient_free_space() -> bool:
"""Return whether there is sufficient free space for dist upgrade."""
output = subprocess.check_output(['df', '--output=avail', '/'])
output = action_utils.run(['df', '--output=avail', '/'], check=True).stdout
free_space = int(output.decode().split('\n')[1])
return free_space >= DIST_UPGRADE_REQUIRED_FREE_SPACE
@@ -100,9 +100,9 @@ def get_sources_list_codename() -> str | None:
def get_current_release():
"""Return current release and codename as a tuple."""
output = subprocess.check_output(
['lsb_release', '--release', '--codename',
'--short']).decode().strip()
output = action_utils.run(
['lsb_release', '--release', '--codename', '--short'],
check=True).stdout.decode().strip()
lines = output.split('\n')
return lines[0], lines[1]