Skip to content

Commit

Permalink
chore: Use setuptools_scm to manage versioning
Browse files Browse the repository at this point in the history
Summary
-------
Pilots whether we can abandon versioneer in all projects, as
setuptools_scm has minimal boilerplate.

Changes
-------
The setuptools_scm module is added to the pyproject.toml file,
as recommended in the documentation.
A tiny hack is added to the root ``__init__.py`` file, so that
the version is read from ``nitransforms/_version.py`` when that
file exists or it is interpolated via ``setuptools_scm.get_version``.

To make the version traverse into container images, before building,
``python setup.py --version`` must be invoked first (and it will create
the ``nitransforms/_version.py`` file.

``nitransforms/_version.py`` has been added to ``.gitignore`` to avoid
adding it by mistake.

Finally, the CircleCI workflow is also modified, so that the packaging
is always tested and installed versions are checked both for python
packages and docker distributions.

Resolves: #82
  • Loading branch information
oesteban committed Mar 27, 2020
1 parent f0c91b4 commit 50c830c
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 5 deletions.
91 changes: 90 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,12 @@ jobs:
docker push localhost:5000/ubuntu
fi
- checkout
- run: pyenv local 3.7.0
- run:
name: Build Docker image & push to registry
no_output_timeout: 60m
command: |
python3 setup.py --version
e=1 && for i in {1..5}; do
docker build --rm --cache-from=nitransforms:latest \
-t nitransforms:latest \
Expand All @@ -63,6 +65,18 @@ jobs:
key: build-v1-{{ .Branch }}-{{ epoch }}
paths:
- /tmp/docker
- run:
name: Check version packaged in Docker image
command: |
THISVERSION=$(python3 setup.py --version)
THISVERSION=${CIRCLE_TAG:-$THISVERSION}
INSTALLED_VERSION=$(\
docker run -it --rm --entrypoint=python nitransforms \
-c 'import nitransforms as nit; print(nit.__version__)')
INSTALLED_VERSION=${INSTALLED_VERSION%$'\r'}
echo "VERSION: \"$THISVERSION\""
echo "INSTALLED: \"$INSTALLED_VERSION\""
test "$INSTALLED_VERSION" = "$THISVERSION"
- run:
name: Store FreeSurfer license file
command: |
Expand All @@ -74,6 +88,72 @@ jobs:
paths:
- fslicense

test_package:
docker:
- image: circleci/python:3.7.4
working_directory: /tmp/src/nitransforms
steps:
- checkout
- run:
name: Setup Python environment with virtualenvs
command: |
python -m pip install --user --upgrade virtualenv pip
- run:
name: Prepare build environment
command: |
virtualenv --python=python3 /tmp/build
source /tmp/build/bin/activate
python3 -m pip install "setuptools ~= 42.0" "setuptools_scm[toml] >= 3.4" "pip>=10.0.1" \
twine docutils
- run:
name: Prepare install environments
command: |
virtualenv --python=python3 /tmp/install_sdist
source /tmp/install_sdist/bin/activate
python3 -m pip install "setuptools ~= 42.0" "setuptools_scm[toml] >= 3.4" "pip>=10.0.1"
deactivate
virtualenv --python=python3 /tmp/install_wheel
source /tmp/install_wheel/bin/activate
python3 -m pip install "setuptools ~= 42.0" "setuptools_scm[toml] >= 3.4" "pip>=10.0.1"
- run:
name: Build NiTransforms in build environment
command: |
source /tmp/build/bin/activate
python setup.py sdist bdist_wheel
- store_artifacts:
path: /tmp/src/nitransforms/dist
- run:
name: Check sdist package in build environment
command: |
source /tmp/build/bin/activate
twine check dist/nitransforms*
- run:
name: Install sdist package into install environment and check version
command: |
source /tmp/install_sdist/bin/activate
THISVERSION=$( python setup.py --version )
THISVERSION=${CIRCLE_TAG:-$THISVERSION}
pip install dist/nitransforms*.tar.gz
which nitransforms | grep install_sdist\\/bin
INSTALLED_VERSION=$(python -c 'import nitransforms as nit; print(nit.__version__)')
INSTALLED_VERSION=${INSTALLED_VERSION%$'\r'}
echo "VERSION: \"$THISVERSION\""
echo "INSTALLED: \"$INSTALLED_VERSION\""
test "$INSTALLED_VERSION" = "$THISVERSION"
- run:
name: Install wheel into install environment and check version
command: |
source /tmp/install_wheel/bin/activate
THISVERSION=$( python setup.py --version )
THISVERSION=${CIRCLE_TAG:-$THISVERSION}
pip install dist/nitransforms*.whl
which nitransforms | grep install_wheel\\/bin
INSTALLED_VERSION=$(python -c 'import nitransforms as nit; print(nit.__version__)')
INSTALLED_VERSION=${INSTALLED_VERSION%$'\r'}
echo "VERSION: \"$THISVERSION\""
echo "INSTALLED: \"$INSTALLED_VERSION\""
test "$INSTALLED_VERSION" = "$THISVERSION"
test_pytest:
machine:
image: circleci/classic:201808-01
Expand Down Expand Up @@ -175,6 +255,14 @@ workflows:
tags:
only: /.*/

- test_package:
filters:
branches:
ignore:
- /docs?\/.*/
tags:
only: /.*/

- test_pytest:
requires:
- build
Expand All @@ -186,9 +274,10 @@ workflows:
tags:
only: /.*/

- test_packaging_and_deploy:
- deploy_pypi:
requires:
- test_pytest
- test_package
filters:
branches:
ignore: /.*/
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# setuptools-scm
nitransforms/_version.py

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
Expand Down
18 changes: 16 additions & 2 deletions nitransforms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,21 @@
"""
from .linear import Affine
from .nonlinear import DisplacementsFieldTransform
from .io import afni, fsl, itk

try:
from ._version import version as __version__
except ModuleNotFoundError:
from pkg_resources import get_distribution, DistributionNotFound
try:
__version__ = get_distribution('nitransforms').version
except DistributionNotFound:
__version__ = 'unknown'
del get_distribution
del DistributionNotFound

__all__ = ['afni', 'fsl', 'itk', 'Affine', 'DisplacementsFieldTransform']

__all__ = [
'Affine',
'DisplacementsFieldTransform',
'__version__',
]
10 changes: 10 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[build-system]
requires = ["setuptools ~= 42.0", "wheel", "setuptools_scm[toml] >= 3.4"]

[tool.setuptools_scm]
write_to = "nitransforms/_version.py"
write_to_template = """\
\"\"\"Version file, automatically generated by setuptools_scm.\"\"\"
__version__ = "{version}"
"""
fallback_version = "0.0"
21 changes: 19 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
[metadata]
provides =
nitransforms
author = The NiPreps developers
author_email = [email protected]
classifiers =
Development Status :: 2 - Pre-Alpha
Intended Audience :: Science/Research
Topic :: Scientific/Engineering :: Image Recognition
License :: OSI Approved :: BSD License
Programming Language :: Python :: 3.6
Programming Language :: Python :: 3.7
Programming Language :: Python :: 3.8
description = NiTransforms -- Neuroimaging spatial transforms in Python.
license = MIT
long_description = file:README.md
long_description_content_type = text/markdown; charset=UTF-8
provides = nitransforms
url = https://github.com/poldracklab/nitransforms

[options]
python_requires = >= 3.6
Expand All @@ -14,6 +28,9 @@ test_requires =
pytest-cov
nose
codecov
setup_requires =
setuptools_scm
toml
packages = find:
include_package_data = True

Expand Down
7 changes: 7 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
"""Prepare package for distribution."""
from pathlib import Path
from setuptools import setup
from toml import loads

if __name__ == "__main__":
scm_cfg = loads(
(Path(__file__).parent / "pyproject.toml").read_text()
)["tool"]["setuptools_scm"]
setup(
name="nitransforms",
use_scm_version=scm_cfg,
)

0 comments on commit 50c830c

Please sign in to comment.