Skip to content

Commit

Permalink
atlocal: Replace deprecated pkg_resources.
Browse files Browse the repository at this point in the history
'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 <[email protected]>
Signed-off-by: Ilya Maximets <[email protected]>
  • Loading branch information
igsilya committed May 22, 2024
1 parent 1f0423a commit ffbce0c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
1 change: 1 addition & 0 deletions python/test_requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
netaddr
packaging
pyftpdlib
pyparsing
pytest
Expand Down
28 changes: 22 additions & 6 deletions tests/atlocal.in
Original file line number Diff line number Diff line change
Expand Up @@ -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 ;;
Expand Down

0 comments on commit ffbce0c

Please sign in to comment.