From 642241eadfd776880dedd3340076c9d59e884a3c Mon Sep 17 00:00:00 2001 From: Michael Dawson-Haggerty Date: Tue, 19 Sep 2023 21:54:22 -0400 Subject: [PATCH] make version.py more complicated --- .github/workflows/release.yml | 2 +- trimesh/version.py | 68 ++++++++++++++++++++++++++++++----- 2 files changed, 61 insertions(+), 9 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 4b8787058..46ab54f14 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -132,7 +132,7 @@ jobs: - name: Tag Version id: set_tag run: | - export VER=$(python -c "print(eval(next(L.split('=')[1] for L in open('pyproject.toml') if 'version' in L)))") + export VER=$(python trimesh/version.py) echo "::set-output name=tag_name::${VER}" - name: Create Release id: create_release diff --git a/trimesh/version.py b/trimesh/version.py index 032e7c9d5..32f93f3f1 100644 --- a/trimesh/version.py +++ b/trimesh/version.py @@ -1,13 +1,65 @@ -# get the version trimesh was installed with from metadata -try: - # Python >= 3.8 +""" +# version.py + +Get the current version from package metadata or pyproject.toml +if everything else fails. +""" + + +def _get_version(): + """ + Try all our methods to get the version. + """ + for method in [_importlib, _pkgresources, _pyproject]: + try: + return method() + except BaseException: + pass + return None + + +def _importlib() -> str: + """ + Get the version string using package metadata on Python >= 3.8 + """ + from importlib.metadata import version - __version__ = version('trimesh') -except BaseException: - # Python < 3.8 + + return version("trimesh") + + +def _pkgresources() -> str: + """ + Get the version string using package metadata on Python < 3.8 + """ from pkg_resources import get_distribution - __version__ = get_distribution('trimesh').version -if __name__ == '__main__': + return get_distribution("trimesh").version + + +def _pyproject() -> str: + """ + Get the version string from the pyproject.toml file. + """ + import json + import os + + # use a path relative to this file + pyproject = os.path.abspath( + os.path.join( + os.path.dirname(os.path.abspath(os.path.expanduser(__file__))), + "..", + "pyproject.toml", + ) + ) + with open(pyproject) as f: + # json.loads cleans up the string and removes the quotes + return next(json.loads(L.split("=")[1]) for L in f if "version" in L) + + +# try all our tricks +__version__ = _get_version() + +if __name__ == "__main__": # print version if run directly i.e. in a CI script print(__version__)