Merge "Replace pkg_resource dep resolution with pip"

This commit is contained in:
Zuul 2024-03-25 17:13:57 +00:00 committed by Gerrit Code Review
commit 16af37e0c9
1 changed files with 19 additions and 10 deletions

View File

@ -14,6 +14,7 @@
import concurrent.futures import concurrent.futures
import configparser import configparser
import json
import logging import logging
import os import os
import shutil import shutil
@ -235,23 +236,31 @@ class AnsibleManager:
result = False result = False
try: try:
extra_packages = self._getAnsible(version).extra_packages extra_packages = self._getAnsible(version).extra_packages
python_package_check = \
"import pkg_resources; pkg_resources.require({})".format(
repr(extra_packages))
command = [self.getAnsibleCommand(version, 'python'), # Formerly this used pkg_resources which has been deprecated. If
'-c', python_package_check] # there is a better way to accomplish this task please change
# this approach.
# Ask pip to resolve a dry run installation to determine if any new
# packages need to be installed. A json doc is emitted to stdout
# including a list of things to install if necessary.
command = [self.getAnsibleCommand(version, 'pip'), 'install',
'--dry-run', '--quiet', '--report', '-']
command += extra_packages
ret = subprocess.run(command, ret = subprocess.run(command,
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT) stderr=subprocess.STDOUT)
# We check manually so that we can log the stdout and stderr to_be_installed = json.loads(ret.stdout)["install"]
# properly which aren't going to be available if we have # We check manually so that we can log the missing packages
# subprocess.run() check and raise. # properly. We also need to check the the JSON output to determine
if ret.returncode != 0: # if any changes were necessary.
if to_be_installed:
self.log.error( self.log.error(
'Ansible version %s installation is missing packages' % 'Ansible version %s installation is missing packages' %
version) version)
self.log.debug("Ansible package check output: %s", ret.stdout) missing = ["%s %s" %
(x["metadata"]["name"], x["metadata"]["version"])
for x in to_be_installed]
self.log.debug("These packages are missing: %s", missing)
else: else:
result = True result = True
except Exception: except Exception: