Skip to content

Commit 05b0c8e

Browse files
committed
NF: add cmp_pkg_version function and test
Add function to compare our package version to others. We'll use this to see whether the deprecations have expired.
1 parent e37cb5d commit 05b0c8e

File tree

2 files changed

+100
-4
lines changed

2 files changed

+100
-4
lines changed

nibabel/info.py

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
""" This file contains defines parameters for nibabel that we use to fill
2-
settings in setup.py, the nibabel top-level docstring, and for building the
3-
docs. In setup.py in particular, we exec this file, so it cannot import
4-
nibabel
1+
""" Define distrubution parameters for nibabel, including package version
2+
3+
This file contains defines parameters for nibabel that we use to fill settings
4+
in setup.py, the nibabel top-level docstring, and for building the docs. In
5+
setup.py in particular, we exec this file, so it cannot import nibabel.
56
"""
67

8+
import re
9+
from distutils.version import StrictVersion
10+
711
# nibabel version information. An empty _version_extra corresponds to a
812
# full release. '.dev' as a _version_extra string means this is a development
913
# version
@@ -19,6 +23,51 @@
1923
_version_micro,
2024
_version_extra)
2125

26+
27+
_VERSION_RE = re.compile(r'([0-9.]*\d+)(.*)')
28+
29+
30+
def _parse_version(version_str):
31+
""" Parse version string `version_str` in our format
32+
"""
33+
match = re.match('([0-9.]*\d)(.*)', version_str)
34+
if match is None:
35+
raise ValueError('Invalid version ' + version_str)
36+
return match.groups()
37+
38+
39+
def _cmp(a, b):
40+
""" Implementation of ``cmp`` for Python 3
41+
"""
42+
return (a > b) - (a < b)
43+
44+
45+
def cmp_pkg_version(version_str, pkg_version_str=__version__):
46+
""" Compare `version_str` to current package version
47+
48+
Parameters
49+
----------
50+
version_str : str
51+
Version string to compare to current package version
52+
pkg_version_str : str, optional
53+
Version of our package. Optional, set fom ``__version__`` by default.
54+
55+
Returns
56+
-------
57+
version_cmp : int
58+
1 if `version_str` higher version than `pkg_version`, 0 if same, -1 if
59+
lower.
60+
"""
61+
version, extra = _parse_version(version_str)
62+
pkg_version, pkg_extra = _parse_version(pkg_version_str)
63+
if version != pkg_version:
64+
return _cmp(StrictVersion(version), StrictVersion(pkg_version))
65+
return (0 if extra == pkg_extra
66+
else 1 if extra == ''
67+
else -1 if pkg_extra == ''
68+
else _cmp(extra, pkg_extra))
69+
70+
2271
CLASSIFIERS = ["Development Status :: 4 - Beta",
2372
"Environment :: Console",
2473
"Intended Audience :: Science/Research",

nibabel/tests/test_info.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
""" Testing info module
2+
"""
3+
4+
import nibabel as nib
5+
from nibabel import info
6+
from nibabel.info import cmp_pkg_version
7+
8+
from nose.tools import (assert_raises, assert_equal)
9+
10+
11+
def test_version():
12+
# Test info version is the same as our own version
13+
assert_equal(info.__version__, nib.__version__)
14+
15+
16+
def test_cmp_pkg_version():
17+
# Test version comparator
18+
assert_equal(cmp_pkg_version(info.__version__), 0)
19+
assert_equal(cmp_pkg_version('0.0'), -1)
20+
assert_equal(cmp_pkg_version('1000.1000.1'), 1)
21+
assert_equal(cmp_pkg_version(info.__version__, info.__version__), 0)
22+
for test_ver, pkg_ver, exp_out in (('1.0', '1.0', 0),
23+
('1.0.0', '1.0', 0),
24+
('1.0', '1.0.0', 0),
25+
('1.1', '1.1', 0),
26+
('1.2', '1.1', 1),
27+
('1.1', '1.2', -1),
28+
('1.1.1', '1.1.1', 0),
29+
('1.1.2', '1.1.1', 1),
30+
('1.1.1', '1.1.2', -1),
31+
('1.1', '1.1dev', 1),
32+
('1.1dev', '1.1', -1),
33+
('1.2.1', '1.2.1rc1', 1),
34+
('1.2.1rc1', '1.2.1', -1),
35+
('1.2.1rc1', '1.2.1rc', 1),
36+
('1.2.1rc', '1.2.1rc1', -1),
37+
('1.2.1rc1', '1.2.1rc', 1),
38+
('1.2.1rc', '1.2.1rc1', -1),
39+
('1.2.1b', '1.2.1a', 1),
40+
('1.2.1b', '1.2.1a', 1),
41+
):
42+
assert_equal(cmp_pkg_version(test_ver, pkg_ver), exp_out)
43+
assert_raises(ValueError, cmp_pkg_version, 'foo.2')
44+
assert_raises(ValueError, cmp_pkg_version, 'foo.2', '1.0')
45+
assert_raises(ValueError, cmp_pkg_version, '1.0', 'foo.2')
46+
assert_raises(ValueError, cmp_pkg_version, '1')
47+
assert_raises(ValueError, cmp_pkg_version, 'foo')

0 commit comments

Comments
 (0)