Skip to content

Commit

Permalink
fixes saltstack#65220 add wildcard removal for aptpkg
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholasmhughes committed Sep 18, 2023
1 parent c5936f9 commit 8fe4892
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 18 deletions.
1 change: 1 addition & 0 deletions changelog/65220.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add ability to remove packages by wildcard via apt execution module
4 changes: 2 additions & 2 deletions salt/modules/aptpkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -1057,9 +1057,9 @@ def _uninstall(action="remove", name=None, pkgs=None, **kwargs):

old = list_pkgs()
old_removed = list_pkgs(removed=True)
targets = [x for x in pkg_params if x in old]
targets = salt.utils.pkg.match_wildcard(old, pkg_params)
if action == "purge":
targets.extend([x for x in pkg_params if x in old_removed])
targets.extend(salt.utils.pkg.match_wildcard(old_removed, pkg_params))
if not targets:
return {}
cmd = ["apt-get", "-q", "-y", action]
Expand Down
17 changes: 1 addition & 16 deletions salt/modules/yumpkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -2153,22 +2153,7 @@ def remove(name=None, pkgs=None, **kwargs): # pylint: disable=W0613
old = list_pkgs()
targets = []

# Loop through pkg_params looking for any
# which contains a wildcard and get the
# real package names from the packages
# which are currently installed.
pkg_matches = {}
for pkg_param in list(pkg_params):
if "*" in pkg_param:
pkg_matches = {
x: pkg_params[pkg_param] for x in old if fnmatch.fnmatch(x, pkg_param)
}

# Remove previous pkg_param
pkg_params.pop(pkg_param)

# Update pkg_params with the matches
pkg_params.update(pkg_matches)
pkg_params = salt.utils.pkg.match_wildcard(old, pkg_params)

for target in pkg_params:
if target not in old:
Expand Down
31 changes: 31 additions & 0 deletions salt/utils/pkg/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""

import errno
import fnmatch
import logging
import os
import re
Expand Down Expand Up @@ -102,3 +103,33 @@ def check_bundled():
if getattr(sys, "frozen", False) and hasattr(sys, "_MEIPASS"):
return True
return False


def match_wildcard(current_pkgs, pkg_params):
"""
Loop through pkg_params looking for any which contains a wildcard and get
the real package names from the packages which are currently installed.
current_pkgs
List of currently installed packages as output by ``list_pkgs``
pkg_params
List of packages as processed by ``pkg_resource.parse_targets``
"""
pkg_matches = {}

for pkg_param in list(pkg_params):
if "*" in pkg_param:
pkg_matches = {
pkg: pkg_params[pkg_param]
for pkg in current_pkgs
if fnmatch.fnmatch(pkg, pkg_param)
}

# Remove previous pkg_param
pkg_params.pop(pkg_param)

# Update pkg_params with the matches
pkg_params.update(pkg_matches)

return pkg_params
30 changes: 30 additions & 0 deletions tests/pytests/unit/utils/test_pkg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import pytest

import salt.utils.pkg

CURRENT_PKGS = {
"acl": "2.2.53-4",
"adduser": "3.118",
"apparmor": "2.13.2-10",
"apt": "1.8.2.3",
"apt-listchanges": "3.19",
"apt-transport-https": "1.8.2.3",
"apt-utils": "1.8.2.3",
"base-files": "10.3+deb10u13",
"base-passwd": "3.5.46",
"bash": "5.0-4",
"bash-completion": "1:2.8-6",
}


@pytest.mark.parametrize(
"current_pkgs,pkg_params,expected",
[
[CURRENT_PKGS, {"apt": ""}, {"apt": ""}],
[CURRENT_PKGS, {"foo": ""}, {"foo": ""}],
[CURRENT_PKGS, {"bash*": ""}, {"bash": "", "bash-completion": ""}],
],
)
def test_match_wildcard(current_pkgs, pkg_params, expected):
result = salt.utils.pkg.match_wildcard(current_pkgs, pkg_params)
assert result == expected

0 comments on commit 8fe4892

Please sign in to comment.