Skip to content

Commit c8fc385

Browse files
authored
Merge pull request #83 from oesteban/maint/versioneer
MAINT: Use setuptools_scm to manage versioning
2 parents 1ed36d0 + 2670b12 commit c8fc385

File tree

7 files changed

+190
-11
lines changed

7 files changed

+190
-11
lines changed

.circleci/config.yml

+94-7
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,19 @@ jobs:
6363
key: build-v1-{{ .Branch }}-{{ epoch }}
6464
paths:
6565
- /tmp/docker
66+
- run:
67+
name: Check version packaged in Docker image
68+
command: |
69+
THISVERSION=$(python3 setup.py --version)
70+
THISVERSION=${CIRCLE_TAG:-$THISVERSION}
71+
INSTALLED_VERSION=$(\
72+
docker run -it --rm --entrypoint=python nitransforms \
73+
-c 'import nitransforms as nit; print(nit.__version__)')
74+
INSTALLED_VERSION=${INSTALLED_VERSION%$'\r'}
75+
echo "VERSION: \"$THISVERSION\""
76+
echo "INSTALLED: \"$INSTALLED_VERSION\""
77+
test "$INSTALLED_VERSION" = "$THISVERSION"
78+
6679
- run:
6780
name: Store FreeSurfer license file
6881
command: |
@@ -125,16 +138,81 @@ jobs:
125138
- store_test_results:
126139
path: /tmp/tests/summaries/
127140

128-
test_packaging_and_deploy:
129-
machine:
130-
image: circleci/classic:201808-01
141+
test_package:
142+
docker:
143+
- image: circleci/python:3.7.4
144+
working_directory: /tmp/src/nitransforms
145+
steps:
146+
- checkout
147+
- run:
148+
name: Setup Python environment with virtualenvs
149+
command: |
150+
python -m pip install --user --upgrade virtualenv pip
151+
- run:
152+
name: Prepare build environment
153+
command: |
154+
virtualenv --python=python3 /tmp/build
155+
source /tmp/build/bin/activate
156+
python3 -m pip install "setuptools ~= 42.0" "setuptools_scm[toml] >= 3.4" "pip>=10.0.1" \
157+
twine docutils
158+
- run:
159+
name: Build NiTransforms in build environment
160+
command: |
161+
source /tmp/build/bin/activate
162+
python setup.py sdist bdist_wheel
163+
- run:
164+
name: Check sdist package in build environment
165+
command: |
166+
source /tmp/build/bin/activate
167+
twine check dist/nitransforms*
168+
- store_artifacts:
169+
path: /tmp/src/nitransforms/dist
170+
- run:
171+
name: Prepare sdist install environment
172+
command: |
173+
virtualenv --python=python3 /tmp/install_sdist
174+
source /tmp/install_sdist/bin/activate
175+
python3 -m pip install "setuptools ~= 42.0" "setuptools_scm[toml] >= 3.4" "pip>=10.0.1"
176+
- run:
177+
name: Install sdist package into install environment and check version
178+
command: |
179+
source /tmp/install_sdist/bin/activate
180+
THISVERSION=$( python setup.py --version )
181+
THISVERSION=${CIRCLE_TAG:-$THISVERSION}
182+
pip install dist/nitransforms*.tar.gz
183+
INSTALLED_VERSION=$(python -c 'import nitransforms as nit; print(nit.__version__)')
184+
INSTALLED_VERSION=${INSTALLED_VERSION%$'\r'}
185+
echo "VERSION: \"$THISVERSION\""
186+
echo "INSTALLED: \"$INSTALLED_VERSION\""
187+
test "$INSTALLED_VERSION" = "$THISVERSION"
188+
- run:
189+
name: Prepare wheel install environment
190+
command: |
191+
virtualenv --python=python3 /tmp/install_wheel
192+
source /tmp/install_wheel/bin/activate
193+
python3 -m pip install "setuptools ~= 42.0" "setuptools_scm[toml] >= 3.4" "pip>=10.0.1"
194+
- run:
195+
name: Install wheel into install environment and check version
196+
command: |
197+
source /tmp/install_wheel/bin/activate
198+
THISVERSION=$( python setup.py --version )
199+
THISVERSION=${CIRCLE_TAG:-$THISVERSION}
200+
pip install dist/nitransforms*.whl
201+
INSTALLED_VERSION=$(python -c 'import nitransforms as nit; print(nit.__version__)')
202+
INSTALLED_VERSION=${INSTALLED_VERSION%$'\r'}
203+
echo "VERSION: \"$THISVERSION\""
204+
echo "INSTALLED: \"$INSTALLED_VERSION\""
205+
test "$INSTALLED_VERSION" = "$THISVERSION"
206+
207+
deploy_pypi:
208+
docker:
209+
- image: circleci/python:3.7.4
131210
working_directory: /tmp/src/nitransforms
132211
steps:
133212
- checkout
134-
- run: pyenv local 3.7.0
135213
- run:
136214
name: Install build depends
137-
command: python3 -m pip install "setuptools>=30.4.0" "pip>=10.0.1" "twine<2.0" docutils
215+
command: python3 -m pip install "setuptools ~= 42.0" "setuptools_scm[toml] >= 3.4" "pip>=10.0.1" "twine<2.0" docutils
138216
- run:
139217
name: Build and check
140218
command: |
@@ -144,7 +222,7 @@ jobs:
144222
- run:
145223
name: Validate version
146224
command: |
147-
THISVERSION=$( python3 get_version.py )
225+
THISVERSION=$( python setup.py --version )
148226
python3 -m pip install dist/*.tar.gz
149227
mkdir empty
150228
cd empty
@@ -167,9 +245,18 @@ workflows:
167245
tags:
168246
only: /.*/
169247

170-
- test_packaging_and_deploy:
248+
- test_package:
249+
filters:
250+
branches:
251+
ignore:
252+
- /docs?\/.*/
253+
tags:
254+
only: /.*/
255+
256+
- deploy_pypi:
171257
requires:
172258
- build_pytest
259+
- test_package
173260
filters:
174261
branches:
175262
ignore: /.*/

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# setuptools-scm
2+
nitransforms/_version.py
3+
14
# Byte-compiled / optimized / DLL files
25
__pycache__/
36
*.py[cod]

nitransforms/__init__.py

+16-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,21 @@
1818
"""
1919
from .linear import Affine
2020
from .nonlinear import DisplacementsFieldTransform
21-
from .io import afni, fsl, itk
2221

22+
try:
23+
from ._version import __version__
24+
except ModuleNotFoundError:
25+
from pkg_resources import get_distribution, DistributionNotFound
26+
try:
27+
__version__ = get_distribution('nitransforms').version
28+
except DistributionNotFound:
29+
__version__ = 'unknown'
30+
del get_distribution
31+
del DistributionNotFound
2332

24-
__all__ = ['afni', 'fsl', 'itk', 'Affine', 'DisplacementsFieldTransform']
33+
34+
__all__ = [
35+
'Affine',
36+
'DisplacementsFieldTransform',
37+
'__version__',
38+
]

nitransforms/tests/test_version.py

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"""Test _version.py."""
2+
import sys
3+
from collections import namedtuple
4+
from pkg_resources import DistributionNotFound
5+
from importlib import reload
6+
import nitransforms
7+
8+
9+
def test_version_scm0(monkeypatch):
10+
"""Retrieve the version via setuptools_scm."""
11+
class _version:
12+
__version__ = "10.0.0"
13+
14+
monkeypatch.setitem(sys.modules, 'nitransforms._version', _version)
15+
reload(nitransforms)
16+
assert nitransforms.__version__ == "10.0.0"
17+
18+
19+
def test_version_scm1(monkeypatch):
20+
"""Retrieve the version via pkg_resources."""
21+
monkeypatch.setitem(sys.modules, 'nitransforms._version', None)
22+
23+
def _dist(name):
24+
Distribution = namedtuple("Distribution", ["name", "version"])
25+
return Distribution(name, "success")
26+
27+
monkeypatch.setattr('pkg_resources.get_distribution', _dist)
28+
reload(nitransforms)
29+
assert nitransforms.__version__ == "success"
30+
31+
32+
def test_version_scm2(monkeypatch):
33+
"""Check version could not be interpolated."""
34+
monkeypatch.setitem(sys.modules, 'nitransforms._version', None)
35+
36+
def _raise(name):
37+
raise DistributionNotFound("No get_distribution mock")
38+
39+
monkeypatch.setattr('pkg_resources.get_distribution', _raise)
40+
reload(nitransforms)
41+
assert nitransforms.__version__ == "unknown"

pyproject.toml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[build-system]
2+
requires = ["setuptools ~= 42.0", "wheel", "setuptools_scm[toml] >= 3.4"]
3+
4+
[tool.setuptools_scm]
5+
write_to = "nitransforms/_version.py"
6+
write_to_template = """\
7+
\"\"\"Version file, automatically generated by setuptools_scm.\"\"\"
8+
__version__ = "{version}"
9+
"""
10+
fallback_version = "0.0"

setup.cfg

+19-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
11
[metadata]
2-
provides =
3-
nitransforms
2+
author = The NiPreps developers
3+
author_email = [email protected]
4+
classifiers =
5+
Development Status :: 2 - Pre-Alpha
6+
Intended Audience :: Science/Research
7+
Topic :: Scientific/Engineering :: Image Recognition
8+
License :: OSI Approved :: BSD License
9+
Programming Language :: Python :: 3.6
10+
Programming Language :: Python :: 3.7
11+
Programming Language :: Python :: 3.8
12+
description = NiTransforms -- Neuroimaging spatial transforms in Python.
13+
license = MIT
14+
long_description = file:README.md
15+
long_description_content_type = text/markdown; charset=UTF-8
16+
provides = nitransforms
17+
url = https://github.com/poldracklab/nitransforms
418

519
[options]
620
python_requires = >= 3.6
@@ -14,6 +28,9 @@ test_requires =
1428
pytest-cov
1529
nose
1630
codecov
31+
setup_requires =
32+
setuptools_scm
33+
toml
1734
packages = find:
1835
include_package_data = True
1936

setup.py

+7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1+
"""Prepare package for distribution."""
2+
from pathlib import Path
13
from setuptools import setup
4+
from toml import loads
25

36
if __name__ == "__main__":
7+
scm_cfg = loads(
8+
(Path(__file__).parent / "pyproject.toml").read_text()
9+
)["tool"]["setuptools_scm"]
410
setup(
511
name="nitransforms",
12+
use_scm_version=scm_cfg,
613
)

0 commit comments

Comments
 (0)