Skip to content

Commit 572fa1d

Browse files
committed
RF: Move all version play from info to pkg_info
1 parent 4dfc7c4 commit 572fa1d

File tree

8 files changed

+117
-124
lines changed

8 files changed

+117
-124
lines changed

nibabel/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99

1010
import os
1111

12-
from .info import __version__, long_description as __doc__
12+
from .pkg_info import __version__
13+
from .info import long_description as __doc__
1314
__doc__ += """
1415
Quickstart
1516
==========

nibabel/deprecated.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import warnings
55

66
from .deprecator import Deprecator
7-
from .info import cmp_pkg_version
7+
from .pkg_info import cmp_pkg_version
88

99

1010
class ModuleProxy(object):

nibabel/info.py

Lines changed: 5 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,11 @@
1-
""" Define distribution parameters for nibabel, including package version
1+
""" Define long_description parameter
22
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.
3+
This parameter is used to fill settings in setup.py, the nibabel top-level
4+
docstring, and in building the docs.
5+
We exec this file in several places, so it cannot import nibabel or use
6+
relative imports.
67
"""
78

8-
import re
9-
from distutils.version import StrictVersion
10-
11-
from ._version import get_versions
12-
__version__ = get_versions()['version']
13-
del get_versions
14-
15-
16-
def _parse_version(version_str):
17-
""" Parse version string `version_str` in our format
18-
"""
19-
match = re.match(r'([0-9.]*\d)(.*)', version_str)
20-
if match is None:
21-
raise ValueError('Invalid version ' + version_str)
22-
return match.groups()
23-
24-
25-
def _cmp(a, b):
26-
""" Implementation of ``cmp`` for Python 3
27-
"""
28-
return (a > b) - (a < b)
29-
30-
31-
def cmp_pkg_version(version_str, pkg_version_str=__version__):
32-
""" Compare `version_str` to current package version
33-
34-
To be valid, a version must have a numerical major version followed by a
35-
dot, followed by a numerical minor version. It may optionally be followed
36-
by a dot and a numerical micro version, and / or by an "extra" string.
37-
*Any* extra string labels the version as pre-release, so `1.2.0somestring`
38-
compares as prior to (pre-release for) `1.2.0`, where `somestring` can be
39-
any string.
40-
41-
Parameters
42-
----------
43-
version_str : str
44-
Version string to compare to current package version
45-
pkg_version_str : str, optional
46-
Version of our package. Optional, set fom ``__version__`` by default.
47-
48-
Returns
49-
-------
50-
version_cmp : int
51-
1 if `version_str` is a later version than `pkg_version_str`, 0 if
52-
same, -1 if earlier.
53-
54-
Examples
55-
--------
56-
>>> cmp_pkg_version('1.2.1', '1.2.0')
57-
1
58-
>>> cmp_pkg_version('1.2.0dev', '1.2.0')
59-
-1
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-
719
# Note: this long_description is the canonical place to edit this text.
7210
# It also appears in README.rst, but it should get there by running
7311
# ``tools/refresh_readme.py`` which pulls in this version.
@@ -157,5 +95,3 @@ def cmp_pkg_version(version_str, pkg_version_str=__version__):
15795
.. _zenodo: https://zenodo.org
15896
.. _Digital Object Identifier: https://en.wikipedia.org/wiki/Digital_object_identifier
15997
"""
160-
161-
VERSION = __version__

nibabel/pkg_info.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,65 @@
11
import sys
2+
import re
3+
from distutils.version import StrictVersion
24
from . import _version
35

6+
__version__ = _version.get_versions()['version']
7+
8+
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+
def _cmp(a, b):
19+
""" Implementation of ``cmp`` for Python 3
20+
"""
21+
return (a > b) - (a < b)
22+
23+
24+
def cmp_pkg_version(version_str, pkg_version_str=__version__):
25+
""" Compare `version_str` to current package version
26+
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.
30+
*Any* extra string labels the version as pre-release, so `1.2.0somestring`
31+
compares as prior to (pre-release for) `1.2.0`, where `somestring` can be
32+
any string.
33+
34+
Parameters
35+
----------
36+
version_str : str
37+
Version string to compare to current package version
38+
pkg_version_str : str, optional
39+
Version of our package. Optional, set fom ``__version__`` by default.
40+
41+
Returns
42+
-------
43+
version_cmp : int
44+
1 if `version_str` is a later version than `pkg_version_str`, 0 if
45+
same, -1 if earlier.
46+
47+
Examples
48+
--------
49+
>>> cmp_pkg_version('1.2.1', '1.2.0')
50+
1
51+
>>> cmp_pkg_version('1.2.0dev', '1.2.0')
52+
-1
53+
"""
54+
version, extra = _parse_version(version_str)
55+
pkg_version, pkg_extra = _parse_version(pkg_version_str)
56+
if version != pkg_version:
57+
return _cmp(StrictVersion(version), StrictVersion(pkg_version))
58+
return (0 if extra == pkg_extra
59+
else 1 if extra == ''
60+
else -1 if pkg_extra == ''
61+
else _cmp(extra, pkg_extra))
62+
463

564
def pkg_commit_hash(pkg_path=None):
665
''' Get short form of commit hash

nibabel/tests/test_deprecated.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import warnings
55

6-
from nibabel import info
6+
from nibabel import pkg_info
77
from nibabel.deprecated import (ModuleProxy, FutureWarningMixin,
88
deprecate_with_version)
99

@@ -14,12 +14,12 @@
1414

1515
def setup():
1616
# Hack nibabel version string
17-
info.cmp_pkg_version.__defaults__ = ('2.0',)
17+
pkg_info.cmp_pkg_version.__defaults__ = ('2.0',)
1818

1919

2020
def teardown():
2121
# Hack nibabel version string back again
22-
info.cmp_pkg_version.__defaults__ = (info.__version__,)
22+
pkg_info.cmp_pkg_version.__defaults__ = (pkg_info.__version__,)
2323

2424

2525
def test_module_proxy():
@@ -76,8 +76,8 @@ def func():
7676
return 99
7777

7878
try:
79-
info.cmp_pkg_version.__defaults__ = ('2.0dev',)
79+
pkg_info.cmp_pkg_version.__defaults__ = ('2.0dev',)
8080
# No error, even though version is dev version of current
8181
assert_equal(func(), 99)
8282
finally:
83-
info.cmp_pkg_version.__defaults__ = ('2.0',)
83+
pkg_info.cmp_pkg_version.__defaults__ = ('2.0',)

nibabel/tests/test_info.py

Lines changed: 0 additions & 47 deletions
This file was deleted.

nibabel/tests/test_pkg_info.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
"""
33

44
import nibabel as nib
5+
from nibabel.pkg_info import cmp_pkg_version
6+
7+
from nose.tools import (assert_raises, assert_equal)
8+
59

610
def test_pkg_info():
711
"""Simple smoke test
@@ -12,3 +16,43 @@ def test_pkg_info():
1216
- nibabel.pkg_info.pkg_commit_hash
1317
"""
1418
info = nib.get_info()
19+
20+
21+
def test_version():
22+
# Test info version is the same as our own version
23+
assert_equal(nib.pkg_info.__version__, nib.__version__)
24+
25+
26+
def test_cmp_pkg_version():
27+
# Test version comparator
28+
assert_equal(cmp_pkg_version(nib.__version__), 0)
29+
assert_equal(cmp_pkg_version('0.0'), -1)
30+
assert_equal(cmp_pkg_version('1000.1000.1'), 1)
31+
assert_equal(cmp_pkg_version(nib.__version__, nib.__version__), 0)
32+
for test_ver, pkg_ver, exp_out in (('1.0', '1.0', 0),
33+
('1.0.0', '1.0', 0),
34+
('1.0', '1.0.0', 0),
35+
('1.1', '1.1', 0),
36+
('1.2', '1.1', 1),
37+
('1.1', '1.2', -1),
38+
('1.1.1', '1.1.1', 0),
39+
('1.1.2', '1.1.1', 1),
40+
('1.1.1', '1.1.2', -1),
41+
('1.1', '1.1dev', 1),
42+
('1.1dev', '1.1', -1),
43+
('1.2.1', '1.2.1rc1', 1),
44+
('1.2.1rc1', '1.2.1', -1),
45+
('1.2.1rc1', '1.2.1rc', 1),
46+
('1.2.1rc', '1.2.1rc1', -1),
47+
('1.2.1rc1', '1.2.1rc', 1),
48+
('1.2.1rc', '1.2.1rc1', -1),
49+
('1.2.1b', '1.2.1a', 1),
50+
('1.2.1a', '1.2.1b', -1),
51+
):
52+
assert_equal(cmp_pkg_version(test_ver, pkg_ver), exp_out)
53+
assert_raises(ValueError, cmp_pkg_version, 'foo.2')
54+
assert_raises(ValueError, cmp_pkg_version, 'foo.2', '1.0')
55+
assert_raises(ValueError, cmp_pkg_version, '1.0', 'foo.2')
56+
assert_raises(ValueError, cmp_pkg_version, '1')
57+
assert_raises(ValueError, cmp_pkg_version, 'foo')
58+

nibabel/tests/test_removalschedule.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from ..info import cmp_pkg_version
1+
from ..pkg_info import cmp_pkg_version
22
from ..testing import assert_raises, assert_false
33

44
MODULE_SCHEDULE = [

0 commit comments

Comments
 (0)