Skip to content

Commit 5877c17

Browse files
authored
Merge pull request #856 from effigies/rf/packaging
RF: Improve fallback version check, require PyPA packaging module
2 parents 34384cb + 5389364 commit 5877c17

File tree

3 files changed

+25
-43
lines changed

3 files changed

+25
-43
lines changed

nibabel/pkg_info.py

Lines changed: 11 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,10 @@
11
import sys
2-
import re
3-
from distutils.version import StrictVersion
2+
from packaging.version import Version
43
from . 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-
188
def _cmp(a, b):
199
""" Implementation of ``cmp`` for Python 3
2010
"""
@@ -24,18 +14,19 @@ def _cmp(a, b):
2414
def 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

9265
def pkg_commit_hash(pkg_path=None):

nibabel/tests/test_pkg_info.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
""" Testing package info
22
"""
33

4+
from packaging.version import Version
5+
46
import nibabel as nib
57
from nibabel.pkg_info import cmp_pkg_version
68
from ..info import VERSION
@@ -30,12 +32,14 @@ def test_fallback_version():
3032
This should only fail if we fail to bump nibabel.info.VERSION immediately
3133
after release
3234
"""
35+
ver = Version(nib.__version__)
36+
fallback = Version(VERSION)
3337
assert (
38+
# Releases have no local information, archive matches versioneer
39+
ver.local is None or
3440
# dev version should be larger than tag+commit-githash
35-
cmp_pkg_version(VERSION) >= 0 or
36-
# Allow VERSION bump to lag releases by one commit
37-
VERSION == nib.__version__ + 'dev'), \
38-
"nibabel.info.VERSION does not match current tag information"
41+
fallback >= ver), \
42+
"nibabel.info.VERSION does not match latest tag information"
3943

4044

4145
def test_cmp_pkg_version():
@@ -76,6 +80,10 @@ def test_cmp_pkg_version():
7680
assert_raises(ValueError, cmp_pkg_version, 'foo.2')
7781
assert_raises(ValueError, cmp_pkg_version, 'foo.2', '1.0')
7882
assert_raises(ValueError, cmp_pkg_version, '1.0', 'foo.2')
79-
assert_raises(ValueError, cmp_pkg_version, '1')
8083
assert_raises(ValueError, cmp_pkg_version, 'foo')
8184

85+
# Check dev/RC sequence
86+
seq = ('3.0.0dev', '3.0.0rc1', '3.0.0rc1.post.dev', '3.0.0rc2', '3.0.0rc2.post.dev', '3.0.0')
87+
for stage1, stage2 in zip(seq[:-1], seq[1:]):
88+
assert_equal(cmp_pkg_version(stage1, stage2), -1)
89+
assert_equal(cmp_pkg_version(stage2, stage1), 1)

setup.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ provides =
3131
python_requires = >=3.5.1
3232
install_requires =
3333
numpy >=1.12
34+
packaging
3435
tests_require =
3536
nose >=0.11
3637
pytest

0 commit comments

Comments
 (0)