versionutils: switch from pkg_resources to packaging

Importing pkg_resources has a side-effect of scanning the metadata of
every installed python package. That is excessive overhead for a
function that needs to compare two version strings.

This change replaces pkg_resources with the packaging library, which
is also used as the implementation for version parsing within
setuptools and pkg_resources.

Change-Id: Ic9bda0783d3664e1f518d513d81b3271028335fd
Signed-off-by: Doug Hellmann <doug@doughellmann.com>
This commit is contained in:
Doug Hellmann 2020-07-05 14:17:47 -04:00
parent 4fe75b7e1b
commit c804ba7da6
3 changed files with 7 additions and 8 deletions

View File

@ -19,6 +19,7 @@ os-client-config==1.28.0
oslo.config==5.2.0 oslo.config==5.2.0
oslo.i18n==3.15.3 oslo.i18n==3.15.3
oslotest==3.2.0 oslotest==3.2.0
packaging==20.4
pbr==2.0.0 pbr==2.0.0
pyparsing==2.1.0 pyparsing==2.1.0
python-mimeparse==1.6.0 python-mimeparse==1.6.0

View File

@ -19,7 +19,7 @@ Helpers for comparing version strings.
.. versionadded:: 1.6 .. versionadded:: 1.6
""" """
import pkg_resources import packaging.version
import six import six
from oslo_utils._i18n import _ from oslo_utils._i18n import _
@ -39,17 +39,14 @@ def is_compatible(requested_version, current_version, same_major=True):
True. True.
:returns: True if compatible, False if not :returns: True if compatible, False if not
""" """
requested_parts = pkg_resources.parse_version(requested_version) requested = packaging.version.Version(requested_version)
current_parts = pkg_resources.parse_version(current_version) current = packaging.version.Version(current_version)
if same_major: if same_major:
# NOTE(jlvillal) pkg_resources issues a warning if we try to access if requested.major != current.major:
# portions of the version, for example requested_parts[0] will issue a
# warning message. So get the major_version from the string instead.
if requested_version.split('.')[0] != current_version.split('.')[0]:
return False return False
return current_parts >= requested_parts return current >= requested
def convert_version_to_int(version): def convert_version_to_int(version):

View File

@ -16,3 +16,4 @@ netaddr>=0.7.18 # BSD
netifaces>=0.10.4 # MIT netifaces>=0.10.4 # MIT
debtcollector>=1.2.0 # Apache-2.0 debtcollector>=1.2.0 # Apache-2.0
pyparsing>=2.1.0 # MIT pyparsing>=2.1.0 # MIT
packaging>=20.4 # BSD