Replace pkg_resource dep resolution with pip

Python 3.12 has more fully excised setuptools and as a result
pkg_resources. Since we are using pip to install things we can instead
rely on pip to resolve whether or not packages are installed. Do this so
that we don't need to explicitly install setuptools where it may not be

Change-Id: I8ee189c828914fd648847b5650b5fb2fb255ff17
This commit is contained in:
Clark Boylan 2024-03-19 14:09:46 -07:00
parent b52af834cc
commit dfe96519b1
1 changed files with 19 additions and 10 deletions

View File

@ -14,6 +14,7 @@
import concurrent.futures
import configparser
import json
import logging
import os
import shutil
@ -235,23 +236,31 @@ class AnsibleManager:
result = False
extra_packages = self._getAnsible(version).extra_packages
python_package_check = \
"import pkg_resources; pkg_resources.require({})".format(
command = [self.getAnsibleCommand(version, 'python'),
'-c', python_package_check]
# Formerly this used pkg_resources which has been deprecated. If
# 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 =,
# We check manually so that we can log the stdout and stderr
# properly which aren't going to be available if we have
# check and raise.
if ret.returncode != 0:
to_be_installed = json.loads(ret.stdout)["install"]
# We check manually so that we can log the missing packages
# properly. We also need to check the the JSON output to determine
# if any changes were necessary.
if to_be_installed:
'Ansible version %s installation is missing packages' %
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)
result = True
except Exception: