diff mbox series

[ovs-dev,2/2] atlocal: Replace deprecated pkg_resources.

Message ID 20240517184716.133519-3-i.maximets@ovn.org
State Accepted
Commit d4bd0a2ad54b63c1e767ea2a7bff761d4676514a
Delegated to: Ilya Maximets
Headers show
Series atlocal: Fixes for dependency detection. | expand

Checks

Context Check Description
ovsrobot/apply-robot success apply and check: success
ovsrobot/github-robot-_Build_and_Test success github build: passed
ovsrobot/intel-ovs-compilation success test: success

Commit Message

Ilya Maximets May 17, 2024, 6:47 p.m. UTC
'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.

Signed-off-by: Ilya Maximets <i.maximets@ovn.org>
---
 python/test_requirements.txt |  1 +
 tests/atlocal.in             | 28 ++++++++++++++++++++++------
 2 files changed, 23 insertions(+), 6 deletions(-)

Comments

Eelco Chaudron May 21, 2024, 6:32 a.m. UTC | #1
On 17 May 2024, at 20:47, Ilya Maximets wrote:

> '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.
>
> Signed-off-by: Ilya Maximets <i.maximets@ovn.org>

Thanks for fixing this. The changes look good to me.

Acked-by: Eelco Chaudron <echaudro@redhat.com>
Ilya Maximets May 23, 2024, 12:19 p.m. UTC | #2
On 5/21/24 08:32, Eelco Chaudron wrote:
> 
> 
> On 17 May 2024, at 20:47, Ilya Maximets wrote:
> 
>> '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.
>>
>> Signed-off-by: Ilya Maximets <i.maximets@ovn.org>
> 
> Thanks for fixing this. The changes look good to me.
> 
> Acked-by: Eelco Chaudron <echaudro@redhat.com>
> 


Thanks!  Applied and backported down to 3.0.

Best regards, Ilya Maximets.
diff mbox series

Patch

diff --git a/python/test_requirements.txt b/python/test_requirements.txt
index 5043c71e2..a1424506b 100644
--- a/python/test_requirements.txt
+++ b/python/test_requirements.txt
@@ -1,4 +1,5 @@ 
 netaddr
+packaging
 pyftpdlib
 pyparsing
 pytest
diff --git a/tests/atlocal.in b/tests/atlocal.in
index 466fd4ed4..8565a0bae 100644
--- a/tests/atlocal.in
+++ b/tests/atlocal.in
@@ -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 ;;