From a07478ecc7681eadd12e5886117bb6c7d4c74b84 Mon Sep 17 00:00:00 2001 From: Eric Prestat Date: Sat, 3 Feb 2024 11:22:03 +0000 Subject: [PATCH] Improve getting `__version__` --- .github/workflows/tests.yml | 9 +++++-- hyperspy_gui_ipywidgets/__init__.py | 42 +++++++++++++++++++---------- pyproject.toml | 2 ++ 3 files changed, 37 insertions(+), 16 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index d9eca67..b37444e 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -29,14 +29,19 @@ jobs: steps: - uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: get repository name + shell: bash + run: echo "REPOSITORY_NAME=${GITHUB_REPOSITORY#*/}" >> $GITHUB_ENV - name: Fetch tags upstream if: ${{ github.repository_owner != 'hyperspy' }} # Needs to fetch the tags from upstream to get the # correct version with setuptools_scm run: | - git remote add upstream https://github.com/hyperspy/hyperspy_gui_ipywidgets.git - git fetch --prune --unshallow + git remote add upstream https://github.com/hyperspy/${{ env.REPOSITORY_NAME }}.git git fetch upstream --tags - uses: actions/setup-python@v4 diff --git a/hyperspy_gui_ipywidgets/__init__.py b/hyperspy_gui_ipywidgets/__init__.py index b0ccaf4..e9303b1 100644 --- a/hyperspy_gui_ipywidgets/__init__.py +++ b/hyperspy_gui_ipywidgets/__init__.py @@ -18,19 +18,33 @@ import importlib +from importlib.metadata import version from pathlib import Path -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("rosettasciio") - __version__ = version("hyperspy_gui_ipywidgets") -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 __all__ = [ @@ -49,11 +63,11 @@ def __dir__(): def __getattr__(name): + # lazy loading of module: this is only call when the attribute "name" is not found + # in the module + # See https://peps.python.org/pep-0562/ if name in __all__: - if name == "__version__": - return __version__ - else: - return importlib.import_module( - "." + name, 'hyperspy_gui_ipywidgets' - ) + return importlib.import_module( + "." + name, 'hyperspy_gui_ipywidgets' + ) raise AttributeError(f"module {__name__!r} has no attribute {name!r}") diff --git a/pyproject.toml b/pyproject.toml index ee0e200..bca1b5d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -91,3 +91,5 @@ include = ["hyperspy_gui_ipywidgets*"] [tool.setuptools_scm] # Presence enables setuptools_scm, the version will be determine at build time from git +# The version will be updated by the `prepare_release.py` script +fallback_version = "2.1.dev0" \ No newline at end of file