2
0
mirror of https://github.com/openvswitch/ovs synced 2025-08-22 09:58:01 +00:00

atlocal: Replace deprecated pkg_resources.

'pkg_resources' module is deprecated and no longer available in newer
versions of python, so pytest tests are skipped:

  DeprecationWarning: pkg_resources is deprecated as an API.
  See https://setuptools.pypa.io/en/latest/pkg_resources.html

Unfortunately, there is no direct replacement for it and functionality
is scattered between different packages.

Using a new standard library importlib.metadata to find installed
packages and their versions.  Using packaging.requirements to parse
lines from the requirements file and compare versions.  This covers
all we need.

The 'packaging' is a project used by pip and a dependency for many
other libraries, so should be available for any supported verison of
python.  'importlib' was introduced in python 3.8.  Since we support
older versions of python and 'packaging' is not part of the standard
library, checking that import is possible and falling back to
'pkg_resources' if needed.  We may remove the fallback when we stop
supporting python below 3.8.

Even though 'packaging' is a common dependency, added to the test
requirements so it will not be missed in CI.

Acked-by: Eelco Chaudron <echaudro@redhat.com>
Signed-off-by: Ilya Maximets <i.maximets@ovn.org>
This commit is contained in:
Ilya Maximets 2024-05-17 20:47:12 +02:00
parent 4d2c64ca1b
commit d4bd0a2ad5
2 changed files with 23 additions and 6 deletions

View File

@ -1,4 +1,5 @@
netaddr
packaging
pyftpdlib
pyparsing
pytest

View File

@ -229,15 +229,31 @@ export UBSAN_OPTIONS
REQUIREMENT_PATH=$abs_top_srcdir/python/test_requirements.txt $PYTHON3 -c '
import os
import pathlib
import pkg_resources
import sys
PACKAGING = True
try:
from packaging import requirements
from importlib import metadata
except ModuleNotFoundError:
PACKAGING = False
import pkg_resources
with pathlib.Path(os.path.join(os.getenv("REQUIREMENT_PATH"))).open() as reqs:
for req in pkg_resources.parse_requirements(reqs):
try:
pkg_resources.require(str(req))
except pkg_resources.DistributionNotFound:
sys.exit(2)
if PACKAGING:
for req in reqs.readlines():
try:
r = requirements.Requirement(req.strip())
if metadata.version(r.name) not in r.specifier:
raise metadata.PackageNotFoundError
except metadata.PackageNotFoundError:
sys.exit(2)
else:
for req in pkg_resources.parse_requirements(reqs):
try:
pkg_resources.require(str(req))
except pkg_resources.DistributionNotFound:
sys.exit(2)
'
case $? in
0) HAVE_PYTEST=yes ;;