Use --json=v2 for listing brew casks

This commit is contained in:
Carlos Álvaro 2021-02-05 19:42:29 +01:00 committed by Megan Wilhite
parent 320588a918
commit 25d4626680
2 changed files with 6 additions and 34 deletions

1
changelog/59439.changed Normal file
View file

@ -0,0 +1 @@
Improve support for listing macOS brew casks

View file

@ -9,9 +9,7 @@ Homebrew for macOS
"""
import copy
import functools
import logging
import re
import salt.utils.data
import salt.utils.functools
@ -181,38 +179,11 @@ def list_pkgs(versions_as_list=False, **kwargs):
for name, version in combinations:
__salt__["pkg_resource.add_pkg"](ret, name, version)
# Grab packages from brew cask, if available.
# Brew Cask doesn't provide a JSON interface, must be parsed the old way.
try:
out = _call_brew("list", "--cask", "--versions")["stdout"]
for line in out.splitlines():
try:
name_and_versions = line.split(" ")
pkg_name = name_and_versions[0]
# Get cask namespace
match = re.search(
r"^From: .*/(.+?)/homebrew-(.+?)/.*$",
_call_brew("info", "--cask", pkg_name)["stdout"],
re.MULTILINE,
)
if match:
namespace = "/".join(
(match.group(1).lower(), match.group(2).lower())
)
else:
namespace = "homebrew/cask"
name = "/".join((namespace, pkg_name))
installed_versions = name_and_versions[1:]
key_func = functools.cmp_to_key(salt.utils.versions.version_cmp)
newest_version = sorted(installed_versions, key=key_func).pop()
except ValueError:
continue
__salt__["pkg_resource.add_pkg"](ret, name, newest_version)
except CommandExecutionError:
pass
for package in package_info["casks"]:
version = package["installed"]
names = [package["full_token"], package["token"]]
for name in names:
__salt__["pkg_resource.add_pkg"](ret, name, version)
__salt__["pkg_resource.sort_pkglist"](ret)
__context__["pkg.list_pkgs"] = copy.deepcopy(ret)