2023-01-09 13:52:13 +01:00
|
|
|
From 8cd50907edeb4a1128681c30e52b2b30cf7f937d Mon Sep 17 00:00:00 2001
|
2022-01-27 16:38:33 +01:00
|
|
|
From: Alexander Graul <agraul@suse.com>
|
|
|
|
Date: Tue, 25 Jan 2022 17:08:57 +0100
|
2019-01-17 10:18:02 +01:00
|
|
|
Subject: [PATCH] Debian info_installed compatibility (#50453)
|
|
|
|
|
|
|
|
Remove unused variable
|
|
|
|
|
|
|
|
Get unit ticks installation time
|
|
|
|
|
|
|
|
Pass on unix ticks installation date time
|
|
|
|
|
|
|
|
Implement function to figure out package build time
|
|
|
|
|
|
|
|
Unify arch attribute
|
|
|
|
|
|
|
|
Add 'attr' support.
|
|
|
|
|
|
|
|
Use attr parameter in aptpkg
|
|
|
|
|
|
|
|
Add 'all_versions' output structure backward compatibility
|
|
|
|
|
|
|
|
Fix docstring
|
|
|
|
|
|
|
|
Add UT for generic test of function 'info'
|
|
|
|
|
|
|
|
Add UT for 'info' function with the parameter 'attr'
|
|
|
|
|
|
|
|
Add UT for info_installed's 'attr' param
|
|
|
|
|
|
|
|
Fix docstring
|
|
|
|
|
|
|
|
Add returned type check
|
|
|
|
|
|
|
|
Add UT for info_installed with 'all_versions=True' output structure
|
|
|
|
|
|
|
|
Refactor UT for 'owner' function
|
|
|
|
|
|
|
|
Refactor UT: move to decorators, add more checks
|
|
|
|
|
|
|
|
Schedule TODO for next refactoring of UT 'show' function
|
|
|
|
|
|
|
|
Refactor UT: get rid of old assertion way, flatten tests
|
|
|
|
|
|
|
|
Refactor UT: move to native assertions, cleanup noise, flatten complexity for better visibility what is tested
|
|
|
|
|
|
|
|
Lintfix: too many empty lines
|
|
|
|
|
|
|
|
Adjust architecture getter according to the lowpkg info
|
|
|
|
|
|
|
|
Fix wrong Git merge: missing function signature
|
2021-09-27 12:08:32 +02:00
|
|
|
|
|
|
|
Reintroducing reverted changes
|
|
|
|
|
|
|
|
Reintroducing changes from commit e20362f6f053eaa4144583604e6aac3d62838419
|
|
|
|
that got partially reverted by this commit:
|
|
|
|
https://github.com/openSUSE/salt/commit/d0ef24d113bdaaa29f180031b5da384cffe08c64#diff-820e6ce667fe3afddbc1b9cf1682fdef
|
2019-01-17 10:18:02 +01:00
|
|
|
---
|
2022-01-27 16:38:33 +01:00
|
|
|
salt/modules/aptpkg.py | 24 ++++-
|
|
|
|
salt/modules/dpkg_lowpkg.py | 108 ++++++++++++++++++----
|
|
|
|
tests/pytests/unit/modules/test_aptpkg.py | 52 +++++++++++
|
|
|
|
3 files changed, 166 insertions(+), 18 deletions(-)
|
2019-01-17 10:18:02 +01:00
|
|
|
|
|
|
|
diff --git a/salt/modules/aptpkg.py b/salt/modules/aptpkg.py
|
2023-01-09 13:52:13 +01:00
|
|
|
index 0cbd611b71..4a2281c47f 100644
|
2019-01-17 10:18:02 +01:00
|
|
|
--- a/salt/modules/aptpkg.py
|
|
|
|
+++ b/salt/modules/aptpkg.py
|
2023-01-09 13:52:13 +01:00
|
|
|
@@ -3458,6 +3458,15 @@ def info_installed(*names, **kwargs):
|
2019-01-17 10:18:02 +01:00
|
|
|
|
|
|
|
.. versionadded:: 2016.11.3
|
|
|
|
|
|
|
|
+ attr
|
|
|
|
+ Comma-separated package attributes. If no 'attr' is specified, all available attributes returned.
|
|
|
|
+
|
|
|
|
+ Valid attributes are:
|
|
|
|
+ version, vendor, release, build_date, build_date_time_t, install_date, install_date_time_t,
|
|
|
|
+ build_host, group, source_rpm, arch, epoch, size, license, signature, packager, url, summary, description.
|
|
|
|
+
|
|
|
|
+ .. versionadded:: Neon
|
|
|
|
+
|
2021-09-27 12:08:32 +02:00
|
|
|
CLI Example:
|
2019-01-17 10:18:02 +01:00
|
|
|
|
|
|
|
.. code-block:: bash
|
2023-01-09 13:52:13 +01:00
|
|
|
@@ -3468,11 +3477,19 @@ def info_installed(*names, **kwargs):
|
2021-01-08 13:41:50 +01:00
|
|
|
"""
|
2019-01-17 10:18:02 +01:00
|
|
|
kwargs = salt.utils.args.clean_kwargs(**kwargs)
|
2021-01-08 13:41:50 +01:00
|
|
|
failhard = kwargs.pop("failhard", True)
|
|
|
|
+ kwargs.pop("errors", None) # Only for compatibility with RPM
|
|
|
|
+ attr = kwargs.pop("attr", None) # Package attributes to return
|
|
|
|
+ all_versions = kwargs.pop(
|
|
|
|
+ "all_versions", False
|
|
|
|
+ ) # This is for backward compatible structure only
|
2019-01-17 10:18:02 +01:00
|
|
|
+
|
|
|
|
if kwargs:
|
|
|
|
salt.utils.args.invalid_kwargs(kwargs)
|
|
|
|
|
|
|
|
ret = dict()
|
2021-01-08 13:41:50 +01:00
|
|
|
- for pkg_name, pkg_nfo in __salt__["lowpkg.info"](*names, failhard=failhard).items():
|
|
|
|
+ for pkg_name, pkg_nfo in __salt__["lowpkg.info"](
|
|
|
|
+ *names, failhard=failhard, attr=attr
|
|
|
|
+ ).items():
|
2019-01-17 10:18:02 +01:00
|
|
|
t_nfo = dict()
|
2021-01-08 13:41:50 +01:00
|
|
|
if pkg_nfo.get("status", "ii")[1] != "i":
|
|
|
|
continue # return only packages that are really installed
|
2023-01-09 13:52:13 +01:00
|
|
|
@@ -3493,7 +3510,10 @@ def info_installed(*names, **kwargs):
|
2019-01-17 10:18:02 +01:00
|
|
|
else:
|
|
|
|
t_nfo[key] = value
|
|
|
|
|
|
|
|
- ret[pkg_name] = t_nfo
|
|
|
|
+ if all_versions:
|
|
|
|
+ ret.setdefault(pkg_name, []).append(t_nfo)
|
|
|
|
+ else:
|
|
|
|
+ ret[pkg_name] = t_nfo
|
|
|
|
|
|
|
|
return ret
|
|
|
|
|
2019-04-12 11:57:21 +02:00
|
|
|
diff --git a/salt/modules/dpkg_lowpkg.py b/salt/modules/dpkg_lowpkg.py
|
2021-09-27 12:08:32 +02:00
|
|
|
index 6a88573a8f..afbd619490 100644
|
2019-04-12 11:57:21 +02:00
|
|
|
--- a/salt/modules/dpkg_lowpkg.py
|
|
|
|
+++ b/salt/modules/dpkg_lowpkg.py
|
2021-09-27 12:08:32 +02:00
|
|
|
@@ -234,6 +234,44 @@ def file_dict(*packages, **kwargs):
|
2021-01-08 13:41:50 +01:00
|
|
|
return {"errors": errors, "packages": ret}
|
2019-01-17 10:18:02 +01:00
|
|
|
|
|
|
|
|
|
|
|
+def _get_pkg_build_time(name):
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ Get package build time, if possible.
|
|
|
|
+
|
|
|
|
+ :param name:
|
|
|
|
+ :return:
|
2021-01-08 13:41:50 +01:00
|
|
|
+ """
|
2019-01-17 10:18:02 +01:00
|
|
|
+ iso_time = iso_time_t = None
|
2021-01-08 13:41:50 +01:00
|
|
|
+ changelog_dir = os.path.join("/usr/share/doc", name)
|
2019-01-17 10:18:02 +01:00
|
|
|
+ if os.path.exists(changelog_dir):
|
|
|
|
+ for fname in os.listdir(changelog_dir):
|
|
|
|
+ try:
|
|
|
|
+ iso_time_t = int(os.path.getmtime(os.path.join(changelog_dir, fname)))
|
2021-01-08 13:41:50 +01:00
|
|
|
+ iso_time = (
|
|
|
|
+ datetime.datetime.utcfromtimestamp(iso_time_t).isoformat() + "Z"
|
|
|
|
+ )
|
2019-01-17 10:18:02 +01:00
|
|
|
+ break
|
|
|
|
+ except OSError:
|
|
|
|
+ pass
|
|
|
|
+
|
|
|
|
+ # Packager doesn't care about Debian standards, therefore Plan B: brute-force it.
|
|
|
|
+ if not iso_time:
|
2021-01-08 13:41:50 +01:00
|
|
|
+ for pkg_f_path in __salt__["cmd.run"](
|
|
|
|
+ "dpkg-query -L {}".format(name)
|
|
|
|
+ ).splitlines():
|
|
|
|
+ if "changelog" in pkg_f_path.lower() and os.path.exists(pkg_f_path):
|
2019-01-17 10:18:02 +01:00
|
|
|
+ try:
|
|
|
|
+ iso_time_t = int(os.path.getmtime(pkg_f_path))
|
2021-01-08 13:41:50 +01:00
|
|
|
+ iso_time = (
|
|
|
|
+ datetime.datetime.utcfromtimestamp(iso_time_t).isoformat() + "Z"
|
|
|
|
+ )
|
2019-01-17 10:18:02 +01:00
|
|
|
+ break
|
|
|
|
+ except OSError:
|
|
|
|
+ pass
|
|
|
|
+
|
|
|
|
+ return iso_time, iso_time_t
|
|
|
|
+
|
|
|
|
+
|
|
|
|
def _get_pkg_info(*packages, **kwargs):
|
2021-01-08 13:41:50 +01:00
|
|
|
"""
|
2019-01-17 10:18:02 +01:00
|
|
|
Return list of package information. If 'packages' parameter is empty,
|
2021-09-27 12:08:32 +02:00
|
|
|
@@ -257,7 +295,7 @@ def _get_pkg_info(*packages, **kwargs):
|
2021-01-08 13:41:50 +01:00
|
|
|
cmd = (
|
|
|
|
"dpkg-query -W -f='package:" + bin_var + "\\n"
|
|
|
|
"revision:${binary:Revision}\\n"
|
|
|
|
- "architecture:${Architecture}\\n"
|
|
|
|
+ "arch:${Architecture}\\n"
|
|
|
|
"maintainer:${Maintainer}\\n"
|
|
|
|
"summary:${Summary}\\n"
|
|
|
|
"source:${source:Package}\\n"
|
2021-09-27 12:08:32 +02:00
|
|
|
@@ -296,9 +334,16 @@ def _get_pkg_info(*packages, **kwargs):
|
2019-01-17 10:18:02 +01:00
|
|
|
key, value = pkg_info_line.split(":", 1)
|
|
|
|
if value:
|
|
|
|
pkg_data[key] = value
|
2021-01-08 13:41:50 +01:00
|
|
|
- install_date = _get_pkg_install_time(pkg_data.get("package"))
|
2019-01-17 10:18:02 +01:00
|
|
|
- if install_date:
|
2021-01-08 13:41:50 +01:00
|
|
|
- pkg_data["install_date"] = install_date
|
|
|
|
+ install_date, install_date_t = _get_pkg_install_time(
|
|
|
|
+ pkg_data.get("package"), pkg_data.get("arch")
|
|
|
|
+ )
|
2019-01-17 10:18:02 +01:00
|
|
|
+ if install_date:
|
2021-01-08 13:41:50 +01:00
|
|
|
+ pkg_data["install_date"] = install_date
|
|
|
|
+ pkg_data["install_date_time_t"] = install_date_t # Unix ticks
|
|
|
|
+ build_date, build_date_t = _get_pkg_build_time(pkg_data.get("package"))
|
2019-01-17 10:18:02 +01:00
|
|
|
+ if build_date:
|
2021-01-08 13:41:50 +01:00
|
|
|
+ pkg_data["build_date"] = build_date
|
|
|
|
+ pkg_data["build_date_time_t"] = build_date_t
|
|
|
|
pkg_data["description"] = pkg_descr.split(":", 1)[-1]
|
2019-01-17 10:18:02 +01:00
|
|
|
ret.append(pkg_data)
|
|
|
|
|
2021-09-27 12:08:32 +02:00
|
|
|
@@ -324,24 +369,34 @@ def _get_pkg_license(pkg):
|
2019-01-17 10:18:02 +01:00
|
|
|
return ", ".join(sorted(licenses))
|
|
|
|
|
|
|
|
|
|
|
|
-def _get_pkg_install_time(pkg):
|
|
|
|
+def _get_pkg_install_time(pkg, arch):
|
2021-01-08 13:41:50 +01:00
|
|
|
"""
|
2019-01-17 10:18:02 +01:00
|
|
|
Return package install time, based on the /var/lib/dpkg/info/<package>.list
|
|
|
|
|
|
|
|
:return:
|
2021-01-08 13:41:50 +01:00
|
|
|
"""
|
2019-01-17 10:18:02 +01:00
|
|
|
- iso_time = None
|
|
|
|
+ iso_time = iso_time_t = None
|
2021-01-08 13:41:50 +01:00
|
|
|
+ loc_root = "/var/lib/dpkg/info"
|
2019-01-17 10:18:02 +01:00
|
|
|
if pkg is not None:
|
2021-01-08 13:41:50 +01:00
|
|
|
- location = "/var/lib/dpkg/info/{}.list".format(pkg)
|
2019-01-17 10:18:02 +01:00
|
|
|
- if os.path.exists(location):
|
2021-01-08 13:41:50 +01:00
|
|
|
- iso_time = (
|
|
|
|
- datetime.datetime.utcfromtimestamp(
|
|
|
|
- int(os.path.getmtime(location))
|
|
|
|
- ).isoformat()
|
|
|
|
- + "Z"
|
|
|
|
- )
|
2019-01-17 10:18:02 +01:00
|
|
|
+ locations = []
|
2021-01-08 13:41:50 +01:00
|
|
|
+ if arch is not None and arch != "all":
|
|
|
|
+ locations.append(os.path.join(loc_root, "{}:{}.list".format(pkg, arch)))
|
|
|
|
|
|
|
|
- return iso_time
|
|
|
|
+ locations.append(os.path.join(loc_root, "{}.list".format(pkg)))
|
2019-01-17 10:18:02 +01:00
|
|
|
+ for location in locations:
|
|
|
|
+ try:
|
|
|
|
+ iso_time_t = int(os.path.getmtime(location))
|
2021-01-08 13:41:50 +01:00
|
|
|
+ iso_time = (
|
|
|
|
+ datetime.datetime.utcfromtimestamp(iso_time_t).isoformat() + "Z"
|
|
|
|
+ )
|
2019-01-17 10:18:02 +01:00
|
|
|
+ break
|
|
|
|
+ except OSError:
|
|
|
|
+ pass
|
2021-01-08 13:41:50 +01:00
|
|
|
+
|
2019-01-17 10:18:02 +01:00
|
|
|
+ if iso_time is None:
|
|
|
|
+ log.debug('Unable to get package installation time for package "%s".', pkg)
|
|
|
|
+
|
|
|
|
+ return iso_time, iso_time_t
|
|
|
|
|
|
|
|
|
|
|
|
def _get_pkg_ds_avail():
|
2021-09-27 12:08:32 +02:00
|
|
|
@@ -391,6 +446,15 @@ def info(*packages, **kwargs):
|
2019-01-17 10:18:02 +01:00
|
|
|
|
|
|
|
.. versionadded:: 2016.11.3
|
|
|
|
|
|
|
|
+ attr
|
|
|
|
+ Comma-separated package attributes. If no 'attr' is specified, all available attributes returned.
|
|
|
|
+
|
|
|
|
+ Valid attributes are:
|
|
|
|
+ version, vendor, release, build_date, build_date_time_t, install_date, install_date_time_t,
|
|
|
|
+ build_host, group, source_rpm, arch, epoch, size, license, signature, packager, url, summary, description.
|
|
|
|
+
|
|
|
|
+ .. versionadded:: Neon
|
|
|
|
+
|
2021-09-27 12:08:32 +02:00
|
|
|
CLI Example:
|
2019-01-17 10:18:02 +01:00
|
|
|
|
|
|
|
.. code-block:: bash
|
2021-09-27 12:08:32 +02:00
|
|
|
@@ -405,6 +469,10 @@ def info(*packages, **kwargs):
|
2019-01-17 10:18:02 +01:00
|
|
|
|
|
|
|
kwargs = salt.utils.args.clean_kwargs(**kwargs)
|
2021-01-08 13:41:50 +01:00
|
|
|
failhard = kwargs.pop("failhard", True)
|
|
|
|
+ attr = kwargs.pop("attr", None) or None
|
2019-01-17 10:18:02 +01:00
|
|
|
+ if attr:
|
2021-01-08 13:41:50 +01:00
|
|
|
+ attr = attr.split(",")
|
2019-01-17 10:18:02 +01:00
|
|
|
+
|
|
|
|
if kwargs:
|
|
|
|
salt.utils.args.invalid_kwargs(kwargs)
|
|
|
|
|
2021-09-27 12:08:32 +02:00
|
|
|
@@ -432,6 +500,14 @@ def info(*packages, **kwargs):
|
2021-01-08 13:41:50 +01:00
|
|
|
lic = _get_pkg_license(pkg["package"])
|
2019-01-17 10:18:02 +01:00
|
|
|
if lic:
|
2021-01-08 13:41:50 +01:00
|
|
|
pkg["license"] = lic
|
|
|
|
- ret[pkg["package"]] = pkg
|
2019-01-17 10:18:02 +01:00
|
|
|
+
|
|
|
|
+ # Remove keys that aren't in attrs
|
2021-01-08 13:41:50 +01:00
|
|
|
+ pkg_name = pkg["package"]
|
2019-01-17 10:18:02 +01:00
|
|
|
+ if attr:
|
|
|
|
+ for k in list(pkg.keys())[:]:
|
|
|
|
+ if k not in attr:
|
|
|
|
+ del pkg[k]
|
|
|
|
+
|
|
|
|
+ ret[pkg_name] = pkg
|
|
|
|
|
|
|
|
return ret
|
2022-01-27 16:38:33 +01:00
|
|
|
diff --git a/tests/pytests/unit/modules/test_aptpkg.py b/tests/pytests/unit/modules/test_aptpkg.py
|
2023-01-09 13:52:13 +01:00
|
|
|
index 6ef27e2d29..8e404a673c 100644
|
2022-01-27 16:38:33 +01:00
|
|
|
--- a/tests/pytests/unit/modules/test_aptpkg.py
|
|
|
|
+++ b/tests/pytests/unit/modules/test_aptpkg.py
|
2023-01-09 13:52:13 +01:00
|
|
|
@@ -359,6 +359,58 @@ def test_info_installed(lowpkg_info_var):
|
2022-01-27 16:38:33 +01:00
|
|
|
assert len(aptpkg.info_installed()) == 1
|
|
|
|
|
|
|
|
|
|
|
|
+def test_info_installed_attr(lowpkg_info_var):
|
|
|
|
+ """
|
|
|
|
+ Test info_installed 'attr'.
|
|
|
|
+ This doesn't test 'attr' behaviour per se, since the underlying function is in dpkg.
|
|
|
|
+ The test should simply not raise exceptions for invalid parameter.
|
2021-09-27 12:08:32 +02:00
|
|
|
+
|
2022-01-27 16:38:33 +01:00
|
|
|
+ :return:
|
|
|
|
+ """
|
|
|
|
+ expected_pkg = {
|
|
|
|
+ "url": "http://www.gnu.org/software/wget/",
|
|
|
|
+ "packager": "Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>",
|
2021-09-27 12:08:32 +02:00
|
|
|
+ "name": "wget",
|
2022-01-27 16:38:33 +01:00
|
|
|
+ "install_date": "2016-08-30T22:20:15Z",
|
|
|
|
+ "description": "retrieves files from the web",
|
2021-09-27 12:08:32 +02:00
|
|
|
+ "version": "1.15-1ubuntu1.14.04.2",
|
|
|
|
+ "architecture": "amd64",
|
2022-01-27 16:38:33 +01:00
|
|
|
+ "group": "web",
|
|
|
|
+ "source": "wget",
|
|
|
|
+ }
|
|
|
|
+ mock = MagicMock(return_value=lowpkg_info_var)
|
|
|
|
+ with patch.dict(aptpkg.__salt__, {"lowpkg.info": mock}):
|
|
|
|
+ ret = aptpkg.info_installed("wget", attr="foo,bar")
|
|
|
|
+ assert ret["wget"] == expected_pkg
|
2021-09-27 12:08:32 +02:00
|
|
|
+
|
|
|
|
+
|
2022-01-27 16:38:33 +01:00
|
|
|
+def test_info_installed_all_versions(lowpkg_info_var):
|
2021-09-27 12:08:32 +02:00
|
|
|
+ """
|
2022-01-27 16:38:33 +01:00
|
|
|
+ Test info_installed 'all_versions'.
|
|
|
|
+ Since Debian won't return same name packages with the different names,
|
|
|
|
+ this should just return different structure, backward compatible with
|
|
|
|
+ the RPM equivalents.
|
2021-09-27 12:08:32 +02:00
|
|
|
+
|
2022-01-27 16:38:33 +01:00
|
|
|
+ :return:
|
2021-09-27 12:08:32 +02:00
|
|
|
+ """
|
2022-01-27 16:38:33 +01:00
|
|
|
+ expected_pkg = {
|
|
|
|
+ "url": "http://www.gnu.org/software/wget/",
|
|
|
|
+ "packager": "Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>",
|
|
|
|
+ "name": "wget",
|
|
|
|
+ "install_date": "2016-08-30T22:20:15Z",
|
|
|
|
+ "description": "retrieves files from the web",
|
|
|
|
+ "version": "1.15-1ubuntu1.14.04.2",
|
|
|
|
+ "architecture": "amd64",
|
|
|
|
+ "group": "web",
|
|
|
|
+ "source": "wget",
|
2019-01-17 10:18:02 +01:00
|
|
|
+ }
|
2022-01-27 16:38:33 +01:00
|
|
|
+ mock = MagicMock(return_value=lowpkg_info_var)
|
|
|
|
+ with patch.dict(aptpkg.__salt__, {"lowpkg.info": mock}):
|
|
|
|
+ ret = aptpkg.info_installed("wget", all_versions=True)
|
2019-01-17 10:18:02 +01:00
|
|
|
+ assert isinstance(ret, dict)
|
2022-01-27 16:38:33 +01:00
|
|
|
+ assert ret["wget"] == [expected_pkg]
|
2019-01-17 10:18:02 +01:00
|
|
|
+
|
|
|
|
+
|
2022-01-27 16:38:33 +01:00
|
|
|
def test_owner():
|
|
|
|
"""
|
|
|
|
Test - Return the name of the package that owns the file.
|
2019-01-17 10:18:02 +01:00
|
|
|
--
|
2023-01-09 13:52:13 +01:00
|
|
|
2.37.3
|
2019-01-17 10:18:02 +01:00
|
|
|
|
|
|
|
|