Stop using pkg_resources

This commit is contained in:
Itamar Turner-Trauring 2023-11-17 10:46:41 -05:00
parent 7c878b9b94
commit fbbf1cf98a

View File

@ -1,8 +1,7 @@
#! /usr/bin/env python #! /usr/bin/env python
from __future__ import print_function
import locale, os, platform, subprocess, sys, traceback import locale, os, platform, subprocess, sys, traceback
from importlib.metadata import version, PackageNotFoundError
def foldlines(s, numlines=None): def foldlines(s, numlines=None):
@ -72,17 +71,10 @@ def print_as_ver():
traceback.print_exc(file=sys.stderr) traceback.print_exc(file=sys.stderr)
sys.stderr.flush() sys.stderr.flush()
def print_setuptools_ver(): def print_setuptools_ver():
try: try:
import pkg_resources print("setuptools:", version("setuptools"))
out = str(pkg_resources.require("setuptools")) except PackageNotFoundError:
print("setuptools:", foldlines(out))
except (ImportError, EnvironmentError):
sys.stderr.write("\nGot exception using 'pkg_resources' to get the version of setuptools. Exception follows\n")
traceback.print_exc(file=sys.stderr)
sys.stderr.flush()
except pkg_resources.DistributionNotFound:
print('setuptools: DistributionNotFound') print('setuptools: DistributionNotFound')
@ -91,14 +83,8 @@ def print_py_pkg_ver(pkgname, modulename=None):
modulename = pkgname modulename = pkgname
print() print()
try: try:
import pkg_resources print(pkgname + ': ' + version(pkgname))
out = str(pkg_resources.require(pkgname)) except PackageNotFoundError:
print(pkgname + ': ' + foldlines(out))
except (ImportError, EnvironmentError):
sys.stderr.write("\nGot exception using 'pkg_resources' to get the version of %s. Exception follows.\n" % (pkgname,))
traceback.print_exc(file=sys.stderr)
sys.stderr.flush()
except pkg_resources.DistributionNotFound:
print(pkgname + ': DistributionNotFound') print(pkgname + ': DistributionNotFound')
try: try:
__import__(modulename) __import__(modulename)