Skip to content

Commit

Permalink
refactor: simplify prerm to not import python
Browse files Browse the repository at this point in the history
LP: #2021988
  • Loading branch information
orndorffgrant committed Oct 13, 2023
1 parent 1356cf8 commit d826d66
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 91 deletions.
14 changes: 8 additions & 6 deletions debian/ubuntu-advantage-tools.prerm
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
set -e

remove_apt_files() {
/usr/bin/python3 -c '
from uaclient.apt import clean_apt_files
clean_apt_files()
'

# This list should be kept up to date with the list of available apt-repo-based services
for service in anbox cc-eal cis esm-apps esm-infra fips fips-preview fips-updates realtime-kernel ros ros-updates; do
rm -f /etc/apt/sources.list.d/ubuntu-${service}.list
done
# preferences are only dynamically created for fips services
for fips_service in fips fips-preview fips-updates; do
rm -f /etc/apt/preferences.d/ubuntu-${fips_service}
done
}

case "$1" in
Expand Down
38 changes: 0 additions & 38 deletions uaclient/apt.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import os
import re
import subprocess
import sys
import tempfile
from functools import lru_cache, wraps
from typing import Dict, Iterable, List, NamedTuple, Optional, Set, Union
Expand Down Expand Up @@ -661,43 +660,6 @@ def remove_apt_list_files(repo_url, series):
system.ensure_file_absent(path)


def clean_apt_files(*, _entitlements=None):
"""
Clean apt files written by uaclient
:param _entitlements:
The uaclient.entitlements module to use, defaults to
uaclient.entitlements. (This is only present for testing, because the
import happens within the function to avoid circular imports.)
"""
from uaclient.entitlements.repo import RepoEntitlement

if _entitlements is None:
from uaclient import entitlements as __entitlements

_entitlements = __entitlements

for ent_cls in _entitlements.ENTITLEMENT_CLASSES:
if not issubclass(ent_cls, RepoEntitlement):
continue
repo_file = ent_cls.repo_list_file_tmpl.format(name=ent_cls.name)
pref_file = ent_cls.repo_pref_file_tmpl.format(name=ent_cls.name)
if os.path.exists(repo_file):
event.info(
messages.APT_REMOVING_SOURCE_FILE.format(filename=repo_file),
file_type=sys.stderr,
)
system.ensure_file_absent(repo_file)
if os.path.exists(pref_file):
event.info(
messages.APT_REMOVING_PREFERENCES_FILE.format(
filename=pref_file
),
file_type=sys.stderr,
)
system.ensure_file_absent(pref_file)


def is_installed(pkg: str) -> bool:
return pkg in get_installed_packages_names()

Expand Down
47 changes: 0 additions & 47 deletions uaclient/tests/test_apt.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
add_auth_apt_repo,
add_ppa_pinning,
assert_valid_apt_credentials,
clean_apt_files,
find_apt_list_files,
get_alternative_versions_for_package,
get_apt_cache_policy,
Expand All @@ -46,7 +45,6 @@
from uaclient.entitlements.base import UAEntitlement
from uaclient.entitlements.entitlement_status import ApplicationStatus
from uaclient.entitlements.repo import RepoEntitlement
from uaclient.entitlements.tests.test_repo import RepoTestEntitlement
from uaclient.testing import fakes

POST_INSTALL_APT_CACHE_NO_UPDATES = """
Expand Down Expand Up @@ -655,51 +653,6 @@ class TestRepo(request.param):

return TestRepo

@mock.patch("os.path.exists", return_value=True)
@mock.patch("uaclient.system.ensure_file_absent")
def test_removals_for_repo_entitlements(
self, m_ensure_file_absent, _m_path_exists
):
m_entitlements = mock.Mock()
m_entitlements.ENTITLEMENT_CLASSES = [RepoTestEntitlement]

clean_apt_files(_entitlements=m_entitlements)

assert 2 == m_ensure_file_absent.call_count

def test_files_for_all_series_removed(self, mock_apt_entitlement, tmpdir):
m_entitlements = mock.Mock()
m_entitlements.ENTITLEMENT_CLASSES = [mock_apt_entitlement]

clean_apt_files(_entitlements=m_entitlements)

if mock_apt_entitlement.is_repo:
assert [] == tmpdir.listdir()
else:
assert sorted(
[tmpdir.join("source-test_ent"), tmpdir.join("pref-test_ent")]
) == sorted(tmpdir.listdir())

def test_other_files_not_removed(self, mock_apt_entitlement, tmpdir):
other_filename = "other_file-acidic"
tmpdir.join(other_filename).ensure()

m_entitlements = mock.Mock()
m_entitlements.ENTITLEMENT_CLASSES = [mock_apt_entitlement]

clean_apt_files(_entitlements=m_entitlements)

if mock_apt_entitlement.is_repo:
assert [tmpdir.join(other_filename)] == tmpdir.listdir()
else:
assert sorted(
[
tmpdir.join("source-test_ent"),
tmpdir.join("pref-test_ent"),
tmpdir.join(other_filename),
]
) == sorted(tmpdir.listdir())


@pytest.fixture(params=(mock.sentinel.default, None, "some_string"))
def remove_auth_apt_repo_kwargs(request):
Expand Down

0 comments on commit d826d66

Please sign in to comment.