dnf - fix whatprovides API block (#74764)

* Added additional query block
This commit is contained in:
Abhijeet Kasurde 2021-05-24 22:32:28 +05:30 committed by GitHub
parent 12f4b0db04
commit 52430d4228
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 8 deletions

View file

@ -0,0 +1,2 @@
bugfixes:
- dnf - refactor code to use `dnf whatprovides` API (https://github.com/ansible/ansible/issues/73503).

View file

@ -26,6 +26,8 @@ options:
You can also pass a url or a local path to a rpm file. You can also pass a url or a local path to a rpm file.
To operate on several packages this can accept a comma separated string of packages or a list of packages." To operate on several packages this can accept a comma separated string of packages or a list of packages."
- Comparison operators for package version are valid here C(>), C(<), C(>=), C(<=). Example - C(name>=1.0) - Comparison operators for package version are valid here C(>), C(<), C(>=), C(<=). Example - C(name>=1.0)
- You can also pass an absolute path for a binary which is provided by the package to install.
See examples for more information.
required: true required: true
aliases: aliases:
- pkg - pkg
@ -295,6 +297,11 @@ EXAMPLES = '''
name: /usr/local/src/nginx-release-centos-6-0.el6.ngx.noarch.rpm name: /usr/local/src/nginx-release-centos-6-0.el6.ngx.noarch.rpm
state: present state: present
- name: Install Package based upon the file it provides
dnf:
name: /usr/bin/cowsay
state: present
- name: Install the 'Development tools' package group - name: Install the 'Development tools' package group
dnf: dnf:
name: '@Development tools' name: '@Development tools'
@ -810,8 +817,12 @@ class DnfModule(YumDnf):
} }
def _whatprovides(self, filepath): def _whatprovides(self, filepath):
self.base.read_all_repos()
available = self.base.sack.query().available() available = self.base.sack.query().available()
pkg_spec = available.filter(provides=filepath).run() # Search in file
files_filter = available.filter(file=filepath)
# And Search in provides
pkg_spec = files_filter.union(available.filter(provides=filepath)).run()
if pkg_spec: if pkg_spec:
return pkg_spec[0].name return pkg_spec[0].name
@ -826,14 +837,13 @@ class DnfModule(YumDnf):
filenames.append(name) filenames.append(name)
elif name.endswith(".rpm"): elif name.endswith(".rpm"):
filenames.append(name) filenames.append(name)
elif name.startswith("@") or ('/' in name): elif name.startswith('/'):
# like "dnf install /usr/bin/vi" # like "dnf install /usr/bin/vi"
if '/' in name: pkg_spec = self._whatprovides(name)
pkg_spec = self._whatprovides(name) if pkg_spec:
if pkg_spec: pkg_specs.append(pkg_spec)
pkg_specs.append(pkg_spec) continue
continue elif name.startswith("@") or ('/' in name):
if not already_loaded_comps: if not already_loaded_comps:
self.base.read_comps() self.base.read_comps()
already_loaded_comps = True already_loaded_comps = True