11import sys
2- import re
3- from distutils .version import StrictVersion
2+ from packaging .version import Version
43from . import _version
54
65__version__ = _version .get_versions ()['version' ]
76
87
9- def _parse_version (version_str ):
10- """ Parse version string `version_str` in our format
11- """
12- match = re .match (r'([0-9.]*\d)(.*)' , version_str )
13- if match is None :
14- raise ValueError ('Invalid version ' + version_str )
15- return match .groups ()
16-
17-
188def _cmp (a , b ):
199 """ Implementation of ``cmp`` for Python 3
2010 """
@@ -24,18 +14,19 @@ def _cmp(a, b):
2414def cmp_pkg_version (version_str , pkg_version_str = __version__ ):
2515 """ Compare ``version_str`` to current package version
2616
27- To be valid, a version must have a numerical major version followed by a
28- dot, followed by a numerical minor version. It may optionally be followed
29- by a dot and a numerical micro version, and / or by an "extra" string.
17+ This comparator follows `PEP-440`_ conventions for determining version
18+ ordering.
19+
20+ To be valid, a version must have a numerical major version. It may be
21+ optionally followed by a dot and a numerical minor version, which may,
22+ in turn, optionally be followed by a dot and a numerical micro version,
23+ and / or by an "extra" string.
3024 The extra string may further contain a "+". Any value to the left of a "+"
3125 labels the version as pre-release, while values to the right indicate a
3226 post-release relative to the values to the left. That is,
3327 ``1.2.0+1`` is post-release for ``1.2.0``, while ``1.2.0rc1+1`` is
3428 post-release for ``1.2.0rc1`` and pre-release for ``1.2.0``.
3529
36- This is an approximation of `PEP-440`_, and future versions will fully
37- implement PEP-440.
38-
3930 Parameters
4031 ----------
4132 version_str : str
@@ -63,30 +54,12 @@ def cmp_pkg_version(version_str, pkg_version_str=__version__):
6354 1
6455 >>> cmp_pkg_version('1.2.0rc1+1', '1.2.0')
6556 -1
57+ >>> cmp_pkg_version('1.2.0.post1', '1.2.0')
58+ 1
6659
6760 .. _`PEP-440`: https://www.python.org/dev/peps/pep-0440/
6861 """
69- version , extra = _parse_version (version_str )
70- pkg_version , pkg_extra = _parse_version (pkg_version_str )
71-
72- # Normalize versions
73- quick_check = _cmp (StrictVersion (version ), StrictVersion (pkg_version ))
74- # Nothing further to check
75- if quick_check != 0 or extra == pkg_extra == '' :
76- return quick_check
77-
78- # Before + is pre-release, after + is additional increment
79- pre , _ , post = extra .partition ('+' )
80- pkg_pre , _ , pkg_post = pkg_extra .partition ('+' )
81- quick_check = _cmp (pre , pkg_pre )
82- if quick_check != 0 : # Excludes case where pre and pkg_pre == ''
83- # Pre-releases are ordered but strictly less than non-pre
84- return (1 if pre == ''
85- else - 1 if pkg_pre == ''
86- else quick_check )
87-
88- # All else being equal, compare additional information lexically
89- return _cmp (post , pkg_post )
62+ return _cmp (Version (version_str ), Version (pkg_version_str ))
9063
9164
9265def pkg_commit_hash (pkg_path = None ):
0 commit comments