Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/70317.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add list_repo_pkgs to modules/pkgng.py
63 changes: 63 additions & 0 deletions salt/modules/pkgng.py
Original file line number Diff line number Diff line change
Expand Up @@ -2326,6 +2326,69 @@ def list_upgrades(refresh=True, **kwargs):
}


def list_repo_pkgs(*args, **kwargs):
"""
Returns all available packages. Optionally, package names (and name globs)
can be passed and the results will be filtered to packages matching those
names.

This function can be helpful in discovering the version or repo to specify
in a :mod:`pkg.installed <salt.states.pkg.installed>` state.

.. code-block:: python

{
'bash': ['5.3.20',
'5.3.20']
'nginx': ['1.30.5,3']
}

CLI Examples:

.. code-block:: bash

salt '*' pkg.list_repo_pkgs
salt '*' pkg.list_repo_pkgs foo bar baz
salt '*' pkg.list_repo_pkgs jail=<jail name or id>
salt '*' pkg.list_repo_pkgs jail=<jail name or id> foo bar baz
"""
jail = kwargs.get("jail")
chroot = kwargs.get("chroot")
root = kwargs.get("root")

cmds = []
if args:
# Get only information about packages in args
for arg in args:
cmd = ["search", "-q", "-S", "name"]
if "*" in arg:
cmd = cmd + ["-g"]
cmd = cmd + [arg]
cmds.append(cmd)

else:
cmd = ["search", "-q", "-g", "*"]
cmds.append(cmd)

ret = {}

for cmd in cmds:
out = __salt__["cmd.run_stdout"](
_pkg(jail=jail, chroot=chroot, root=root) + cmd,
output_loglevel="trace",
python_shell=False
)

for line in salt.utils.itertools.split(out, "\n"):
line = line.strip()
if not line or "-" not in line:
continue
pkg, version = line.rsplit("-", 1)
ret.setdefault(pkg, []).append(version)

return ret


def _parse_upgrade(stdout):
"""
Parse the output from the ``pkg upgrade --dry-run`` command
Expand Down
78 changes: 78 additions & 0 deletions tests/pytests/unit/modules/test_pkgng.py
Original file line number Diff line number Diff line change
Expand Up @@ -994,3 +994,81 @@ def test_which_with_origin_flag():
output_loglevel="trace",
python_shell=False,
)


def test_list_repo_pkgs():
"""
Test pkgng.list_repo_pkgs with argument
"""
list_repo_pkgs_cmd = MagicMock(return_value="vim-9.2.0738")
with patch.dict(pkgng.__salt__, {"cmd.run_stdout": list_repo_pkgs_cmd}):

result = pkgng.list_repo_pkgs("vim")
assert result
list_repo_pkgs_cmd.assert_called_with(
["pkg", "search", "-q", "-S", "name", "vim"],
output_loglevel="trace",
python_shell=False,
)
assert isinstance(result, dict)


def test_list_repo_pkgs_multiple_args():
"""
Test pkgng.list_repo_pkgs with multiple arguments
"""
list_repo_pkgs_cmd = MagicMock(
side_effect=[
"vim-9.2.0738",
"nginx-1.30.5,3"
]
)
with patch.dict(pkgng.__salt__, {"cmd.run_stdout": list_repo_pkgs_cmd}):

result = pkgng.list_repo_pkgs("nginx", "vim")
list_repo_pkgs_cmd.assert_called_with(
["pkg", "search", "-q", "-S", "name", "vim"],
output_loglevel="trace",
python_shell=False,
)
assert {'vim': ['9.2.0738'], 'nginx': ['1.30.5,3']} == result


def test_list_repo_pkgs_arg_glob():
"""
Test pkgng.list_repo_pkgs with glob argument
"""

list_repo_pkgs_cmd = MagicMock(
side_effect=[
"bash-5.3.20",
"bash-completion-zfs-2.4.1",
]
)
with patch.dict(pkgng.__salt__, {"cmd.run_stdout": list_repo_pkgs_cmd}):

result = pkgng.list_repo_pkgs("bash*")
list_repo_pkgs_cmd.assert_called_with(
["pkg", "search", "-q", "-S", "name", "-g", "bash*"],
output_loglevel="trace",
python_shell=False,
)
assert {'bash': ['5.3.20']} == result
result = pkgng.list_repo_pkgs("bash*")
assert {'bash-completion-zfs': ['2.4.1']} == result


def test_list_repo_pkgs_unavailable():
"""
Test pkgng.list_repo_pkgs when nothing is returned
"""
list_repo_pkgs_cmd = MagicMock(return_value="")
with patch.dict(pkgng.__salt__, {"cmd.run_stdout": list_repo_pkgs_cmd}):

result = pkgng.list_repo_pkgs("vim")
assert not result
list_repo_pkgs_cmd.assert_called_with(
["pkg", "search", "-q", "-S", "name", "vim"],
output_loglevel="trace",
python_shell=False,
)
Loading