Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MAINT: Use setuptools_scm to manage versioning #83

Merged
merged 1 commit into from
Mar 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 94 additions & 7 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,19 @@ 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 Down Expand Up @@ -125,16 +138,81 @@ jobs:
- store_test_results:
path: /tmp/tests/summaries/

test_packaging_and_deploy:
machine:
image: circleci/classic:201808-01
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: Build NiTransforms in build environment
command: |
source /tmp/build/bin/activate
python setup.py sdist bdist_wheel
- run:
name: Check sdist package in build environment
command: |
source /tmp/build/bin/activate
twine check dist/nitransforms*
- store_artifacts:
path: /tmp/src/nitransforms/dist
- run:
name: Prepare sdist install environment
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"
- 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
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: Prepare wheel install environment
command: |
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: 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
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"

deploy_pypi:
docker:
- image: circleci/python:3.7.4
working_directory: /tmp/src/nitransforms
steps:
- checkout
- run: pyenv local 3.7.0
- run:
name: Install build depends
command: python3 -m pip install "setuptools>=30.4.0" "pip>=10.0.1" "twine<2.0" docutils
command: python3 -m pip install "setuptools ~= 42.0" "setuptools_scm[toml] >= 3.4" "pip>=10.0.1" "twine<2.0" docutils
- run:
name: Build and check
command: |
Expand All @@ -144,7 +222,7 @@ jobs:
- run:
name: Validate version
command: |
THISVERSION=$( python3 get_version.py )
THISVERSION=$( python setup.py --version )
python3 -m pip install dist/*.tar.gz
mkdir empty
cd empty
Expand All @@ -167,9 +245,18 @@ workflows:
tags:
only: /.*/

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

- deploy_pypi:
requires:
- build_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__
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__',
]
41 changes: 41 additions & 0 deletions nitransforms/tests/test_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"""Test _version.py."""
import sys
from collections import namedtuple
from pkg_resources import DistributionNotFound
from importlib import reload
import nitransforms


def test_version_scm0(monkeypatch):
"""Retrieve the version via setuptools_scm."""
class _version:
__version__ = "10.0.0"

monkeypatch.setitem(sys.modules, 'nitransforms._version', _version)
reload(nitransforms)
assert nitransforms.__version__ == "10.0.0"


def test_version_scm1(monkeypatch):
"""Retrieve the version via pkg_resources."""
monkeypatch.setitem(sys.modules, 'nitransforms._version', None)

def _dist(name):
Distribution = namedtuple("Distribution", ["name", "version"])
return Distribution(name, "success")

monkeypatch.setattr('pkg_resources.get_distribution', _dist)
reload(nitransforms)
assert nitransforms.__version__ == "success"


def test_version_scm2(monkeypatch):
"""Check version could not be interpolated."""
monkeypatch.setitem(sys.modules, 'nitransforms._version', None)

def _raise(name):
raise DistributionNotFound("No get_distribution mock")

monkeypatch.setattr('pkg_resources.get_distribution', _raise)
reload(nitransforms)
assert nitransforms.__version__ == "unknown"
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,
)