From e2d2073148f54736a48c13ab53425072d9af2acb Mon Sep 17 00:00:00 2001 From: Eric Prestat Date: Sat, 4 May 2024 07:46:53 +0100 Subject: [PATCH 1/4] Fix getting version in editable install --- hyperspy_gui_traitsui/__init__.py | 30 ++++++++++++++++++++++-------- pyproject.toml | 2 +- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/hyperspy_gui_traitsui/__init__.py b/hyperspy_gui_traitsui/__init__.py index 7a54dbd..2da2b1a 100644 --- a/hyperspy_gui_traitsui/__init__.py +++ b/hyperspy_gui_traitsui/__init__.py @@ -18,6 +18,7 @@ import importlib +from importlib.metadata import version import logging from pathlib import Path @@ -27,16 +28,29 @@ from hyperspy.defaults_parser import preferences -if Path(__file__).parent.parent.name == "site-packages": # pragma: no cover - # Tested in the "Package & Test" workflow on GitHub CI - from importlib.metadata import version +__version__ = version("hyperspy_gui_traitsui") - __version__ = version("hyperspy_gui_traitsui") -else: - # Editable install - from setuptools_scm import get_version +# For development version, `setuptools_scm` will be used at build time +# to get the dev version, in case of missing vcs information (git archive, +# shallow repository), the fallback version defined in pyproject.toml will +# be used - __version__ = get_version(Path(__file__).parent.parent) +# if we have a editable install from a git repository try to use +# `setuptools_scm` to find a more accurate version: +# `importlib.metadata` will provide the version at installation +# time and for editable version this may be different + +# we only do that if we have enough git history, e.g. not shallow checkout +_root = Path(__file__).resolve().parents[1] +if (_root / ".git").exists() and not (_root / ".git/shallow").exists(): + try: + # setuptools_scm may not be installed + from setuptools_scm import get_version + + __version__ = get_version(_root) + except ImportError: # pragma: no cover + # setuptools_scm not install, we keep the existing __version__ + pass diff --git a/pyproject.toml b/pyproject.toml index e28d5be..d286d5b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -91,4 +91,4 @@ include = ["hyperspy_gui_traitsui*"] "*" = ["*.yaml"] [tool.setuptools_scm] -# Presence enables setuptools_scm, the version will be determine at build time from git +fallback_version = "2.1.0.dev0" From 6d47065d6dbd59b4c7fe8696aee102196e012487 Mon Sep 17 00:00:00 2001 From: Eric Prestat Date: Sat, 4 May 2024 08:17:28 +0100 Subject: [PATCH 2/4] Add `releasing_guide.md` and `prepare_release.py` script --- prepare_release.py | 53 ++++++++++++++++++++++++++++++++++++++++++++++ pyproject.toml | 1 + releasing_guide.md | 33 +++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+) create mode 100644 prepare_release.py create mode 100644 releasing_guide.md diff --git a/prepare_release.py b/prepare_release.py new file mode 100644 index 0000000..7f88a97 --- /dev/null +++ b/prepare_release.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python3 +import argparse +import re +import subprocess + + +def run_towncrier(tag): + cmd = ("towncrier", "build", "--version", tag.strip("v")) + + return subprocess.call(cmd) + + +def update_fallback_version_in_pyproject(tag, fname="pyproject.toml"): + version = tag.strip("v").split(".") + if len(version) < 3: + version += ["0"] + + # Default to +1 on patch version + major, minor, patch = version[0], version[1], int(version[2]) + 1 + + with open(fname, "r") as file: + lines = file.readlines() + + pattern = "fallback_version" + new_version = f"{major}.{minor}.{patch}.dev0" + # Iterate through the lines and find the pattern + for i, line in enumerate(lines): + if re.search(pattern, line): + lines[i] = f'{pattern} = "{new_version}"\n' + break + + # Write the updated content back to the file + with open(fname, "w") as file: + file.writelines(lines) + + print( + f"\nNew (fallback) dev version ({new_version}) written to `pyproject.toml`.\n" + ) + + +if __name__ == "__main__": + # Get tag argument + parser = argparse.ArgumentParser() + parser.add_argument("tag") + args = parser.parse_args() + tag = args.tag + + # Update release notes + # towncrier is not used in this repository + # run_towncrier(tag) + + # Update fallback version for setuptools_scm + update_fallback_version_in_pyproject(tag) diff --git a/pyproject.toml b/pyproject.toml index d286d5b..05ea35a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -71,6 +71,7 @@ source = ["hyperspy_gui_traitsui"] omit = [ "hyperspy_gui_traitsui/tests/*", "hyperspy_gui_traitsui/conftest.py", + "prepare_release.py", ] [tool.coverage.report] diff --git a/releasing_guide.md b/releasing_guide.md new file mode 100644 index 0000000..caa284c --- /dev/null +++ b/releasing_guide.md @@ -0,0 +1,33 @@ + +Cut a Release +============= + +Create a PR to the `main` branch and go through the following steps: + +**Preparation** +- Prepare the release by running the `prepare_release.py` python script (e.g. `python prepare_release.py 2.0.1`) , which will do the following: + - update the `setuptools_scm` fallback version in `pyproject.toml`. +- Check release notes +- (optional) check conda-forge and wheels build. Pushing a tag to a fork will run the release workflow without uploading to pypi +- Let that PR collect comments for a day to ensure that other maintainers are comfortable with releasing + +**Tag and release** +:warning: this is a point of no return point :warning: +- push tag (`vx.y.z`) to the upstream repository and the following will be triggered: + - build of the wheels and their upload to pypi + - creation of a Github Release + +**Post-release action** +- Merge the PR + +Follow-up +========= + +- Tidy up and close corresponding milestone +- A PR to the conda-forge feedstock will be created by the conda-forge bot + +Additional information +====================== + +The version is defined by ``setuptools_scm`` at build time when packaging HyperSpy. In an editable installation (when using ``pip install -e .``), the version is defined from the +git repository. From 0e23e5dcbe4a3692f488cda3aa1703773f1ce706 Mon Sep 17 00:00:00 2001 From: Eric Prestat Date: Sat, 4 May 2024 08:37:32 +0100 Subject: [PATCH 3/4] CI: Fetch tags when checking out on GitHub CI --- .github/workflows/tests.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 9f52ccf..1bdcdc0 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -34,6 +34,8 @@ jobs: steps: - uses: actions/checkout@v4 + with: + fetch-depth: 0 - name: Fetch tags upstream if: ${{ github.repository_owner != 'hyperspy' }} From eb419a637a7967e47de57ad7ccb91adb41840e11 Mon Sep 17 00:00:00 2001 From: Eric Prestat Date: Sat, 4 May 2024 08:38:13 +0100 Subject: [PATCH 4/4] Add changelog entries --- CHANGES.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index b71a027..03ba8df 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -4,6 +4,8 @@ ## v2.1 (UNRELEASED) +* Fix getting version with editable installation ([#75](https://github.com/hyperspy/hyperspy_gui_traitsui/pull/75)). +* Add releasing guide and release script ([#75](https://github.com/hyperspy/hyperspy_gui_traitsui/pull/75)). * Fix regression with editable installation ([#74](https://github.com/hyperspy/hyperspy_gui_traitsui/pull/74)). ## v2.0 (2023-11-16)