1
1
import sys
2
- import re
3
- from distutils .version import StrictVersion
2
+ from packaging .version import Version
4
3
from . import _version
5
4
6
5
__version__ = _version .get_versions ()['version' ]
7
6
8
7
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
-
18
8
def _cmp (a , b ):
19
9
""" Implementation of ``cmp`` for Python 3
20
10
"""
@@ -24,18 +14,19 @@ def _cmp(a, b):
24
14
def cmp_pkg_version (version_str , pkg_version_str = __version__ ):
25
15
""" Compare ``version_str`` to current package version
26
16
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.
30
24
The extra string may further contain a "+". Any value to the left of a "+"
31
25
labels the version as pre-release, while values to the right indicate a
32
26
post-release relative to the values to the left. That is,
33
27
``1.2.0+1`` is post-release for ``1.2.0``, while ``1.2.0rc1+1`` is
34
28
post-release for ``1.2.0rc1`` and pre-release for ``1.2.0``.
35
29
36
- This is an approximation of `PEP-440`_, and future versions will fully
37
- implement PEP-440.
38
-
39
30
Parameters
40
31
----------
41
32
version_str : str
@@ -63,30 +54,12 @@ def cmp_pkg_version(version_str, pkg_version_str=__version__):
63
54
1
64
55
>>> cmp_pkg_version('1.2.0rc1+1', '1.2.0')
65
56
-1
57
+ >>> cmp_pkg_version('1.2.0.post1', '1.2.0')
58
+ 1
66
59
67
60
.. _`PEP-440`: https://www.python.org/dev/peps/pep-0440/
68
61
"""
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 ))
90
63
91
64
92
65
def pkg_commit_hash (pkg_path = None ):
0 commit comments